Skip to content

Commit 74de92e

Browse files
authored
Added IpFilterTest class with unit test verifying IpFilter allows whi… (#76)
* Added IpFilterTest class with unit test verifying IpFilter allows whitelisted IPs. * Fix IpFilterTest to verify response interaction instead of mock state * Added unit test for blocking IP that is not in the whitelist, results in 403 Forbidden response. Fixed HttpResponse construtors to always initialize headers and body to prevent NPE when filters call setHeader or setBody. * Update IpFilter whitelist allow test to use real HttpResponse * Assert expected status code in IpFilter whitelist allow test
1 parent 3d11a7c commit 74de92e

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/main/java/org/juv25d/http/HttpResponse.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ public class HttpResponse {
1515
private byte[] body;
1616

1717
public HttpResponse(){
18+
this.statusCode = 200;
19+
this.statusText = "OK";
1820
this.headers = new LinkedHashMap<>();
1921
this.body = new byte[0];
2022
}
2123

2224
public HttpResponse(int statusCode, String statusText, Map<String, String> headers, byte[] body) {
2325
this.statusCode = statusCode;
2426
this.statusText = statusText;
25-
this.headers = new LinkedHashMap<>(headers);
27+
this.headers = headers != null ? new LinkedHashMap<>(headers) : new LinkedHashMap<>();
2628
this.body = body != null ? body.clone() : new byte[0];
2729
}
2830

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.juv25d.filter;
2+
import org.junit.jupiter.api.Test;
3+
import org.juv25d.http.HttpRequest;
4+
import org.juv25d.http.HttpResponse;
5+
import java.io.IOException;
6+
import java.util.Set;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.mockito.Mockito.*;
9+
10+
class IpFilterTest {
11+
12+
@Test
13+
void whitelist_allowsIp() throws IOException {
14+
IpFilter filter = new IpFilter(Set.of("127.0.0.1"), null);
15+
16+
HttpRequest req = mock(HttpRequest.class);
17+
when(req.remoteIp()).thenReturn("127.0.0.1");
18+
19+
HttpResponse res = new HttpResponse();
20+
FilterChain chain = mock(FilterChain.class);
21+
22+
filter.doFilter(req, res, chain);
23+
24+
verify(chain).doFilter(req, res);
25+
assertEquals(200, res.statusCode());
26+
}
27+
28+
@Test
29+
void whitelist_blocksIpNotInList() throws IOException {
30+
IpFilter filter = new IpFilter(Set.of("10.0.0.1"), null);
31+
32+
HttpRequest req = mock(HttpRequest.class);
33+
when(req.remoteIp()).thenReturn("127.0.0.1");
34+
35+
HttpResponse res = new HttpResponse();
36+
FilterChain chain = mock(FilterChain.class);
37+
38+
filter.doFilter(req, res, chain);
39+
verify(chain, never()).doFilter(req, res);
40+
41+
assertEquals(403, res.statusCode());
42+
assertEquals("Forbidden", res.statusText());
43+
44+
}
45+
}

0 commit comments

Comments
 (0)