Showing posts with label Automation Testing Tutorial. Show all posts
Showing posts with label Automation Testing Tutorial. Show all posts

How to Test a Single Response Header by Rest Assured

 



What is response header in API?

A response header in an API is metadata returned by the server along with the body of the response. It provides additional information about the response, such as:

  • Content-Type → tells the format of the response (e.g., application/json, text/html).
  • Content-Length → specifies the size of the response body.
  • Server → identifies the server software handling the request.
  • Cache-Control → gives caching directives to the client.
  • Set-Cookie → passes cookies from server to client.

These headers don’t contain the main data (which is usually in the response body), but they are critical for understanding how to interpret and handle the response.


When testing with Rest Assured, you usually verify that:

1. The response contains a specific header

  • Example: Check if the header Content-Type is present.
2. The header has the expected value
  • Example: Verify that Content-Type equals application/json.
3. The header value matches certain conditions
  • Example: Check if the value starts with application/.
In Rest Assured, testing a single response header means making an API request, capturing the response, and then asserting the value of that particular header against the expected outcome.








We want to:

  • Send a GET request to https://reqres.in/api/users?page=2
  • Extract and validate a specific response header (example: Content-Type)

Below is the screenshot of response of API: https://reqres.in/api/users?page=2













To test a single response header in a GET API using Rest Assured in Java, you can follow these steps:

  • Set the base URI
  • Send a GET request
  • Extract the specific header
  • Validate it using assertion



Sample Java Code Using Rest Assured

import io.restassured.RestAssured;
import io.restassured.response.Response;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

public class GetResponseHeaderTest {

    public static void main(String[] args) {
        
        // Step 1: Set base URI
        RestAssured.baseURI = "https://reqres.in";

        // Step 2: Send GET request and get response
        Response response = RestAssured
                .given()
                .when()
                .get("/api/users?page=2")
                .then()
                .extract()
                .response();

        // Step 3: Print and verify a single header, e.g. Content-Type
        String contentType = response.getHeader("Content-Type");
        System.out.println("Content-Type header is: " + contentType);

        // Step 4: Assert the header
        assertThat("Header validation failed", contentType, equalTo("application/json; charset=utf-8"));
    }
}


Code explanation:
  • Set base URI
  • Send GET request and get response from API
  • Print a single response header names content type
  • Assert the response header


Maven Dependency:

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.4.0</version>
    <scope>test</scope>
</dependency>


Suggested Posts:

1. Test Single Response Header in RestAssured
2. Test Digesh Auth in RestAssured
3. Test DELETE API in RestAssured
4. First RestAssured Code to Test API
5. How to Test Basic Authentication in RestAssured