### Question:
RequestUtils { public static String getRemoteAddress(HttpServletRequest request) { String ip = null; for (String header : headersToCheck) { ip = request.getHeader(header); if (!isRemoteAddressUnknown(ip)) { break; } } if (isRemoteAddressUnknown(ip)) { ip = request.getRemoteAddr(); } if (containsMultipleRemoteAddresses(ip)) { ip = ip.substring(0, ip.indexOf(",")); } return ip; }  static String getRemoteAddress(HttpServletRequest request); static String getRemoteAddress(); static void logRequestHeadersAndQueryParams(HttpServletRequest request, Logger logger); static MultiValueMap<String, String> getQueryStringParameters(HttpServletRequest request); static List<String> getQueryStringParameterValues(HttpServletRequest request, String parameterName); static String getQueryStringParameterValue(HttpServletRequest request, String parameterName);  }### Answer:
@Test public void testGetRemoteAddress() { HttpServletRequest mockedRequest = createMock(HttpServletRequest.class); expect(mockedRequest.getHeader("X-Forwarded-For")).andReturn(null); expect(mockedRequest.getHeader("Proxy-Client-IP")).andReturn("unknown"); expect(mockedRequest.getHeader("WL-Proxy-Client-IP")).andReturn(""); expect(mockedRequest.getHeader("HTTP_CLIENT_IP")).andReturn("unknown"); expect(mockedRequest.getHeader("HTTP_X_FORWARDED_FOR")).andReturn(REMOTE_ADDRESS); replayAll(); String remoteAddress = RequestUtils.getRemoteAddress(mockedRequest); assertEquals(REMOTE_ADDRESS, remoteAddress); verifyAll(); }
@Test public void testGetMultipleRemoteAddress() { HttpServletRequest mockedRequest = createMock(HttpServletRequest.class); expect(mockedRequest.getHeader("X-Forwarded-For")).andReturn(null); expect(mockedRequest.getHeader("Proxy-Client-IP")).andReturn("unknown"); expect(mockedRequest.getHeader("WL-Proxy-Client-IP")).andReturn(""); expect(mockedRequest.getHeader("HTTP_CLIENT_IP")).andReturn("unknown"); expect(mockedRequest.getHeader("HTTP_X_FORWARDED_FOR")).andReturn(REMOTE_ADDRESS_MULTIPLE); replayAll(); String remoteAddress = RequestUtils.getRemoteAddress(mockedRequest); assertEquals(REMOTE_ADDRESS, remoteAddress); verifyAll(); }
@Test public void testGetRemoteAddressFoundFirstHeader() { HttpServletRequest mockedRequest = createMock(HttpServletRequest.class); expect(mockedRequest.getHeader("X-Forwarded-For")).andReturn(REMOTE_ADDRESS); replayAll(); String remoteAddress = RequestUtils.getRemoteAddress(mockedRequest); assertEquals(REMOTE_ADDRESS, remoteAddress); verifyAll(); }
@Test public void testGetRemoteAddressWhenHeadersAreMissing() { HttpServletRequest mockedRequest = createMock(HttpServletRequest.class); expect(mockedRequest.getHeader(anyString())).andReturn(null).times(5); expect(mockedRequest.getRemoteAddr()).andReturn(REMOTE_ADDRESS); replayAll(); String remoteAddress = RequestUtils.getRemoteAddress(mockedRequest); assertEquals(REMOTE_ADDRESS, remoteAddress); verifyAll(); }