### Question:
TrustedProxyAuthorizationService extends AbstractLifeCycle implements AuthorizationService { @Override public UserIdentity getUserIdentity(HttpServletRequest request, String name) { int nameHostSeparatorIndex = name.indexOf('/'); String serviceName = nameHostSeparatorIndex > 0 ? name.substring(0, nameHostSeparatorIndex) : name; UserIdentity serviceIdentity = _adminUserStore.getUserIdentity(serviceName); if (_trustedProxyIpPattern != null) { return _trustedProxyIpPattern.matcher(request.getRemoteAddr()).matches() ? serviceIdentity : null; } else { return serviceIdentity; } } TrustedProxyAuthorizationService(List<String> userNames, String trustedProxyIpPattern); @Override UserIdentity getUserIdentity(HttpServletRequest request, String name);  }### Answer:
@Test public void testSuccessfulLoginWithIpFiltering() throws Exception { TrustedProxyAuthorizationService srv = new TrustedProxyAuthorizationService(Collections.singletonList(AUTH_SERVICE_NAME), IP_FILTER); HttpServletRequest mockRequest = mock(HttpServletRequest.class); expect(mockRequest.getRemoteAddr()).andReturn("192.168.0.1"); replay(mockRequest); srv.start(); try { UserIdentity result = srv.getUserIdentity(mockRequest, AUTH_SERVICE_NAME); assertNotNull(result); assertEquals(AUTH_SERVICE_NAME, result.getUserPrincipal().getName()); } finally { srv.stop(); } }
@Test public void testUnsuccessfulLoginWithIpFiltering() throws Exception { TrustedProxyAuthorizationService srv = new TrustedProxyAuthorizationService(Collections.singletonList(AUTH_SERVICE_NAME), IP_FILTER); HttpServletRequest mockRequest = mock(HttpServletRequest.class); expect(mockRequest.getRemoteAddr()).andReturn("192.167.0.1"); replay(mockRequest); srv.start(); try { UserIdentity result = srv.getUserIdentity(mockRequest, AUTH_SERVICE_NAME); assertNull(result); } finally { srv.stop(); } }
@Test public void testSuccessfulLoginWithoutIpFiltering() throws Exception { TrustedProxyAuthorizationService srv = new TrustedProxyAuthorizationService(Collections.singletonList(AUTH_SERVICE_NAME), null); HttpServletRequest mockRequest = mock(HttpServletRequest.class); srv.start(); try { UserIdentity result = srv.getUserIdentity(mockRequest, AUTH_SERVICE_NAME); assertNotNull(result); assertEquals(AUTH_SERVICE_NAME, result.getUserPrincipal().getName()); } finally { srv.stop(); } }