public class ApacheImageCacheIT {
@Test
public void testCachedResource() throws JSONException, Exception {
MechanizeAgent agent = new MechanizeAgent();
Resource res1 = agent.get("http://apache.org/images/feather-small.gif");
assertNull(res1.getResponse().getFirstHeader("Via"));
Resource res2 = agent.get("http://apache.org/images/feather-small.gif");
assertTrue(res2.getResponse().getFirstHeader("Via").getValue().indexOf("mechanize")>-1);
assertEquals(date(res1), date(res2));
// tweak response to shorten cache time
res2.getResponse().setHeader("Cache-Control", "max-age=0;must-revalidate");
res2.getResponse().removeHeaders("Expires");
// check between the cache and HttpClient to make sure the response is 304 not modified
agent.addFilter(new MechanizeChainFilter() {
@Override
public HttpResponse execute(final HttpUriRequest request, final HttpContext context, final MechanizeFilter chain) {
HttpResponse response = chain.execute(request, context);
assertEquals(304, response.getStatusLine().getStatusCode());
return response;
}
});
Resource res3 = agent.get("http://apache.org/images/feather-small.gif");
assertTrue(res3.getResponse().getFirstHeader("Via").getValue().indexOf("mechanize")>-1);
}