From: Kai Moritz Date: Thu, 21 Nov 2019 08:28:04 +0000 (+0100) Subject: WIP: WebClient X-Git-Tag: wip-webclient~35 X-Git-Url: https://juplo.de/gitweb/?p=facebook-errors;a=commitdiff_plain;h=fd44e482ceb2606db17fc243c9cd4ed59269ddc9 WIP: WebClient --- diff --git a/pom.xml b/pom.xml index f67dcfc..2e09f82 100644 --- a/pom.xml +++ b/pom.xml @@ -111,6 +111,11 @@ mockwebserver test + + io.projectreactor + reactor-test + 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 index d17c7f7..640fa6a 100644 --- a/src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java +++ b/src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java @@ -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 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() + 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 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()); } }