Validate Response Header using Rest Assured

HTTP headers is additional information is passed between the client and the server with an HTTP response. Thus, it can be said that each response can have greater or equals to zero headers. Each header entry is basically a Key-Value pair. This additional information is also known as Metadata of the Response.  An HTTP header consists of its case-insensitive name followed by a colon (:), then by its value. Whitespace before the value is ignored.

 

Read different Header Types from HTTP Response

Let us read the below header types from the response

We would use the method, .header(String arg0) to get a particular header and pass the header name in the argument.

1. Content-Type: It is used to indicate the media type of the resource. If the data present in the Body of the Response in the form of JSON, then the value of the Content-Type header will be application/json. And if the data is in the form of XML, then the Content-Type header will be application/xml.

Example:

//Read the header Content-Type

String contentType = response.header("content-type");


//Compare it with Expected Result 

Assert.assertEquals(contentType, "application/json");

 

2. Server: It describes the software used by the origin server that handled the request

Example:

//Read the header Server

String serverType = response.header(“Server”);


//Compare it with Expected Result

Assert.assertEquals(serverType, "Jetty(9.2.9.v20150224)");

 

3. Transfer-Encoding: It is used to specifies the used encoding form to safely transfer the payload body to the user.

Example:

//Read the header Transfer-Encoding

String encodingType = response.header(“transfer-encoding”);


//Compare it with Expected Result

Assert.assertEquals(encodingType, "chunked");


Complete Example Code 
@Test 
public void GetDetail() {

RestAssured.baseURI = "https://petstore.swagger.io/v2/store/order";
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.get("/1");

// Get the header of content-type
String contentType= response.header("content-type");

// Assert that correct content type header is returned.
Assert.assertEquals(contentType, "application/json", "Correct content type returned");

}
}

Method2:

The above can also be written as below examples:

Example1:

Validate the header of Content-Type

.then().assertThat().header(CONTENT-TYPE, “application/json”);


Example2:

Validate the header Server

.then().assertThat().header(Server, “application/json”);


Example3:

Validate the header Transfer-Encoding

.then().assertThat().header(Transfer-Encoding, “chunked”);


Complete Example Code
@Test 
public void GetDetail() {

RestAssured.baseURI = "https://petstore.swagger.io/v2/store/order";

RestAssured.given()

.when()

.get("/1")

//You can also validate the content type returned is not null
.then().assertThat().header("content-type", notNullValue());

}

 

Practice Yourself: Validate Server and Transfer-Encoding headers using both the methods.

 

Note: Response.GetHeader(String headerName) method does exactly the same thing as that of Response.Header(String headerName) method. Thus, the above can be written with replacing .Header() with .GetHeader(). Try it out yourself.

 

Get All Headers from HTTP Response

Response interface provides two methods

headers() : returns Headers

getHeaders() : returns Headers