How to Test Status Line of API by Rest Assured

 



What is the Status Line of an API?

  • The status line is the first line of the HTTP response sent by the server.
  • It provides three parts:

HTTP Version → Example: HTTP/1.1 or HTTP/2
Status Code → Example: 200, 404, 500
Reason Phrase → Example: OK, Not Found, Internal Server Error

Example of a status line:
HTTP/1.1 200 OK

In this,
  • HTTP/1.1 → HTTP protocol version
  • 200 → Status code (success)
  • OK → Reason phrase

How to Test Status Line of API in Rest Assured

When testing an API’s status line using Rest Assured, you:
  • Send the API Request (GET, POST, etc.).
  • Extract the Status Line from the response.
  • Verify the Status Line against the expected value.

What to Check in Status Line:
  • Ensure HTTP version is correct (e.g., HTTP/1.1).
  • Ensure status code is as expected (e.g., 200).
  • Ensure reason phrase is correct (e.g., OK).


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.
















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().

API to be automated: 
https://reqres.in/api/users?page=2


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);
    }
}



Code explanation:

1. Set the base URI
2. Send GET API request
3. validate status line
4. Print the actual status line on console



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.


Suggested Posts:

1. Test Form Authentication in RestAssured
2. How to extract Response in Rest Assured by JSONPath
3. Test OAth2 in RestAssured
4. How to Test SOAP API by RestAssured
5. How to Test Basic Authentication in RestAssured

How to Test DELETE API by Rest Assured

 



What is a DELETE API?

  • A DELETE API is an HTTP method used to remove a resource from a server.
  • It is part of the standard CRUD operations (Create, Read, Update, Delete).
  • Example scenario: If your API has a list of users, a DELETE request to /users/5 would remove the user with ID 5.

Key Points:
  • DELETE requests usually don’t return a body, but sometimes may return a confirmation message.
  • After a DELETE operation, the resource should no longer exist.
  • The server typically returns:
Status 200 (OK) if deletion is successful and a response body is returned.
Status 204 (No Content) if deletion is successful but no content is returned.
Status 404 (Not Found) if the resource does not exist.


How to Test a DELETE API in Rest Assured

Testing a DELETE API is similar to other HTTP methods:
  • Set the Base URI and Endpoint: Specify the API URL and the resource to delete.
  • Send the DELETE Request: Make a DELETE call to the resource endpoint.
  • Validate the Response:
Check the HTTP status code (200, 204, or 404 depending on the case).
Optionally, check response headers (like Content-Type).
Optionally, verify response body if the API sends a confirmation message.
  • Verify Deletion:
Perform a GET request on the same resource to ensure it is no longer available (should return 404).


To test a DELETE API using Rest Assured in Java, you follow a standard procedure:



API used for DELETE operation:

DELETE https://reqres.in/api/users/2

This would delete user with ID 2.














Steps to Test DELETE API via Rest Assured:

  • Set the Base URI
  • Specify the Endpoint (example: /users/2)
  • Use given() to prepare request
  • Use .when().delete() to send the request
  • Use .then() to validate the response (status code, etc.)

Expected Response from https://reqres.in/api/users/2:

The ReqRes API will respond with HTTP status code 204 No Content which indicates successful deletion, but no body is returned.


Maven Dependencies

<dependencies>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20230227</version>
    </dependency>
</dependencies>




Java Code using Rest Assured:

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

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

public class DeleteUserTest {

    public static void main(String[] args) {

        // Step 1: Set Base URI
        RestAssured.baseURI = "https://reqres.in/api";

        // Step 2: Perform DELETE request and validate
        Response response = given()
                                .when()
                                .delete("/users/2") // Endpoint
                                .then()
                                .statusCode(204) // Assert expected status code
                                .extract().response();

        // Step 3: Print response for debugging (though it should be empty)
        System.out.println("Status Code: " + response.getStatusCode());
        System.out.println("Response Body: '" + response.getBody().asString() + "'");
    }
}


Code explanation:

1. Set the base URI
2. Perform delete request and validate the response server code
3. Print the server code and response body


Just to Inform:
  • statusCode(204) ensures deletion is successful.
  • The response body should be empty (""), as per REST standard for DELETE.


Suggested Posts:

1. Validate XML Schema in RestAssured
2. How to extract Response in Rest Assured by JSONPath
3. Test OAth2 in RestAssured
4. Validate Request and Response in POJO by RestAssured
5. How to Test Basic Authentication in RestAssured