private void checkIfUnmodifiedSinceUsingSuppliedDateFormat(SimpleDateFormat formatter)
throws IOException, HttpException {
Date d2 = new Date(System.currentTimeMillis() - 120000);
Date d = new Date(System.currentTimeMillis() - 60000);
String date = DateFormat.getDateTimeInstance().format(d);
ClientResponse response =
client.resource(getBaseURI() + "/context/request/date").contentType("text/string")
.put(date);
assertEquals(204, response.getStatusCode());
/*
* verifies that if the exact date is sent in and used in
* If-Unmodified-Since header, then the server will be ok and that it
* will return 200
*/
response =
client.resource(getBaseURI() + "/context/request/date")
.header(HttpHeaders.IF_UNMODIFIED_SINCE, formatter.format(d)).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("Last-Modified"));
rfc1123Format.setTimeZone(TimeZone.getDefault());
/*
* verifies that if no If-Unmodified-Since header is sent, then the
* server will be ok and the Request instance won't build a response.
*/
response = client.resource(getBaseURI() + "/context/request/date").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-Unmodified-Since request header, then the server will return the
* entity
*/
String lastModified = response.getHeaders().getFirst(HttpHeaders.LAST_MODIFIED);
response =
client.resource(getBaseURI() + "/context/request/date")
.header(HttpHeaders.IF_UNMODIFIED_SINCE, lastModified).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-Unmodified-Since earlier than the
* Last-Modified response header sent by server then the server will
* return a 412
*/
response =
client.resource(getBaseURI() + "/context/request/date")
.header(HttpHeaders.IF_UNMODIFIED_SINCE, formatter.format(d2)).get();
assertEquals(412, response.getStatusCode());
response =
client.resource(getBaseURI() + "/context/request/date")
.header(HttpHeaders.IF_UNMODIFIED_SINCE, formatter.format(new Date())).get();
/*
* verifies that using a If-Unmodified-Since later than the
* Last-Modified response header sent by server, then the server will
* return 200 and the entity
*/
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());
}