use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method defaultCharset.
// SPR-13631
@Test
public void defaultCharset() throws Exception {
Method method = JacksonController.class.getMethod("defaultCharset");
HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
MethodParameter methodReturnType = handlerMethod.getReturnType();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
Object returnValue = new JacksonController().defaultCharset();
processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);
assertEquals("UTF-8", this.servletResponse.getCharacterEncoding());
}
use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method jacksonJsonViewWithResponseEntityAndJsonMessageConverter.
@Test
public void jacksonJsonViewWithResponseEntityAndJsonMessageConverter() throws Exception {
Method method = JacksonController.class.getMethod("handleResponseEntity");
HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
MethodParameter methodReturnType = handlerMethod.getReturnType();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice()));
Object returnValue = new JacksonController().handleResponseEntity();
processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);
String content = this.servletResponse.getContentAsString();
assertFalse(content.contains("\"withView1\":\"with\""));
assertTrue(content.contains("\"withView2\":\"with\""));
assertFalse(content.contains("\"withoutView\":\"without\""));
}
use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class RequestMappingHandlerAdapterTests method jsonpResponseBodyAdvice.
@Test
public void jsonpResponseBodyAdvice() throws Exception {
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
this.handlerAdapter.setMessageConverters(converters);
this.webAppContext.registerSingleton("jsonpAdvice", JsonpAdvice.class);
this.webAppContext.refresh();
testJsonp("callback", true);
testJsonp("_callback", true);
testJsonp("_Call.bAcK", true);
testJsonp("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.", true);
testJsonp("<script>", false);
testJsonp("!foo!bar", false);
}
use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class RequestPartMethodArgumentResolverTests method setup.
@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
messageConverter = mock(HttpMessageConverter.class);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
resolver = new RequestPartMethodArgumentResolver(Collections.<HttpMessageConverter<?>>singletonList(messageConverter));
reset(messageConverter);
byte[] content = "doesn't matter as long as not empty".getBytes(StandardCharsets.UTF_8);
multipartFile1 = new MockMultipartFile("requestPart", "", "text/plain", content);
multipartFile2 = new MockMultipartFile("requestPart", "", "text/plain", content);
multipartRequest = new MockMultipartHttpServletRequest();
multipartRequest.addFile(multipartFile1);
multipartRequest.addFile(multipartFile2);
multipartRequest.addFile(new MockMultipartFile("otherPart", "", "text/plain", content));
webRequest = new ServletWebRequest(multipartRequest, new MockHttpServletResponse());
Method method = ReflectionUtils.findMethod(getClass(), "handle", (Class<?>[]) null);
paramRequestPart = new SynthesizingMethodParameter(method, 0);
paramRequestPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
paramNamedRequestPart = new SynthesizingMethodParameter(method, 1);
paramValidRequestPart = new SynthesizingMethodParameter(method, 2);
paramMultipartFile = new SynthesizingMethodParameter(method, 3);
paramMultipartFileList = new SynthesizingMethodParameter(method, 4);
paramMultipartFileArray = new SynthesizingMethodParameter(method, 5);
paramInt = new SynthesizingMethodParameter(method, 6);
paramMultipartFileNotAnnot = new SynthesizingMethodParameter(method, 7);
paramMultipartFileNotAnnot.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
paramPart = new SynthesizingMethodParameter(method, 8);
paramPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
paramPartList = new SynthesizingMethodParameter(method, 9);
paramPartArray = new SynthesizingMethodParameter(method, 10);
paramRequestParamAnnot = new SynthesizingMethodParameter(method, 11);
optionalMultipartFile = new SynthesizingMethodParameter(method, 12);
optionalMultipartFile.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
optionalMultipartFileList = new SynthesizingMethodParameter(method, 13);
optionalMultipartFileList.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
optionalPart = new SynthesizingMethodParameter(method, 14);
optionalPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
optionalPartList = new SynthesizingMethodParameter(method, 15);
optionalPartList.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
optionalRequestPart = new SynthesizingMethodParameter(method, 16);
}
use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method resolveArgumentRawTypeFromParameterizedType.
@Test
public void resolveArgumentRawTypeFromParameterizedType() throws Exception {
String content = "fruit=apple&vegetable=kale";
this.servletRequest.setMethod("GET");
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new AllEncompassingFormHttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
@SuppressWarnings("unchecked") MultiValueMap<String, String> result = (MultiValueMap<String, String>) processor.resolveArgument(paramMultiValueMap, container, request, factory);
assertNotNull(result);
assertEquals("apple", result.getFirst("fruit"));
assertEquals("kale", result.getFirst("vegetable"));
}
Aggregations