IHttpRequestHandler reqHdl = new IHttpRequestHandler() {
public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
HttpResponse resp = null;
if (exchange.getRequest().getRequestURI().endsWith("/redirected")) {
resp = new HttpResponse(200, "text/plain", "OK");
} else {
try {
resp = new HttpResponse(302);
resp.setHeader("Location", exchange.getRequest().getRequestUrl().toURI() + "/redirected");
} catch (Exception e) {
throw new IOException(e.toString());
}
}
resp.setHeader("Cache-Control", "public, max-age=3600");
exchange.send(resp);
}
};
HttpServer server = new HttpServer(reqHdl);
server.start();
HttpClient httpClient = new HttpClient();
httpClient.setFollowsRedirectMode(FollowsRedirectMode.ALL);
httpClient.setCacheMaxSizeKB(100);
IHttpResponse resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/test"));
Assert.assertEquals(200, resp.getStatus());
QAUtil.sleep(1000);
resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/test"));
Assert.assertEquals(200, resp.getStatus());
Assert.assertTrue(resp.getHeader(CacheHandler.XHEADER_NAME).startsWith("HIT"));
Assert.assertEquals(2, httpClient.getNumCacheHit());
Assert.assertEquals(2, httpClient.getNumCacheMiss());
httpClient.close();