From b4a025a1af059d5fb844dc1f0c3d4c9ae76ac17b Mon Sep 17 00:00:00 2001
From: Kai Moritz <kai@jupl.de>
Date: Thu, 21 Nov 2019 09:27:20 +0100
Subject: [PATCH] WIP: WebClient

---
 pom.xml                                       |  5 ++
 ...ExchangeFilterFunctionIntegrationTest.java | 88 +++++++++++++++++++
 2 files changed, 93 insertions(+)
 create mode 100644 src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java

diff --git a/pom.xml b/pom.xml
index 9bc175d..f67dcfc 100644
--- a/pom.xml
+++ b/pom.xml
@@ -106,6 +106,11 @@
       <artifactId>spring-boot-configuration-processor</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>com.squareup.okhttp3</groupId>
+      <artifactId>mockwebserver</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
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<String> 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<String> 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());
+  }
+}
-- 
2.20.1