@Before
 public void setUp() {
   entry = HttpTestUtils.makeCacheEntry(new HashMap<String, String>());
   request = HttpRequestWrapper.wrap(HttpTestUtils.makeDefaultRequest());
   mockValidityPolicy = mock(CacheValidityPolicy.class);
   impl = new CachedHttpResponseGenerator(mockValidityPolicy);
 }
 @Test
 public void testUpdateCacheEntryReturnsDifferentEntryInstance() throws IOException {
   entry = HttpTestUtils.makeCacheEntry();
   HttpCacheEntry newEntry =
       impl.updateCacheEntry(null, entry, requestDate, responseDate, response);
   assertNotSame(newEntry, entry);
 }
  @Test
  public void testNewHeadersAreAddedByMerge() throws IOException {

    Header[] headers = {
      new BasicHeader("Date", formatDate(requestDate)), new BasicHeader("ETag", "\"etag\"")
    };

    entry = HttpTestUtils.makeCacheEntry(headers);
    response.setHeaders(
        new Header[] {
          new BasicHeader("Last-Modified", formatDate(responseDate)),
          new BasicHeader("Cache-Control", "public"),
        });

    HttpCacheEntry updatedEntry =
        impl.updateCacheEntry(null, entry, new Date(), new Date(), response);

    Header[] updatedHeaders = updatedEntry.getAllHeaders();
    assertEquals(4, updatedHeaders.length);

    headersContain(updatedHeaders, "Date", formatDate(requestDate));
    headersContain(updatedHeaders, "ETag", "\"etag\"");
    headersContain(updatedHeaders, "Last-Modified", formatDate(responseDate));
    headersContain(updatedHeaders, "Cache-Control", "public");
  }
 public static final HttpResponse make200Response(Date date, String cacheControl) {
   HttpResponse response = HttpTestUtils.make200Response();
   response.setHeader("Date", DateUtils.formatDate(date));
   response.setHeader("Cache-Control", cacheControl);
   response.setHeader("Etag", "\"etag\"");
   return response;
 }
  @Test
  public void testUpdatedEntryHasLatestRequestAndResponseDates() throws IOException {
    entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo);
    HttpCacheEntry updated =
        impl.updateCacheEntry(null, entry, twoSecondsAgo, oneSecondAgo, response);

    assertEquals(twoSecondsAgo, updated.getRequestDate());
    assertEquals(oneSecondAgo, updated.getResponseDate());
  }
  @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 cannotUpdateFromANon304OriginResponse() throws Exception {
   entry = HttpTestUtils.makeCacheEntry();
   response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
   try {
     impl.updateCacheEntry("A", entry, new Date(), new Date(), response);
     fail("should have thrown exception");
   } catch (IllegalArgumentException expected) {
   }
 }
  @Test
  public void entryWithMalformedDateIsStillUpdated() throws Exception {
    Header[] headers = {new BasicHeader("ETag", "\"old\""), new BasicHeader("Date", "bad-date")};
    entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo, headers);
    response.setHeader("ETag", "\"new\"");
    response.setHeader("Date", formatDate(twoSecondsAgo));
    HttpCacheEntry updated =
        impl.updateCacheEntry(null, entry, twoSecondsAgo, oneSecondAgo, response);

    assertEquals("\"new\"", updated.getFirstHeader("ETag").getValue());
  }
 @Test
 public void oldHeadersRetainedIfResponseOlderThanEntry() throws Exception {
   Header[] headers = {
     new BasicHeader("Date", formatDate(oneSecondAgo)), new BasicHeader("ETag", "\"new-etag\"")
   };
   entry = HttpTestUtils.makeCacheEntry(twoSecondsAgo, now, headers);
   response.setHeader("Date", formatDate(tenSecondsAgo));
   response.setHeader("ETag", "\"old-etag\"");
   HttpCacheEntry result = impl.updateCacheEntry("A", entry, new Date(), new Date(), response);
   assertEquals(2, result.getAllHeaders().length);
   headersContain(result.getAllHeaders(), "Date", formatDate(oneSecondAgo));
   headersContain(result.getAllHeaders(), "ETag", "\"new-etag\"");
 }
  @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);
  }
Example #11
0
  @Test
  public void entry1xxWarningsAreRemovedOnUpdate() throws Exception {
    Header[] headers = {
      new BasicHeader("Warning", "110 fred \"Response is stale\""),
      new BasicHeader("ETag", "\"old\""),
      new BasicHeader("Date", formatDate(eightSecondsAgo))
    };
    entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo, headers);
    response.setHeader("ETag", "\"new\"");
    response.setHeader("Date", formatDate(twoSecondsAgo));
    HttpCacheEntry updated =
        impl.updateCacheEntry(null, entry, twoSecondsAgo, oneSecondAgo, response);

    assertEquals(0, updated.getHeaders("Warning").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()));
  }
Example #13
0
  @Test
  public void testHeadersAreMergedCorrectly() throws IOException {
    Header[] headers = {
      new BasicHeader("Date", formatDate(responseDate)), new BasicHeader("ETag", "\"etag\"")
    };
    entry = HttpTestUtils.makeCacheEntry(headers);
    response.setHeaders(new Header[] {});

    HttpCacheEntry updatedEntry =
        impl.updateCacheEntry(null, entry, new Date(), new Date(), response);

    Header[] updatedHeaders = updatedEntry.getAllHeaders();
    assertEquals(2, updatedHeaders.length);
    headersContain(updatedHeaders, "Date", formatDate(responseDate));
    headersContain(updatedHeaders, "ETag", "\"etag\"");
  }
 @Test(expected = MemcachedSerializationException.class)
 public void cannotReconstituteFromGarbage() {
   impl = new MemcachedCacheEntryImpl();
   byte[] bytes = HttpTestUtils.getRandomBytes(128);
   impl.set(bytes);
 }
 @Before
 public void setUp() {
   entry = HttpTestUtils.makeCacheEntry();
   impl = new MemcachedCacheEntryImpl("foo", entry);
 }