Date d = new Date(System.currentTimeMillis() - 60000);
String date = DateFormat.getDateTimeInstance().format(d);
/*
* sets a last modified date
*/
Resource dateResource = client.resource(getBaseURI() + "/context/request/date");
ClientResponse response = dateResource.contentType("text/string").put(date);
assertEquals(204, response.getStatusCode());
response = dateResource.header(HttpHeaders.IF_MODIFIED_SINCE, formatter.format(d)).get();
/*
* verifies that if the exact date is sent in and used in
* If-Modified-Since header, then the server will be ok and that it will
* return 304
*/
assertEquals(304, response.getStatusCode());
/*
* verifies that if no If-Modified-Since header is sent, then the server
* will be ok and the Request instance won't build a response.
*/
dateResource = client.resource(getBaseURI() + "/context/request/date");
response = dateResource.get();
assertEquals(200, response.getStatusCode());
assertEquals("the date: " + rfc1123Format.format(d), response.getEntity(String.class));
rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
assertEquals(rfc1123Format.format(d), response.getHeaders()
.getFirst(HttpHeaders.LAST_MODIFIED));
rfc1123Format.setTimeZone(TimeZone.getDefault());
/*
* verifies that using Last-Modified response header sent by server as
* If-Modified-Since request header, then the server will return a 304
*/
String lastModified = response.getHeaders().getFirst(HttpHeaders.LAST_MODIFIED);
dateResource = client.resource(getBaseURI() + "/context/request/date");
response = dateResource.header(HttpHeaders.IF_MODIFIED_SINCE, lastModified).get();
assertEquals(304, response.getStatusCode());
/*
* verifies that using a If-Modified-Since earlier than the
* Last-Modified response header sent by server then the server will
* return a 200 with entity
*/
dateResource = client.resource(getBaseURI() + "/context/request/date");
response = dateResource.header(HttpHeaders.IF_MODIFIED_SINCE, formatter.format(d2)).get();
assertEquals(200, response.getStatusCode());
assertEquals("the date: " + rfc1123Format.format(d), response.getEntity(String.class));
rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
assertEquals(rfc1123Format.format(d), response.getHeaders()
.getFirst(HttpHeaders.LAST_MODIFIED));
rfc1123Format.setTimeZone(TimeZone.getDefault());
/*
* verifies that using a If-Modified-Since later than the Last-Modified
* response header sent by server, then the server will return a 304
*/
dateResource = client.resource(getBaseURI() + "/context/request/date");
response =
dateResource.header(HttpHeaders.IF_MODIFIED_SINCE, formatter.format(new Date())).get();
assertEquals(304, response.getStatusCode());
}