Search in sources :

Example 21 with FilterableRequestSpecification

use of io.restassured.specification.FilterableRequestSpecification in project rest-assured by rest-assured.

the class FilterITest method content_type_in_filter_contains_charset_by_default.

@Test
public void content_type_in_filter_contains_charset_by_default() {
    final AtomicReference<String> contentType = new AtomicReference<String>();
    given().filter(new Filter() {

        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            contentType.set(requestSpec.getContentType());
            return ctx.next(requestSpec, responseSpec);
        }
    }).formParam("firstName", "John").formParam("lastName", "Doe").when().post("/greet").then().statusCode(200);
    assertThat(contentType.get(), equalTo("application/x-www-form-urlencoded; charset=ISO-8859-1"));
}
Also used : Response(io.restassured.response.Response) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) ErrorLoggingFilter(io.restassured.filter.log.ErrorLoggingFilter) SpookyGreetJsonResponseFilter(io.restassured.itest.java.support.SpookyGreetJsonResponseFilter) FormAuthFilter(io.restassured.internal.filter.FormAuthFilter) ResponseLoggingFilter(io.restassured.filter.log.ResponseLoggingFilter) Filter(io.restassured.filter.Filter) AtomicReference(java.util.concurrent.atomic.AtomicReference) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Example 22 with FilterableRequestSpecification

use of io.restassured.specification.FilterableRequestSpecification in project rest-assured by rest-assured.

the class FilterITest method content_type_in_filter_doesnt_contain_charset_if_configured_not_to.

@Test
public void content_type_in_filter_doesnt_contain_charset_if_configured_not_to() {
    final AtomicReference<String> contentType = new AtomicReference<String>();
    given().config(RestAssuredConfig.config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false))).filter(new Filter() {

        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            contentType.set(requestSpec.getContentType());
            return ctx.next(requestSpec, responseSpec);
        }
    }).formParam("firstName", "John").formParam("lastName", "Doe").when().post("/greet").then().statusCode(200);
    assertThat(contentType.get(), equalTo("application/x-www-form-urlencoded"));
}
Also used : Response(io.restassured.response.Response) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) ErrorLoggingFilter(io.restassured.filter.log.ErrorLoggingFilter) SpookyGreetJsonResponseFilter(io.restassured.itest.java.support.SpookyGreetJsonResponseFilter) FormAuthFilter(io.restassured.internal.filter.FormAuthFilter) ResponseLoggingFilter(io.restassured.filter.log.ResponseLoggingFilter) Filter(io.restassured.filter.Filter) AtomicReference(java.util.concurrent.atomic.AtomicReference) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Example 23 with FilterableRequestSpecification

use of io.restassured.specification.FilterableRequestSpecification in project rest-assured by rest-assured.

the class HttpClientConfigITest method http_client_config_allows_specifying_that_the_http_client_instance_is_reused_in_multiple_requests.

@Test
public void http_client_config_allows_specifying_that_the_http_client_instance_is_reused_in_multiple_requests() {
    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().header("name", "value").filter((requestSpec, responseSpec, ctx) -> {
            client2.setValue(requestSpec.getHttpClient());
            return ctx.next(requestSpec, responseSpec);
        }).when().post("/reflect");
    } 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 24 with FilterableRequestSpecification

use of io.restassured.specification.FilterableRequestSpecification in project rest-assured by rest-assured.

the class AcceptHeaderITest method accept_headers_are_merged_from_request_spec_and_request_when_configured_to.

@Test
public void accept_headers_are_merged_from_request_spec_and_request_when_configured_to() {
    RequestSpecification spec = new RequestSpecBuilder().setAccept("text/jux").build();
    final MutableObject<List<String>> headers = new MutableObject<List<String>>();
    RestAssured.given().config(RestAssuredConfig.config().headerConfig(HeaderConfig.headerConfig().mergeHeadersWithName("Accept"))).accept(ContentType.JSON).spec(spec).body("{ \"message\" : \"hello world\"}").filter(new Filter() {

        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            headers.setValue(requestSpec.getHeaders().getValues("Accept"));
            return ctx.next(requestSpec, responseSpec);
        }
    }).when().post("/jsonBodyAcceptHeader").then().body(equalTo("hello world"));
    assertThat(headers.getValue(), contains("application/json, application/javascript, text/javascript, text/json", "text/jux"));
}
Also used : Response(io.restassured.response.Response) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) Filter(io.restassured.filter.Filter) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) RequestSpecification(io.restassured.specification.RequestSpecification) List(java.util.List) RequestSpecBuilder(io.restassured.builder.RequestSpecBuilder) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) MutableObject(org.apache.commons.lang3.mutable.MutableObject) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Example 25 with FilterableRequestSpecification

use of io.restassured.specification.FilterableRequestSpecification in project rest-assured by rest-assured.

the class PathParamITest method mixingUnnamedPathParametersAndQueryParametersWorks.

@Test
public void mixingUnnamedPathParametersAndQueryParametersWorks() throws Exception {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    given().config(RestAssuredConfig.config().logConfig(new LogConfig(captor, true))).log().all().filter(new Filter() {

        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            return new ResponseBuilder().setStatusCode(200).setBody("changed").build();
        }
    }).get("/{channelName}/item-import/rss/import?source={url}", "games", "http://myurl.com");
    // Then
    assertThat(RequestPathFromLogExtractor.loggedRequestPathIn(writer), equalTo("http://localhost:8080/games/item-import/rss/import?source=http%3A%2F%2Fmyurl.com"));
}
Also used : PrintStream(java.io.PrintStream) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) StringWriter(java.io.StringWriter) Filter(io.restassured.filter.Filter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) ResponseBuilder(io.restassured.builder.ResponseBuilder) LogConfig(io.restassured.config.LogConfig) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Aggregations

FilterableRequestSpecification (io.restassured.specification.FilterableRequestSpecification)50 Test (org.junit.Test)50 FilterContext (io.restassured.filter.FilterContext)49 FilterableResponseSpecification (io.restassured.specification.FilterableResponseSpecification)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