diff --git a/src/test/java/org/elasticsearch/test/rest/client/RestResponse.java b/src/test/java/org/elasticsearch/test/rest/client/RestResponse.java index fd6df250f9e..8144aed97a3 100644 --- a/src/test/java/org/elasticsearch/test/rest/client/RestResponse.java +++ b/src/test/java/org/elasticsearch/test/rest/client/RestResponse.java @@ -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; diff --git a/src/test/java/org/elasticsearch/test/rest/section/DoSection.java b/src/test/java/org/elasticsearch/test/rest/section/DoSection.java index 55902ffdc56..cf15029a9df 100644 --- a/src/test/java/org/elasticsearch/test/rest/section/DoSection.java +++ b/src/test/java/org/elasticsearch/test/rest/section/DoSection.java @@ -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>> catches = Maps.newHashMap();