@Override public void completed(final byte[] content) { final BasicAsyncEntityProducer entityProducer = new BasicAsyncEntityProducer(content, contentType); entityProducerRef.set(entityProducer); try { responseChannel.sendResponse(new BasicHttpResponse(HttpStatus.SC_OK), entityProducer, context); } catch (final IOException | HttpException ex) { failed(ex); } }
@Override public String getReasonPhrase() { return this.reasonPhrase != null ? this.reasonPhrase : getReason(this.code); }
@Test public void testResponseStatusLineMutation() { final BasicHttpResponse response = new BasicHttpResponse(200, "OK"); Assert.assertEquals(200, response.getCode()); Assert.assertEquals("OK", response.getReasonPhrase()); response.setReasonPhrase("Kind of OK"); Assert.assertEquals(200, response.getCode()); Assert.assertEquals("Kind of OK", response.getReasonPhrase()); response.setCode(299); Assert.assertEquals(299, response.getCode()); Assert.assertEquals(null, response.getReasonPhrase()); }
@Test public void testResponseBasics() { final BasicHttpResponse response = new BasicHttpResponse(200, "OK"); Assert.assertEquals(200, response.getCode()); Assert.assertEquals("OK", response.getReasonPhrase()); }
@Test public void testResponseInvalidStatusCode() { try { new BasicHttpResponse(-200, "OK"); Assert.fail("IllegalArgumentException expected"); } catch (final IllegalArgumentException expected) { } final BasicHttpResponse response = new BasicHttpResponse(200, "OK"); try { response.setCode(-1); Assert.fail("IllegalArgumentException expected"); } catch (final IllegalArgumentException expected) { } }
@Override public void addHeader(final String name, final Object value) { Args.notNull(name, "Header name"); addHeader(new BasicHeader(name, value)); }
@Override public void addHeader(final String name, final Object value) { Args.notNull(name, "Header name"); addHeader(new BasicHeader(name, value)); }
@Before public void setUp() throws Exception { requestDate = new Date(System.currentTimeMillis() - 1000); responseDate = new Date(); now = new Date(); oneSecondAgo = new Date(now.getTime() - 1000L); twoSecondsAgo = new Date(now.getTime() - 2000L); eightSecondsAgo = new Date(now.getTime() - 8000L); tenSecondsAgo = new Date(now.getTime() - 10000L); response = new BasicHttpResponse(HttpStatus.SC_NOT_MODIFIED, "Not Modified"); impl = new CacheUpdateHandler(); }
@Override public String getReasonPhrase() { return this.reasonPhrase != null ? this.reasonPhrase : getReason(this.code); }
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK); final AsyncEntityProducer entityProducer = new RandomBinAsyncEntityProducer(n); entityProducerRef.set(entityProducer);
entityProducerRef.set(entityProducer); try { responseChannel.sendResponse(new BasicHttpResponse(HttpStatus.SC_OK), entityProducer, context); } catch (final IOException | HttpException ex) { failed(ex); responseChannel.sendResponse(new BasicHttpResponse(HttpStatus.SC_OK), null, context); entityConsumer.releaseResources();
@Test public void doesNotFlushForResponsesWithoutContentLocation() throws Exception { final HttpRequest request = new BasicHttpRequest("POST","/"); final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK); impl.flushCacheEntriesInvalidatedByExchange(host, request, response, cacheKeyResolver, mockStorage); verifyNoMoreInteractions(mockStorage); }
@Before public void setUp() throws Exception { now = new Date(); sixSecondsAgo = new Date(now.getTime() - 6 * 1000L); tenSecondsFromNow = new Date(now.getTime() + 10 * 1000L); policy = new ResponseCachingPolicy(0, true, false, false); request = new BasicHttpRequest("GET","/"); response = new BasicHttpResponse(HttpStatus.SC_OK, ""); response.setHeader("Date", DateUtils.formatDate(new Date())); response.setHeader("Content-Length", "0"); }
@Test public void doesNotFlushForResponsesWithoutContentLocation() throws Exception { final HttpRequest request = new BasicHttpRequest("POST","/"); final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK); impl.flushCacheEntriesInvalidatedByExchange(host, request, response, cacheKeyResolver, mockStorage, operationCallback); verifyNoMoreInteractions(mockStorage); }
@Test public void cannotUpdateFromANon304OriginResponse() throws Exception { entry = HttpTestUtils.makeCacheEntry(); response = new BasicHttpResponse(HttpStatus.SC_OK, "OK"); try { impl.updateCacheEntry("A", entry, new Date(), new Date(), response); fail("should have thrown exception"); } catch (final IllegalArgumentException expected) { } }
@Test public void testStaleResponse() { final HttpResponse response1 = new BasicHttpResponse(HttpStatus.SC_OK); response1.addHeader(HeaderConstants.WARNING, "110 localhost \"Response is stale\""); Assert.assertThat(impl.isStale(response1), CoreMatchers.equalTo(true)); final HttpResponse response2 = new BasicHttpResponse(HttpStatus.SC_OK); response2.addHeader(HeaderConstants.WARNING, "111 localhost \"Revalidation failed\""); Assert.assertThat(impl.isStale(response2), CoreMatchers.equalTo(true)); final HttpResponse response3 = new BasicHttpResponse(HttpStatus.SC_OK); response3.addHeader(HeaderConstants.WARNING, "xxx localhost \"Huh?\""); Assert.assertThat(impl.isStale(response3), CoreMatchers.equalTo(false)); final HttpResponse response4 = new BasicHttpResponse(HttpStatus.SC_OK); Assert.assertThat(impl.isStale(response4), CoreMatchers.equalTo(false)); }
@Test public void testIsGetWithout200Cacheable() { HttpResponse response404 = new BasicHttpResponse(HttpStatus.SC_NOT_FOUND, ""); Assert.assertFalse(policy.isResponseCacheable("GET", response404)); response404 = new BasicHttpResponse(HttpStatus.SC_GATEWAY_TIMEOUT, ""); Assert.assertFalse(policy.isResponseCacheable("GET", response404)); }
@Test public void testIsHeadWithout200Cacheable() { HttpResponse response404 = new BasicHttpResponse(HttpStatus.SC_NOT_FOUND, ""); Assert.assertFalse(policy.isResponseCacheable("HEAD", response404)); response404 = new BasicHttpResponse(HttpStatus.SC_GATEWAY_TIMEOUT, ""); Assert.assertFalse(policy.isResponseCacheable("HEAD", response404)); }
private HttpResponse makePartialResponse(final int nbytes) { final HttpResponse resp = new BasicHttpResponse(HttpStatus.SC_PARTIAL_CONTENT, "Partial Content"); setMinimalResponseHeaders(resp); resp.setHeader("Content-Length","" + nbytes); resp.setHeader("Content-Range","0-127/256"); return resp; }
@Test public void doesNotFlushEntryIfNotInCache() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK); response.setHeader("ETag","\"new-etag\""); response.setHeader("Date", DateUtils.formatDate(now)); final String key = "http://foo.example.com:80/bar"; response.setHeader("Content-Location", key); when(mockStorage.getEntry(key)).thenReturn(null); impl.flushCacheEntriesInvalidatedByExchange(host, request, response, cacheKeyResolver, mockStorage); verify(mockStorage).getEntry(key); verifyNoMoreInteractions(mockStorage); }