Search in sources :

Example 1 with FormHttpMessageConverter

use of org.springframework.http.converter.FormHttpMessageConverter in project spring-framework by spring-projects.

the class ContentRequestMatchers method formData.

/**
	 * Parse the body as form data and compare to the given {@code MultiValueMap}.
	 * @since 4.3
	 */
public RequestMatcher formData(final MultiValueMap<String, String> expectedContent) {
    return new RequestMatcher() {

        @Override
        public void match(final ClientHttpRequest request) throws IOException, AssertionError {
            HttpInputMessage inputMessage = new HttpInputMessage() {

                @Override
                public InputStream getBody() throws IOException {
                    MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
                    return new ByteArrayInputStream(mockRequest.getBodyAsBytes());
                }

                @Override
                public HttpHeaders getHeaders() {
                    return request.getHeaders();
                }
            };
            FormHttpMessageConverter converter = new FormHttpMessageConverter();
            assertEquals("Request content", expectedContent, converter.read(null, inputMessage));
        }
    };
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) FormHttpMessageConverter(org.springframework.http.converter.FormHttpMessageConverter) RequestMatcher(org.springframework.test.web.client.RequestMatcher) ByteArrayInputStream(java.io.ByteArrayInputStream) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest)

Example 2 with FormHttpMessageConverter

use of org.springframework.http.converter.FormHttpMessageConverter in project spring-framework by spring-projects.

the class MockHttpServletRequestBuilder method parseFormData.

private MultiValueMap<String, String> parseFormData(final MediaType mediaType) {
    HttpInputMessage message = new HttpInputMessage() {

        @Override
        public InputStream getBody() throws IOException {
            return new ByteArrayInputStream(content);
        }

        @Override
        public HttpHeaders getHeaders() {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(mediaType);
            return headers;
        }
    };
    try {
        return new FormHttpMessageConverter().read(null, message);
    } catch (IOException ex) {
        throw new IllegalStateException("Failed to parse form data in request body", ex);
    }
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) FormHttpMessageConverter(org.springframework.http.converter.FormHttpMessageConverter) HttpHeaders(org.springframework.http.HttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 HttpInputMessage (org.springframework.http.HttpInputMessage)2 FormHttpMessageConverter (org.springframework.http.converter.FormHttpMessageConverter)2 IOException (java.io.IOException)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)1 MockClientHttpRequest (org.springframework.mock.http.client.MockClientHttpRequest)1 RequestMatcher (org.springframework.test.web.client.RequestMatcher)1