Search in sources :

Example 11 with GreetingController

use of io.restassured.module.mockmvc.http.GreetingController in project rest-assured by rest-assured.

the class MockMvcRequestSpecificationMergingTest method mock_mvc_instance_is_overwritten_when_defined_in_specification.

@Test
public void mock_mvc_instance_is_overwritten_when_defined_in_specification() {
    // Given
    MockMvc otherMockMvcInstance = MockMvcBuilders.standaloneSetup(new PostController()).build();
    MockMvc thisMockMvcInstance = MockMvcBuilders.standaloneSetup(new GreetingController()).build();
    MockMvcRequestSpecification specToMerge = new MockMvcRequestSpecBuilder().setMockMvc(otherMockMvcInstance).build();
    // When
    MockMvcRequestSpecification spec = RestAssuredMockMvc.given().mockMvc(thisMockMvcInstance).spec(specToMerge);
    // Then
    assertThat(Whitebox.getInternalState(implOf(spec).getMockMvcFactory(), "mockMvc")).isSameAs(otherMockMvcInstance);
}
Also used : MockMvcRequestSpecification(io.restassured.module.mockmvc.specification.MockMvcRequestSpecification) PostController(io.restassured.module.mockmvc.http.PostController) MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) GreetingController(io.restassured.module.mockmvc.http.GreetingController) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 12 with GreetingController

use of io.restassured.module.mockmvc.http.GreetingController in project rest-assured by rest-assured.

the class MockMvcRequestSpecificationMergingTest method mock_mvc_factory_is_not_overwritten_when_not_defined_in_specification.

@Test
public void mock_mvc_factory_is_not_overwritten_when_not_defined_in_specification() {
    // Given
    MockMvc mockMvcInstance = MockMvcBuilders.standaloneSetup(new GreetingController()).build();
    MockMvcRequestSpecification specToMerge = new MockMvcRequestSpecBuilder().addQueryParam("param1", "value1").build();
    // When
    MockMvcRequestSpecification spec = RestAssuredMockMvc.given().mockMvc(mockMvcInstance).spec(specToMerge);
    // Then
    assertThat(Whitebox.getInternalState(implOf(spec).getMockMvcFactory(), "mockMvc")).isSameAs(mockMvcInstance);
    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) GreetingController(io.restassured.module.mockmvc.http.GreetingController) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 13 with GreetingController

use of io.restassured.module.mockmvc.http.GreetingController in project rest-assured by rest-assured.

the class ContentTypeTest method adds_default_charset_to_content_type_by_default.

@Test
public void adds_default_charset_to_content_type_by_default() {
    final AtomicReference<String> contentType = new AtomicReference<String>();
    RestAssuredMockMvc.given().standaloneSetup(new GreetingController()).contentType(ContentType.JSON).interceptor(new MockHttpServletRequestBuilderInterceptor() {

        public void intercept(MockHttpServletRequestBuilder requestBuilder) {
            MultiValueMap<String, Object> headers = Whitebox.getInternalState(requestBuilder, "headers");
            contentType.set(String.valueOf(headers.getFirst("Content-Type")));
        }
    }).when().get("/greeting?name={name}", "Johan").then().statusCode(200);
    assertThat(contentType.get()).isEqualTo("application/json;charset=" + RestAssuredMockMvc.config().getEncoderConfig().defaultContentCharset());
}
Also used : MockHttpServletRequestBuilderInterceptor(io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) GreetingController(io.restassured.module.mockmvc.http.GreetingController) MultiValueMap(org.springframework.util.MultiValueMap) Test(org.junit.Test)

Example 14 with GreetingController

use of io.restassured.module.mockmvc.http.GreetingController in project rest-assured by rest-assured.

the class ContentTypeTest method doesnt_add_default_charset_to_content_type_if_charset_is_defined_explicitly.

@Test
public void doesnt_add_default_charset_to_content_type_if_charset_is_defined_explicitly() {
    final AtomicReference<String> contentType = new AtomicReference<String>();
    RestAssuredMockMvc.given().standaloneSetup(new GreetingController()).contentType(ContentType.JSON.withCharset("UTF-16")).interceptor(new MockHttpServletRequestBuilderInterceptor() {

        public void intercept(MockHttpServletRequestBuilder requestBuilder) {
            MultiValueMap<String, Object> headers = Whitebox.getInternalState(requestBuilder, "headers");
            contentType.set(String.valueOf(headers.getFirst("Content-Type")));
        }
    }).when().get("/greeting?name={name}", "Johan").then().statusCode(200);
    assertThat(contentType.get()).isEqualTo("application/json;charset=UTF-16");
}
Also used : MockHttpServletRequestBuilderInterceptor(io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) GreetingController(io.restassured.module.mockmvc.http.GreetingController) MultiValueMap(org.springframework.util.MultiValueMap) Test(org.junit.Test)

Example 15 with GreetingController

use of io.restassured.module.mockmvc.http.GreetingController in project rest-assured by rest-assured.

the class ExtractTest method can_extract_rest_assureds_mock_mvc_response.

@Test
public void can_extract_rest_assureds_mock_mvc_response() {
    MockMvcResponse response = RestAssuredMockMvc.given().standaloneSetup(new GreetingController()).param("name", "Johan").when().get("/greeting").then().statusCode(200).body("id", equalTo(1)).extract().response();
    assertThat(response.path("content")).isEqualTo("Hello, Johan!");
}
Also used : MockMvcResponse(io.restassured.module.mockmvc.response.MockMvcResponse) GreetingController(io.restassured.module.mockmvc.http.GreetingController) Test(org.junit.Test)

Aggregations

GreetingController (io.restassured.module.mockmvc.http.GreetingController)19 Test (org.junit.Test)19 MockMvcRequestSpecBuilder (io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder)7 MockMvc (org.springframework.test.web.servlet.MockMvc)5 MockHttpServletRequestBuilderInterceptor (io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor)4 MockMvcRequestSpecification (io.restassured.module.mockmvc.specification.MockMvcRequestSpecification)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)4 MultiValueMap (org.springframework.util.MultiValueMap)4 ResponseSpecBuilder (io.restassured.builder.ResponseSpecBuilder)3 PrintStream (java.io.PrintStream)2 StringWriter (java.io.StringWriter)2 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)2 PostController (io.restassured.module.mockmvc.http.PostController)1 MockMvcResponse (io.restassured.module.mockmvc.response.MockMvcResponse)1 JsonPath (io.restassured.path.json.JsonPath)1 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 MvcResult (org.springframework.test.web.servlet.MvcResult)1