ClientInfoServiceImpl implements ClientInfoService { @Override public String getClientIp(HttpServletRequest request) { return getClientIp(request.getRemoteAddr(), request.getHeader(X_FORWARDED_FOR_HEADER)); } @Autowired(required = true)  ClientInfoServiceImpl(ConfigurationService configurationService); @Override String getClientIp(HttpServletRequest request); @Override String getClientIp(String remoteIp, String xForwardedForHeaderValue); @Override boolean isUseProxiesEnabled();  }
@Test public void getClientIpFromRequest() { configurationService.setProperty("useProxies", true); configurationService.setProperty("proxies.trusted.ipranges", "127.0.0"); clientInfoService = new ClientInfoServiceImpl(configurationService); DummyHttpServletRequest req = new DummyHttpServletRequest(); req.setAddress("127.0.0.1"); req.addHeader("X-Forwarded-For", "192.168.1.24"); assertEquals("192.168.1.24", clientInfoService.getClientIp(req)); }
@Test public void getClientIpWithTrustedProxy() { configurationService.setProperty("useProxies", true); configurationService.setProperty("proxies.trusted.ipranges", "127.0.0"); clientInfoService = new ClientInfoServiceImpl(configurationService); String remoteIp = "127.0.0.1"; String xForwardedFor = "192.168.1.24"; assertEquals("192.168.1.24", clientInfoService.getClientIp(remoteIp, xForwardedFor)); }
@Test public void getClientIpWithUntrustedProxy() { configurationService.setProperty("useProxies", true); configurationService.setProperty("proxies.trusted.ipranges", "192.168.1.1"); clientInfoService = new ClientInfoServiceImpl(configurationService); String remoteIp = "10.24.64.14"; String xForwardedFor = "192.168.1.24"; assertEquals("10.24.64.14", clientInfoService.getClientIp(remoteIp, xForwardedFor)); }
@Test public void getClientIpWithMultipleTrustedProxies() { configurationService.setProperty("useProxies", true); configurationService.setProperty("proxies.trusted.ipranges", "127.0.0,192.168.1"); clientInfoService = new ClientInfoServiceImpl(configurationService); String remoteIp = "127.0.0.1"; String xForwardedFor = "10.24.64.14,192.168.1.24"; assertEquals("10.24.64.14", clientInfoService.getClientIp(remoteIp, xForwardedFor)); xForwardedFor = "192.168.1.24,10.24.64.14"; assertEquals("10.24.64.14", clientInfoService.getClientIp(remoteIp, xForwardedFor)); }
@Test public void getClientIpWithoutTrustedProxies() { configurationService.setProperty("useProxies", true); configurationService.setProperty("proxies.trusted.ipranges", ""); clientInfoService = new ClientInfoServiceImpl(configurationService); String remoteIp = "127.0.0.1"; String xForwardedFor = "10.24.64.14"; assertEquals("10.24.64.14", clientInfoService.getClientIp(remoteIp, xForwardedFor)); xForwardedFor = "127.0.0.1,10.24.64.14"; assertEquals("10.24.64.14", clientInfoService.getClientIp(remoteIp, xForwardedFor)); xForwardedFor = "10.24.64.14,127.0.0.1"; assertEquals("10.24.64.14", clientInfoService.getClientIp(remoteIp, xForwardedFor)); }
@Test public void getClientIpWithoutUseProxies() { configurationService.setProperty("useProxies", false); configurationService.setProperty("proxies.trusted.ipranges", ""); clientInfoService = new ClientInfoServiceImpl(configurationService); String remoteIp = "127.0.0.1"; String xForwardedFor = "10.24.64.14"; assertEquals("127.0.0.1", clientInfoService.getClientIp(remoteIp, xForwardedFor)); assertEquals("127.0.0.1", clientInfoService.getClientIp(remoteIp, null)); assertEquals("127.0.0.1", clientInfoService.getClientIp(remoteIp, "")); }