[TEST] parse response body as json depending on the content-type in our REST tests

This commit is contained in:
javanna 2014-09-11 14:23:01 +02:00 committed by Luca Cavanna
parent 4ab268bab2
commit fd6798df69
2 changed files with 21 additions and 2 deletions

View File

@ -44,7 +44,21 @@ public class RestResponse {
return response.getReasonPhrase();
}
public String getBody() {
/**
* Returns the body properly parsed depending on the content type.
* Might be a string or a json object parsed as a map.
*/
public Object getBody() throws IOException {
if (isJson()) {
return parsedResponse().evaluate("");
}
return response.getBody();
}
/**
* Returns the body as a string
*/
public String getBodyAsString() {
return response.getBody();
}
@ -76,6 +90,11 @@ public class RestResponse {
return jsonPath.evaluate(path);
}
private boolean isJson() {
String contentType = response.getHeaders().get("Content-Type");
return contentType != null && contentType.contains("application/json");
}
private JsonPath parsedResponse() throws IOException {
if (parsedResponse != null) {
return parsedResponse;

View File

@ -122,7 +122,7 @@ public class DoSection implements ExecutableSection {
private String formatStatusCodeMessage(RestResponse restResponse, String expected) {
return "expected [" + expected + "] status code but api [" + apiCallSection.getApi() + "] returned ["
+ restResponse.getStatusCode() + " " + restResponse.getReasonPhrase() + "] [" + restResponse.getBody() + "]";
+ restResponse.getStatusCode() + " " + restResponse.getReasonPhrase() + "] [" + restResponse.getBodyAsString() + "]";
}
private static Map<String, Tuple<String, org.hamcrest.Matcher<Integer>>> catches = Maps.newHashMap();