Browse Source

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
pull/1111/head
Brian Clozel 10 years ago
parent
commit
a3b371bf4b
  1. 3
      spring-web-reactive/src/main/java/org/springframework/core/codec/support/StringDecoder.java
  2. 3
      spring-web-reactive/src/test/java/org/springframework/core/codec/support/StringDecoderTests.java
  3. 22
      spring-web-reactive/src/test/java/org/springframework/web/client/reactive/WebClientIntegrationTests.java

3
spring-web-reactive/src/main/java/org/springframework/core/codec/support/StringDecoder.java

@ -25,6 +25,7 @@ import reactor.core.publisher.Flux; @@ -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<String> { @@ -63,7 +64,7 @@ public class StringDecoder extends AbstractDecoder<String> {
* 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;
}

3
spring-web-reactive/src/test/java/org/springframework/core/codec/support/StringDecoderTests.java

@ -48,8 +48,9 @@ public class StringDecoderTests extends AbstractAllocatingTestCase { @@ -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

22
spring-web-reactive/src/test/java/org/springframework/web/client/reactive/WebClientIntegrationTests.java

@ -130,6 +130,28 @@ public class WebClientIntegrationTests { @@ -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<String> result = this.webClient
.perform(get(baseUrl.toString())
.accept(MediaType.APPLICATION_JSON))
.extract(body(String.class));
TestSubscriber<String> 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 {

Loading…
Cancel
Save