How to Test Status Line of API by Rest Assured

 

To test the status line of a GET API using Rest Assured in Java, you need to understand what a status line is and how to retrieve it.


What is a Status Line?

The status line in an HTTP response consists of:

  • HTTP version (example: HTTP/1.1)

  • Status code (example: 200)

  • Reason phrase (example: OK)


Example:


HTTP/1.1 200 OK


Steps to Test Status Line Using Rest Assured

  • Set the base URI.
  • Use given() to prepare the request.
  • Use .when().get() to make the GET call.
  • Use .then().statusLine() to verify the status line.
  • You can also extract the status line using .extract().statusLine().


Maven Dependencies:


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





Java Code Using Rest Assured to Test Status Line


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

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

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

        // Send GET request and validate status line
        given()
            .when()
            .get("/api/users?page=2")
            .then()
            .assertThat()
            .statusLine("HTTP/1.1 200 OK");  // Validate full status line

        // Optionally, print the actual status line
        Response response = get("/api/users?page=2");
        String actualStatusLine = response.getStatusLine();
        System.out.println("Status Line is: " + actualStatusLine);
    }
}




Output:


Status Line is: HTTP/1.1 200 OK




At a glance:

  • Use .statusLine("HTTP/1.1 200 OK") to validate the exact status line.
  • Use .getStatusLine() to fetch and print it.
  • This is useful to ensure your API response is well-formed and meets expectations.

No comments:

Post a Comment