Beispiel #1
0
  private static String assert200Ok(
      CloseableHttpResponse res, String expectedContentType, String expectedContent)
      throws Exception {

    assertStatusLine(res, "HTTP/1.1 200 OK");

    // Ensure that the 'Last-Modified' header exists and is well-formed.
    final String lastModified;
    assertThat(res.containsHeader(HttpHeaders.LAST_MODIFIED), is(true));
    lastModified = res.getFirstHeader(HttpHeaders.LAST_MODIFIED).getValue();
    HttpHeaderDateFormat.get().parse(lastModified);

    // Ensure the content and its type are correct.
    assertThat(EntityUtils.toString(res.getEntity()), is(expectedContent));

    assertThat(res.containsHeader(HttpHeaders.CONTENT_TYPE), is(true));
    assertThat(
        res.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue(), startsWith(expectedContentType));

    return lastModified;
  }
 /**
  * Tests that caching headers are disabled during transactions. The Last-Modified date is only
  * updated when Modeshape's <code>Session#save()</code> is invoked. Since this operation is not
  * invoked during a Fedora transaction, the Last-Modified date never gets updated during a
  * transaction and the delivered content may be stale. Etag won't work either because it is
  * directly derived from Last-Modified.
  *
  * @throws IOException exception thrown during this function
  */
 @Test
 public void testNoCachingHeadersDuringTransaction() throws IOException {
   final String txLocation = createTransaction();
   final String location;
   try (final CloseableHttpResponse resp = execute(new HttpPost(txLocation))) {
     assertFalse(
         "Last-Modified musn't be present during a transaction",
         resp.containsHeader("Last-Modified"));
     assertFalse("ETag must not be present during a transaction", resp.containsHeader("ETag"));
     // Assert Cache-Control headers are present to invalidate caches
     location = getLocation(resp);
   }
   try (final CloseableHttpResponse responseFromGet = execute(new HttpGet(location))) {
     final Header[] headers = responseFromGet.getHeaders(CACHE_CONTROL);
     assertEquals("Two cache control headers expected: ", 2, headers.length);
     assertEquals(CACHE_CONTROL + "expected", CACHE_CONTROL, headers[0].getName());
     assertEquals(CACHE_CONTROL + "expected", CACHE_CONTROL, headers[1].getName());
     assertEquals("must-revalidate expected", "must-revalidate", headers[0].getValue());
     assertEquals("max-age=0 expected", "max-age=0", headers[1].getValue());
     consume(responseFromGet.getEntity());
   }
 }
Beispiel #3
0
  private static void assert304NotModified(
      CloseableHttpResponse res, String expectedLastModified, String expectedContentType) {

    assertStatusLine(res, "HTTP/1.1 304 Not Modified");

    // Ensure that the 'Last-Modified' header did not change.
    assertThat(res.getFirstHeader(HttpHeaders.LAST_MODIFIED).getValue(), is(expectedLastModified));

    // Ensure that the content does not exist but its type does.
    assertThat(res.getEntity(), is(nullValue()));

    assertThat(res.containsHeader(HttpHeaders.CONTENT_TYPE), is(true));
    assertThat(
        res.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue(), startsWith(expectedContentType));
  }