API Testing with REST Assured-II

Malsha Nishadini
4 min readFeb 24, 2020

--

In my previous article, I explained a REST Assured Test which is made a call to a web service. Today I’m going to write tests to verify the responses returned from various endpoints using REST Assured. (Note that all the tests are carried out based on the weather web service mentioned in the previous article)

HTTP Response contains status, headers & a body. We are able to read these status, headers and body using REST Assured methods and we can validate them using TestNG Assert. So let’s see how to do those verifications and validations.

Verify & Validate Response Status

When we consider the response status, we can write tests to verify & validate Response Status Code, Error Status Code and Response Status Line.

Testing Response Status Code

@Test
public void GetWeatherDetails()
{
RestAssured.baseURI = "http://restapi.demoqa.com/utilities/weather/city";
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.request(Method.GET, "/Colombo");

// Get the status code from the Response.
int statusCode = response.getStatusCode();

// Assert that the correct status code is returned.
Assert.assertEquals(statusCode, 200, "Correct status code is not returned");
}

We can get response status code using getStatusCode() method which is written in the Response interface in the package io.restassured.response. That method returns an integer value and and above test verify the value of this integer using assertEquals() method in TestNG. In this test, web service returns 200 status code and the test will pass.

Testing Error Status Code

@Test
public void GetWeatherDetails()
{
RestAssured.baseURI = "http://restapi.demoqa.com/utilities/weather/city";
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.request(Method.GET, "/city123");
int statusCode = response.getStatusCode();
Assert.assertEquals(statusCode, 200, "Correct status code is not returned");
}

Error status code is returned when the request is not successful and the status code can not be 200. Above test will fail because the web service returns an error code of 400 when invalid city name(/city123) is sent to it.

Refer HTTP response status codes to find out more about various response status codes.

Testing Response Status Line

@Test
public void GetWeatherDetails()
{
RestAssured.baseURI = "http://restapi.demoqa.com/utilities/weather/city";
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.request(Method.GET, "/Colombo");

// Get the status line from the Response
String statusLine = response.getStatusLine();
Assert.assertEquals(statusLine, "HTTP/1.1 200 OK", "Correct status code is not returned");
}

Status line is the first line which is returned in the response and it includes http protocol version, status code and status code’s string value respectively.

Ex:

Status line of a success request — “HTTP/1.1 200 OK”

Http protocol — HTTP/1.1.

Status Code — 200

Status message — OK

We can get status line using the getStatusLine() method in the response interface. This test verifies the status line of a success request and it will pass.

Verify & Validate Response Headers

Headers are used to send extra information(Metadata) of the response. For example, Content-Type header indicates the media type of the resource. If the response body contains data in the JSON form, then the Content-Type will be application/json and if the response body data in the XML form, then the Content-Type will be application/xml.

Refer HTTP headers to find out more about various headers sent by the server.

REST Assured provides the ability to read different header types from the response. We can use getHeader(String headerName) or header(String headerName) to get the exact header and value of the headers can be validated using TestNG Assertions. Here, I’m going to test Content-Type, Server and Content-Encoding headers.

@Test
public void GetWeatherDetails()
{
RestAssured.baseURI = "http://restapi.demoqa.com/utilities/weather/city";
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.request(Method.GET, "/Colombo");

// Get Content-Type Header
String contentType = response.header("Content-Type");
Assert.assertEquals(contentType, "application/json");

// Get Server Header
String server = response.header("Server");
Assert.assertEquals(server, "nginx");

// Get Content-Encoding Header
String contentEncoding = response.header("Content-Encoding");
Assert.assertEquals(contentEncoding, "gzip");
}

Verify & Validate Response Body

REST-Assured also provides various methods through Response interface to read response body. We can simply get the JsonPath object and then query for the particular node using jsonPath() method which is written in the Response interface in the package io.restassured.path.json.JsonPath. It’s the best way to get the response body for the verifications and validations. In the following test, I’m going to retrieve City node from the response and validate it.

@Test
public void GetWeatherDetails()
{
RestAssured.baseURI = "http://restapi.demoqa.com/utilities/weather/city";
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.request(Method.GET, "/Colombo");

// Get the JsonPath object instance from the Response interface
JsonPath jsonPathObject = response.jsonPath();

// Query the JsonPath object to get a String value of the node
String city = jsonPathObject.get("City");

// Print the city variable to see what we got
System.out.println("City received from Response " + city);

// Validate the response
Assert.assertEquals(city, "Colombo", "Correct city name received in the Response");
}

I hope that you will be able to get an idea about how to verify and validate HTTP response using various simple methods provided by REST Assured.

Thank you !!!

--

--