REST assured has been of great use to us for testing APIs and validating a lot of things. For instance, in the last tutorials, we performed a sample Test call using Rest Assured and also validated response status. But that is just one part of the overall response testing when it comes to automation. There is a lot more that we need to validate in order to be an efficient tester and test the quality of our APIs. In this article, let us validate the Response Header using Rest Assured with the following topics in focus:
- What is an HTTP Response Header in REST API?
- REST Assured methods for validating HTTP Response Headers.
- How to access and read HTTP Response headers using REST Assured?
- How to validate Response Header using Rest Assured.
What is an HTTP Response Header in REST API?
The response received from the server consists of zero or more headers along with response status and response body. Each header is a key-value pair. The header part of the response is used by the server to send extra information which is also referred to as "Metadata" of the response.
For example, headers contain a "Content-Type" attribute that tells us how to interpret the data of the response body. So if the response body contains JSON data, then the corresponding content-type attribute in the header will be "application/json". Similarly, if the data in the body is XML the Content-Type header will be "application/xml".
For example, for the following request URL: https://demoqa.com/BookStore/v1/Books
when we give the following GET request:
curl -X GET "https://demoqa.com/BookStore/v1/Books" -H "accept: application/json"
we get the following response.
Note the response header that is obtained (red rectangle). Since the body is JSON, the Content-Type is set to "application/JSON".
Rest Assured methods for validating HTTP Response Headers
The Response interface of the Rest Assured library provides methods to access all headers or individual headers. Just typing "Response.head" in Eclipse (or any such editor) will show all the methods supported to access headers.
As shown in the above screenshot, the Response interface of REST Assured provides methods related to headers.
- headers() : returns Headers
- getHeader(): returns a Header
- getHeaders(): returns Headers
When all the headers in a Response are returned, we can print each header by simply iterating over each of them.
Note: The collection of headers is represented by a class called io.restassured. HTTP.Headers. This class implements the Iterable interface. Hence, we can use the "for each (for( : ))" loop to read all the headers.
How to access and read HTTP Response headers using REST Assured?
Now let us see how we can read a Header using Rest-Assured. Let’s write a test to record the following Header Types from the Response:
- Content-Type.
- Server.
- Content-Encoding.
Shown below is the code for this test:
@Test
public void IteratingHeaders()
{ RestAssured.baseURI = "https://demoqa.com/BookStore/v1/Books";
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.get("");
// Get all the headers and then iterate over allHeaders to print each header
Headers allHeaders = response.headers();
// Iterate over all the Headers
for(Header header : allHeaders) {
System.out.println("Key: " + header.getName() + " Value: " + header.getValue());
}
}
In the above code, we access all headers and then extract individual headers by looping through the Headers collection. Shown below is the console output of the above test.
The above output shows all the header key-value pairs that we displayed using for loop in the code.
Let us demonstrate the .header(String arg0) method to get a particular header. Here we pass the exact header name as an argument.
@Test
public void GetBookHeaders() {
RestAssured.baseURI = "https://demoqa.com/BookStore/v1/Books";
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.get("");
// Access header with a given name.
String contentType = response.header("Content-Type");
System.out.println("Content-Type value: " + contentType);
// Access header with a given name.
String serverType = response.header("Server");
System.out.println("Server value: " + serverType);
// Access header with a given name. Header = Content-Encoding
String acceptLanguage = response.header("Content-Encoding");
System.out.println("Content-Encoding: " + acceptLanguage);
}
}
}
In the above code, we are accessing Content-Type, Server, and Content-Encoding headers by specifying respective headers names as an argument to the header() method. Shown below is the console output of the above code.
Note: Response.GetHeader(String headerName) method behaves exactly the same way as the Response.Header(String headerName) method. So the above code can be written by replacing the ".Header()" method with the ".GetHeader()" method.
How to validate HTTP Response Header using Rest Assured?
Now that we have had a discussion about methods that access header, let us write a test to validate the values of the header by putting it as Assert. The below code shows the test that is similar to the code in previous sections but only uses Assert to validate the results.
@Test
public void ValidateBookHeaders()
{
RestAssured.baseURI = "https://demoqa.com/BookStore/v1/Books";
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.get("");
// Access header with a given name. Header = Content-Type
String contentType = response.header("Content-Type");
Assert.assertEquals(contentType /* actual value */, "application/json; charset=utf-8" /* expected value */);
// Access header with a given name. Header = Server
String serverType = response.header("Server");
Assert.assertEquals(serverType /* actual value */, "nginx/1.17.10 (Ubuntu)" /* expected value */);
In the above code, we verify the actual value of each header viz., Content-Type, Server, and Content-Encoding with the expected value. Shown below is the screenshot of the test result.
This output indicates that the actual value and expected value of each header matches.
Now suppose we provide the expected value of Content-Type to "application/XML" in the above code as below.
Assert.assertEquals(contentType /* actual value /, "application/xml" / expected value */);
If we run the same test above after the change, we will get the following output.
As we can see, the difference in actual value "application/JSON" and the expected value "application/XML" raises an assertion error.
In this way by using the header-specific methods of Response interface, we can validate the Response Header in Rest API.
Note: The video tutorial on the same topic can be found at Verify Response Headers in Rest Assured.
Key TakeAways
In this article, we discussed the ways to validate the response header in REST using Rest Assured.
- Every response obtained from the server may contain zero or more headers that provide metadata about the response.
- Rest Assured provides various methods using which we can read individual or all headers sent by the server.
- headers() and getHeaders() return a collection of headers that is iterable for individual entries. The method "getHeader()" and header() returns individual header whose name is specified as an argument.
- Using these methods and the TestNG Assert we can then validate the headers by comparing the actual and expected values of each header.
After validating the status and headers of the response, we will move on to reading the actual Response body in the next article.