Search in sources :

Example 41 with FilterContext

use of io.restassured.filter.FilterContext in project rest-assured by rest-assured.

the class OrderedFilterITest method high_precedence_are_sorted_before_low_precedence.

@Test
public void high_precedence_are_sorted_before_low_precedence() {
    List<String> list = new CopyOnWriteArrayList<>();
    given().filter((requestSpec, responseSpec, ctx) -> {
        list.add("Default");
        return ctx.next(requestSpec, responseSpec);
    }).filter(new OrderedFilter() {

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

        @Override
        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            list.add("Custom");
            return ctx.next(requestSpec, responseSpec);
        }
    }).filter(new OrderedFilter() {

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

        @Override
        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            list.add("Lowest");
            return ctx.next(requestSpec, responseSpec);
        }
    }).filter(new OrderedFilter() {

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

        @Override
        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            list.add("Highest");
            return ctx.next(requestSpec, responseSpec);
        }
    }).when().get("/lotto");
    assertThat(list, contains("Highest", "Default", "Custom", "Lowest"));
}
Also used : Assert.assertThat(org.junit.Assert.assertThat) FilterContext(io.restassured.filter.FilterContext) List(java.util.List) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) Response(io.restassured.response.Response) Matchers.contains(org.hamcrest.Matchers.contains) OrderedFilter(io.restassured.filter.OrderedFilter) Test(org.junit.Test) RestAssured.given(io.restassured.RestAssured.given) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) WithJetty(io.restassured.itest.java.support.WithJetty) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Response(io.restassured.response.Response) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) OrderedFilter(io.restassured.filter.OrderedFilter) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Example 42 with FilterContext

use of io.restassured.filter.FilterContext in project rest-assured by rest-assured.

the class HttpClientConfigITest method local_http_client_config_reuse_reuse_static_http_client_instance_when_local_config_changes_other_configs_than_http_client_config.

@Test
public void local_http_client_config_reuse_reuse_static_http_client_instance_when_local_config_changes_other_configs_than_http_client_config() {
    final MutableObject<HttpClient> client1 = new MutableObject<HttpClient>();
    final MutableObject<HttpClient> client2 = new MutableObject<HttpClient>();
    RestAssured.config = RestAssuredConfig.newConfig().httpClient(HttpClientConfig.httpClientConfig().reuseHttpClientInstance());
    // When
    try {
        given().param("url", "/hello").filter(new Filter() {

            public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
                client1.setValue(requestSpec.getHttpClient());
                return ctx.next(requestSpec, responseSpec);
            }
        }).expect().body("hello", equalTo("Hello Scalatra")).when().get("/redirect");
        given().config(RestAssured.config.decoderConfig(DecoderConfig.decoderConfig().with().contentDecoders(DecoderConfig.ContentDecoder.DEFLATE))).filter(new Filter() {

            public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
                client2.setValue(requestSpec.getHttpClient());
                return ctx.next(requestSpec, responseSpec);
            }
        }).expect().body("Accept-Encoding", contains("deflate")).when().get("/headersWithValues");
    } finally {
        RestAssured.reset();
    }
    assertThat(client1.getValue(), sameInstance(client2.getValue()));
}
Also used : Response(io.restassured.response.Response) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) Filter(io.restassured.filter.Filter) SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) MutableObject(org.apache.commons.lang3.mutable.MutableObject) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Example 43 with FilterContext

use of io.restassured.filter.FilterContext in project rest-assured by rest-assured.

the class HttpClientConfigITest method httpClientIsConfigurableFromAStaticHttpClientConfigWithOtherConfigurations.

@Test
public void httpClientIsConfigurableFromAStaticHttpClientConfigWithOtherConfigurations() {
    // Given
    final MutableObject<HttpClient> client = new MutableObject<>();
    RestAssured.config = RestAssuredConfig.newConfig().httpClient(HttpClientConfig.httpClientConfig().setParam(HANDLE_REDIRECTS, true).and().setParam(MAX_REDIRECTS, 0).and().httpClientFactory(SystemDefaultHttpClient::new));
    // When
    try {
        given().param("url", "/hello").filter(new Filter() {

            public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
                client.setValue(requestSpec.getHttpClient());
                return ctx.next(requestSpec, responseSpec);
            }
        }).expect().body("hello", equalTo("Hello Scalatra")).when().get("/redirect");
    } finally {
        RestAssured.reset();
    }
    // Then
    assertThat(client.getValue(), instanceOf(SystemDefaultHttpClient.class));
}
Also used : SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) Response(io.restassured.response.Response) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) Filter(io.restassured.filter.Filter) SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) MutableObject(org.apache.commons.lang3.mutable.MutableObject) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Example 44 with FilterContext

