WIP: WebClient
authorKai Moritz <kai@jupl.de>
Thu, 21 Nov 2019 08:27:20 +0000 (09:27 +0100)
committerKai Moritz <kai@jupl.de>
Thu, 21 Nov 2019 08:27:20 +0000 (09:27 +0100)
pom.xml
src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 9bc175d..f67dcfc 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <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 (file)
index 0000000..d17c7f7
--- /dev/null
@@ -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());
+  }
+}