Search in sources :

Example 1 with ResponseBuilder

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

the class LoggingITest method prettifying_empty_xml_body_doesnt_log_premature_end_of_file.

// This was previously a bug (https://github.com/rest-assured/rest-assured/issues/960)
@Test
public void prettifying_empty_xml_body_doesnt_log_premature_end_of_file() {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    given().filter(new ResponseLoggingFilter(LogDetail.ALL, true, captor)).filter((requestSpec, responseSpec, ctx) -> new ResponseBuilder().setStatusCode(200).setContentType(ContentType.XML).setBody("").build()).when().get("/xml-empty-body").then().statusCode(200);
    assertThat(writer.toString(), equalTo("200" + LINE_SEPARATOR + "Content-Type: application/xml" + LINE_SEPARATOR));
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) ResponseLoggingFilter(io.restassured.filter.log.ResponseLoggingFilter) ResponseBuilder(io.restassured.builder.ResponseBuilder) Test(org.junit.Test)

Example 2 with ResponseBuilder

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

the class URLEncodingITest method doesntDoubleEncodeParamsWhenDefiningUrlEncodingToTrue.

@Test
public void doesntDoubleEncodeParamsWhenDefiningUrlEncodingToTrue() throws Exception {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    given().urlEncodingEnabled(true).pathParam("pathParam", "path/param").formParam("formParam", "form/param").filter(new RequestLoggingFilter(captor)).filter(new Filter() {

        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            /*
                         * Note that Scalatra cannot handle request with path parameters containing "/" (like path/param) even though it's URL encoded.
                         * Scalatra decodes the path prior to finding the method to invoke and thus we'll get an error back (since no resource mapping to /path/param/manyParams exist).
                         */
            return new ResponseBuilder().setStatusCode(200).setBody("changed").build();
        }
    }).then().body(equalTo("changed")).when().post("/{pathParam}/manyParams?queryParam=query/param");
    assertThat(loggedRequestPathIn(writer), equalTo("http://localhost:8080/path%2Fparam/manyParams?queryParam=query%2Fparam"));
}
Also used : Response(io.restassured.response.Response) PrintStream(java.io.PrintStream) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) StringWriter(java.io.StringWriter) Filter(io.restassured.filter.Filter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) ResponseBuilder(io.restassured.builder.ResponseBuilder) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Example 3 with ResponseBuilder

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

the class URLEncodingITest method doesntUrlEncodePathFragmentsWhenUrlEncodingIsDisabled.

@Test
public void doesntUrlEncodePathFragmentsWhenUrlEncodingIsDisabled() throws Exception {
    // Given
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    String agentUrl = URLEncoder.encode("https://localhost:9888", "UTF-8");
    RestAssured.urlEncodingEnabled = false;
    RestAssured.basePath = "/tmc/api";
    // When
    try {
        given().contentType(JSON).filter(new RequestLoggingFilter(captor)).filter(new Filter() {

            public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
                /*
                         * Note that Scalatra cannot handle request with path parameters containing "/" (like path/param) even though it's URL encoded.
                         * Scalatra decodes the path prior to finding the method to invoke and thus we'll get an error back (since no resource mapping to /path/param/manyParams exist).
                         */
                return new ResponseBuilder().setStatusCode(200).setBody("changed").build();
            }
        }).expect().statusCode(200).body(equalTo("changed")).when().get("/agents/probeUrl/" + agentUrl);
    } finally {
        RestAssured.reset();
    }
    assertThat(loggedRequestPathIn(writer), equalTo("http://localhost:8080/tmc/api/agents/probeUrl/https%3A%2F%2Flocalhost%3A9888"));
}
Also used : PrintStream(java.io.PrintStream) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) StringWriter(java.io.StringWriter) Filter(io.restassured.filter.Filter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) ResponseBuilder(io.restassured.builder.ResponseBuilder) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Example 4 with ResponseBuilder

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

the class URLEncodingITest method urlEncodesPathFragmentsRegardlessIfTheyHaveBeenManuallyEncodedWhenUrlEncodingIsEnabled.

@Test
public void urlEncodesPathFragmentsRegardlessIfTheyHaveBeenManuallyEncodedWhenUrlEncodingIsEnabled() throws Exception {
    // Given
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    String agentUrl = URLEncoder.encode("https://localhost:9888", "UTF-8");
    RestAssured.urlEncodingEnabled = true;
    // When
    try {
        given().contentType(JSON).filter(new RequestLoggingFilter(captor)).filter(new Filter() {

            public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
                /*
                         * Note that Scalatra cannot handle request with path parameters containing "/" (like path/param) even though it's URL encoded.
                         * Scalatra decodes the path prior to finding the method to invoke and thus we'll get an error back (since no resource mapping to /path/param/manyParams exist).
                         */
                return new ResponseBuilder().setStatusCode(200).setBody("changed").build();
            }
        }).expect().statusCode(200).body(equalTo("changed")).when().get("/agents/probeUrl/" + agentUrl);
    } finally {
        RestAssured.reset();
    }
    assertThat(loggedRequestPathIn(writer), equalTo("http://localhost:8080/agents/probeUrl/https%253A%252F%252Flocalhost%253A9888"));
}
Also used : PrintStream(java.io.PrintStream) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) StringWriter(java.io.StringWriter) Filter(io.restassured.filter.Filter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) ResponseBuilder(io.restassured.builder.ResponseBuilder) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Example 5 with ResponseBuilder

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

the class URLITest method trailingSlashesAreRetainedWhenPassedAsArgumentToGetMethod.

@Test
public void trailingSlashesAreRetainedWhenPassedAsArgumentToGetMethod() throws Exception {
    // Given
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    // When
    given().filter(new RequestLoggingFilter(captor)).filter(new Filter() {

        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            return new ResponseBuilder().setStatusCode(200).setBody("changed").build();
        }
    }).expect().statusCode(200).body(equalTo("changed")).when().get("/v1/");
    // Then
    assertThat(loggedRequestPathIn(writer), equalTo("http://localhost:8080/v1/"));
}
Also used : PrintStream(java.io.PrintStream) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) StringWriter(java.io.StringWriter) Filter(io.restassured.filter.Filter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) ResponseBuilder(io.restassured.builder.ResponseBuilder) 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