Search in sources :

Example 26 with LogConfig

use of io.restassured.config.LogConfig in project rest-assured by rest-assured.

the class RequestLoggingTest method given_config_is_stored_in_writer.

@Before
public void given_config_is_stored_in_writer() {
    writer = new StringWriter();
    PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    RestAssuredMockMvc.config = new RestAssuredMockMvcConfig().logConfig(new LogConfig(captor, true));
}
Also used : PrintStream(java.io.PrintStream) RestAssuredMockMvcConfig(io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig) StringWriter(java.io.StringWriter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) LogConfig(io.restassured.config.LogConfig) Before(org.junit.Before)

Example 27 with LogConfig

use of io.restassured.config.LogConfig in project rest-assured by rest-assured.

the class LoggingIfValidationFailsTest method doesnt_log_if_request_or_response_when_validation_succeeds_when_request_and_response_logging_if_validation_fails_is_enabled.

@Test
public void doesnt_log_if_request_or_response_when_validation_succeeds_when_request_and_response_logging_if_validation_fails_is_enabled() {
    RestAssuredMockMvc.config = new RestAssuredMockMvcConfig().logConfig(new LogConfig(captor, true).enableLoggingOfRequestAndResponseIfValidationFails());
    given().standaloneSetup(new PostController()).param("name", "Johan").when().post("/greetingPost").then().body("id", equalTo(1)).body("content", equalTo("Hello, Johan!"));
    assertThat(writer.toString(), isEmptyString());
}
Also used : RestAssuredMockMvcConfig(io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig) PostController(io.restassured.module.mockmvc.http.PostController) LogConfig(io.restassured.config.LogConfig) Test(org.junit.Test)

Example 28 with LogConfig

use of io.restassured.config.LogConfig in project rest-assured by rest-assured.

the class LoggingIfValidationFailsTest method logging_is_applied_when_using_non_static_response_specifications.

@Test
public void logging_is_applied_when_using_non_static_response_specifications() {
    RestAssuredMockMvc.config = new RestAssuredMockMvcConfig().logConfig(new LogConfig(captor, true).enableLoggingOfRequestAndResponseIfValidationFails());
    try {
        given().standaloneSetup(new PostController()).param("name", "Johan").when().post("/greetingPost").then().spec(new ResponseSpecBuilder().expectBody("id", equalTo(2)).expectBody("content", equalTo("Hello, Johan2!")).build());
        fail("Should throw AssertionError");
    } catch (AssertionError e) {
        assertThat(writer.toString(), not(isEmptyOrNullString()));
    }
}
Also used : RestAssuredMockMvcConfig(io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig) PostController(io.restassured.module.mockmvc.http.PostController) ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) LogConfig(io.restassured.config.LogConfig) Test(org.junit.Test)

Example 29 with LogConfig

use of io.restassured.config.LogConfig in project rest-assured by rest-assured.

the class ValidatableResponseOptionsImpl method specification.

public T specification(ResponseSpecification responseSpecification) {
    notNull(responseSpecification, ResponseSpecification.class);
    // We parse the response as a string here because we need to enforce it otherwise specs won't work
    response.asString();
    // The following is a work-around to enable logging of request and response if validation fails
    if (responseSpecification instanceof ResponseSpecificationImpl) {
        ResponseSpecificationImpl impl = (ResponseSpecificationImpl) responseSpecification;
        LogConfig globalLogConfig = responseSpec.getConfig().getLogConfig();
        if (globalLogConfig.isLoggingOfRequestAndResponseIfValidationFailsEnabled()) {
            impl.setConfig(impl.getConfig().logConfig(globalLogConfig));
            impl.setLogRepository(responseSpec.getLogRepository());
        }
    }
    // Finally validate the response
    responseSpecification.validate(response);
    return (T) this;
}
Also used : LogConfig(io.restassured.config.LogConfig)

Example 30 with LogConfig

use of io.restassured.config.LogConfig in project rest-assured by rest-assured.

the class RequestSpecBuilder 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 RequestSpecBuilder
 */
public RequestSpecBuilder log(LogDetail logDetail) {
    notNull(logDetail, LogDetail.class);
    RestAssuredConfig restAssuredConfig = spec.getConfig();
    LogConfig logConfig;
    if (restAssuredConfig == null) {
        logConfig = new RestAssuredConfig().getLogConfig();
    } else {
        logConfig = restAssuredConfig.getLogConfig();
    }
    PrintStream printStream = logConfig.defaultStream();
    boolean prettyPrintingEnabled = logConfig.isPrettyPrintingEnabled();
    boolean shouldUrlEncodeRequestUri = logConfig.shouldUrlEncodeRequestUri();
    spec.filter(new RequestLoggingFilter(logDetail, prettyPrintingEnabled, printStream, shouldUrlEncodeRequestUri));
    return this;
}
Also used : PrintStream(java.io.PrintStream) RestAssuredConfig(io.restassured.config.RestAssuredConfig) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) LogConfig(io.restassured.config.LogConfig)

Aggregations

LogConfig (io.restassured.config.LogConfig)56 PrintStream (java.io.PrintStream)48 Test (org.junit.Test)48 StringWriter (java.io.StringWriter)46 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)46 RestAssuredMockMvcConfig (io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig)7 PostController (io.restassured.module.mockmvc.http.PostController)5 ResponseBuilder (io.restassured.builder.ResponseBuilder)4 Filter (io.restassured.filter.Filter)4 FilterContext (io.restassured.filter.FilterContext)4 FilterableRequestSpecification (io.restassured.specification.FilterableRequestSpecification)4 FilterableResponseSpecification (io.restassured.specification.FilterableResponseSpecification)4 ResponseSpecBuilder (io.restassured.builder.ResponseSpecBuilder)3 RequestLoggingFilter (io.restassured.filter.log.RequestLoggingFilter)3 Before (org.junit.Before)2 RequestSpecBuilder (io.restassured.builder.RequestSpecBuilder)1 RestAssuredConfig (io.restassured.config.RestAssuredConfig)1 ResponseLoggingFilter (io.restassured.filter.log.ResponseLoggingFilter)1 Greeting (io.restassured.itest.java.objects.Greeting)1 Message (io.restassured.itest.java.objects.Message)1