Search code examples
javaspring-bootrest-assured

Add header to all requests


Is there any way to add an header to all requests?

I have to add an header to all requests. In production environments the header is added by a proxy.

Adding this header manually in all tests is annoying.


Solution

  • As @pvpkiran suggested I've created the Filter

    public class AddHeadersFilter implements Filter {
    
        @Override
        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            requestSpec.header(new Header(IntegrationBaseTest.HEADER_USER_NAME, "test-user"));
    
            return ctx.next(requestSpec, responseSpec);
        }
    
    }
    

    Then I've added it to all tests

        @BeforeClass
        public static void configureRestAssured() {
            RestAssured.filters(new AddHeadersFilter());
        }
    

    Seems to work.

    I've also added (I hope) helpful configuration

            HeaderConfig headerConfig = headerConfig()
                    .overwriteHeadersWithName(HEADER_USER_NAME);
            RestAssured.config().headerConfig(headerConfig);
    

    So there is a way to override the header in some tests