Revisit meaning of response.close() in HTTP client
Prior to this issue, SPR-15920 added this new `close()` method which was supposed to be called to clean resources after response processing. This commit changes the meaning of that method: calling `close()` will close the underlying HTTP connection. This has to be called if the response body is not consumed by the application, since at that point the underlying connection might be in an inconsistent state if shared in a connection pool. Issue: SPR-15993
This commit is contained in:
@@ -2,19 +2,15 @@ package org.springframework.web.reactive.function.client;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.reactive.ClientHttpConnector;
|
||||
import org.springframework.mock.http.client.reactive.test.MockClientHttpResponse;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -44,7 +40,7 @@ public class WebClientMockTests {
|
||||
|
||||
@Test
|
||||
public void shouldDisposeResponseManually() {
|
||||
Mono<HttpHeaders> headers= this.webClient
|
||||
Mono<HttpHeaders> headers = this.webClient
|
||||
.get().uri("/test")
|
||||
.exchange()
|
||||
.map(response -> response.headers().asHttpHeaders());
|
||||
@@ -54,63 +50,4 @@ public class WebClientMockTests {
|
||||
assertFalse(this.response.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDisposeResponseExchangeMono() {
|
||||
Mono<String> body = this.webClient
|
||||
.get().uri("/test")
|
||||
.exchange()
|
||||
.flatMap(response -> response.bodyToMono(String.class));
|
||||
StepVerifier.create(body)
|
||||
.expectNext("example")
|
||||
.verifyComplete();
|
||||
assertTrue(this.response.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDisposeResponseExchangeFlux() {
|
||||
Flux<String> body = this.webClient
|
||||
.get().uri("/test")
|
||||
.exchange()
|
||||
.flatMapMany(response -> response.bodyToFlux(String.class));
|
||||
StepVerifier.create(body)
|
||||
.expectNext("example")
|
||||
.verifyComplete();
|
||||
assertTrue(this.response.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDisposeResponseExchangeEntity() {
|
||||
ResponseEntity<String> entity = this.webClient
|
||||
.get().uri("/test")
|
||||
.exchange()
|
||||
.flatMap(response -> response.toEntity(String.class))
|
||||
.block();
|
||||
assertEquals("example", entity.getBody());
|
||||
assertTrue(this.response.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDisposeResponseRetrieveMono() {
|
||||
Mono<String> body = this.webClient
|
||||
.get().uri("/test")
|
||||
.retrieve()
|
||||
.bodyToMono(String.class);
|
||||
StepVerifier.create(body)
|
||||
.expectNext("example")
|
||||
.verifyComplete();
|
||||
assertTrue(this.response.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDisposeResponseRetrieveFlux() {
|
||||
Flux<String> body = this.webClient
|
||||
.get().uri("/test")
|
||||
.retrieve()
|
||||
.bodyToFlux(String.class);
|
||||
StepVerifier.create(body)
|
||||
.expectNext("example")
|
||||
.verifyComplete();
|
||||
assertTrue(this.response.isClosed());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user