Skip to content

Commit

Permalink
Add CIDR unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosbarbero committed Jul 8, 2019
1 parent 483aa06 commit 17787bc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<version>Finchley.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,9 @@ public enum RateLimitType {
ORIGIN {
@Override
public boolean apply(HttpServletRequest request, Route route, RateLimitUtils rateLimitUtils, String matcher) {
if(matcher.contains("/")) {
try {
SubnetUtils subnetUtils = new SubnetUtils(matcher);
return subnetUtils.getInfo().isInRange(rateLimitUtils.getRemoteAddress(request));
}
catch(Exception e) {
return false;
}
if (matcher.contains("/")) {
SubnetUtils subnetUtils = new SubnetUtils(matcher);
return subnetUtils.getInfo().isInRange(rateLimitUtils.getRemoteAddress(request));
}
return matcher.equals(rateLimitUtils.getRemoteAddress(request));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public void applyOrigin() {
assertThat(apply).isTrue();
}

@Test
public void applyOriginInRange() {
when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.10");

boolean apply = RateLimitType.ORIGIN.apply(httpServletRequest, route, rateLimitUtils, "127.0.0.8/29");
assertThat(apply).isTrue();
}

@Test
public void applyOriginNoMatch() {
when(httpServletRequest.getRemoteAddr()).thenReturn("testAddr");
Expand Down

0 comments on commit 17787bc

Please sign in to comment.