rest-assured / rest-assured

Java DSL for easy testing of REST services

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Canot change the `RestAssuredConfig` httpClient field in `Filter`

alterhu2020 opened this issue · comments

Hi Team,
I create a RestAssured filter as following code, which i used to print the curl command when execute the RestAssured request, the code to change the RestAssuredConfig's httpclient to addRequestInterceptor method in it:

import com.github.dzieciou.testing.curl.CurlCommand;
import com.github.dzieciou.testing.curl.CurlRestAssuredConfigFactory;
import com.github.dzieciou.testing.curl.Options;
import com.github.dzieciou.testing.curl.Platform;
import io.restassured.RestAssured;
import io.restassured.config.HttpClientConfig;
import io.restassured.config.RestAssuredConfig;
import io.restassured.filter.FilterContext;
import io.restassured.filter.OrderedFilter;
import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;
import org.slf4j.event.Level;

public class CommandFilter implements OrderedFilter {


    @Override
    public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
         Options options = Options.builder()
                .dontLogStacktrace()
                .printMultiliner()
                .useLogLevel(Level.INFO)
                .targetPlatform(Platform.UNIX).build();
        RestAssured.config = CurlRestAssuredConfigFactory.updateConfig(RestAssured.config, options);
        return ctx.next(requestSpec, responseSpec);
    }

    @Override
    public int getOrder() {
        return LOWEST_PRECEDENCE;
    }
}

and test code as following:

 RestAssured.given().baseUri("https://google.com").config(RestAssured.config).filters(new CommandFilter()).get("/");

But the filter's custom config code never be executed, it seems that we cannot change the RestAssuredConfig's HttpClientFactory data. please help confirm. and if i want to change the RestAssuredConfig http client in Filter, how can i achieve it? thanks for ur response.

The reason is that RequestSpecificationImpl uses RestAssuredConfig instance (to send an HTTP request) before it gets updated by your filter:

https://github.com/rest-assured/rest-assured/blob/master/rest-assured/src/main/groovy/io/restassured/internal/RequestSpecificationImpl.groovy#L1686

Why are you trying to modify the config inside of a filter?