Skip to content

Commit

Permalink
Test Rest Accept Header
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolovison committed Mar 9, 2018
1 parent dafb586 commit 6a57964
Showing 1 changed file with 26 additions and 3 deletions.
Expand Up @@ -332,6 +332,23 @@ public void testCustomKeysAndByteValues() throws Exception {
assertArrayEquals((byte[]) bytesFromRest, value);
}

@Test
public void testAcceptHeader() throws Exception {

String key = "a-1";
String value = "<foo><bar/><foo>";

defaultMarshalledRemoteCache.put(key, value);
assertEquals(value, (String) defaultMarshalledRemoteCache.get(key));

HttpMethod response = new RestRequest()
.cache(MARSHALLED_CACHE_NAME)
.key(key).accept("text/plain;q=0.7, application/xml;q=0.8, */*;q=0.6")
.readResponse();

assertEquals(response.getResponseHeader("content-type").getValue(), "application/xml");
}

String getEndpoint(String cache) {
return String.format("http://localhost:%s/rest/%s", restServer.getPort(), cache);
}
Expand All @@ -348,7 +365,7 @@ private class RestRequest {
private Object key;
private Object value;
private String keyContentType;
private MediaType accept;
private Object accept;
private String contentType;

public RestRequest cache(String cacheName) {
Expand Down Expand Up @@ -384,7 +401,7 @@ public RestRequest value(Object value) {
return this;
}

public RestRequest accept(MediaType valueContentType) {
public RestRequest accept(Object valueContentType) {
this.accept = valueContentType;
return this;
}
Expand All @@ -406,7 +423,8 @@ public void write() throws Exception {
assertEquals(post.getStatusCode(), HttpStatus.SC_OK);
}

public Object read() throws IOException {
public HttpMethod readResponse() throws IOException {

HttpMethod get = new GetMethod(getEndpoint(this.cacheName) + "/" + this.key);
if (this.accept != null) {
get.setRequestHeader(ACCEPT, this.accept.toString());
Expand All @@ -415,6 +433,11 @@ public Object read() throws IOException {
get.setRequestHeader("Key-Content-Type", this.keyContentType);
}
restClient.executeMethod(get);
return get;
}

public Object read() throws IOException {
HttpMethod get = readResponse();
assertEquals(get.getStatusCode(), HttpStatus.SC_OK);
return get.getResponseBody();
}
Expand Down

0 comments on commit 6a57964

Please sign in to comment.