Skip to main content

Response Validations

SHAFT API Response Validations​

Call assertThatResponse() for a hard assertion or verifyThatResponse() for a soft verification. Each response condition runs immediately.

Body​

Validate on the response body. Chains to JSON/body validation methods, including structural JSON equality that ignores array/object ordering.

api.assertThatResponse().body().contains("data");
api.assertThatResponse().body()
.equalsIgnoringOrder("{\"roles\":[\"admin\",\"tester\"]}");

Usage​

SHAFT.API api = new SHAFT.API("http://api.zippopotam.us/");
api.get("us/90210");
api.assertThatResponse().body().contains("Beverly Hills");

Extracted Json Value​

Validate an extracted value from the response body by parsing the target JSONPath.

info

You can learn the JSONPath syntax from the JSONPath documentation and test your expressions at jsonpath.com.

Chains to Object validation methods to continue building your validation.

api.assertThatResponse().extractedJsonValue("$.data").isEqualTo("data");

Usage​

SHAFT.API api = new SHAFT.API("https://jsonplaceholder.typicode.com");
api.get("/users");
api.assertThatResponse().extractedJsonValue("$[?(@.name=='Chelsey Dietrich')].id").isEqualTo("5");

Extracted Json Value As List​

Validate an extracted value from the response body by parsing the target JSONPath as a list and check every item against it.

Chains to Object validation methods to continue building your validation.

api.assertThatResponse().extractedJsonValueAsList("jsonPath").isEqualTo("data");

Usage​

SHAFT.API api = new SHAFT.API("https://jsonplaceholder.typicode.com");
api.get("/todos");
api.verifyThatResponse().extractedJsonValueAsList("$[?(@.completed==true)].completed").contains(true);

Time​

Validate on the response time. Chains to Number validation methods to continue building your validation.

api.assertThatResponse().time().isEqualTo(expectedNumberValue);

Usage​

SHAFT.API api = new SHAFT.API("http://api.zippopotam.us/");
api.get("us/90210");
api.verifyThatResponse().time().isGreaterThanOrEquals(100);
api.verifyThatResponse().time().isLessThanOrEquals(100000);

Is Equal To File Content​

Validate if the content of the provided actual response object is equal to the expected file content.

api.assertThatResponse().isEqualToFileContent("fileRelativePath");

Does Not Equal File Content​

Validate if the content of the provided actual response object is not equal to the expected file content.

api.assertThatResponse().doesNotEqualFileContent("fileRelativePath");

Is Equal To File Content Ignoring Order​

Validate if the content of the provided actual response object is equal to the expected file content while ignoring Order of the json objects.

api.assertThatResponse().isEqualToFileContentIgnoringOrder("fileRelativePath");

Does Not Equal File Content Ignoring Order​

Validate if the content of the provided actual response object is not equal to the expected file content while ignoring Order of the json objects.

api.assertThatResponse().doesNotEqualFileContentIgnoringOrder("fileRelativePath");

Contains File Content​

Validate if the content of the provided actual response object contains the expected file content.

api.assertThatResponse().containsFileContent("fileRelativePath");

Does Not Contain File Content​

Validate if the content of the provided actual response object does not contain the expected file content.

api.assertThatResponse().doesNotContainFileContent("fileRelativePath");

Matches Schema​

Validate if the content of the provided actual response object matches the schema for the expected file content.

api.assertThatResponse().matchesSchema("fileRelativePath");

Does Not Match Schema​

Validate if the content of the provided actual response object does not match the schema for the expected file content.

api.assertThatResponse().doesNotMatchSchema("fileRelativePath");

Response value builders​

These api.assertThatResponse() / api.verifyThatResponse() methods read a value from the completed response and return a validation builder:

MethodValue
statusCodeValue()Response status code as a number.
bodyValue()Whole response body.
headerValue(String name)One response header.
cookieValue(String name)One response cookie.
jsonValue(String path)One JSONPath result.
jsonValues(String path)A JSONPath list result.
responseTimeMillis()Response time in milliseconds.
matchesContract(String fileRelativePath)Validates the response against an order-insensitive JSON contract file.
api.assertThatResponse().statusCodeValue().isEqualTo(200);
api.assertThatResponse().jsonValue("$.name").isEqualTo("SHAFT");