Search in sources :

Example 6 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_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 7 with MockHttpServletRequestBuilderInterceptor

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

the class MockMvcRequestSpecificationImpl method spec.

public MockMvcRequestSpecification spec(MockMvcRequestSpecification requestSpecificationToMerge) {
    notNull(requestSpecificationToMerge, MockMvcRequestSpecification.class);
    if (!(requestSpecificationToMerge instanceof MockMvcRequestSpecificationImpl)) {
        throw new IllegalArgumentException("requestSpecificationToMerge must be an instance of " + MockMvcRequestSpecificationImpl.class.getName());
    }
    MockMvcRequestSpecificationImpl that = (MockMvcRequestSpecificationImpl) requestSpecificationToMerge;
    Object otherRequestBody = that.getRequestBody();
    if (otherRequestBody != null) {
        this.requestBody = otherRequestBody;
    }
    if (isNotEmpty(that.getBasePath())) {
        this.basePath = that.getBasePath();
    }
    MockMvcFactory otherMockMvcFactory = that.getMockMvcFactory();
    if (otherMockMvcFactory != null && otherMockMvcFactory.isAssigned()) {
        this.changeMockMvcFactoryTo(otherMockMvcFactory);
    }
    this.cookies(that.getCookies());
    this.headers(that.getRequestHeaders());
    mergeConfig(this, that);
    MockHttpServletRequestBuilderInterceptor otherInterceptor = that.getInterceptor();
    if (otherInterceptor != null) {
        this.interceptor = otherInterceptor;
    }
    this.formParams(that.getFormParams());
    this.queryParams(that.getQueryParams());
    this.params(that.getParams());
    this.attributes(that.getAttributes());
    this.multiParts.addAll(that.getMultiParts());
    this.resultHandlers.addAll(that.getResultHandlers());
    this.requestPostProcessors.addAll(that.getRequestPostProcessors());
    RequestLoggingFilter otherRequestLoggingFilter = that.getRequestLoggingFilter();
    if (otherRequestLoggingFilter != null) {
        this.requestLoggingFilter = otherRequestLoggingFilter;
    }
    Object otherAuth = that.getAuthentication();
    if (otherAuth != null) {
        this.authentication = otherAuth;
    }
    AsyncConfig otherAsyncConfig = that.getAsyncConfig();
    if (otherAsyncConfig != null) {
        this.asyncConfig = otherAsyncConfig;
    }
    return this;
}
Also used : MockHttpServletRequestBuilderInterceptor(io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor) AsyncConfig(io.restassured.module.mockmvc.config.AsyncConfig) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter)

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