REST Assured Tutorial
Validate Response Header
How To Validate Response Header in REST Assured ?
REST Assured facilitates us to fetch and validate the header of REST APIs.Rest API has a set of headers that can be divided into two parts as 'Header name' and 'Header value'. A class 'Headers' is provided by REST Assured for fetching the entire headers :
import io.restassured.http.Headers;
Headers allHeaders = response.headers();
import io.restassured.http.Header;
To fetch header name : Header header = header.getName()
To fetch header value : Header header = header.getValue()
When headers have been fetched then assertions are done on it :
import org.testng.Assert;
Assert.assertEquals(header.getName(), expectedHeaderName[i], "Incorrect Header Name Returned"); // Validating header name
Assert.assertEquals(header.getValue(), expectedHeaderValue[i], "Incorrect Header Value Returned"); // Validating header value
Assert.assertEquals(header.getName()+header.getValue(), expectedHeaderName[i]+expectedHeaderValue[i], "Incorrect Header Returned"); //
Validating both at the same time
Here's an Sample Code for Validating Response Header Names and Values :
package apiTesting; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.http.Header; import io.restassured.http.Headers; import io.restassured.specification.RequestSpecification; import org.testng.Assert; import org.testng.annotations.BeforeClass; public class ValidateResponseHeader { RequestSpecification httpRequest; io.restassured.response.Response response; String expectedHeaderName[] = { "Content-Type", "Content-Length", "Date", "Keep-Alive", "Connection" }; String expectedHeaderValue[] = { "application/json", "493", "", "timeout=20", "keep-alive" }; @Test public void validateResponseHeaderName() { Headers allHeaders = response.headers(); int i = 0; for (Header header : allHeaders) { Assert.assertEquals(header.getName(), expectedHeaderName[i], "Incorrect Header Name Returned"); i++; } } @Test public void validateResponseHeaderValue() { Headers allHeaders = response.headers(); int i = 0; for (Header header : allHeaders) { if (i != 2) // To skip the Date Header Assert.assertEquals(header.getValue(), expectedHeaderValue[i], "Incorrect Header Value Returned"); i++; } } @Test public void validateResponseHeaderName_Value() { Headers allHeaders = response.headers(); int i = 0; for (Header header : allHeaders) { if (i != 2) // To skip the Date Header Assert.assertEquals(header.getName() + header.getValue(), expectedHeaderName[i] + expectedHeaderValue[i], "Incorrect Header Returned"); i++; } } @BeforeClass public void apiSetUP() { RestAssured.baseURI = "http://localhost:8080/msg/webapi"; httpRequest = RestAssured.given(); response = httpRequest.get("/userProfiles"); } }
Output :
[RemoteTestNG] detected TestNG version 6.14.3
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
PASSED: validateResponseHeaderName
PASSED: validateResponseHeaderName_Value
PASSED: validateResponseHeaderValue
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
=============================================== ===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
Validating A Particular Response Header :
io.restassured.response.Response response = httpRequest.get("/Books");
response.header(Write header name here);
package apiTesting; import org.testng.Assert; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.specification.RequestSpecification; public class ValidateSpecificHeader { @Test public void validateHeader_ContentLength() { RestAssured.baseURI = "http://localhost:8080/msg/webapi";
RequestSpecification httpRequest = RestAssured.given();
io.restassured.response.Response response = httpRequest.get("/userProfiles");
Assert.assertEquals(response.header("Content-Length"), "493", "Incorrect Header Content-Length Returned");
}
}
Output :
[RemoteTestNG] detected TestNG version 6.14.3
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
PASSED: validateHeader_ContentLength
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================