use of org.springframework.http.HttpInputMessage in project spring-framework by spring-projects.
the class RequestResponseBodyAdviceChainTests method requestBodyAdvice.
@SuppressWarnings("unchecked")
@Test
public void requestBodyAdvice() throws IOException {
RequestBodyAdvice requestAdvice = Mockito.mock(RequestBodyAdvice.class);
ResponseBodyAdvice<String> responseAdvice = Mockito.mock(ResponseBodyAdvice.class);
List<Object> advice = Arrays.asList(requestAdvice, responseAdvice);
RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(advice);
HttpInputMessage wrapped = new ServletServerHttpRequest(new MockHttpServletRequest());
given(requestAdvice.supports(this.paramType, String.class, this.converterType)).willReturn(true);
given(requestAdvice.beforeBodyRead(eq(this.request), eq(this.paramType), eq(String.class), eq(this.converterType))).willReturn(wrapped);
assertSame(wrapped, chain.beforeBodyRead(this.request, this.paramType, String.class, this.converterType));
String modified = "body++";
given(requestAdvice.afterBodyRead(eq(this.body), eq(this.request), eq(this.paramType), eq(String.class), eq(this.converterType))).willReturn(modified);
assertEquals(modified, chain.afterBodyRead(this.body, this.request, this.paramType, String.class, this.converterType));
}
use of org.springframework.http.HttpInputMessage in project spring-security-oauth by spring-projects.
the class OAuth2ErrorHandlerTests method testHandleMessageConversionExceptions.
@Test
public void testHandleMessageConversionExceptions() throws Exception {
HttpMessageConverter<?> extractor = new HttpMessageConverter() {
@Override
public boolean canRead(Class clazz, MediaType mediaType) {
return true;
}
@Override
public boolean canWrite(Class clazz, MediaType mediaType) {
return false;
}
@Override
public List<MediaType> getSupportedMediaTypes() {
return null;
}
@Override
public Object read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
throw new HttpMessageConversionException("error");
}
@Override
public void write(Object o, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
}
};
ArrayList<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(extractor);
handler.setMessageConverters(messageConverters);
HttpHeaders headers = new HttpHeaders();
final String appSpecificBodyContent = "This user is not authorized";
InputStream appSpecificErrorBody = new ByteArrayInputStream(appSpecificBodyContent.getBytes("UTF-8"));
ClientHttpResponse response = new TestClientHttpResponse(headers, 401, appSpecificErrorBody);
expected.expect(HttpClientErrorException.class);
handler.handleError(response);
}
use of org.springframework.http.HttpInputMessage in project spring-framework by spring-projects.
the class HttpPutFormContentFilter method doFilterInternal.
@Override
protected void doFilterInternal(final HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if (("PUT".equals(request.getMethod()) || "PATCH".equals(request.getMethod())) && isFormContentType(request)) {
HttpInputMessage inputMessage = new ServletServerHttpRequest(request) {
@Override
public InputStream getBody() throws IOException {
return request.getInputStream();
}
};
MultiValueMap<String, String> formParameters = formConverter.read(null, inputMessage);
HttpServletRequest wrapper = new HttpPutFormContentRequestWrapper(request, formParameters);
filterChain.doFilter(wrapper, response);
} else {
filterChain.doFilter(request, response);
}
}
use of org.springframework.http.HttpInputMessage 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));
}
};
}
use of org.springframework.http.HttpInputMessage 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);
}
}
Aggregations