Sunday 24 November 2019

Rest Assured: Validate Response content type

'contentType' method is used to validate the response content type.

Example 1: Validate response code 200 and content type application/json.
given().header("Accept", "application/json").when().get("api/v1/employees/").then().assertThat().statusCode(200).and().contentType(ContentType.JSON);

Example 2:  Validate response code 200 and content type application/xml.
given().header("Accept", "application/xml").when().get("api/v1/employees/").then().assertThat().statusCode(200).and().contentType(ContentType.XML);

TestApp.java
package com.sample.app;

import org.junit.Test;
import static io.restassured.RestAssured.given;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;

public class TestApp {

    @Test
    public void testApp() {
        RestAssured.baseURI = "http://localhost:8080";

        given().header("Accept", "application/json").when().get("api/v1/employees/").then().assertThat().statusCode(200)
                .and().contentType(ContentType.JSON);

        given().header("Accept", "application/xml").when().get("api/v1/employees/").then().assertThat().statusCode(200)
                .and().contentType(ContentType.XML);

    }

}



Previous                                                    Next                                                    Home

No comments:

Post a Comment