ATS : REST Operations - Working with the response

You should always carefully examine the response otherwise you will not be confident in the success of your REST call.

Additionally, often you will need to get data from the response in order to decide how to continue your test execution.


Specify response media type

You can specify the response media type in the same ways as you do with the request media type:

// receive as text/plain
client.setResponseMediaType( RestMediaType.TEXT_PLAIN );

// receive as text/html
client.setResponseMediaType( RestMediaType.TEXT_HTML );

// receive as application/xml
client.setResponseMediaType( RestMediaType.APPLICATION_XML );

 


Explore the response

Every REST method returns a RestResponse and here are some useful methods it has:

// get status code, for example "200"
int statusCode = response.getStatusCode();

// get status message, for example "SUCCESS"
String statusMessage = response.getStatusMessage();

// get received headers, you can iterate them and read their "name" and "value"
Rest	Header[] headers = response.getHeaders();

// get received body as a String
String body = response.getBodyAsString();

// get received body as java Object
Person person = response.getBodyAsObject( Person.class );

 


Verify the response

Every HTTP method could be verified by header, body, status code, status message

ATS verify methods check if the condition is met, if not an exception is thrown and this fails the test with an appropriate error message.

	// verify status code, for example "200"
response.verifyStatusCode(200);

// verify status message, for example "SUCCESS"
response.verifyStatusMessage("SUCCESS");
 
// verify JSON 
response.verifyJsonBody("age", "30");

// verify received header, e.g. header name "Content-Type", with value "text/html"
response.verifyHeader( "Content-Type", "text/html" );

// verify part of response body, e.g. response body "Some simple page response"
response.verifyBodyContent("page");
 
// verify full body, e.g. response body "Some simple page response"
response.verifyBodyMatch("Some simple page response");

// verify response body by REGEX, e.g. response body "Some simple page response"
response.verifyBodyRegex(".*page\\D");

 


Back to parent page

Go to Table of Contents