String isWeak = isEntityTagWeak ? "true" : "false";
/*
* sets an entity tag
*/
ClientResponse response =
client.resource(getBaseURI() + "/context/request/etag").contentType("text/string")
.put(setETag);
assertEquals(204, response.getStatusCode());
/*
* verifies that if the exact etag is sent in, then the response is a
* 304
*/
response =
client.resource(getBaseURI() + "/context/request/etag")
.header(HttpHeaders.IF_NONE_MATCH, setETag).get();
assertEquals(304, response.getStatusCode());
assertEquals(setETag, response.getHeaders().getFirst(HttpHeaders.ETAG));
/*
* verifies that if a "*" etag is sent in, then the response returns a
* 304
*/
response =
client.resource(getBaseURI() + "/context/request/etag")
.header(HttpHeaders.IF_NONE_MATCH, "\"*\"").get();
assertEquals(304, response.getStatusCode());
assertEquals(setETag, response.getHeaders().getFirst(HttpHeaders.ETAG));
/*
* verifies that if a "*" etag is sent in, then the response returns a
* 412
*/
response =
client.resource(getBaseURI() + "/context/request/etag")
.header(HttpHeaders.IF_NONE_MATCH, "\"*\"").post(null);
assertEquals(412, response.getStatusCode());
assertEquals(setETag, response.getHeaders().getFirst(HttpHeaders.ETAG));
/*
* verifies that if the set etag is sent in, then the response returns a
* 412
*/
response =
client.resource(getBaseURI() + "/context/request/etag")
.header(HttpHeaders.IF_NONE_MATCH, setETag).post(null);
assertEquals(412, response.getStatusCode());
assertEquals(setETag, response.getHeaders().getFirst(HttpHeaders.ETAG));
/*
* verifies that a request without an If-None-Match header will still
* proceed
*/
response = client.resource(getBaseURI() + "/context/request/etag").get();
assertEquals(200, response.getStatusCode());
assertEquals("the etag: " + justTheTag + isWeak, response.getEntity(String.class));
/*
* verifies that a request without an If-None-Match header will still
* proceed
*/
response = client.resource(getBaseURI() + "/context/request/etag").get();
assertEquals(200, response.getStatusCode());
assertEquals("the etag: " + justTheTag + isWeak, response.getEntity(String.class));
/*
* verifies that an unquoted entity tag is invalid
*/
response =
client.resource(getBaseURI() + "/context/request/etag")
.header(HttpHeaders.IF_NONE_MATCH, setETag.substring(1, setETag.length() - 1))
.get();
assertEquals(400, response.getStatusCode());
/*
* verifies that a misquoted entity tag is invalid
*/
response =
client.resource(getBaseURI() + "/context/request/etag")
.header(HttpHeaders.IF_NONE_MATCH, setETag.substring(0, setETag.length() - 1))
.get();
assertEquals(400, response.getStatusCode());
/*
* verifies that a misquoted entity tag is invalid
*/
response =
client.resource(getBaseURI() + "/context/request/etag")
.header(HttpHeaders.IF_NONE_MATCH, setETag.substring(1, setETag.length())).get();
assertEquals(400, response.getStatusCode());
/*
* verifies that if an etag is sent that does not match the server etag,
* that request is allowed to proceed
*/
response =
client.resource(getBaseURI() + "/context/request/etag")
.header(HttpHeaders.IF_NONE_MATCH, "\"someothervalue\"").get();
assertEquals(200, response.getStatusCode());
assertEquals("the etag: " + justTheTag + isWeak, response.getEntity(String.class));
/*
* verifies that if multiple etags are sent that do not match the server
* etag, that the request is allowed to proceed
*/
response =
client.resource(getBaseURI() + "/context/request/etag")
.header(HttpHeaders.IF_NONE_MATCH, "\"austin\", \"powers\"").get();
assertEquals(200, response.getStatusCode());
assertEquals("the etag: " + justTheTag + isWeak, response.getEntity(String.class));
/*
* verifies that if multiple etags are sent that do not match the server
* etag, then a 200 and the request entity is returned
*/
response =
client.resource(getBaseURI() + "/context/request/etag")
.header(HttpHeaders.IF_NONE_MATCH, "\"austin\", \"powers\"").post(null);
assertEquals(200, response.getStatusCode());
assertEquals("the etag: " + justTheTag + isWeak, response.getEntity(String.class));
/*
* verifies that if multiple etags are sent that do match the server
* etag, that a 304 is returned
*/
response =
client.resource(getBaseURI() + "/context/request/etag")
.header(HttpHeaders.IF_NONE_MATCH, "\"austin\", " + setETag + " , \"powers\"")
.get();
assertEquals(304, response.getStatusCode());
assertEquals(setETag, response.getHeaders().getFirst(HttpHeaders.ETAG));
/*
* verifies that a request with an If-None-Match header will fail
*/
response =
client.resource(getBaseURI() + "/context/request/etag")
.header(HttpHeaders.IF_NONE_MATCH, "\"austin\", " + setETag + " , \"powers\"")
.post(null);
assertEquals(412, response.getStatusCode());
assertEquals(setETag, response.getHeaders().getFirst(HttpHeaders.ETAG));
}