Search in sources :

Example 1 with RestAssuredMockMvcConfig

use of io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig in project rest-assured by rest-assured.

the class MockMvcRequestSpecificationImpl method mergeConfig.

private static void mergeConfig(MockMvcRequestSpecificationImpl thisOne, MockMvcRequestSpecificationImpl other) {
    RestAssuredMockMvcConfig thisConfig = thisOne.getRestAssuredMockMvcConfig();
    RestAssuredMockMvcConfig otherConfig = other.getRestAssuredMockMvcConfig();
    boolean thisIsUserConfigured = thisConfig.isUserConfigured();
    boolean otherIsUserConfigured = otherConfig.isUserConfigured();
    if (thisIsUserConfigured && otherIsUserConfigured) {
        if (otherConfig.getDecoderConfig().isUserConfigured()) {
            thisConfig = thisConfig.decoderConfig(otherConfig.getDecoderConfig());
        }
        if (otherConfig.getEncoderConfig().isUserConfigured()) {
            thisConfig = thisConfig.encoderConfig(otherConfig.getEncoderConfig());
        }
        if (otherConfig.getHeaderConfig().isUserConfigured()) {
            thisConfig = thisConfig.headerConfig(otherConfig.getHeaderConfig());
        }
        if (otherConfig.getJsonConfig().isUserConfigured()) {
            thisConfig = thisConfig.jsonConfig(otherConfig.getJsonConfig());
        }
        if (otherConfig.getLogConfig().isUserConfigured()) {
            thisConfig = thisConfig.logConfig(otherConfig.getLogConfig());
        }
        if (otherConfig.getObjectMapperConfig().isUserConfigured()) {
            thisConfig = thisConfig.objectMapperConfig(otherConfig.getObjectMapperConfig());
        }
        if (otherConfig.getSessionConfig().isUserConfigured()) {
            thisConfig = thisConfig.sessionConfig(otherConfig.getSessionConfig());
        }
        if (otherConfig.getXmlConfig().isUserConfigured()) {
            thisConfig = thisConfig.xmlConfig(otherConfig.getXmlConfig());
        }
        if (otherConfig.getAsyncConfig().isUserConfigured()) {
            thisConfig = thisConfig.asyncConfig(otherConfig.getAsyncConfig());
        }
        if (otherConfig.getMultiPartConfig().isUserConfigured()) {
            thisConfig = thisConfig.multiPartConfig(otherConfig.getMultiPartConfig());
        }
        if (otherConfig.getMockMvcConfig().isUserConfigured()) {
            thisConfig = thisConfig.mockMvcConfig(otherConfig.getMockMvcConfig());
        }
        if (otherConfig.getParamConfig().isUserConfigured()) {
            thisConfig = thisConfig.paramConfig(otherConfig.getParamConfig());
        }
        thisOne.config(thisConfig);
    } else if (!thisIsUserConfigured && otherIsUserConfigured) {
        thisOne.config(otherConfig);
    }
}
Also used : RestAssuredMockMvcConfig(io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig)

Example 2 with RestAssuredMockMvcConfig

use of io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig in project rest-assured by rest-assured.

the class RestAssuredMockMvc method enableLoggingOfRequestAndResponseIfValidationFails.

/**
 * Enable logging of both the request and the response if REST Assureds test validation fails with the specified log detail.
 * <p/>
 * <p>
 * This is just a shortcut for:
 * </p>
 * <pre>
 * RestAssured.config = new RestAssuredMockMvcConfig().logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails(logDetail));
 * </pre>
 *
 * @param logDetail The log detail to show in the log
 */
public static void enableLoggingOfRequestAndResponseIfValidationFails(LogDetail logDetail) {
    config = config == null ? new RestAssuredMockMvcConfig() : config;
    config = config.logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails(logDetail));
    // Note that request spec also influence response spec when it comes to logging if validation fails due to the way filters work
    if (requestSpecification != null && requestSpecification instanceof MockMvcRequestSpecificationImpl) {
        RestAssuredMockMvcConfig restAssuredConfig = ((MockMvcRequestSpecificationImpl) requestSpecification).getRestAssuredMockMvcConfig();
        if (restAssuredConfig == null) {
            restAssuredConfig = config;
        } else {
            LogConfig logConfigForRequestSpec = restAssuredConfig.getLogConfig().enableLoggingOfRequestAndResponseIfValidationFails(logDetail);
            restAssuredConfig = restAssuredConfig.logConfig(logConfigForRequestSpec);
        }
        requestSpecification.config(restAssuredConfig);
    }
}
Also used : MockMvcRequestSpecificationImpl(io.restassured.module.mockmvc.internal.MockMvcRequestSpecificationImpl) RestAssuredMockMvcConfig(io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig) LogConfig(io.restassured.config.LogConfig)

Example 3 with RestAssuredMockMvcConfig

use of io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig 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 4 with RestAssuredMockMvcConfig

use of io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig 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 5 with RestAssuredMockMvcConfig

use of io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig 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)

Aggregations

RestAssuredMockMvcConfig (io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig)11 LogConfig (io.restassured.config.LogConfig)7 Test (org.junit.Test)7 PostController (io.restassured.module.mockmvc.http.PostController)4 MockMvcRequestSpecBuilder (io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder)3 MockMvcRequestSpecification (io.restassured.module.mockmvc.specification.MockMvcRequestSpecification)3 PrintStream (java.io.PrintStream)2 StringWriter (java.io.StringWriter)2 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)2 Before (org.junit.Before)2 ResponseSpecBuilder (io.restassured.builder.ResponseSpecBuilder)1 RestAssuredConfig (io.restassured.config.RestAssuredConfig)1 MockMvcRequestSpecificationImpl (io.restassured.module.mockmvc.internal.MockMvcRequestSpecificationImpl)1