Search in sources :

Example 1 with MockHttpServletRequestBuilderInterceptor

use of io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor 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 MockHttpServletRequestBuilderInterceptor

use of io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor 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 MockHttpServletRequestBuilderInterceptor

use of io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor in project rest-assured by rest-assured.

the class MockMvcRequestSpecificationMergingTest method interception_is_overwritten_when_defined_in_specification.

@Test
public void interception_is_overwritten_when_defined_in_specification() {
    // Given
    MockHttpServletRequestBuilderInterceptor otherInterceptor = new MockHttpServletRequestBuilderInterceptor() {

        public void intercept(MockHttpServletRequestBuilder requestBuilder) {
        }
    };
    MockHttpServletRequestBuilderInterceptor thisInterceptor = new MockHttpServletRequestBuilderInterceptor() {

        public void intercept(MockHttpServletRequestBuilder requestBuilder) {
        }
    };
    MockMvcRequestSpecification specToMerge = new MockMvcRequestSpecBuilder().setMockHttpServletRequestBuilderInterceptor(otherInterceptor).build();
    // When
    MockMvcRequestSpecification spec = RestAssuredMockMvc.given().interceptor(thisInterceptor).spec(specToMerge);
    // Then
    Assertions.assertThat(implOf(spec).getInterceptor()).isEqualTo(otherInterceptor);
}
Also used : MockMvcRequestSpecification(io.restassured.module.mockmvc.specification.MockMvcRequestSpecification) MockHttpServletRequestBuilderInterceptor(io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor) MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) Test(org.junit.Test)

Example 4 with MockHttpServletRequestBuilderInterceptor

use of io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor in project rest-assured by rest-assured.

the class MockMvcRequestSpecificationMergingTest method interception_is_not_overwritten_when_not_defined_in_specification.

@Test
public void interception_is_not_overwritten_when_not_defined_in_specification() {
    // Given
    MockHttpServletRequestBuilderInterceptor thisInterceptor = new MockHttpServletRequestBuilderInterceptor() {

        public void intercept(MockHttpServletRequestBuilder requestBuilder) {
        }
    };
    MockMvcRequestSpecification specToMerge = new MockMvcRequestSpecBuilder().addQueryParam("param1", "value1").build();
    // When
    MockMvcRequestSpecification spec = RestAssuredMockMvc.given().interceptor(thisInterceptor).spec(specToMerge);
    // Then
    Assertions.assertThat(implOf(spec).getInterceptor()).isEqualTo(thisInterceptor);
    Assertions.assertThat(implOf(spec).getQueryParams()).containsOnly(entry("param1", "value1"));
}
Also used : MockMvcRequestSpecification(io.restassured.module.mockmvc.specification.MockMvcRequestSpecification) MockHttpServletRequestBuilderInterceptor(io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor) MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) Test(org.junit.Test)

Example 5 with MockHttpServletRequestBuilderInterceptor

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

Aggregations

MockHttpServletRequestBuilderInterceptor (io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor)7 Test (org.junit.Test)6 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)6 GreetingController (io.restassured.module.mockmvc.http.GreetingController)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 MultiValueMap (org.springframework.util.MultiValueMap)4 MockMvcRequestSpecBuilder (io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder)2 MockMvcRequestSpecification (io.restassured.module.mockmvc.specification.MockMvcRequestSpecification)2 RequestLoggingFilter (io.restassured.filter.log.RequestLoggingFilter)1 AsyncConfig (io.restassured.module.mockmvc.config.AsyncConfig)1