private boolean canReadResponse(Type responseType, HttpMessageConverter<?> converter) { Class<?> responseClass = (responseType instanceof Class ? (Class<?>) responseType : null); if (responseClass != null) { return converter.canRead(responseClass, null); } else if (converter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<?> genericConverter = (GenericHttpMessageConverter<?>) converter; return genericConverter.canRead(responseType, null, null); } return false; }
@Test public void canRead() { MediaType mediaType = new MediaType("foo", "bar"); HttpMessageConverter<MyType> converter = new MyHttpMessageConverter<>(mediaType); assertTrue(converter.canRead(MyType.class, mediaType)); assertFalse(converter.canRead(MyType.class, new MediaType("foo", "*"))); assertFalse(converter.canRead(MyType.class, MediaType.ALL)); }
@Test public void canReadWithWildcardSubtype() { MediaType mediaType = new MediaType("foo"); HttpMessageConverter<MyType> converter = new MyHttpMessageConverter<>(mediaType); assertTrue(converter.canRead(MyType.class, new MediaType("foo", "bar"))); assertTrue(converter.canRead(MyType.class, new MediaType("foo", "*"))); assertFalse(converter.canRead(MyType.class, MediaType.ALL)); }
@Test(expected = HttpMediaTypeNotSupportedException.class) public void resolveArgumentNoContentType() throws Exception { servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8)); given(stringMessageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false); processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null); }
@Test public void resolveArgumentOptionalNoContentNoContentType() throws Exception { servletRequest.setContent(new byte[0]); given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); given(stringMessageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false); assertEquals(Optional.empty(), processor.resolveArgument(paramOptionalString, mavContainer, webRequest, new ValidatingBinderFactory())); }
private void mockHttpMessageConverter(MediaType mediaType, Class type) { given(converter.canRead(type, null)).willReturn(true); given(converter.canRead(type, mediaType)).willReturn(true); given(converter.getSupportedMediaTypes()) .willReturn(Collections.singletonList(mediaType)); given(converter.canRead(type, mediaType)).willReturn(true); given(converter.canWrite(type, null)).willReturn(true); given(converter.canWrite(type, mediaType)).willReturn(true); }
@Test // SPR-13417 public void resolveArgumentNotRequiredNoContentNoContentType() throws Exception { servletRequest.setContent(new byte[0]); given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); given(stringMessageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false); assertNull(processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory())); }
@Test(expected = HttpMediaTypeNotSupportedException.class) public void resolveArgumentCannotRead() throws Exception { MediaType contentType = MediaType.TEXT_PLAIN; servletRequest.addHeader("Content-Type", contentType.toString()); servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8)); given(stringMessageConverter.canRead(String.class, contentType)).willReturn(false); processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null); }
@Test public void resolveArgumentOptionalNoContent() throws Exception { servletRequest.setContentType("text/plain"); servletRequest.setContent(new byte[0]); given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); assertEquals(Optional.empty(), processor.resolveArgument(paramOptionalString, mavContainer, webRequest, new ValidatingBinderFactory())); }
@Test public void resolveArgumentOptionalWithContent() throws Exception { servletRequest.setContentType("text/plain"); servletRequest.setContent("body".getBytes()); given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); given(stringMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn("body"); assertEquals(Optional.of("body"), processor.resolveArgument(paramOptionalString, mavContainer, webRequest, new ValidatingBinderFactory())); }
private void testResolveArgumentWithValidation(SimpleBean simpleBean) throws Exception { MediaType contentType = MediaType.TEXT_PLAIN; servletRequest.addHeader("Content-Type", contentType.toString()); servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8)); @SuppressWarnings("unchecked") HttpMessageConverter<SimpleBean> beanConverter = mock(HttpMessageConverter.class); given(beanConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN)); given(beanConverter.canRead(SimpleBean.class, contentType)).willReturn(true); given(beanConverter.read(eq(SimpleBean.class), isA(HttpInputMessage.class))).willReturn(simpleBean); processor = new RequestResponseBodyMethodProcessor(Collections.singletonList(beanConverter)); processor.resolveArgument(paramValidBean, mavContainer, webRequest, new ValidatingBinderFactory()); }
@Test public void shouldFailResolvingWhenConverterCannotRead() throws Exception { MediaType contentType = TEXT_PLAIN; servletRequest.setMethod("POST"); servletRequest.addHeader("Content-Type", contentType.toString()); given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(contentType)); given(stringHttpMessageConverter.canRead(String.class, contentType)).willReturn(false); this.thrown.expect(HttpMediaTypeNotSupportedException.class); processor.resolveArgument(paramHttpEntity, mavContainer, webRequest, null); }
@Test public void resolveArgumentNotRequiredNoContent() throws Exception { servletRequest.setContentType("text/plain"); servletRequest.setContent(new byte[0]); given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); assertNull(processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory())); }
@Test public void resolveArgumentNotGetRequests() throws Exception { servletRequest.setMethod("GET"); servletRequest.setContent(new byte[0]); given(stringMessageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false); assertNull(processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory())); }
@Test public void resolveArgumentNotRequiredWithContent() throws Exception { servletRequest.setContentType("text/plain"); servletRequest.setContent("body".getBytes()); given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); given(stringMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn("body"); assertEquals("body", processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory())); }
@Test public void resolveOptionalRequestPart() throws Exception { SimpleBean simpleBean = new SimpleBean("foo"); given(messageConverter.canRead(SimpleBean.class, MediaType.TEXT_PLAIN)).willReturn(true); given(messageConverter.read(eq(SimpleBean.class), isA(HttpInputMessage.class))).willReturn(simpleBean); ModelAndViewContainer mavContainer = new ModelAndViewContainer(); Object actualValue = resolver.resolveArgument( optionalRequestPart, mavContainer, webRequest, new ValidatingBinderFactory()); assertEquals("Invalid argument value", Optional.of(simpleBean), actualValue); assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled()); actualValue = resolver.resolveArgument(optionalRequestPart, mavContainer, webRequest, new ValidatingBinderFactory()); assertEquals("Invalid argument value", Optional.of(simpleBean), actualValue); assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled()); }
@Test public void resolveArgument() throws Exception { MediaType contentType = MediaType.TEXT_PLAIN; servletRequest.addHeader("Content-Type", contentType.toString()); String body = "Foo"; servletRequest.setContent(body.getBytes(StandardCharsets.UTF_8)); given(stringMessageConverter.canRead(String.class, contentType)).willReturn(true); given(stringMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body); Object result = processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, new ValidatingBinderFactory()); assertEquals("Invalid argument", body, result); assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled()); }
@Test public void shouldResolveHttpEntityArgument() throws Exception { String body = "Foo"; MediaType contentType = TEXT_PLAIN; servletRequest.addHeader("Content-Type", contentType.toString()); servletRequest.setContent(body.getBytes(StandardCharsets.UTF_8)); given(stringHttpMessageConverter.canRead(String.class, contentType)).willReturn(true); given(stringHttpMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body); Object result = processor.resolveArgument(paramHttpEntity, mavContainer, webRequest, null); assertTrue(result instanceof HttpEntity); assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled()); assertEquals("Invalid argument", body, ((HttpEntity<?>) result).getBody()); }
@Test(expected = HttpMessageNotReadableException.class) // SPR-9942 public void resolveArgumentRequiredNoContent() throws Exception { servletRequest.setContentType(MediaType.TEXT_PLAIN_VALUE); servletRequest.setContent(new byte[0]); given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); given(stringMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(null); assertNull(processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, new ValidatingBinderFactory())); }
private void testResolveArgument(SimpleBean argValue, MethodParameter parameter) throws Exception { given(messageConverter.canRead(SimpleBean.class, MediaType.TEXT_PLAIN)).willReturn(true); given(messageConverter.read(eq(SimpleBean.class), isA(HttpInputMessage.class))).willReturn(argValue); ModelAndViewContainer mavContainer = new ModelAndViewContainer(); Object actualValue = resolver.resolveArgument(parameter, mavContainer, webRequest, new ValidatingBinderFactory()); assertEquals("Invalid argument value", argValue, actualValue); assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled()); }