X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Ffacebook%2Ferrors%2FGraphApiExchangeFilterFunctionIntegrationTest.java;h=53b2e6a31628957662ae6b7a9a296c07c10fe399;hb=2ee955aa31aac3dff18457f032fce4ed15049cd2;hp=d17c7f7760fef086893a0eded85a59bdbfff2974;hpb=b4a025a1af059d5fb844dc1f0c3d4c9ae76ac17b;p=facebook-errors diff --git a/src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java b/src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java index d17c7f7..53b2e6a 100644 --- a/src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java +++ b/src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java @@ -1,17 +1,23 @@ package de.juplo.facebook.errors; -import java.time.Duration; -import java.util.List; +import de.juplo.facebook.errors.GraphApiException.Type; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import org.junit.After; -import org.junit.Assert; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; 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.http.HttpStatus; +import org.springframework.http.client.reactive.JettyClientHttpConnector; import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.reactive.function.client.WebClientResponseException.BadRequest; +import org.springframework.web.reactive.function.client.WebClientResponseException.InternalServerError; import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; /** @@ -20,69 +26,246 @@ 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() + .clientConnector(new JettyClientHttpConnector()) + .baseUrl(server.url("/").toString()) + .filter(new GraphApiExchangeFilterFunction()) + .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 testNoError() { + Mono result; - 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!") + + //server + // .enqueue(new MockResponse() + // .setResponseCode(HttpStatus.CONTINUE.value()) + // .setHeader("Content-Type", "application/json") + // .setBody("Hallo Welt!")); + // + //result = + // webClient + // .get() + // .uri("/egal") + // .retrieve() + // .bodyToMono(String.class); + // + //StepVerifier + // .create(result) + // .expectNext("Hallo Welt!") + // .expectComplete() + // .verify(); + + + server + .enqueue(new MockResponse() + .setResponseCode(HttpStatus.OK.value()) + .setHeader("Content-Type", "text/plain") + .setBody("Hallo Welt!")); + + result = + webClient + .get() + .uri("/egal") + .retrieve() + .bodyToMono(String.class); + + StepVerifier + .create(result) + .expectNext("Hallo Welt!") + .expectComplete() + .verify(); + + + server + .enqueue(new MockResponse() + .setResponseCode(HttpStatus.TEMPORARY_REDIRECT.value()) + .setHeader("Location", server.url("/woanders")) + .setHeader("Content-Type", "text/plain") + .setBody("Jetzt doch woanders...")); + server + .enqueue(new MockResponse() + .setResponseCode(HttpStatus.OK.value()) + .setHeader("Content-Type", "text/plain") + .setBody("Hallo Welt!")); + + result = + webClient + .get() + .uri("/egal") + .retrieve() + .bodyToMono(String.class); + + StepVerifier + .create(result) + .expectNext("Hallo Welt!") .expectComplete() - .verify(Duration.ofSeconds(3)); + .verify(); + + + server + .enqueue(new MockResponse() + .setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.value()) + .setHeader("Content-Type", "text/plain") + .setBody("Hallo Welt!")); + + result = + webClient + .get() + .uri("/egal") + .retrieve() + .bodyToMono(String.class); + + StepVerifier + .create(result) + .expectError(InternalServerError.class) + .verify(); + } + + @Test + public void testValidError() + { + server + .enqueue(new MockResponse() + .setResponseCode(HttpStatus.BAD_REQUEST.value()) + .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(); + } + + @Test + public void testUnmappedError() + { + server + .enqueue(new MockResponse() + .setResponseCode(HttpStatus.BAD_REQUEST.value()) + .setHeader("Content-Type", "application/json") + .setBody( + "{\n" + + " \"error\":\n" + + " {\n" + + " \"message\": \"This error does not exist.\",\n" + + " \"type\": \"NonexistentTypeException\",\n" + + " \"code\": 999999999\n" + + " }\n" + + "}")); + + + Mono result = + webClient + .get() + .uri("/egal") + .retrieve() + .bodyToMono(String.class); + + StepVerifier.create(result).expectErrorSatisfies(throwable -> + { + assertEquals(UnmappedErrorException.class, throwable.getClass()); + UnmappedErrorException e = (UnmappedErrorException)throwable; + LOG.debug("{}", e.toString()); + assertEquals(new Integer(999999999), e.getCode()); + assertEquals("This error does not exist.", e.getMessage()); + try + { + Type type = e.getType(); + LOG.error("unknown type: {}", type); + fail("unmapped type was resolved by enum: " + type); + } + catch (IllegalArgumentException ee) {} + }) + .verify(); + } + + @Test + public void testInvlalidError() + { + Mono result; + + + server + .enqueue(new MockResponse() + .setResponseCode(HttpStatus.BAD_REQUEST.value()) + .setHeader("Content-Type", "application/json") + .setBody( + "{\n" + + " \"error\":\n" + + " {\n" + + " \"message\": \"Not a Graph-Api-Exception.\",\n" + + " \"type\": \"Whatever\",\n" + + " \"code\": \"some string\"\n" + + " }\n" + + "}")); + + result = + webClient + .get() + .uri("/egal") + .retrieve() + .bodyToMono(String.class); + + StepVerifier.create(result).expectError(BadRequest.class).verify(); + + + server + .enqueue(new MockResponse() + .setResponseCode(HttpStatus.BAD_REQUEST.value()) + .setHeader("Content-Type", "text/plain") + .setBody("Hallo Welt!")); + + result = + webClient + .get() + .uri("/egal") + .retrieve() + .bodyToMono(String.class); - Assert.assertEquals(2, server.getRequestCount()); + StepVerifier.create(result).expectError(BadRequest.class).verify(); } }