Search in sources :

Example 26 with RequestLoggingFilter

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

the class MockMvcRequestSpecBuilder method log.

/**
 * Enabled logging with the specified log detail. Set a {@link LogConfig} to configure the print stream and pretty printing options.
 *
 * @param logDetail The log detail.
 * @return MockMvcRequestSpecBuilder
 */
public MockMvcRequestSpecBuilder log(LogDetail logDetail) {
    notNull(logDetail, LogDetail.class);
    LogConfig logConfig = spec.getRestAssuredMockMvcConfig().getLogConfig();
    PrintStream printStream = logConfig.defaultStream();
    boolean prettyPrintingEnabled = logConfig.isPrettyPrintingEnabled();
    boolean shouldUrlEncodeRequestUri = logConfig.shouldUrlEncodeRequestUri();
    spec.setRequestLoggingFilter(new RequestLoggingFilter(logDetail, prettyPrintingEnabled, printStream, shouldUrlEncodeRequestUri));
    return this;
}
Also used : PrintStream(java.io.PrintStream) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) LogConfig(io.restassured.config.LogConfig)

Example 27 with RequestLoggingFilter

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

the class MockMvcRequestSpecificationImpl method spec.

public MockMvcRequestSpecification spec(MockMvcRequestSpecification requestSpecificationToMerge) {
    notNull(requestSpecificationToMerge, MockMvcRequestSpecification.class);
    if (!(requestSpecificationToMerge instanceof MockMvcRequestSpecificationImpl)) {
        throw new IllegalArgumentException("requestSpecificationToMerge must be an instance of " + MockMvcRequestSpecificationImpl.class.getName());
    }
    MockMvcRequestSpecificationImpl that = (MockMvcRequestSpecificationImpl) requestSpecificationToMerge;
    Object otherRequestBody = that.getRequestBody();
    if (otherRequestBody != null) {
        this.requestBody = otherRequestBody;
    }
    if (isNotEmpty(that.getBasePath())) {
        this.basePath = that.getBasePath();
    }
    MockMvcFactory otherMockMvcFactory = that.getMockMvcFactory();
    if (otherMockMvcFactory != null && otherMockMvcFactory.isAssigned()) {
        this.changeMockMvcFactoryTo(otherMockMvcFactory);
    }
    this.cookies(that.getCookies());
    this.headers(that.getRequestHeaders());
    mergeConfig(this, that);
    MockHttpServletRequestBuilderInterceptor otherInterceptor = that.getInterceptor();
    if (otherInterceptor != null) {
        this.interceptor = otherInterceptor;
    }
    this.formParams(that.getFormParams());
    this.queryParams(that.getQueryParams());
    this.params(that.getParams());
    this.attributes(that.getAttributes());
    this.multiParts.addAll(that.getMultiParts());
    this.resultHandlers.addAll(that.getResultHandlers());
    this.requestPostProcessors.addAll(that.getRequestPostProcessors());
    RequestLoggingFilter otherRequestLoggingFilter = that.getRequestLoggingFilter();
    if (otherRequestLoggingFilter != null) {
        this.requestLoggingFilter = otherRequestLoggingFilter;
    }
    Object otherAuth = that.getAuthentication();
    if (otherAuth != null) {
        this.authentication = otherAuth;
    }
    AsyncConfig otherAsyncConfig = that.getAsyncConfig();
    if (otherAsyncConfig != null) {
        this.asyncConfig = otherAsyncConfig;
    }
    return this;
}
Also used : MockHttpServletRequestBuilderInterceptor(io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor) AsyncConfig(io.restassured.module.mockmvc.config.AsyncConfig) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter)

Example 28 with RequestLoggingFilter

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

the class URLEncodingITest method urlsWithSchemeIsOkToSendInUrlWithoutBeingUrlEncoded.

@Test
public void urlsWithSchemeIsOkToSendInUrlWithoutBeingUrlEncoded() throws Exception {
    // Given
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    String agentUrl = "https://localhost:9888";
    RestAssured.urlEncodingEnabled = false;
    // 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();
            }
        }).log().all().expect().statusCode(200).body(equalTo("changed")).when().get("/agents/probeUrl/" + agentUrl);
    } finally {
        RestAssured.reset();
    }
    assertThat(loggedRequestPathIn(writer), equalTo("http://localhost:8080/agents/probeUrl/https://localhost:9888"));
}
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 29 with RequestLoggingFilter

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

the class URLEncodingITest method urlEncodesNamedPathParameters.

@Test
public void urlEncodesNamedPathParameters() throws Exception {
    // Given
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    String agentUrl = "https://localhost:9888";
    RestAssured.urlEncodingEnabled = true;
    // When
    try {
        given().contentType(JSON).pathParam("x", agentUrl).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).when().get("/agents/probeUrl/{x}");
    } finally {
        RestAssured.reset();
    }
    assertThat(loggedRequestPathIn(writer), equalTo("http://localhost:8080/agents/probeUrl/https%3A%2F%2Flocalhost%3A9888"));
}
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 30 with RequestLoggingFilter

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

the class URLEncodingITest method urlEncodesUnnamedPathParameters.

@Test
public void urlEncodesUnnamedPathParameters() throws Exception {
    // Given
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    String agentUrl = "https://localhost:9888";
    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).when().get("/agents/probeUrl/{x}", agentUrl);
    } finally {
        RestAssured.reset();
    }
    assertThat(loggedRequestPathIn(writer), equalTo("http://localhost:8080/agents/probeUrl/https%3A%2F%2Flocalhost%3A9888"));
}
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)

Aggregations

RequestLoggingFilter (io.restassured.filter.log.RequestLoggingFilter)47 PrintStream (java.io.PrintStream)45 StringWriter (java.io.StringWriter)43 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)43 Test (org.junit.Test)43 ResponseBuilder (io.restassured.builder.ResponseBuilder)31 Filter (io.restassured.filter.Filter)31 FilterContext (io.restassured.filter.FilterContext)31 FilterableRequestSpecification (io.restassured.specification.FilterableRequestSpecification)31 FilterableResponseSpecification (io.restassured.specification.FilterableResponseSpecification)31 ScalatraObject (io.restassured.itest.java.objects.ScalatraObject)3 Response (io.restassured.response.Response)3 LogConfig (io.restassured.config.LogConfig)2 ResponseLoggingFilter (io.restassured.filter.log.ResponseLoggingFilter)2 RestAssuredConfig (io.restassured.config.RestAssuredConfig)1 AsyncConfig (io.restassured.module.mockmvc.config.AsyncConfig)1 MockHttpServletRequestBuilderInterceptor (io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor)1