In this article, we will discuss how to validate the HTTP response status using REST Assured. Every HTTP Response received from a server in response to an HTTP request sent by the client has a status code. This value tells us whether the response was successful or not. We will continue from where we left off in our previous article, where we performed a sample REST API test call. The content of this article is organized according to the following index:
- Verifying HTTP Response Status with Rest Assured
- Validation of HTTP Response Status Codes
- Verifying the Accuracy of the HTTP Error Status Code
- Checking the Validity of an HTTP Response Status Line
Verifying HTTP Response Status with Rest Assured
A response code indicating the status of the request (e.g., 200 OK, 404 Not Found).
An optional set of response headers.
The optional body of the response, containing the server’s response data.
An HTTP Response object typically consists of an HTTP packet (response packet) sent back by a Web Service Server in response to a client request. It contains a response code indicating the status of the request (e.g., 200 OK, 404 Not Found), an optional set of response headers, and the optional body of the response containing the server’s response data.
When we say we need to validate an HTTP response status, what we mean is having a process to read and validate all the response components, such as the status, headers, and body. In this article, we will address the validation of an HTTP response status in three parts:
- Verifying HTTP Response Status Code
- Validation Techniques for Error Status Codes
- Verifying Response Status Code Line
The response message from a REST API can vary depending on the Media-Type specified in the HTTP request. This Media-Type attribute determines whether the response message is returned in XML or JSON format.
The response header contains a content type attribute that informs the client about the type of response body format. This will typically be either text/plain or application/json.
Consider the https://reqres.in/api/users?page=2 example . Suppose we send a GET request to the Book Store through POSTMAN tool as follows:
From the screenshot, the response has a status, headers, and a body. Checking the “Response headers” section, in the above screen, we can see that the content-type attribute has the value “application/json”. This tells the client that the response contains JSON data.
Let us now proceed with validating the status part of the response.
Validation Techniques for Error Status Codes
When the client requests a piece of particular information from the server, the server sends a response with a status code back to the client. The status code that the server returns tells us whether the request was successful or not. If the request was successful, the server sends the status code in the range of 200-299. If the request was not successful, then the status code other than the range is returned. We can get the list of HTTP status codes along with their description on the W3 page.
Methods of the response interface include:
- getBody() : Returns the body of the response as a string.
- getHeaders() : Returns a list of the HTTP headers of the response.
- getStatusCode() : Returns the HTTP status code of the response.
The getStatusCode() method returns an integer and then we can verify its value.
TestNG Assert is used to verify the Status Code. Now consider the code given below:
The below line of code extracts the status code from the message:
If the statusCode value is equal to 200, then the appropriate message is returned.
Assert.assertEquals(statusCode, 200,"Status Code in correct");
If you run the above test, you will see that the test pass since the web service returns the status code as 200
Verifying Response Status Code Line
A “Status-Line” is the first line returned in the HTTP response. The status line consists of three substrings: the protocol, the status code, and the message.
The protocol is "HTTP/1.1". The status code is "200". The message is "OK".
when the request is successful the status line will have the value “HTTP/1.1 200 OK“. Here, the first part is the HTTP protocol (HTTP/1.1). Next is the HTTP status code (200). The third is the status message (OK).
We can read the entire status line using the method getStatusLine() of the response interface. The following code shows the demonstration.
We read the status line using the getStatusLine() method into a string value. Then we compare this returned value with “HTTP/1.1 200 OK” to check if the status is successful.