WIP: WebClient
authorKai Moritz <kai@jupl.de>
Thu, 21 Nov 2019 08:28:04 +0000 (09:28 +0100)
committerKai Moritz <kai@jupl.de>
Thu, 21 Nov 2019 08:28:04 +0000 (09:28 +0100)
pom.xml
src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java

diff --git a/pom.xml b/pom.xml
index f67dcfc..2e09f82 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <artifactId>mockwebserver</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>io.projectreactor</groupId>
+      <artifactId>reactor-test</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
index d17c7f7..640fa6a 100644 (file)
@@ -1,17 +1,19 @@
 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;
-import org.junit.Assert;
+import static org.junit.Assert.assertEquals;
 import org.junit.Before;
 import org.junit.Test;
-import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.web.reactive.function.client.WebClient;
 import reactor.core.publisher.Mono;
+import reactor.test.StepVerifier;
 
 
 /**
@@ -20,69 +22,71 @@ import reactor.core.publisher.Mono;
  */
 public class GraphApiExchangeFilterFunctionIntegrationTest
 {
-       private MockWebServer server;
+  private static final Logger LOG =
+      LoggerFactory.getLogger(GraphApiExchangeFilterFunctionIntegrationTest.class);
 
+  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);
+       public void setup()
+  {
+    server = new MockWebServer();
+    webClient =
+        WebClient
+            .builder()
+            .baseUrl(server.url("/").toString())
+            .filter(GraphApiExchangeFilterFunction.INSTANCE)
+            .build();
        }
 
        @After
-       public void shutdown() throws Exception {
-               this.server.shutdown();
+       public void shutdown() throws Exception
+  {
+    this.server.shutdown();
        }
 
 
   @Test
-  public void errorHandlingFilter() throws Exception
+  public void testValidError()
   {
-
-    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()
+    LOG.info("testValidError");
+
+
+    server
+        .enqueue(new MockResponse()
+            .setStatus("400")
+            .setHeader("Content-Type", "application/json")
+            .setBody(
+                "{\n" +
+                "  \"error\":\n" +
+                "  {\n" +
+                "    \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
+                "    \"type\": \"OAuthException\",\n" +
+                "    \"code\": 613\n" +
+                "  }\n" +
+                "}"));
+
+
+    Mono<String> result =
+        webClient
+            .get()
+            .uri("/egal")
+            .retrieve()
+            .bodyToMono(String.class);
+
+    StepVerifier
+        .create(result)
+        .expectErrorSatisfies(throwable ->
+        {
+          assertEquals(RateLimitExceededException.class, throwable.getClass());
+          RateLimitExceededException e = (RateLimitExceededException)throwable;
+          LOG.debug("{}", e.toString());
+          assertEquals(new Integer(613), e.getCode());
+          assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
+          assertEquals(Type.OAuthException, e.getType());
+        })
         .verify(Duration.ofSeconds(3));
-
-    Assert.assertEquals(2, server.getRequestCount());
   }
 }