[TEST] parse response body as json depending on the content-type in our REST tests
This commit is contained in:
parent
4ab268bab2
commit
fd6798df69
|
@ -44,7 +44,21 @@ public class RestResponse {
|
||||||
return response.getReasonPhrase();
|
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();
|
return response.getBody();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,6 +90,11 @@ public class RestResponse {
|
||||||
return jsonPath.evaluate(path);
|
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 {
|
private JsonPath parsedResponse() throws IOException {
|
||||||
if (parsedResponse != null) {
|
if (parsedResponse != null) {
|
||||||
return parsedResponse;
|
return parsedResponse;
|
||||||
|
|
|
@ -122,7 +122,7 @@ public class DoSection implements ExecutableSection {
|
||||||
|
|
||||||
private String formatStatusCodeMessage(RestResponse restResponse, String expected) {
|
private String formatStatusCodeMessage(RestResponse restResponse, String expected) {
|
||||||
return "expected [" + expected + "] status code but api [" + apiCallSection.getApi() + "] returned ["
|
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();
|
private static Map<String, Tuple<String, org.hamcrest.Matcher<Integer>>> catches = Maps.newHashMap();
|
||||||
|
|
Loading…
Reference in New Issue