Search in sources :

Example 41 with ResponseBuilder

use of io.restassured.builder.ResponseBuilder in project rest-assured by rest-assured.

the class LoggingITest method logsFullyQualifiedUrlsAreLoggedCorrectly.

@Test
public void logsFullyQualifiedUrlsAreLoggedCorrectly() throws Exception {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    given().config(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("http://www.beijingchina.net.cn/transportation/train/train-to-shanghai.html");
    assertThat(writer.toString(), startsWith("Request method:\tGET\nRequest URI:\thttp://www.beijingchina.net.cn/transportation/train/train-to-shanghai.html"));
}
Also used : PrintStream(java.io.PrintStream) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) StringWriter(java.io.StringWriter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) ResponseLoggingFilter(io.restassured.filter.log.ResponseLoggingFilter) 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)

Example 42 with ResponseBuilder

use of io.restassured.builder.ResponseBuilder in project rest-assured by rest-assured.

the class OAuthITest method oauth2_works_with_preemptive_header_signing.

@Test
public void oauth2_works_with_preemptive_header_signing() {
    final String accessToken = "accessToken";
    given().auth().preemptive().oauth2(accessToken).filter(new Filter() {

        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            assertThat(requestSpec.getHeaders().getValue("Authorization"), equalTo("Bearer " + accessToken));
            return new ResponseBuilder().setBody("ok").setStatusCode(200).build();
        }
    }).when().get("/somewhere").then().statusCode(200);
}
Also used : Response(io.restassured.response.Response) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) Filter(io.restassured.filter.Filter) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) ResponseBuilder(io.restassured.builder.ResponseBuilder) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Example 43 with ResponseBuilder

use of io.restassured.builder.ResponseBuilder 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 44 with ResponseBuilder

use of io.restassured.builder.ResponseBuilder in project rest-assured by rest-assured.

the class PathParamITest method namedPathParametersWorksWhenThereAreMultipleTemplatesBetweenEachSlash.

@Test
public void namedPathParametersWorksWhenThereAreMultipleTemplatesBetweenEachSlash() throws Exception {
    String param1Value = "Hello";
    String param2Value = "Hello2";
    given().filter((requestSpec, responseSpec, ctx) -> new ResponseBuilder().setStatusCode(200).setStatusLine("HTTP/1.1 200 OK").setBody(requestSpec.getURI()).build()).pathParam("param1Value", param1Value).pathParam("param2Value", param2Value).when().get("param1={param1Value}&param2={param2Value}").then().body(equalTo("http://localhost:8080/param1%3DHello%26param2%3DHello2"));
}
Also used : ResponseBuilder(io.restassured.builder.ResponseBuilder) Test(org.junit.Test)

Example 45 with ResponseBuilder

use of io.restassured.builder.ResponseBuilder in project rest-assured by rest-assured.

the class PathParamITest method urlEncodesNamedPathParametersThatContainsCurlyBracesAndEquals.

@Test
public void urlEncodesNamedPathParametersThatContainsCurlyBracesAndEquals() throws Exception {
    // When
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    given().config(RestAssuredConfig.config().logConfig(new LogConfig(captor, true))).pathParam("trackingName", "{trackingName='trackingname1'}").pathParam("platform", "platform").log().all().filter(new Filter() {

        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            return new ResponseBuilder().setStatusCode(200).setBody("changed").build();
        }
    }).get("/feed?canonicalName={trackingName}&{platform}=ed4");
    // Then
    assertThat(RequestPathFromLogExtractor.loggedRequestPathIn(writer), equalTo("http://localhost:8080/feed?canonicalName=%7BtrackingName%3D%27trackingname1%27%7D&platform=ed4"));
}
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

ResponseBuilder (io.restassured.builder.ResponseBuilder)49 Test (org.junit.Test)47 Filter (io.restassured.filter.Filter)40 FilterContext (io.restassured.filter.FilterContext)40 FilterableRequestSpecification (io.restassured.specification.FilterableRequestSpecification)40 FilterableResponseSpecification (io.restassured.specification.FilterableResponseSpecification)40 PrintStream (java.io.PrintStream)36 StringWriter (java.io.StringWriter)36 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)36 RequestLoggingFilter (io.restassured.filter.log.RequestLoggingFilter)32 Response (io.restassured.response.Response)11 LogConfig (io.restassured.config.LogConfig)4 Headers (io.restassured.http.Headers)4 ResponseLoggingFilter (io.restassured.filter.log.ResponseLoggingFilter)3 MutableObject (org.apache.commons.lang3.mutable.MutableObject)2 HttpClient (org.apache.http.client.HttpClient)2 AuthenticationScheme (io.restassured.authentication.AuthenticationScheme)1 PreemptiveOAuth2HeaderScheme (io.restassured.authentication.PreemptiveOAuth2HeaderScheme)1 RequestSpecBuilder (io.restassured.builder.RequestSpecBuilder)1 ErrorLoggingFilter (io.restassured.filter.log.ErrorLoggingFilter)1