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;
/**
*/
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());
}
}