How to blacklist headers in Rest Assured

HOME

 .config(RestAssured.config().logConfig(LogConfig.logConfig().blacklistHeader("Accept"))).log().headers()

import io.restassured.RestAssured;
import io.restassured.config.LogConfig;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;

public class BlackListDemo {

    @Test
    public void verifyUser() {

        // Given
        given()
                
                .config(RestAssured.config().logConfig(LogConfig.logConfig().blacklistHeader("Accept")))
                .log().headers()

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

                // Then
                .then()
                .statusCode(200).statusLine("HTTP/1.1 200 OK")
                .body("data.email", equalTo("janet.weaver@reqres.in"))
                .body("data.first_name", equalTo("Janet"))
                .body("data.last_name", equalTo("Weaver")).log().all();
    }

import io.restassured.RestAssured;
import io.restassured.config.LogConfig;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;

public class BlackListDemo {

    @Test
    public void verifyUser() {

        // Given
        given()

              .config(RestAssured.config().logConfig(LogConfig.logConfig().blacklistHeader("Accept","Content-Type")))
                .log().headers()

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

                // Then
                .then()
                .statusCode(200).statusLine("HTTP/1.1 200 OK")
                .body("data.email", equalTo("janet.weaver@reqres.in"))
                .body("data.first_name", equalTo("Janet"))
                .body("data.last_name", equalTo("Weaver")).log().all();
    }

import io.restassured.RestAssured;
import io.restassured.config.LogConfig;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;

public class BlackListDemo {

    @Test
    public void verifyUser1() {

        List headers = new ArrayList<String>();
        headers.add("Accept");
        headers.add("Content-Type");

        // Given
        given()
                .config(RestAssured.config().logConfig(LogConfig.logConfig().blacklistHeader(headers.toArray(new String[0]))))
                .log().headers()

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

                // Then
                .then()
                .statusCode(200).statusLine("HTTP/1.1 200 OK")

                // To verify booking id at index 3
                .body("data.email", equalTo("janet.weaver@reqres.in"))
                .body("data.first_name", equalTo("Janet"))
                .body("data.last_name", equalTo("Weaver")).log().all();
    }

}

2 thoughts on “How to blacklist headers in Rest Assured

  1. Great tutorial, thanks for sharing!

    I just wanted to point out that in the section “BlackList Request and Response using collection”, the provided code example doesn’t actually use the List that is created:

    List headers = new ArrayList<String>();

    headers.add(“Accept”);

    headers.add(“Content-Type”);

    Later, the code calls:

    .config(RestAssured.config().logConfig(LogConfig.logConfig().blacklistHeader(“Accept”,”Content-Type”)))

    But it doesn’t make sense, to make the example meaningful, it should use the list like this:

    .blacklistHeader(headers.toArray(new String[0]))

    That way, the list of blacklisted headers is actually applied dynamically.

    Just wanted to share this in case others wonder why the List was there but not used 😀

    Greetings from Germany!

    Like

Leave a reply to Alejandro Moreno Cancel reply