@@ -145,4 +145,73 @@ void testGetIpFromRequest_MultipleValidIPsInXForwardedFor() {
145145 String result = getIpFromRequest ("1.2.3.4" , headers );
146146 assertEquals ("5.6.7.8" , result );
147147 }
148+
149+ @ Test
150+ void testGetIpFromRequest_RawIpWithIPv4Port () {
151+ String result = getIpFromRequest ("109.132.232.101:58780" , new HashMap <>());
152+ assertEquals ("109.132.232.101" , result );
153+ }
154+
155+ @ Test
156+ void testGetIpFromRequest_RawIpWithBracketedIPv6Port () {
157+ String result = getIpFromRequest ("[2001:db8::1]:443" , new HashMap <>());
158+ assertEquals ("2001:db8::1" , result );
159+ }
160+
161+ @ Test
162+ void testGetIpFromRequest_RawIpWithBracketedIPv6WithoutPort () {
163+ String result = getIpFromRequest ("[2001:db8::1]" , new HashMap <>());
164+ assertEquals ("2001:db8::1" , result );
165+ }
166+
167+ @ Test
168+ void testGetIpFromRequest_XForwardedForWithBracketedIPv6Port () {
169+ headers .put ("X-Forwarded-For" , List .of ("[2001:db8::1]:443, 203.0.113.5" ));
170+ String result = getIpFromRequest ("10.0.0.1" , headers );
171+ assertEquals ("2001:db8::1" , result );
172+ }
173+
174+ @ Test
175+ @ SetEnvironmentVariable (key = "AIKIDO_TRUST_PROXY" , value = "0" )
176+ void testGetIpFromRequest_TrustProxyFalseStillNormalizesRawIp () {
177+ headers .put ("X-Forwarded-For" , List .of ("1.2.3.4" ));
178+ String result = getIpFromRequest ("109.132.232.101:58780" , headers );
179+ assertEquals ("109.132.232.101" , result );
180+ }
181+
182+ @ Test
183+ void testGetIpFromRequest_RawIpWithPlainIPv4 () {
184+ String result = getIpFromRequest ("109.132.232.101" , new HashMap <>());
185+ assertEquals ("109.132.232.101" , result );
186+ }
187+
188+ @ Test
189+ void testGetIpFromRequest_RawIpWithPlainIPv6 () {
190+ String result = getIpFromRequest ("2001:db8::1" , new HashMap <>());
191+ assertEquals ("2001:db8::1" , result );
192+ }
193+
194+ @ Test
195+ void testGetIpFromRequest_RawIpWithInvalidBracketedIpFallsBackToRawIp () {
196+ String result = getIpFromRequest ("[not-an-ip]" , new HashMap <>());
197+ assertEquals ("[not-an-ip]" , result );
198+ }
199+
200+ @ Test
201+ void testGetIpFromRequest_RawIpWithInvalidBracketedIpAndPortFallsBackToRawIp () {
202+ String result = getIpFromRequest ("[not-an-ip]:443" , new HashMap <>());
203+ assertEquals ("[not-an-ip]:443" , result );
204+ }
205+
206+ @ Test
207+ void testGetIpFromRequest_RawIpWithInvalidIPv4PortFallsBackToRawIp () {
208+ String result = getIpFromRequest ("not-an-ip:58780" , new HashMap <>());
209+ assertEquals ("not-an-ip:58780" , result );
210+ }
211+
212+ @ Test
213+ void testGetIpFromRequest_RawIpWithEmptyStringFallsBackToRawIp () {
214+ String result = getIpFromRequest ("" , new HashMap <>());
215+ assertEquals ("" , result );
216+ }
148217}
0 commit comments