use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project spring-framework by spring-projects.
the class HttpEntityMethodProcessorTests method handleReturnValueCharSequence.
// SPR-13423
@Test
public void handleReturnValueCharSequence() throws Exception {
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new StringHttpMessageConverter());
Method method = getClass().getDeclaredMethod("handle");
MethodParameter returnType = new MethodParameter(method, -1);
ResponseEntity<StringBuilder> returnValue = ResponseEntity.ok(new StringBuilder("Foo"));
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
processor.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
assertEquals("text/plain;charset=ISO-8859-1", servletResponse.getHeader("Content-Type"));
assertEquals("Foo", servletResponse.getContentAsString());
}
use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method handleReturnValueString.
@Test
public void handleReturnValueString() throws Exception {
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new StringHttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
processor.handleReturnValue("Foo", returnTypeString, container, request);
assertEquals("text/plain;charset=ISO-8859-1", servletResponse.getHeader("Content-Type"));
assertEquals("Foo", servletResponse.getContentAsString());
}
use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method handleReturnValueCharSequence.
// SPR-13423
@Test
public void handleReturnValueCharSequence() throws Exception {
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new StringHttpMessageConverter());
Method method = ResponseBodyController.class.getMethod("handleWithCharSequence");
MethodParameter returnType = new MethodParameter(method, -1);
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
processor.handleReturnValue(new StringBuilder("Foo"), returnType, container, request);
assertEquals("text/plain;charset=ISO-8859-1", servletResponse.getHeader("Content-Type"));
assertEquals("Foo", servletResponse.getContentAsString());
}
use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project spring-framework by spring-projects.
the class RequestPartIntegrationTests method standardMultipartResolverWithEncodedFileName.
// SPR-13319
@Test
public void standardMultipartResolverWithEncodedFileName() throws Exception {
byte[] boundary = MimeTypeUtils.generateMultipartBoundary();
String boundaryText = new String(boundary, "US-ASCII");
Map<String, String> params = Collections.singletonMap("boundary", boundaryText);
String content = "--" + boundaryText + "\n" + "Content-Disposition: form-data; name=\"file\"; filename*=\"utf-8''%C3%A9l%C3%A8ve.txt\"\n" + "Content-Type: text/plain\n" + "Content-Length: 7\n" + "\n" + "content\n" + "--" + boundaryText + "--";
RequestEntity<byte[]> requestEntity = RequestEntity.post(new URI(baseUrl + "/standard-resolver/spr13319")).contentType(new MediaType(MediaType.MULTIPART_FORM_DATA, params)).body(content.getBytes(StandardCharsets.US_ASCII));
ByteArrayHttpMessageConverter converter = new ByteArrayHttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.MULTIPART_FORM_DATA));
this.restTemplate.setMessageConverters(Collections.singletonList(converter));
ResponseEntity<Void> responseEntity = restTemplate.exchange(requestEntity, Void.class);
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
}
use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method unsupportedRequestBody.
@Test
public void unsupportedRequestBody() throws ServletException, IOException {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
adapterDef.getPropertyValues().add("messageConverters", new ByteArrayHttpMessageConverter());
wac.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, RequestResponseBodyController.class);
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
String requestBody = "Hello World";
request.setContent(requestBody.getBytes("UTF-8"));
request.addHeader("Content-Type", "application/pdf");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals(415, response.getStatus());
assertNotNull("No Accept response header set", response.getHeader("Accept"));
}
Aggregations