From: Kai Moritz Date: Thu, 21 Nov 2019 08:27:20 +0000 (+0100) Subject: WIP: WebClient X-Git-Tag: wip-webclient~36 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=b4a025a1af059d5fb844dc1f0c3d4c9ae76ac17b;p=facebook-errors WIP: WebClient --- diff --git a/pom.xml b/pom.xml index 9bc175d..f67dcfc 100644 --- a/pom.xml +++ b/pom.xml @@ -106,6 +106,11 @@ spring-boot-configuration-processor test + + com.squareup.okhttp3 + mockwebserver + test + javax.servlet javax.servlet-api diff --git a/src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java b/src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java new file mode 100644 index 0000000..d17c7f7 --- /dev/null +++ b/src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java @@ -0,0 +1,88 @@ +package de.juplo.facebook.errors; + + +import java.time.Duration; +import java.util.List; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.web.reactive.function.client.ExchangeFilterFunction; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; + + +/** + * + * @author Kai Moritz + */ +public class GraphApiExchangeFilterFunctionIntegrationTest +{ + private MockWebServer server; + + private WebClient webClient; + + + @Before + public void setup() { + this.server = new MockWebServer(); + String baseUrl = this.server.url("/").toString(); + this.webClient = WebClient.create(baseUrl); + } + + @After + public void shutdown() throws Exception { + this.server.shutdown(); + } + + + @Test + public void errorHandlingFilter() throws Exception + { + + ExchangeFilterFunction filter = ExchangeFilterFunction.ofResponseProcessor( + clientResponse -> + { + List headerValues = clientResponse.headers().header("Foo"); + return headerValues.isEmpty() ? Mono.error( + new MyException("Response does not contain Foo header")) : Mono.just( + clientResponse); + } + ); + + WebClient filteredClient = this.webClient.filter(filter); + + // header not present + this.server.enqueue(new MockResponse().setHeader("Content-Type", + "text/plain").setBody("Hello Spring!")); + + Mono result = filteredClient.get() + .uri("/greeting?name=Spring") + .retrieve() + .bodyToMono(String.class); + + StepVerifier.create(result) + .expectError(MyException.class) + .verify(Duration.ofSeconds(3)); + + // header present + this.server.enqueue(new MockResponse().setHeader("Content-Type", + "text/plain") + .setHeader("Foo", "Bar") + .setBody("Hello Spring!")); + + result = filteredClient.get() + .uri("/greeting?name=Spring") + .retrieve() + .bodyToMono(String.class); + + StepVerifier.create(result) + .expectNext("Hello Spring!") + .expectComplete() + .verify(Duration.ofSeconds(3)); + + Assert.assertEquals(2, server.getRequestCount()); + } +}