Search in sources :

Example 21 with MockMvcRequestSpecBuilder

use of io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder in project rest-assured by rest-assured.

the class MockMvcRequestSpecificationMergingTest method base_path_is_not_overwritten_when_not_defined_in_specification.

@Test
public void base_path_is_not_overwritten_when_not_defined_in_specification() {
    // Given
    MockMvcRequestSpecification specToMerge = new MockMvcRequestSpecBuilder().addQueryParam("param1", "value1").build();
    // When
    MockMvcRequestSpecification spec = RestAssuredMockMvc.given().body("body1").spec(specToMerge);
    // Then
    Assertions.assertThat(implOf(spec).getBasePath()).isEqualTo(RestAssuredMockMvc.basePath);
    Assertions.assertThat(implOf(spec).getQueryParams()).containsOnly(entry("param1", "value1"));
}
Also used : MockMvcRequestSpecification(io.restassured.module.mockmvc.specification.MockMvcRequestSpecification) MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) Test(org.junit.Test)

Example 22 with MockMvcRequestSpecBuilder

use of io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder in project rest-assured by rest-assured.

the class SecuredControllerTest method statically_defined_auth_has_precedence_over_statically_defined_request_spec.

@Test
public void statically_defined_auth_has_precedence_over_statically_defined_request_spec() {
    RestAssuredMockMvc.authentication = RestAssuredMockMvc.principal(new User("authorized_user", "password", Collections.<GrantedAuthority>emptyList()));
    RestAssuredMockMvc.requestSpecification = new MockMvcRequestSpecBuilder().setAuth(RestAssuredMockMvc.authentication(new TestingAuthenticationToken("name", "pw"))).build();
    try {
        RestAssuredMockMvc.given().standaloneSetup(new SecuredController()).param("name", "Johan").when().get("/springSecurityGreeting").then().statusCode(200).body("content", equalTo("Hello, Johan!"));
    } finally {
        RestAssuredMockMvc.reset();
    }
}
Also used : SecuredController(io.restassured.module.mockmvc.http.SecuredController) User(org.springframework.security.core.userdetails.User) MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.Test)

Example 23 with MockMvcRequestSpecBuilder

use of io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder 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 24 with MockMvcRequestSpecBuilder

use of io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder in project rest-assured by rest-assured.

the class MockMvcRequestSpecificationMergingTest method content_type_is_overwritten_when_defined_in_specification.

@Test
public void content_type_is_overwritten_when_defined_in_specification() {
    // Given
    MockMvcRequestSpecification specToMerge = new MockMvcRequestSpecBuilder().setContentType(ContentType.JSON).build();
    // When
    MockMvcRequestSpecification spec = RestAssuredMockMvc.given().contentType(ContentType.XML).spec(specToMerge);
    // Then
    Assertions.assertThat(implOf(spec).getRequestContentType()).isEqualTo(ContentType.JSON.toString());
}
Also used : MockMvcRequestSpecification(io.restassured.module.mockmvc.specification.MockMvcRequestSpecification) MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) Test(org.junit.Test)

Example 25 with MockMvcRequestSpecBuilder

use of io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder in project rest-assured by rest-assured.

the class MockMvcRequestSpecificationMergingTest method authentication_is_overwritten_when_using_dsl_and_defined_in_specification.

@Test
public void authentication_is_overwritten_when_using_dsl_and_defined_in_specification() {
    // Given
    MockMvcAuthenticationScheme otherAuth = RestAssuredMockMvc.principal("other");
    MockMvcRequestSpecification specToMerge = new MockMvcRequestSpecBuilder().setAuth(otherAuth).build();
    // When
    MockMvcRequestSpecification spec = RestAssuredMockMvc.given().auth().principal("this").and().spec(specToMerge);
    // Then
    assertThat(((TestingAuthenticationToken) implOf(spec).getAuthentication()).getPrincipal()).isEqualTo("other");
}
Also used : MockMvcRequestSpecification(io.restassured.module.mockmvc.specification.MockMvcRequestSpecification) MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) MockMvcAuthenticationScheme(io.restassured.module.mockmvc.specification.MockMvcAuthenticationScheme) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.Test)

Aggregations

MockMvcRequestSpecBuilder (io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder)36 Test (org.junit.Test)36 MockMvcRequestSpecification (io.restassured.module.mockmvc.specification.MockMvcRequestSpecification)31 GreetingController (io.restassured.module.mockmvc.http.GreetingController)7 ResponseSpecBuilder (io.restassured.builder.ResponseSpecBuilder)4 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)4 RestAssuredMockMvcConfig (io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig)3 MockMvcAuthenticationScheme (io.restassured.module.mockmvc.specification.MockMvcAuthenticationScheme)3 PrintStream (java.io.PrintStream)3 StringWriter (java.io.StringWriter)3 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)3 Cookie (io.restassured.http.Cookie)2 Header (io.restassured.http.Header)2 PostController (io.restassured.module.mockmvc.http.PostController)2 MockHttpServletRequestBuilderInterceptor (io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor)2 MockMvc (org.springframework.test.web.servlet.MockMvc)2 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)2 LogConfig (io.restassured.config.LogConfig)1 RestAssuredConfig (io.restassured.config.RestAssuredConfig)1 SecuredController (io.restassured.module.mockmvc.http.SecuredController)1