|
| 1 | +package com.redjanvier.apigateway; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.ServerSocket; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.AfterAll; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest; |
| 12 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 13 | +import org.springframework.test.context.DynamicPropertyRegistry; |
| 14 | +import org.springframework.test.context.DynamicPropertySource; |
| 15 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 16 | + |
| 17 | +import okhttp3.mockwebserver.Dispatcher; |
| 18 | +import okhttp3.mockwebserver.MockResponse; |
| 19 | +import okhttp3.mockwebserver.MockWebServer; |
| 20 | +import okhttp3.mockwebserver.RecordedRequest; |
| 21 | +import redis.embedded.RedisServer; |
| 22 | + |
| 23 | +/** |
| 24 | + * End-to-end test of the gateway's two headline responsibilities: |
| 25 | + * <ol> |
| 26 | + * <li>routing a request to the correct downstream service, and</li> |
| 27 | + * <li>enforcing the Redis-backed rate limit (HTTP 429 once the bucket drains).</li> |
| 28 | + * </ol> |
| 29 | + * |
| 30 | + * <p>Uses a real, in-process Redis (embedded-redis, no Docker) and a stubbed |
| 31 | + * downstream (MockWebServer), so it exercises the actual {@code RequestRateLimiter} |
| 32 | + * Lua script and runs anywhere — locally and in CI. |
| 33 | + */ |
| 34 | +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) |
| 35 | +class RateLimitingIntegrationTest { |
| 36 | + |
| 37 | + static RedisServer redisServer; |
| 38 | + static MockWebServer downstream; |
| 39 | + |
| 40 | + @DynamicPropertySource |
| 41 | + static void properties(DynamicPropertyRegistry registry) throws IOException { |
| 42 | + int redisPort = findFreePort(); |
| 43 | + redisServer = new RedisServer(redisPort); |
| 44 | + redisServer.start(); |
| 45 | + |
| 46 | + downstream = new MockWebServer(); |
| 47 | + downstream.setDispatcher(new Dispatcher() { |
| 48 | + @Override |
| 49 | + public MockResponse dispatch(RecordedRequest request) { |
| 50 | + return new MockResponse().setResponseCode(200).setBody("ok"); |
| 51 | + } |
| 52 | + }); |
| 53 | + downstream.start(); |
| 54 | + |
| 55 | + String base = "http://" + downstream.getHostName() + ":" + downstream.getPort(); |
| 56 | + registry.add("spring.data.redis.host", () -> "localhost"); |
| 57 | + registry.add("spring.data.redis.port", () -> redisPort); |
| 58 | + registry.add("SMS_SERVICE_URI", () -> base); |
| 59 | + registry.add("EMAIL_SERVICE_URI", () -> base); |
| 60 | + } |
| 61 | + |
| 62 | + @AfterAll |
| 63 | + static void tearDown() throws IOException { |
| 64 | + if (downstream != null) { |
| 65 | + downstream.shutdown(); |
| 66 | + } |
| 67 | + if (redisServer != null) { |
| 68 | + redisServer.stop(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static int findFreePort() throws IOException { |
| 73 | + try (ServerSocket socket = new ServerSocket(0)) { |
| 74 | + return socket.getLocalPort(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Autowired |
| 79 | + private WebTestClient client; |
| 80 | + |
| 81 | + @Test |
| 82 | + void routesToDownstreamThenRateLimitsTheBurst() { |
| 83 | + // 1) Routing: the first request is proxied to the stub downstream. |
| 84 | + client.post().uri("/api/v1/notifications/sms") |
| 85 | + .exchange() |
| 86 | + .expectStatus().isOk() |
| 87 | + .expectBody(String.class).isEqualTo("ok"); |
| 88 | + |
| 89 | + // 2) Rate limiting: a rapid burst must be partly rejected with 429. |
| 90 | + int ok = 1; |
| 91 | + int limited = 0; |
| 92 | + for (int i = 0; i < 40; i++) { |
| 93 | + int status = client.post().uri("/api/v1/notifications/sms") |
| 94 | + .exchange() |
| 95 | + .returnResult(String.class) |
| 96 | + .getStatus() |
| 97 | + .value(); |
| 98 | + if (status == 429) { |
| 99 | + limited++; |
| 100 | + } else if (status == 200) { |
| 101 | + ok++; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + assertThat(limited) |
| 106 | + .as("the rate limiter must reject part of a 41-request burst") |
| 107 | + .isGreaterThan(0); |
| 108 | + assertThat(ok) |
| 109 | + .as("some requests should still pass through") |
| 110 | + .isGreaterThanOrEqualTo(1) |
| 111 | + .isLessThan(41); |
| 112 | + } |
| 113 | +} |
0 commit comments