From a3b371bf4bd04ce590dc2b3df0a7e373b015e6d5 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 8 Apr 2016 22:18:46 +0200 Subject: [PATCH] Accept all media types in StringDecodere This commit adds the "*/*" media type in the list of compatible media types for the StringDecoder. This allows this decoder to decoder payloads of any media type as long as the target type is String. Fixes #87 --- .../core/codec/support/StringDecoder.java | 3 ++- .../codec/support/StringDecoderTests.java | 3 ++- .../reactive/WebClientIntegrationTests.java | 22 +++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/spring-web-reactive/src/main/java/org/springframework/core/codec/support/StringDecoder.java b/spring-web-reactive/src/main/java/org/springframework/core/codec/support/StringDecoder.java index 13d19e0e95..090adca2d0 100644 --- a/spring-web-reactive/src/main/java/org/springframework/core/codec/support/StringDecoder.java +++ b/spring-web-reactive/src/main/java/org/springframework/core/codec/support/StringDecoder.java @@ -25,6 +25,7 @@ import reactor.core.publisher.Flux; import org.springframework.core.ResolvableType; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.util.MimeType; +import org.springframework.util.MimeTypeUtils; /** * Decode from a bytes stream to a String stream. @@ -63,7 +64,7 @@ public class StringDecoder extends AbstractDecoder { * and decode a single consolidated String or re-emit items as they are provided */ public StringDecoder(boolean reduceToSingleBuffer) { - super(new MimeType("text", "*", DEFAULT_CHARSET)); + super(new MimeType("text", "*", DEFAULT_CHARSET), MimeTypeUtils.ALL); this.reduceToSingleBuffer = reduceToSingleBuffer; } diff --git a/spring-web-reactive/src/test/java/org/springframework/core/codec/support/StringDecoderTests.java b/spring-web-reactive/src/test/java/org/springframework/core/codec/support/StringDecoderTests.java index 33463f96dd..44af9239a5 100644 --- a/spring-web-reactive/src/test/java/org/springframework/core/codec/support/StringDecoderTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/core/codec/support/StringDecoderTests.java @@ -48,8 +48,9 @@ public class StringDecoderTests extends AbstractAllocatingTestCase { public void canDecode() { assertTrue(decoder.canDecode(ResolvableType.forClass(String.class), MediaType.TEXT_PLAIN)); assertTrue(decoder.canDecode(ResolvableType.forClass(String.class), MediaType.TEXT_HTML)); + assertTrue(decoder.canDecode(ResolvableType.forClass(String.class), MediaType.APPLICATION_JSON)); assertFalse(decoder.canDecode(ResolvableType.forClass(Integer.class), MediaType.TEXT_PLAIN)); - assertFalse(decoder.canDecode(ResolvableType.forClass(String.class), MediaType.APPLICATION_JSON)); + assertFalse(decoder.canDecode(ResolvableType.forClass(Pojo.class), MediaType.APPLICATION_JSON)); } @Test diff --git a/spring-web-reactive/src/test/java/org/springframework/web/client/reactive/WebClientIntegrationTests.java b/spring-web-reactive/src/test/java/org/springframework/web/client/reactive/WebClientIntegrationTests.java index 0d3f15c7cd..5f37a963fb 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/client/reactive/WebClientIntegrationTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/client/reactive/WebClientIntegrationTests.java @@ -130,6 +130,28 @@ public class WebClientIntegrationTests { assertEquals("text/plain", request.getHeader(HttpHeaders.ACCEPT)); } + @Test + public void shouldGetJsonAsMonoOfString() throws Exception { + + HttpUrl baseUrl = server.url("/json"); + String content = "{\"bar\":\"barbar\",\"foo\":\"foofoo\"}"; + this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json") + .setBody(content)); + + Mono result = this.webClient + .perform(get(baseUrl.toString()) + .accept(MediaType.APPLICATION_JSON)) + .extract(body(String.class)); + + TestSubscriber ts = new TestSubscriber(); + result.subscribe(ts); + ts.awaitAndAssertNextValues(content).assertComplete(); + RecordedRequest request = server.takeRequest(); + assertEquals(1, server.getRequestCount()); + assertEquals("/json", request.getPath()); + assertEquals("application/json", request.getHeader(HttpHeaders.ACCEPT)); + } + @Test public void shouldGetJsonAsMonoOfPojo() throws Exception {