ServletRequestUtil { public static String getRemoteAddress(final HttpServletRequest request) { String addr = request.getRemoteAddr(); if (LOOPBACK_ADDRESS.equals(addr)) { String xff = request.getHeader(XFF_HEADER); if (xff != null) { String[] addrs = xff.split(","); final String xffAddr = addrs[addrs.length - 1].trim(); if (InetAddresses.isInetAddress(xffAddr)) { addr = xffAddr; } } } return addr; }  static String getRemoteAddress(final HttpServletRequest request); static final String LOOPBACK_ADDRESS; static final String XFF_HEADER; }
@Test public void testGetRemoteAddressNoLoopBack() { HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); Mockito.when(httpServletRequest.getRemoteAddr()).thenReturn("1.2.3.4"); assertEquals(ServletRequestUtil.getRemoteAddress(httpServletRequest), "1.2.3.4"); }
@Test public void testGetRemoteAddressNull() { HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); Mockito.when(httpServletRequest.getRemoteAddr()).thenReturn(null); assertNull(ServletRequestUtil.getRemoteAddress(httpServletRequest)); }
@Test public void testGetRemoteAddressLoopBackNoXFF() { HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); Mockito.when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1"); assertEquals(ServletRequestUtil.getRemoteAddress(httpServletRequest), "127.0.0.1"); }
@Test public void testGetRemoteAddressLoopBackSingleXFF() { HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); Mockito.when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1"); Mockito.when(httpServletRequest.getHeader("X-Forwarded-For")).thenReturn("1.2.3.4"); assertEquals(ServletRequestUtil.getRemoteAddress(httpServletRequest), "1.2.3.4"); }
@Test public void testGetRemoteAddressLoopBackMultipleXFF() { HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); Mockito.when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1"); Mockito.when(httpServletRequest.getHeader("X-Forwarded-For")).thenReturn("1.2.3.4, 1.3.4.5, 1.4.5.6"); assertEquals(ServletRequestUtil.getRemoteAddress(httpServletRequest), "1.4.5.6"); }
@Test public void testGetRemoteAddressLoopBackSingleXFFInvalidIP() { HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); Mockito.when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1"); Mockito.when(httpServletRequest.getHeader("X-Forwarded-For")).thenReturn("1.2.300.4") .thenReturn("testip").thenReturn(";s=signature"); assertEquals(ServletRequestUtil.getRemoteAddress(httpServletRequest), "127.0.0.1"); assertEquals(ServletRequestUtil.getRemoteAddress(httpServletRequest), "127.0.0.1"); assertEquals(ServletRequestUtil.getRemoteAddress(httpServletRequest), "127.0.0.1"); }
@Test public void testGetRemoteAddressLoopBackMultipleXFFInvalidIP() { HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); Mockito.when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1"); Mockito.when(httpServletRequest.getHeader("X-Forwarded-For")) .thenReturn("1.2.3.4, 1.3.4.5, 1.4.5.600") .thenReturn("1.2.3.4, testSTring") .thenReturn("1.2.3.4, ;s=signature"); assertEquals(ServletRequestUtil.getRemoteAddress(httpServletRequest), "127.0.0.1"); assertEquals(ServletRequestUtil.getRemoteAddress(httpServletRequest), "127.0.0.1"); assertEquals(ServletRequestUtil.getRemoteAddress(httpServletRequest), "127.0.0.1"); }