public HttpResponse returnResponse() throws IOException { assertNotConsumed(); try { final HttpEntity entity = this.response.getEntity(); if (entity != null) { final ByteArrayEntity byteArrayEntity = new ByteArrayEntity( EntityUtils.toByteArray(entity)); final ContentType contentType = EntityUtils.getContentTypeOrDefault(entity); byteArrayEntity.setContentType(contentType.toString()); this.response.setEntity(byteArrayEntity); } return this.response; } finally { this.consumed = true; } }
private ClassicHttpResponse make200Response() { final ClassicHttpResponse out = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); out.setHeader("Date", DateUtils.formatDate(new Date())); out.setHeader("Server", "MockOrigin/1.0"); out.setEntity(makeBody(128)); return out; }
@Test public void testDoesNotModifyContentLengthOnResponses() throws Exception { originResponse.setEntity(HttpTestUtils.makeBody(128)); originResponse.setHeader("Content-Length","128"); testDoesNotModifyHeaderOnResponses("Content-Length"); }
@Test public void testDoesNotModifyContentMD5OnResponses() throws Exception { originResponse.setEntity(HttpTestUtils.makeBody(128)); originResponse.setHeader("Content-MD5","Q2hlY2sgSW50ZWdyaXR5IQ=="); testDoesNotModifyHeaderOnResponses("Content-MD5"); }
public static ClassicHttpResponse make200Response() { final ClassicHttpResponse out = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); out.setHeader("Date", DateUtils.formatDate(new Date())); out.setHeader("Server", "MockOrigin/1.0"); out.setHeader("Content-Length", "128"); out.setEntity(makeBody(128)); return out; }
@Test public void test204ResponsesDoNotContainMessageBodies() throws Exception { originResponse = new BasicClassicHttpResponse(HttpStatus.SC_NO_CONTENT, "No Content"); originResponse.setEntity(HttpTestUtils.makeBody(entityLength)); EasyMock.expect( mockExecChain.proceed( EasyMock.isA(ClassicHttpRequest.class), EasyMock.isA(ExecChain.Scope.class))).andReturn(originResponse); replayMocks(); final ClassicHttpResponse result = execute(request); verifyMocks(); }
@Test public void testDoesNotModifyContentRangeHeaderFromOriginResponseWithNoTransform() throws Exception { request.setHeader("If-Range","\"etag\""); request.setHeader("Range","bytes=0-49"); originResponse = new BasicClassicHttpResponse(206, "Partial Content"); originResponse.setEntity(HttpTestUtils.makeBody(50)); testDoesNotModifyHeaderFromOriginResponseWithNoTransform("Content-Range","bytes 0-49/128"); }
private void testUnknownResponseStatusCodeIsNotCached(final int code) throws Exception { emptyMockCacheExpectsNoPuts(); originResponse = new BasicClassicHttpResponse(code, "Moo"); originResponse.setHeader("Date", DateUtils.formatDate(new Date())); originResponse.setHeader("Server", "MockOrigin/1.0"); originResponse.setHeader("Cache-Control", "max-age=3600"); originResponse.setEntity(body); EasyMock.expect( mockExecChain.proceed( EasyMock.isA(ClassicHttpRequest.class), EasyMock.isA(ExecChain.Scope.class))).andReturn(originResponse); replayMocks(); execute(request); // in particular, there were no storage calls on the cache verifyMocks(); }
@Test public void testDoesNotModifyContentRangeOnResponses() throws Exception { request.setHeader("Range","bytes=0-128"); originResponse.setCode(HttpStatus.SC_PARTIAL_CONTENT); originResponse.setReasonPhrase("Partial Content"); originResponse.setEntity(HttpTestUtils.makeBody(128)); originResponse.setHeader("Content-Range","bytes 0-127/256"); testDoesNotModifyHeaderOnResponses("Content-Range"); }
@Test public void test304ResponseDoesNotContainABody() throws Exception { request.setHeader("If-None-Match", "\"etag\""); originResponse = new BasicClassicHttpResponse(HttpStatus.SC_NOT_MODIFIED,"Not Modified"); originResponse.setHeader("Date", DateUtils.formatDate(new Date())); originResponse.setHeader("Server", "MockServer/1.0"); originResponse.setHeader("Content-Length", "128"); originResponse.setEntity(HttpTestUtils.makeBody(entityLength)); EasyMock.expect( mockExecChain.proceed( EasyMock.isA(ClassicHttpRequest.class), EasyMock.isA(ExecChain.Scope.class))).andReturn(originResponse); replayMocks(); final ClassicHttpResponse result = execute(request); verifyMocks(); }
@Test public void testLowerOriginResponsesUpgradedToOurVersion1_1() throws Exception { originResponse = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); originResponse.setVersion(new ProtocolVersion("HTTP", 1, 2)); originResponse.setHeader("Date", DateUtils.formatDate(new Date())); originResponse.setHeader("Server", "MockOrigin/1.0"); originResponse.setEntity(body); // not testing this internal behavior in this test, just want // to check the protocol version that comes out the other end EasyMock.expect( mockExecChain.proceed( EasyMock.isA(ClassicHttpRequest.class), EasyMock.isA(ExecChain.Scope.class))).andReturn(originResponse); replayMocks(); final ClassicHttpResponse result = execute(request); verifyMocks(); Assert.assertEquals(HttpVersion.HTTP_1_1, result.getVersion()); }
@Test public void test200ResponseToOPTIONSWithNoBodyShouldIncludeContentLengthZero() throws Exception { request = new BasicClassicHttpRequest("OPTIONS", "/"); originResponse.setEntity(null); originResponse.setHeader("Content-Length", "0"); EasyMock.expect( mockExecChain.proceed( EasyMock.isA(ClassicHttpRequest.class), EasyMock.isA(ExecChain.Scope.class))).andReturn(originResponse); replayMocks(); final ClassicHttpResponse result = execute(request); verifyMocks(); final Header contentLength = result.getFirstHeader("Content-Length"); Assert.assertNotNull(contentLength); Assert.assertEquals("0", contentLength.getValue()); }
@Test public void test303ResponsesAreNotCached() throws Exception { emptyMockCacheExpectsNoPuts(); request = new BasicClassicHttpRequest("GET", "/"); originResponse = new BasicClassicHttpResponse(HttpStatus.SC_SEE_OTHER, "See Other"); originResponse.setHeader("Date", DateUtils.formatDate(new Date())); originResponse.setHeader("Server", "MockServer/1.0"); originResponse.setHeader("Cache-Control", "max-age=3600"); originResponse.setHeader("Content-Type", "application/x-cachingclient-test"); originResponse.setHeader("Location", "http://foo.example.com/other"); originResponse.setEntity(HttpTestUtils.makeBody(entityLength)); EasyMock.expect( mockExecChain.proceed( EasyMock.isA(ClassicHttpRequest.class), EasyMock.isA(ExecChain.Scope.class))).andReturn(originResponse); replayMocks(); execute(request); verifyMocks(); }
@Test public void testSetViaHeaderIfValidationFailsButNotRequired() throws Exception { final Date now = new Date(); final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L); impl = createCachingExecChain(new BasicHttpCache(), CacheConfig.DEFAULT); final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/"); final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/"); final ClassicHttpResponse resp1 = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); resp1.setEntity(HttpTestUtils.makeBody(128)); resp1.setHeader("Content-Length", "128"); resp1.setHeader("ETag", "\"etag\""); resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo)); resp1.setHeader("Cache-Control", "public, max-age=5"); backendExpectsAnyRequestAndReturn(resp1); backendExpectsAnyRequestAndThrows(new IOException()); replayMocks(); execute(req1); final ClassicHttpResponse result = execute(req2); verifyMocks(); Assert.assertNotNull(result.getFirstHeader("Via")); }
@Test public void test206ResponseReturnedToClientMustHaveDateHeader() throws Exception { request.addHeader("Range", "bytes=0-50"); originResponse = new BasicClassicHttpResponse(HttpStatus.SC_PARTIAL_CONTENT, "Partial Content"); originResponse.setHeader("Date", DateUtils.formatDate(new Date())); originResponse.setHeader("Server", "MockOrigin/1.0"); originResponse.setEntity(HttpTestUtils.makeBody(500)); originResponse.setHeader("Content-Range", "bytes 0-499/1234"); originResponse.removeHeaders("Date"); EasyMock.expect( mockExecChain.proceed( EasyMock.isA(ClassicHttpRequest.class), EasyMock.isA(ExecChain.Scope.class))).andReturn(originResponse); replayMocks(); final ClassicHttpResponse result = execute(request); Assert.assertTrue(result.getCode() != HttpStatus.SC_PARTIAL_CONTENT || result.getFirstHeader("Date") != null); verifyMocks(); }
@Test public void responseToGetWithQueryFrom1_0OriginAndNoExpiresIsNotCached() throws Exception { final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/bar?baz=quux"); final ClassicHttpResponse resp2 = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); resp2.setVersion(HttpVersion.HTTP_1_0); resp2.setEntity(HttpTestUtils.makeBody(200)); resp2.setHeader("Content-Length","200"); resp2.setHeader("Date", DateUtils.formatDate(now)); backendExpectsAnyRequestAndReturn(resp2); replayMocks(); execute(req2); verifyMocks(); }
@Test public void testSetsViaHeaderOnResponseForCacheMiss() throws Exception { impl = createCachingExecChain(new BasicHttpCache(), CacheConfig.DEFAULT); final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/"); final ClassicHttpResponse resp1 = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); resp1.setEntity(HttpTestUtils.makeBody(128)); resp1.setHeader("Content-Length", "128"); resp1.setHeader("ETag", "\"etag\""); resp1.setHeader("Date", DateUtils.formatDate(new Date())); resp1.setHeader("Cache-Control", "public, max-age=3600"); backendExpectsAnyRequestAndReturn(resp1); replayMocks(); final ClassicHttpResponse result = execute(req1); verifyMocks(); Assert.assertNotNull(result.getFirstHeader("Via")); }
@Test public void responseToGetWithQueryFrom1_0OriginVia1_1ProxyAndNoExpiresIsNotCached() throws Exception { final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/bar?baz=quux"); final ClassicHttpResponse resp2 = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); resp2.setVersion(HttpVersion.HTTP_1_0); resp2.setEntity(HttpTestUtils.makeBody(200)); resp2.setHeader("Content-Length","200"); resp2.setHeader("Date", DateUtils.formatDate(now)); resp2.setHeader("Via","1.0 someproxy"); backendExpectsAnyRequestAndReturn(resp2); replayMocks(); execute(req2); verifyMocks(); }
@Test public void testSetsCacheHitContextIfRequestServedFromCache() throws Exception { impl = createCachingExecChain(new BasicHttpCache(), CacheConfig.DEFAULT); final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/"); final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/"); final ClassicHttpResponse resp1 = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); resp1.setEntity(HttpTestUtils.makeBody(128)); resp1.setHeader("Content-Length", "128"); resp1.setHeader("ETag", "\"etag\""); resp1.setHeader("Date", DateUtils.formatDate(new Date())); resp1.setHeader("Cache-Control", "public, max-age=3600"); backendExpectsAnyRequestAndReturn(resp1); replayMocks(); execute(req1); execute(req2); verifyMocks(); Assert.assertEquals(CacheResponseStatus.CACHE_HIT, context.getCacheResponseStatus()); }
@Test public void testSetsViaHeaderOnResponseIfRequestServedFromCache() throws Exception { impl = createCachingExecChain(new BasicHttpCache(), CacheConfig.DEFAULT); final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/"); final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/"); final ClassicHttpResponse resp1 = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); resp1.setEntity(HttpTestUtils.makeBody(128)); resp1.setHeader("Content-Length", "128"); resp1.setHeader("ETag", "\"etag\""); resp1.setHeader("Date", DateUtils.formatDate(new Date())); resp1.setHeader("Cache-Control", "public, max-age=3600"); backendExpectsAnyRequestAndReturn(resp1); replayMocks(); execute(req1); final ClassicHttpResponse result = execute(req2); verifyMocks(); Assert.assertNotNull(result.getFirstHeader("Via")); }