From 15f9e41fc87c2cc44058af0b2822d96baf599c35 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Thu, 21 Nov 2019 20:30:51 +0100 Subject: [PATCH] WIP: WebClient --- ...ExchangeFilterFunctionIntegrationTest.java | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java b/src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java index 4e07259..306f044 100644 --- a/src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java +++ b/src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java @@ -3,7 +3,6 @@ package de.juplo.facebook.errors; import de.juplo.facebook.errors.GraphApiException.Type; import java.time.Duration; -import java.util.List; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import org.junit.After; @@ -14,6 +13,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.client.reactive.JettyClientHttpConnector; +import org.springframework.web.reactive.function.client.ExchangeFilterFunction; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -94,19 +94,6 @@ public class GraphApiExchangeFilterFunctionIntegrationTest @Test public void testTest() { - WebClient webClient = - WebClient - .builder() - .clientConnector(new JettyClientHttpConnector()) - .baseUrl(server.url("/").toString()) - .filter((request, next) -> next.exchange(request).flatMap(response -> - { - List headerValues = response.headers().header("Foo"); - return headerValues.isEmpty() ? Mono.error(GraphApiException.create(response).block()) : - Mono.just(response); - })) - .build(); - server .enqueue(new MockResponse() .setResponseCode(400) @@ -135,6 +122,7 @@ public class GraphApiExchangeFilterFunctionIntegrationTest result = webClient.get() .uri("/greeting?name=Spring") .retrieve() + .onStatus(status -> status.is4xxClientError(), response -> GraphApiException.create(response)) .bodyToMono(String.class); StepVerifier.create(result) @@ -142,4 +130,24 @@ public class GraphApiExchangeFilterFunctionIntegrationTest .expectComplete() .verify(Duration.ofSeconds(3)); } + + public static ExchangeFilterFunction errorHandlingFilter() + { + return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> + { + if (clientResponse.statusCode() != null && (clientResponse.statusCode() + .is5xxServerError() || clientResponse.statusCode().is4xxClientError())) + { + return clientResponse.bodyToMono(String.class) + .flatMap(errorBody -> + { + return Mono.error(new Exception(errorBody)); + }); + } + else + { + return Mono.just(clientResponse); + } + }); + } } -- 2.20.1