Search in sources :

Example 1 with GreetingController

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

the class ContentTypeTest method adds_specific_charset_to_content_type_by_default.

@Test
public void adds_specific_charset_to_content_type_by_default() {
    final AtomicReference<String> contentType = new AtomicReference<String>();
    RestAssuredMockMvc.given().standaloneSetup(new GreetingController()).config(RestAssuredMockMvc.config().encoderConfig(EncoderConfig.encoderConfig().defaultCharsetForContentType(StandardCharsets.UTF_16.toString(), ContentType.JSON))).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=" + StandardCharsets.UTF_16.toString());
    assertThat(contentType.get()).doesNotContain(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 2 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_configured_not_to_do_so.

@Test
public void doesnt_add_default_charset_to_content_type_if_configured_not_to_do_so() {
    final AtomicReference<String> contentType = new AtomicReference<String>();
    RestAssuredMockMvc.given().config(RestAssuredMockMvc.config().encoderConfig(EncoderConfig.encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false))).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");
}
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 3 with GreetingController

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

the class ExtractTest method can_extract_spring_mvcs_response.

@Test
public void can_extract_spring_mvcs_response() {
    MockHttpServletResponse response = RestAssuredMockMvc.given().standaloneSetup(new GreetingController()).param("name", "Johan").when().get("/greeting").then().statusCode(200).body("id", equalTo(1)).extract().response().mockHttpServletResponse();
    assertThat(response.getContentType()).contains("application/json");
}
Also used : GreetingController(io.restassured.module.mockmvc.http.GreetingController) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 4 with GreetingController

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

the class GreetingControllerRestAssuredTest method param_with_int.

@Test
public void param_with_int() throws Exception {
    MockMvc mockMvc = standaloneSetup(new GreetingController()).build();
    RestAssuredMockMvc.given().mockMvc(mockMvc).param("name", 1).when().get("/greeting").then().body("id", equalTo(1)).body("content", equalTo("Hello, 1!"));
}
Also used : GreetingController(io.restassured.module.mockmvc.http.GreetingController) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 5 with GreetingController

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

the class MockMvcStaticRequestAndResponseSpecTest method request_and_response_spec_can_be_defined_statically.

@Test
public void request_and_response_spec_can_be_defined_statically() {
    RestAssuredMockMvc.requestSpecification = new MockMvcRequestSpecBuilder().addQueryParam("name", "Johan").build();
    RestAssuredMockMvc.responseSpecification = new ResponseSpecBuilder().expectStatusCode(200).expectBody("content", equalTo("Hello, Johan!")).build();
    try {
        // When
        RestAssuredMockMvc.given().standaloneSetup(new GreetingController()).when().get("/greeting").then().body("id", equalTo(1));
    } finally {
        RestAssuredMockMvc.reset();
    }
}
Also used : MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) 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