use of org.springframework.http.converter.StringHttpMessageConverter in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method overlappingMessageConvertersRequestBody.
// SPR-6877
@PathPatternsParameterizedTest
void overlappingMessageConvertersRequestBody(boolean usePathPatterns) throws Exception {
initDispatcherServlet(RequestResponseBodyController.class, usePathPatterns, wac -> {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new SimpleMessageConverter(new MediaType("application", "json"), MediaType.ALL));
adapterDef.getPropertyValues().add("messageConverters", messageConverters);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
});
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
request.setContent("Hello World".getBytes(StandardCharsets.UTF_8));
request.addHeader("Content-Type", "text/plain; charset=utf-8");
request.addHeader("Accept", "application/json, text/javascript, */*");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getHeader("Content-Type")).as("Invalid content-type").isEqualTo("application/json");
}
use of org.springframework.http.converter.StringHttpMessageConverter in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseBodyNoAcceptableMediaType.
@PathPatternsParameterizedTest
void responseBodyNoAcceptableMediaType(boolean usePathPatterns) throws Exception {
initDispatcherServlet(RequestResponseBodyProducesController.class, usePathPatterns, wac -> {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
StringHttpMessageConverter converter = new StringHttpMessageConverter();
adapterDef.getPropertyValues().add("messageConverters", converter);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
});
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
String requestBody = "Hello World";
request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
request.addHeader("Content-Type", "text/plain; charset=utf-8");
request.addHeader("Accept", "application/pdf, application/msword");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(406);
}
use of org.springframework.http.converter.StringHttpMessageConverter in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method unsupportedRequestBody.
@PathPatternsParameterizedTest
void unsupportedRequestBody(boolean usePathPatterns) throws Exception {
initDispatcherServlet(RequestResponseBodyController.class, usePathPatterns, wac -> {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
StringHttpMessageConverter converter = new StringHttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_PLAIN));
adapterDef.getPropertyValues().add("messageConverters", converter);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
});
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
String requestBody = "Hello World";
request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
request.addHeader("Content-Type", "application/pdf");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(415);
assertThat(response.getHeader("Accept")).isEqualTo("text/plain");
}
use of org.springframework.http.converter.StringHttpMessageConverter in project spring-framework by spring-projects.
the class TestPathHelper method verifyMessageConverters.
@SuppressWarnings("unchecked")
private void verifyMessageConverters(Object bean, boolean hasDefaultRegistrations) {
assertThat(bean).isNotNull();
Object value = new DirectFieldAccessor(bean).getPropertyValue("messageConverters");
assertThat(value).isNotNull();
assertThat(value instanceof List).isTrue();
List<HttpMessageConverter<?>> converters = (List<HttpMessageConverter<?>>) value;
if (hasDefaultRegistrations) {
assertThat(converters.size() > 2).as("Default and custom converter expected").isTrue();
} else {
assertThat(converters.size() == 2).as("Only custom converters expected").isTrue();
}
assertThat(converters.get(0) instanceof StringHttpMessageConverter).isTrue();
assertThat(converters.get(1) instanceof ResourceHttpMessageConverter).isTrue();
}
use of org.springframework.http.converter.StringHttpMessageConverter in project spring-framework by spring-projects.
the class DelegatingWebMvcConfigurationTests method configureMessageConverters.
@Test
public void configureMessageConverters() {
HttpMessageConverter<?> customConverter = mock(HttpMessageConverter.class);
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
WebMvcConfigurer configurer = new WebMvcConfigurer() {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(stringConverter);
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(0, customConverter);
}
};
webMvcConfig.setConfigurers(Collections.singletonList(configurer));
RequestMappingHandlerAdapter adapter = webMvcConfig.requestMappingHandlerAdapter(this.webMvcConfig.mvcContentNegotiationManager(), this.webMvcConfig.mvcConversionService(), this.webMvcConfig.mvcValidator());
assertThat(adapter.getMessageConverters().size()).as("One custom converter expected").isEqualTo(2);
assertThat(adapter.getMessageConverters().get(0)).isSameAs(customConverter);
assertThat(adapter.getMessageConverters().get(1)).isSameAs(stringConverter);
}
Aggregations