private final String date = HttpUtils.toRFC1123DateString(new Date());
public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
IHttpRequest request = exchange.getRequest();
String ifModifiedSince = request.getHeader("If-Modified-Since");
if ((ifModifiedSince != null) && (ifModifiedSince.equals(date))) {
exchange.send(new HttpResponse(304));
return;
}
HttpResponse resp = new HttpResponse(200, "text/plain", "test");
resp.setHeader("Last-Modified", date);
exchange.send(resp);
}
};
HttpServer server = new HttpServer(reqHdl);
server.start();
HttpClient httpClient = new HttpClient();
httpClient.setCacheMaxSizeKB(100);
IHttpResponse resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
Assert.assertEquals(200, resp.getStatus());
Assert.assertEquals("test", resp.getBlockingBody().readString());
Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
String lastModified = resp.getHeader("Last-Modified");
QAUtil.sleep(1000);
IHttpRequest request = new GetRequest("http://localhost:" + server.getLocalPort() + "/");
request.setHeader("If-Modified-Since", lastModified);
resp = httpClient.call(request);
Assert.assertEquals(304, resp.getStatus());
Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
QAUtil.sleep(1000);