Add status-based error filter
This commit introduces an ExchangeFilterFunction that throws an exception given a HTTP status predicate. Issue: SPR-15724
This commit is contained in:
@@ -20,8 +20,10 @@ import java.net.URI;
|
||||
|
||||
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.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
@@ -133,4 +135,46 @@ public class ExchangeFilterFunctionsTests {
|
||||
assertEquals(response, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void statusHandlerMatch() throws Exception {
|
||||
ClientRequest request = ClientRequest.method(GET, URI.create("http://example.com")).build();
|
||||
ClientResponse response = mock(ClientResponse.class);
|
||||
when(response.statusCode()).thenReturn(HttpStatus.NOT_FOUND);
|
||||
|
||||
ExchangeFunction exchange = r -> Mono.just(response);
|
||||
|
||||
ExchangeFilterFunction errorHandler = ExchangeFilterFunctions.statusError(
|
||||
HttpStatus::is4xxClientError, r -> new MyException());
|
||||
|
||||
Mono<ClientResponse> result = errorHandler.filter(request, exchange);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectError(MyException.class)
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void statusHandlerNoMatch() throws Exception {
|
||||
ClientRequest request = ClientRequest.method(GET, URI.create("http://example.com")).build();
|
||||
ClientResponse response = mock(ClientResponse.class);
|
||||
when(response.statusCode()).thenReturn(HttpStatus.NOT_FOUND);
|
||||
|
||||
ExchangeFunction exchange = r -> Mono.just(response);
|
||||
|
||||
ExchangeFilterFunction errorHandler = ExchangeFilterFunctions.statusError(
|
||||
HttpStatus::is5xxServerError, r -> new MyException());
|
||||
|
||||
Mono<ClientResponse> result = errorHandler.filter(request, exchange);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext(response)
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class MyException extends Exception {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user