Search in sources :

Example 31 with LogConfig

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

the class LoggingIfValidationFailsTest method logging_of_both_request_and_response_validation_works_when_test_fails.

@Test
public void logging_of_both_request_and_response_validation_works_when_test_fails() {
    RestAssuredMockMvc.config = new RestAssuredMockMvcConfig().logConfig(new LogConfig(captor, true).enableLoggingOfRequestAndResponseIfValidationFails());
    try {
        given().standaloneSetup(new PostController()).param("name", "Johan").when().post("/greetingPost").then().body("id", equalTo(1)).body("content", equalTo("Hello, Johan2!"));
        fail("Should throw AssertionError");
    } catch (AssertionError e) {
        assertThat(writer.toString(), equalTo("Request method:\tPOST\nRequest URI:\thttp://localhost:8080/greetingPost\nProxy:\t\t\t<none>\nRequest params:\tname=Johan\nQuery params:\t<none>\nForm params:\t<none>\nPath params:\t<none>\nHeaders:\t\tContent-Type=application/x-www-form-urlencoded;charset=" + RestAssuredMockMvcConfig.config().getEncoderConfig().defaultContentCharset() + "\nCookies:\t\t<none>\nMultiparts:\t\t<none>\nBody:\t\t\t<none>\n\n" + "" + "200\nContent-Type: application/json;charset=UTF-8\n\n{\n    \"id\": 1,\n    \"content\": \"Hello, Johan!\"\n}\n"));
    }
}
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 32 with LogConfig

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

the class LoggingIfValidationFailsTest method logging_of_both_request_and_response_validation_works_when_test_fails_when_using_static_response_and_request_specs_declared_before_enable_logging.

@Test
public void logging_of_both_request_and_response_validation_works_when_test_fails_when_using_static_response_and_request_specs_declared_before_enable_logging() {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    RestAssuredMockMvc.responseSpecification = new ResponseSpecBuilder().expectStatusCode(200).build();
    RestAssuredMockMvc.requestSpecification = new MockMvcRequestSpecBuilder().setConfig(config().logConfig(new LogConfig(captor, true))).addHeader("Api-Key", "1234").build();
    RestAssuredMockMvc.enableLoggingOfRequestAndResponseIfValidationFails(HEADERS);
    try {
        given().standaloneSetup(new PostController()).param("name", "Johan").when().post("/greetingPost").then().body("id", equalTo(1)).body("content", equalTo("Hello, Johan2!"));
        fail("Should throw AssertionError");
    } catch (AssertionError e) {
        assertThat(writer.toString(), equalTo("Headers:\t\tApi-Key=1234\n\t\t\t\tContent-Type=application/x-www-form-urlencoded;charset=" + RestAssuredMockMvcConfig.config().getEncoderConfig().defaultContentCharset() + "\n\nContent-Type: application/json;charset=UTF-8\n"));
    }
}
Also used : PrintStream(java.io.PrintStream) PostController(io.restassured.module.mockmvc.http.PostController) MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) StringWriter(java.io.StringWriter) ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) LogConfig(io.restassured.config.LogConfig) Test(org.junit.Test)

Example 33 with LogConfig

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

the class LoggingIfValidationFailsTest method logging_of_both_request_and_response_validation_works_when_test_fails_when_configured_with_log_detail.

@Test
public void logging_of_both_request_and_response_validation_works_when_test_fails_when_configured_with_log_detail() {
    RestAssuredMockMvc.config = new RestAssuredMockMvcConfig().logConfig(new LogConfig(captor, true).enableLoggingOfRequestAndResponseIfValidationFails(LogDetail.HEADERS));
    try {
        given().standaloneSetup(new PostController()).param("name", "Johan").when().post("/greetingPost").then().body("id", equalTo(1)).body("content", equalTo("Hello, Johan2!"));
        fail("Should throw AssertionError");
    } catch (AssertionError e) {
        assertThat(writer.toString(), equalTo("Headers:\t\tContent-Type=application/x-www-form-urlencoded;charset=" + RestAssuredMockMvcConfig.config().getEncoderConfig().defaultContentCharset() + "\n\n" + "Content-Type: application/json;charset=UTF-8\n"));
    }
}
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 34 with LogConfig

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

the class ResponseLoggingTest 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 35 with LogConfig

use of io.restassured.config.LogConfig 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)

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