Download src/test/java/com/anthony/failsafeapi/service/IpRateLimiterServiceTest.java from tonyy1/tickets: direct link, hf CLI and curl.
- Browser
- Download file 4.69 kB
-
https://huggingface.co/spaces/tonyy1/tickets/resolve/main/src/test/java/com/anthony/failsafeapi/service/IpRateLimiterServiceTest.java
- Command line
-
hf download hf://spaces/tonyy1/tickets/src/test/java/com/anthony/failsafeapi/service/IpRateLimiterServiceTest.java
-
curl -L -o IpRateLimiterServiceTest.java https://huggingface.co/spaces/tonyy1/tickets/resolve/main/src/test/java/com/anthony/failsafeapi/service/IpRateLimiterServiceTest.java
4.69 kB
| package com.anthony.failsafeapi.service; | |
| import com.anthony.failsafeapi.exception.RateLimitExceededException; | |
| import org.junit.jupiter.api.BeforeEach; | |
| import org.junit.jupiter.api.DisplayName; | |
| import org.junit.jupiter.api.Nested; | |
| import org.junit.jupiter.api.Test; | |
| import org.junit.jupiter.api.extension.ExtendWith; | |
| import org.junit.jupiter.params.ParameterizedTest; | |
| import org.junit.jupiter.params.provider.NullAndEmptySource; | |
| import org.junit.jupiter.params.provider.ValueSource; | |
| import org.mockito.InjectMocks; | |
| import org.mockito.Mock; | |
| import org.mockito.junit.jupiter.MockitoExtension; | |
| import static org.assertj.core.api.Assertions.assertThatNoException; | |
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | |
| import static org.mockito.Mockito.*; | |
| class IpRateLimiterServiceTest { | |
| private RateLimiterStrategy rateLimiterStrategy; | |
| private IpRateLimiterService ipRateLimiterService; | |
| // ───────────────────────────────────────────────────────────── | |
| // IPs inválidas | |
| // ───────────────────────────────────────────────────────────── | |
| class InvalidIpTests { | |
| void shouldThrowForNullOrBlankIp(String ip) { | |
| assertThatThrownBy(() -> ipRateLimiterService.validate(ip)) | |
| .isInstanceOf(RateLimitExceededException.class) | |
| .hasMessageContaining("IP inválida"); | |
| verifyNoInteractions(rateLimiterStrategy); | |
| } | |
| } | |
| // ───────────────────────────────────────────────────────────── | |
| // Peticiones permitidas | |
| // ───────────────────────────────────────────────────────────── | |
| class AllowedRequestTests { | |
| void shouldNotThrowWhenAllowed() { | |
| when(rateLimiterStrategy.tryConsume("192.168.1.1")).thenReturn(true); | |
| assertThatNoException() | |
| .isThrownBy(() -> ipRateLimiterService.validate("192.168.1.1")); | |
| } | |
| void shouldDelegateToStrategyWithCorrectIp() { | |
| String ip = "10.0.0.5"; | |
| when(rateLimiterStrategy.tryConsume(ip)).thenReturn(true); | |
| ipRateLimiterService.validate(ip); | |
| verify(rateLimiterStrategy, times(1)).tryConsume(ip); | |
| } | |
| } | |
| // ───────────────────────────────────────────────────────────── | |
| // Peticiones bloqueadas | |
| // ───────────────────────────────────────────────────────────── | |
| class BlockedRequestTests { | |
| void shouldThrowWhenRateLimitExceeded() { | |
| String ip = "1.2.3.4"; | |
| when(rateLimiterStrategy.tryConsume(ip)).thenReturn(false); | |
| assertThatThrownBy(() -> ipRateLimiterService.validate(ip)) | |
| .isInstanceOf(RateLimitExceededException.class) | |
| .hasMessageContaining(ip); | |
| } | |
| void exceptionMessageShouldContainBlockedIp() { | |
| String ip = "5.6.7.8"; | |
| when(rateLimiterStrategy.tryConsume(ip)).thenReturn(false); | |
| assertThatThrownBy(() -> ipRateLimiterService.validate(ip)) | |
| .isInstanceOf(RateLimitExceededException.class) | |
| .hasMessageContaining(ip); | |
| } | |
| } | |
| } | |