提交 a5339d71 编写于 作者: R Rossen Stoyanchev

WebClient handles no Content-Type with data correctly

Issue: SPR-17482
上级 e4c84ec7
......@@ -224,16 +224,15 @@ public abstract class BodyExtractors {
Flux<T> result;
if (message.getHeaders().getContentType() == null) {
// Maybe it's okay, if there is no content..
result = message.getBody().map(o -> {
// Maybe it's okay there is no content type, if there is no content..
result = message.getBody().map(buffer -> {
DataBufferUtils.release(buffer);
throw ex;
});
}
else {
result = Flux.error(ex);
}
if (message instanceof ClientHttpResponse) {
result = consumeAndCancel(message).thenMany(result);
result = message instanceof ClientHttpResponse ?
consumeAndCancel(message).thenMany(Flux.error(ex)) : Flux.error(ex);
}
return result;
}
......
......@@ -16,6 +16,7 @@
package org.springframework.web.reactive.function.client;
import java.time.Duration;
import java.util.Map;
import java.util.function.Function;
import io.netty.buffer.ByteBufAllocator;
......@@ -28,12 +29,14 @@ import org.junit.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.http.client.reactive.ReactorResourceFactory;
import org.springframework.web.reactive.function.UnsupportedMediaTypeException;
import static org.junit.Assert.*;
......@@ -103,6 +106,21 @@ public class WebClientDataBufferAllocatingTests extends AbstractDataBufferAlloca
assertEquals(1, this.server.getRequestCount());
}
@Test // SPR-17482
public void bodyToMonoVoidWithoutContentType() {
this.server.enqueue(new MockResponse()
.setResponseCode(HttpStatus.ACCEPTED.value())
.setChunkedBody("{\"foo\" : \"123\", \"baz\" : \"456\", \"baz\" : \"456\"}", 5));
Mono<Map<String, String>> mono = this.webClient.get()
.uri("/sample").accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<Map<String, String>>() {});
StepVerifier.create(mono).expectError(UnsupportedMediaTypeException.class).verify(Duration.ofSeconds(3));
assertEquals(1, this.server.getRequestCount());
}
@Test
public void onStatusWithBodyNotConsumed() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册