Browse Source

WebClient handles no Content-Type with data correctly

Issue: SPR-17482
pull/2014/head
Rossen Stoyanchev 6 years ago
parent
commit
a5339d71ea
  1. 11
      spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyExtractors.java
  2. 18
      spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientDataBufferAllocatingTests.java

11
spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyExtractors.java

@ -224,16 +224,15 @@ public abstract class BodyExtractors {
Flux<T> result; Flux<T> result;
if (message.getHeaders().getContentType() == null) { if (message.getHeaders().getContentType() == null) {
// Maybe it's okay, if there is no content.. // Maybe it's okay there is no content type, if there is no content..
result = message.getBody().map(o -> { result = message.getBody().map(buffer -> {
DataBufferUtils.release(buffer);
throw ex; throw ex;
}); });
} }
else { else {
result = Flux.error(ex); result = message instanceof ClientHttpResponse ?
} consumeAndCancel(message).thenMany(Flux.error(ex)) : Flux.error(ex);
if (message instanceof ClientHttpResponse) {
result = consumeAndCancel(message).thenMany(result);
} }
return result; return result;
} }

18
spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientDataBufferAllocatingTests.java

@ -16,6 +16,7 @@
package org.springframework.web.reactive.function.client; package org.springframework.web.reactive.function.client;
import java.time.Duration; import java.time.Duration;
import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufAllocator;
@ -28,12 +29,14 @@ import org.junit.Test;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.test.StepVerifier; import reactor.test.StepVerifier;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase; import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
import org.springframework.core.io.buffer.NettyDataBufferFactory; import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.http.client.reactive.ReactorResourceFactory; import org.springframework.http.client.reactive.ReactorResourceFactory;
import org.springframework.web.reactive.function.UnsupportedMediaTypeException;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -103,6 +106,21 @@ public class WebClientDataBufferAllocatingTests extends AbstractDataBufferAlloca
assertEquals(1, this.server.getRequestCount()); 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 @Test
public void onStatusWithBodyNotConsumed() { public void onStatusWithBodyNotConsumed() {

Loading…
Cancel
Save