|
| 1 | +package com.baeldung.spring.cloud.zuulratelimitdemo.controller; |
| 2 | + |
| 3 | +import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_LIMIT; |
| 4 | +import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_QUOTA; |
| 5 | +import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_REMAINING; |
| 6 | +import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_REMAINING_QUOTA; |
| 7 | +import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_RESET; |
| 8 | +import static org.junit.Assert.assertEquals; |
| 9 | +import static org.junit.Assert.assertNotEquals; |
| 10 | +import static org.junit.Assert.assertNotNull; |
| 11 | +import static org.junit.Assert.assertNull; |
| 12 | +import static org.springframework.http.HttpStatus.OK; |
| 13 | +import static org.springframework.http.HttpStatus.TOO_MANY_REQUESTS; |
| 14 | + |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | +import org.junit.Test; |
| 17 | +import org.junit.runner.RunWith; |
| 18 | +import org.springframework.beans.factory.annotation.Autowired; |
| 19 | +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; |
| 20 | +import org.springframework.boot.test.context.SpringBootTest; |
| 21 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 22 | +import org.springframework.http.HttpHeaders; |
| 23 | +import org.springframework.http.ResponseEntity; |
| 24 | +import org.springframework.test.context.junit4.SpringRunner; |
| 25 | + |
| 26 | +@AutoConfigureTestDatabase |
| 27 | +@RunWith(SpringRunner.class) |
| 28 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 29 | +public class GreetingControllerTest { |
| 30 | + |
| 31 | + private static final String SIMPLE_GREETING = "/greeting/simple"; |
| 32 | + private static final String ADVANCED_GREETING = "/greeting/advanced"; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private TestRestTemplate restTemplate; |
| 36 | + |
| 37 | + @Test |
| 38 | + public void whenRequestNotExceedingCapacity_thenReturnOkResponse() { |
| 39 | + ResponseEntity<String> response = this.restTemplate.getForEntity(SIMPLE_GREETING, String.class); |
| 40 | + HttpHeaders headers = response.getHeaders(); |
| 41 | + String key = "rate-limit-application_serviceSimple_127.0.0.1"; |
| 42 | + |
| 43 | + String limit = headers.getFirst(HEADER_LIMIT + key); |
| 44 | + String remaining = headers.getFirst(HEADER_REMAINING + key); |
| 45 | + String reset = headers.getFirst(HEADER_RESET + key); |
| 46 | + |
| 47 | + assertEquals(limit, "5"); |
| 48 | + assertEquals(remaining, "4"); |
| 49 | + assertEquals(reset, "60000"); |
| 50 | + |
| 51 | + assertEquals(OK, response.getStatusCode()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void whenRequestExceedingCapacity_thenReturnTooManyRequestsResponse() throws InterruptedException { |
| 56 | + ResponseEntity<String> response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class); |
| 57 | + HttpHeaders headers = response.getHeaders(); |
| 58 | + String key = "rate-limit-application_serviceAdvanced_127.0.0.1"; |
| 59 | + assertHeaders(headers, key, false, false); |
| 60 | + assertEquals(OK, response.getStatusCode()); |
| 61 | + |
| 62 | + for (int i = 0; i < 2; i++) { |
| 63 | + response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class); |
| 64 | + } |
| 65 | + |
| 66 | + headers = response.getHeaders(); |
| 67 | + String limit = headers.getFirst(HEADER_LIMIT + key); |
| 68 | + String remaining = headers.getFirst(HEADER_REMAINING + key); |
| 69 | + String reset = headers.getFirst(HEADER_RESET + key); |
| 70 | + |
| 71 | + assertEquals(limit, "1"); |
| 72 | + assertEquals(remaining, "0"); |
| 73 | + assertNotEquals(reset, "2000"); |
| 74 | + |
| 75 | + assertEquals(TOO_MANY_REQUESTS, response.getStatusCode()); |
| 76 | + |
| 77 | + TimeUnit.SECONDS.sleep(2); |
| 78 | + |
| 79 | + response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class); |
| 80 | + headers = response.getHeaders(); |
| 81 | + assertHeaders(headers, key, false, false); |
| 82 | + assertEquals(OK, response.getStatusCode()); |
| 83 | + } |
| 84 | + |
| 85 | + private void assertHeaders(HttpHeaders headers, String key, boolean nullable, boolean quotaHeaders) { |
| 86 | + String quota = headers.getFirst(HEADER_QUOTA + key); |
| 87 | + String remainingQuota = headers.getFirst(HEADER_REMAINING_QUOTA + key); |
| 88 | + String limit = headers.getFirst(HEADER_LIMIT + key); |
| 89 | + String remaining = headers.getFirst(HEADER_REMAINING + key); |
| 90 | + String reset = headers.getFirst(HEADER_RESET + key); |
| 91 | + |
| 92 | + if (nullable) { |
| 93 | + if (quotaHeaders) { |
| 94 | + assertNull(quota); |
| 95 | + assertNull(remainingQuota); |
| 96 | + } else { |
| 97 | + assertNull(limit); |
| 98 | + assertNull(remaining); |
| 99 | + } |
| 100 | + assertNull(reset); |
| 101 | + } else { |
| 102 | + if (quotaHeaders) { |
| 103 | + assertNotNull(quota); |
| 104 | + assertNotNull(remainingQuota); |
| 105 | + } else { |
| 106 | + assertNotNull(limit); |
| 107 | + assertNotNull(remaining); |
| 108 | + } |
| 109 | + assertNotNull(reset); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments