4
4
import static org .mockito .Mockito .mock ;
5
5
import static org .mockito .Mockito .when ;
6
6
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
7
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
7
8
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
9
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
8
10
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
9
11
10
12
import java .net .InetAddress ;
23
25
public class GeoIpRestControllerTest {
24
26
25
27
private static final InetAddress IPV4_ADDR = InetAddresses .forString ("192.168.1.1" );
28
+ private static final InetAddress IPV4_ADDR2 = InetAddresses .forString ("172.16.0.1" );
29
+ private static final InetAddress IPV4_ADDR3 = InetAddresses .forString ("10.0.0.1" );
26
30
private static final InetAddress IPV6_ADDR = InetAddresses .forString ("2001:db8:1::1" );
27
31
28
32
private MockMvc mockMvc ;
@@ -33,6 +37,8 @@ public class GeoIpRestControllerTest {
33
37
public void setUp () {
34
38
provider = mock (GeolocationProvider .class );
35
39
when (provider .lookup (eq (IPV4_ADDR ))).thenReturn (Optional .of (GeoIpEntry .builder ().setCountry ("ZZ" ).build ()));
40
+ when (provider .lookup (eq (IPV4_ADDR2 ))).thenReturn (Optional .of (GeoIpEntry .builder ().setCountry ("ZZ" ).build ()));
41
+ when (provider .lookup (eq (IPV4_ADDR3 ))).thenReturn (Optional .of (GeoIpEntry .builder ().setCountry ("ZZ" ).build ()));
36
42
when (provider .lookup (eq (IPV6_ADDR ))).thenReturn (Optional .of (GeoIpEntry .builder ().setCountry ("ZZ" ).build ()));
37
43
restController = new GeoIpRestController (provider );
38
44
mockMvc = MockMvcBuilders .standaloneSetup (restController ).build ();
@@ -58,6 +64,45 @@ public void testIpv4Address() throws Exception {
58
64
.andExpect (content ().contentType (MediaType .APPLICATION_JSON_VALUE ))
59
65
.andExpect (content ().json ("{\" country\" :\" ZZ\" }" ));
60
66
}
67
+
68
+ @ Test
69
+ public void testMultiIpv4Addresses () throws Exception {
70
+ mockMvc .perform (post ("/" ).contentType (MediaType .APPLICATION_JSON ).content (
71
+ "[\" " +
72
+ IPV4_ADDR .getHostAddress () +
73
+ "\" , \" " +
74
+ IPV4_ADDR2 .getHostAddress () +
75
+ "\" , \" " +
76
+ IPV4_ADDR3 .getHostAddress () +
77
+ "\" ]"
78
+ ))
79
+ .andExpect (status ().isOk ())
80
+ .andExpect (content ().contentType (MediaType .APPLICATION_JSON ))
81
+ .andExpect (jsonPath ("$[\" " + IPV4_ADDR .getHostAddress () + "\" ].country" ).value ("ZZ" ))
82
+ .andExpect (jsonPath ("$[\" " + IPV4_ADDR2 .getHostAddress () + "\" ].country" ).value ("ZZ" ))
83
+ .andExpect (jsonPath ("$[\" " + IPV4_ADDR3 .getHostAddress () + "\" ].country" ).value ("ZZ" ));
84
+ }
85
+
86
+ @ Test
87
+ public void testMultiIpv4AddressesExceedingLimit () throws Exception {
88
+ InetAddress [] ipAddresses = new InetAddress [101 ];
89
+ for (int i = 0 ; i < 101 ; i ++) {
90
+ ipAddresses [i ] = InetAddress .getByName ("192.168.1." + i );
91
+ }
92
+
93
+ String jsonContent = "[" ;
94
+ for (InetAddress address : ipAddresses ) {
95
+ jsonContent += "\" " + address .getHostAddress () + "\" ," ;
96
+ }
97
+ jsonContent = jsonContent .substring (0 , jsonContent .length () - 1 ) + "]" ;
98
+
99
+ mockMvc .perform (post ("/" )
100
+ .contentType (MediaType .APPLICATION_JSON )
101
+ .content (jsonContent ))
102
+ .andExpect (status ().isBadRequest ())
103
+ .andExpect (content ().contentType ("text/plain;charset=ISO-8859-1" ))
104
+ .andExpect (content ().string ("Only 100 address requests allowed at once" ));
105
+ }
61
106
62
107
@ Test
63
108
public void testIpv6Address () throws Exception {
0 commit comments