Add status code check to bodyTo[Mono|Flux]
- Add 4xx/5xx status code check to ClientResponse.bodyToMono and bodyToFlux. - Removed WebClient.retrieveMono and retrieveFlux. Issue: SPR-14977
This commit is contained in:
@@ -29,6 +29,7 @@ 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.core.codec.StringDecoder;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
@@ -125,6 +126,7 @@ public class DefaultClientResponseTests {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
|
||||
when(mockResponse.getHeaders()).thenReturn(httpHeaders);
|
||||
when(mockResponse.getStatusCode()).thenReturn(HttpStatus.OK);
|
||||
when(mockResponse.getBody()).thenReturn(body);
|
||||
|
||||
Set<HttpMessageReader<?>> messageReaders = Collections
|
||||
@@ -135,6 +137,24 @@ public class DefaultClientResponseTests {
|
||||
assertEquals("foo", resultMono.block());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bodyToMonoError() throws Exception {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
|
||||
when(mockResponse.getHeaders()).thenReturn(httpHeaders);
|
||||
when(mockResponse.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND);
|
||||
|
||||
Set<HttpMessageReader<?>> messageReaders = Collections
|
||||
.singleton(new DecoderHttpMessageReader<String>(new StringDecoder()));
|
||||
when(mockWebClientStrategies.messageReaders()).thenReturn(messageReaders::stream);
|
||||
|
||||
Mono<String> resultMono = defaultClientResponse.bodyToMono(String.class);
|
||||
|
||||
StepVerifier.create(resultMono)
|
||||
.expectError(WebClientException.class)
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bodyToFlux() throws Exception {
|
||||
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
||||
@@ -145,6 +165,7 @@ public class DefaultClientResponseTests {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
|
||||
when(mockResponse.getHeaders()).thenReturn(httpHeaders);
|
||||
when(mockResponse.getStatusCode()).thenReturn(HttpStatus.OK);
|
||||
when(mockResponse.getBody()).thenReturn(body);
|
||||
|
||||
Set<HttpMessageReader<?>> messageReaders = Collections
|
||||
@@ -156,5 +177,22 @@ public class DefaultClientResponseTests {
|
||||
assertEquals(Collections.singletonList("foo"), result.block());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bodyToFluxError() throws Exception {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
|
||||
when(mockResponse.getHeaders()).thenReturn(httpHeaders);
|
||||
when(mockResponse.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
|
||||
Set<HttpMessageReader<?>> messageReaders = Collections
|
||||
.singleton(new DecoderHttpMessageReader<String>(new StringDecoder()));
|
||||
when(mockWebClientStrategies.messageReaders()).thenReturn(messageReaders::stream);
|
||||
|
||||
Flux<String> resultFlux = defaultClientResponse.bodyToFlux(String.class);
|
||||
StepVerifier.create(resultFlux)
|
||||
.expectError(WebClientException.class)
|
||||
.verify();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -16,20 +16,15 @@
|
||||
|
||||
package org.springframework.web.client.reactive;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -85,75 +80,6 @@ public class ExchangeFilterFunctionsTests {
|
||||
assertTrue(filterInvoked[0]);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void clientNoError() throws Exception {
|
||||
ClientRequest<Void> request = ClientRequest.GET("http://example.com").build();
|
||||
ClientResponse response = mock(ClientResponse.class);
|
||||
when(response.statusCode()).thenReturn(HttpStatus.OK);
|
||||
ExchangeFunction exchange = r -> Mono.just(response);
|
||||
|
||||
ExchangeFilterFunction standardErrors = ExchangeFilterFunctions.clientError();
|
||||
|
||||
Mono<ClientResponse> result = standardErrors.filter(request, exchange);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext(response)
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serverError() throws Exception {
|
||||
ClientRequest<Void> request = ClientRequest.GET("http://example.com").build();
|
||||
ClientResponse response = mock(ClientResponse.class);
|
||||
when(response.statusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
ExchangeFunction exchange = r -> Mono.just(response);
|
||||
|
||||
ExchangeFilterFunction standardErrors = ExchangeFilterFunctions.serverError();
|
||||
|
||||
Mono<ClientResponse> result = standardErrors.filter(request, exchange);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectError(WebClientException.class)
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void errorPredicate() throws Exception {
|
||||
ClientRequest<Void> request = ClientRequest.GET("http://example.com").build();
|
||||
ClientResponse response = mock(ClientResponse.class);
|
||||
when(response.statusCode()).thenReturn(HttpStatus.NOT_FOUND);
|
||||
ExchangeFunction exchange = r -> Mono.just(response);
|
||||
|
||||
ExchangeFilterFunction errorPredicate = ExchangeFilterFunctions
|
||||
.errorPredicate(clientResponse -> clientResponse.statusCode().is4xxClientError());
|
||||
|
||||
Mono<ClientResponse> result = errorPredicate.filter(request, exchange);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectError(WebClientException.class)
|
||||
.verify();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void errorMapperFunction() throws Exception {
|
||||
ClientRequest<Void> request = ClientRequest.GET("http://example.com").build();
|
||||
ClientResponse response = mock(ClientResponse.class);
|
||||
ExchangeFunction exchange = r -> Mono.just(response);
|
||||
|
||||
ExchangeFilterFunction errorMapper = ExchangeFilterFunctions
|
||||
.errorMapper(clientResponse -> Optional.of(new IllegalStateException()));
|
||||
|
||||
Mono<ClientResponse> result = errorMapper.filter(request, exchange);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectError(IllegalStateException.class)
|
||||
.verify();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void basicAuthentication() throws Exception {
|
||||
ClientRequest<Void> request = ClientRequest.GET("http://example.com").build();
|
||||
|
||||
@@ -110,48 +110,6 @@ public class WebClientIntegrationTests {
|
||||
assertEquals("/greeting?name=Spring", recordedRequest.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrieveMono() throws Exception {
|
||||
HttpUrl baseUrl = server.url("/greeting?name=Spring");
|
||||
this.server.enqueue(new MockResponse().setBody("Hello Spring!"));
|
||||
|
||||
ClientRequest<Void> request = ClientRequest.GET(baseUrl.toString()).build();
|
||||
|
||||
Mono<String> result = this.webClient
|
||||
.retrieveMono(request, String.class);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext("Hello Spring!")
|
||||
.expectComplete()
|
||||
.verify();
|
||||
|
||||
RecordedRequest recordedRequest = server.takeRequest();
|
||||
assertEquals(1, server.getRequestCount());
|
||||
assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT));
|
||||
assertEquals("/greeting?name=Spring", recordedRequest.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrieveFlux() throws Exception {
|
||||
HttpUrl baseUrl = server.url("/greeting?name=Spring");
|
||||
this.server.enqueue(new MockResponse().setBody("Hello Spring!"));
|
||||
|
||||
ClientRequest<Void> request = ClientRequest.GET(baseUrl.toString()).build();
|
||||
|
||||
Flux<String> result = this.webClient
|
||||
.retrieveFlux(request, String.class);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext("Hello Spring!")
|
||||
.expectComplete()
|
||||
.verify();
|
||||
|
||||
RecordedRequest recordedRequest = server.takeRequest();
|
||||
assertEquals(1, server.getRequestCount());
|
||||
assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT));
|
||||
assertEquals("/greeting?name=Spring", recordedRequest.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonString() throws Exception {
|
||||
HttpUrl baseUrl = server.url("/json");
|
||||
@@ -309,48 +267,6 @@ public class WebClientIntegrationTests {
|
||||
assertEquals("/greeting?name=Spring", recordedRequest.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrieveNotFound() throws Exception {
|
||||
HttpUrl baseUrl = server.url("/greeting?name=Spring");
|
||||
this.server.enqueue(new MockResponse().setResponseCode(404)
|
||||
.setHeader("Content-Type", "text/plain").setBody("Not Found"));
|
||||
|
||||
ClientRequest<Void> request = ClientRequest.GET(baseUrl.toString()).build();
|
||||
|
||||
Mono<String> result = this.webClient
|
||||
.retrieveMono(request, String.class);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectError(WebClientException.class)
|
||||
.verify(Duration.ofSeconds(3));
|
||||
|
||||
RecordedRequest recordedRequest = server.takeRequest();
|
||||
assertEquals(1, server.getRequestCount());
|
||||
assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT));
|
||||
assertEquals("/greeting?name=Spring", recordedRequest.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrieveServerError() throws Exception {
|
||||
HttpUrl baseUrl = server.url("/greeting?name=Spring");
|
||||
this.server.enqueue(new MockResponse().setResponseCode(500)
|
||||
.setHeader("Content-Type", "text/plain").setBody("Not Found"));
|
||||
|
||||
ClientRequest<Void> request = ClientRequest.GET(baseUrl.toString()).build();
|
||||
|
||||
Mono<String> result = this.webClient
|
||||
.retrieveMono(request, String.class);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectError(WebClientException.class)
|
||||
.verify(Duration.ofSeconds(3));
|
||||
|
||||
RecordedRequest recordedRequest = server.takeRequest();
|
||||
assertEquals(1, server.getRequestCount());
|
||||
assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT));
|
||||
assertEquals("/greeting?name=Spring", recordedRequest.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filter() throws Exception {
|
||||
HttpUrl baseUrl = server.url("/greeting?name=Spring");
|
||||
|
||||
Reference in New Issue
Block a user