use of io.restassured.filter.FilterContext in project rest-assured by rest-assured.

the class HttpClientConfigITest method httpClientIsConfigurableFromANonStaticHttpClientConfig.

@Test
public void httpClientIsConfigurableFromANonStaticHttpClientConfig() {
    // Given
    final MutableObject<HttpClient> client = new MutableObject<>();
    // When
    given().config(RestAssuredConfig.newConfig().httpClient(HttpClientConfig.httpClientConfig().httpClientFactory(SystemDefaultHttpClient::new))).filter(new Filter() {

        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            client.setValue(requestSpec.getHttpClient());
            return new ResponseBuilder().setStatusCode(200).setContentType("application/json").setBody("{ \"message\" : \"hello\"}").build();
        }
    }).expect().body("message", equalTo("hello")).when().get("/something");
    // Then
    assertThat(client.getValue(), instanceOf(SystemDefaultHttpClient.class));
}
Also used : Response(io.restassured.response.Response) SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) Filter(io.restassured.filter.Filter) SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) ResponseBuilder(io.restassured.builder.ResponseBuilder) MutableObject(org.apache.commons.lang3.mutable.MutableObject) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Example 45 with FilterContext

use of io.restassured.filter.FilterContext in project rest-assured by rest-assured.

the class HttpClientConfigITest method local_http_client_config_doesnt_reuse_static_http_client_instance_when_local_config_specifies_reuse.

@Test
public void local_http_client_config_doesnt_reuse_static_http_client_instance_when_local_config_specifies_reuse() {
    final MutableObject<HttpClient> client1 = new MutableObject<HttpClient>();
    final MutableObject<HttpClient> client2 = new MutableObject<HttpClient>();
    RestAssured.config = RestAssuredConfig.newConfig().httpClient(HttpClientConfig.httpClientConfig().reuseHttpClientInstance());
    // When
    try {
        given().param("url", "/hello").filter(new Filter() {

            public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
                client1.setValue(requestSpec.getHttpClient());
                return ctx.next(requestSpec, responseSpec);
            }
        }).expect().body("hello", equalTo("Hello Scalatra")).when().get("/redirect");
        given().config(RestAssuredConfig.newConfig().httpClient(HttpClientConfig.httpClientConfig().reuseHttpClientInstance())).header("name", "value").filter(new Filter() {

            public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
                client2.setValue(requestSpec.getHttpClient());
                return ctx.next(requestSpec, responseSpec);
            }
        }).when().post("/reflect");
    } finally {
        RestAssured.reset();
    }
    assertThat(client1.getValue(), not(sameInstance(client2.getValue())));
}
Also used : Response(io.restassured.response.Response) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) Filter(io.restassured.filter.Filter) SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) MutableObject(org.apache.commons.lang3.mutable.MutableObject) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Aggregations

FilterContext (io.restassured.filter.FilterContext)49 FilterableRequestSpecification (io.restassured.specification.FilterableRequestSpecification)49 FilterableResponseSpecification (io.restassured.specification.FilterableResponseSpecification)49 Test (org.junit.Test)49 Filter (io.restassured.filter.Filter)48 ResponseBuilder (io.restassured.builder.ResponseBuilder)40 PrintStream (java.io.PrintStream)35 StringWriter (java.io.StringWriter)35 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)35 RequestLoggingFilter (io.restassured.filter.log.RequestLoggingFilter)32 Response (io.restassured.response.Response)17 MutableObject (org.apache.commons.lang3.mutable.MutableObject)8 HttpClient (org.apache.http.client.HttpClient)6 SystemDefaultHttpClient (org.apache.http.impl.client.SystemDefaultHttpClient)5 LogConfig (io.restassured.config.LogConfig)4 ResponseLoggingFilter (io.restassured.filter.log.ResponseLoggingFilter)4 RequestSpecBuilder (io.restassured.builder.RequestSpecBuilder)3 ErrorLoggingFilter (io.restassured.filter.log.ErrorLoggingFilter)3 FormAuthFilter (io.restassured.internal.filter.FormAuthFilter)3 SpookyGreetJsonResponseFilter (io.restassured.itest.java.support.SpookyGreetJsonResponseFilter)3