@Test public void testResponseContainsEntityToServeGETRequestIfEntryContainsResource() throws Exception { final HttpResponse response = impl.generateResponse(request, entry); Assert.assertNotNull(response.getEntity()); }
@Test public void testResponseDoesNotContainEntityToServeHEADRequestIfEntryContainsResource() throws Exception { final HttpRequestWrapper headRequest = HttpRequestWrapper.wrap(HttpTestUtils.makeDefaultHEADRequest()); final HttpResponse response = impl.generateResponse(headRequest, entry); Assert.assertNull(response.getEntity()); }
@Test public void testResponseMatchesCacheEntry() { final HttpResponse response = impl.generateResponse(request, entry); Assert.assertTrue(response.containsHeader("Content-Length")); Assert.assertSame("HTTP", response.getProtocolVersion().getProtocol()); Assert.assertEquals(1, response.getProtocolVersion().getMajor()); Assert.assertEquals(1, response.getProtocolVersion().getMinor()); }
@Test public void testAgeHeaderIsNotPopulatedIfCurrentAgeOfCacheEntryIsZero() { currentAge(0L); final HttpResponse response = impl.generateResponse(request, entry); verify(mockValidityPolicy).getCurrentAgeSecs(same(entry), isA(Date.class)); final Header ageHdr = response.getFirstHeader("Age"); Assert.assertNull(ageHdr); }
@Test public void testAgeHeaderIsPopulatedWithMaxAgeIfCurrentAgeTooBig() { currentAge(CacheValidityPolicy.MAX_AGE + 1L); final HttpResponse response = impl.generateResponse(request, entry); verify(mockValidityPolicy).getCurrentAgeSecs(same(entry), isA(Date.class)); final Header ageHdr = response.getFirstHeader("Age"); Assert.assertNotNull(ageHdr); Assert.assertEquals(CacheValidityPolicy.MAX_AGE, Long.parseLong(ageHdr.getValue())); }
@Test public void testAgeHeaderIsPopulatedWithCurrentAgeOfCacheEntryIfNonZero() { currentAge(10L); final HttpResponse response = impl.generateResponse(request, entry); verify(mockValidityPolicy).getCurrentAgeSecs(same(entry), isA(Date.class)); final Header ageHdr = response.getFirstHeader("Age"); Assert.assertNotNull(ageHdr); Assert.assertEquals(10L, Long.parseLong(ageHdr.getValue())); }
@Test public void testContentLengthIsNotAddedWhenTransferEncodingIsPresent() { final Header[] hdrs = new Header[] {new BasicHeader("Transfer-Encoding", "chunked")}; final byte[] buf = new byte[] {1, 2, 3, 4, 5}; final HttpCacheEntry entry1 = HttpTestUtils.makeCacheEntry(hdrs, buf); final HttpResponse response = impl.generateResponse(request, entry1); final Header length = response.getFirstHeader("Content-Length"); Assert.assertNull(length); }
@Test public void testResponseHasContentLength() { final byte[] buf = new byte[] {1, 2, 3, 4, 5}; final HttpCacheEntry entry1 = HttpTestUtils.makeCacheEntry(buf); final HttpResponse response = impl.generateResponse(request, entry1); final Header length = response.getFirstHeader("Content-Length"); Assert.assertNotNull("Content-Length Header is missing", length); Assert.assertEquals( "Content-Length does not match buffer length", buf.length, Integer.parseInt(length.getValue())); }
@Test public void testResponseStatusCodeMatchesCacheEntry() { final HttpResponse response = impl.generateResponse(request, entry); Assert.assertEquals(entry.getStatusCode(), response.getStatusLine().getStatusCode()); }