REST Assured: Validating HTTP Response Status Code, Line, Body, Headers, Content Type

HOME

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

@Test
    public void verifyStatusCode() {

        String BaseURL = "https://dummy.restapiexample.com/api";

        // GIVEN
       Response response =  given()

                // WHEN
                .when()
                .get("https://reqres.in/api/users/2")

                // THEN
                .then()
               .extract().response();

       int actualStatusCode = response.getStatusCode();
       System.out.println("Status Code : " + actualStatusCode);

        Assert.assertEquals(200, actualStatusCode);

    }

 @Test
    public void verifyStatusLine() {

        // GIVEN
        Response response =  given()

                // WHEN
                .when()
                .get("https://reqres.in/api/users/2")

                // THEN
                .then()
                .extract().response();

        String actualStatusLine = response.getStatusLine();
        System.out.println("Status Line : " + actualStatusLine);

        Assert.assertEquals("HTTP/1.1 200 OK", actualStatusLine);

    }

  @Test
    public void verifyResponseBody() {

        // GIVEN
        Response response =  given()

                // WHEN
                .when()
                .get("https://reqres.in/api/users/2")

                // THEN
                .then()
                .extract().response();

        String actualResponseBody = response.getBody().asString();
        System.out.println("Response Body : " + actualResponseBody);

        JsonPath jsonPath = response.jsonPath();
        String actualResponse_Id = jsonPath.getString("data.id");
        System.out.println("Response Id : " + actualResponse_Id);

        Assert.assertEquals("2", actualResponse_Id);

    }

@Test
    public void verifyResponseHeader() {

        // GIVEN
        Response response =  given()

                // WHEN
                .when()
                .get("https://reqres.in/api/users/2")

                // THEN
                .then()
                .extract().response();

        Headers allHeaders = response.getHeaders();
        System.out.println("All Headers : " + allHeaders);

       for(Header header: allHeaders){
           System.out.println(header.getName() +  ":" + header.getValue());
       }

       Assert.assertTrue(allHeaders.getValue("Content-Encoding").contains("gzip"));

    }

 @Test
    public void verifyContentType() {

        // GIVEN
        Response response =  given()

                // WHEN
                .when()
                .get("https://reqres.in/api/users/2")

                // THEN
                .then()
                .extract().response();

        String actualContentType = response.getContentType();
        System.out.println("Actual ContentType : " + actualContentType);

         Assert.assertEquals("application/json; charset=utf-8", actualContentType);

    }

Leave a comment