@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); } }
@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(); }
@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); }
@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 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 getsWithQueryParametersDirectlyFrom1_0OriginsAreNotCacheable() { request = new BasicHttpRequest("GET", "/foo?s=bar"); response = new BasicHttpResponse(HttpStatus.SC_OK, "OK"); response.setVersion(HttpVersion.HTTP_1_0); Assert.assertFalse(policy.isResponseCacheable(request, response)); }
@Test public void headsWithQueryParametersDirectlyFrom1_0OriginsAreNotCacheable() { request = new BasicHttpRequest("HEAD", "/foo?s=bar"); response = new BasicHttpResponse(HttpStatus.SC_OK, "OK"); response.setVersion(HttpVersion.HTTP_1_0); Assert.assertFalse(policy.isResponseCacheable(request, response)); }
@Test public void doesNotFlushEntryForUnsuccessfulResponse() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_BAD_REQUEST, "Bad Request"); 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); impl.flushCacheEntriesInvalidatedByExchange(host, request, response, cacheKeyResolver, mockStorage); verifyNoMoreInteractions(mockStorage); }
@Test public void doesNotFlushEntryForUnsuccessfulResponse() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_BAD_REQUEST, "Bad Request"); 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); impl.flushCacheEntriesInvalidatedByExchange(host, request, response, cacheKeyResolver, mockStorage, operationCallback); verifyNoMoreInteractions(mockStorage); }
@Test public void getsWithQueryParametersDirectlyFrom1_0OriginsAreNotCacheableEvenWithSetting() { policy = new ResponseCachingPolicy(0, true, true, false); request = new BasicHttpRequest("GET", "/foo?s=bar"); response = new BasicHttpResponse(HttpStatus.SC_OK, "OK"); response.setVersion(HttpVersion.HTTP_1_0); Assert.assertFalse(policy.isResponseCacheable(request, response)); }
@Test public void testIsGetWithAnyCacheControlCacheable() { response.addHeader("Cache-Control", "max=10"); Assert.assertTrue(policy.isResponseCacheable("GET", response)); response = new BasicHttpResponse(HttpStatus.SC_OK, ""); response.setHeader("Date", DateUtils.formatDate(new Date())); response.addHeader("Cache-Control", "no-transform"); response.setHeader("Content-Length", "0"); Assert.assertTrue(policy.isResponseCacheable("GET", response)); }
@Test public void testIsHeadWithAnyCacheControlCacheable() { policy = new ResponseCachingPolicy(0, true, false, false); response.addHeader("Cache-Control", "max=10"); Assert.assertTrue(policy.isResponseCacheable("HEAD", response)); response = new BasicHttpResponse(HttpStatus.SC_OK, ""); response.setHeader("Date", DateUtils.formatDate(new Date())); response.addHeader("Cache-Control", "no-transform"); response.setHeader("Content-Length", "0"); Assert.assertTrue(policy.isResponseCacheable("HEAD", response)); }
@Test public void getsWithQueryParametersDirectlyFrom1_0OriginsAreCacheableWithExpires() { request = new BasicHttpRequest("GET", "/foo?s=bar"); response = new BasicHttpResponse(HttpStatus.SC_OK, "OK"); response.setVersion(HttpVersion.HTTP_1_0); response.setHeader("Date", DateUtils.formatDate(now)); response.setHeader("Expires", DateUtils.formatDate(tenSecondsFromNow)); Assert.assertTrue(policy.isResponseCacheable(request, response)); }
@Test public void headsWithQueryParametersDirectlyFrom1_0OriginsAreCacheableWithExpires() { policy = new ResponseCachingPolicy(0, true, false, false); request = new BasicHttpRequest("HEAD", "/foo?s=bar"); response = new BasicHttpResponse(HttpStatus.SC_OK, "OK"); response.setVersion(HttpVersion.HTTP_1_0); response.setHeader("Date", DateUtils.formatDate(now)); response.setHeader("Expires", DateUtils.formatDate(tenSecondsFromNow)); Assert.assertTrue(policy.isResponseCacheable(request, response)); }
@Test public void getsWithQueryParametersDirectlyFrom1_0OriginsCanBeNotCacheableEvenWithExpires() { policy = new ResponseCachingPolicy(0, true, true, false); request = new BasicHttpRequest("GET", "/foo?s=bar"); response = new BasicHttpResponse(HttpStatus.SC_OK, "OK"); response.setVersion(HttpVersion.HTTP_1_0); response.setHeader("Date", DateUtils.formatDate(now)); response.setHeader("Expires", DateUtils.formatDate(tenSecondsFromNow)); Assert.assertFalse(policy.isResponseCacheable(request, response)); }
@Test public void getsWithQueryParametersFrom1_1OriginsVia1_0ProxiesAreCacheableWithExpires() { request = new BasicHttpRequest("GET", "/foo?s=bar"); response = new BasicHttpResponse(HttpStatus.SC_OK, "OK"); response.setVersion(HttpVersion.HTTP_1_0); response.setHeader("Date", DateUtils.formatDate(now)); response.setHeader("Expires", DateUtils.formatDate(tenSecondsFromNow)); response.setHeader("Via", "1.1 someproxy"); Assert.assertTrue(policy.isResponseCacheable(request, response)); }
@Test public void headsWithQueryParametersFrom1_1OriginsVia1_0ProxiesAreCacheableWithExpires() { policy = new ResponseCachingPolicy(0, true, false, false); request = new BasicHttpRequest("HEAD", "/foo?s=bar"); response = new BasicHttpResponse(HttpStatus.SC_OK, "OK"); response.setVersion(HttpVersion.HTTP_1_0); response.setHeader("Date", DateUtils.formatDate(now)); response.setHeader("Expires", DateUtils.formatDate(tenSecondsFromNow)); response.setHeader("Via", "1.1 someproxy"); Assert.assertTrue(policy.isResponseCacheable(request, response)); }