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
This commit is contained in:
Brian Clozel
2016-04-08 22:18:46 +02:00
parent da2276a954
commit a3b371bf4b
3 changed files with 26 additions and 2 deletions

View File

@@ -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> {
* 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;
}

View File

@@ -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

View File

@@ -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 {