AbstractContext implements Context.Impl { @Override public String getRemoteAddr() { boolean isUsageOfXForwardedHeaderEnabled = ninjaProperties.getBooleanWithDefault( Context.NINJA_PROPERTIES_X_FORWARDED_FOR, false); if (isUsageOfXForwardedHeaderEnabled) { String forwardHeader = getHeader(X_FORWARD_HEADER); if (forwardHeader != null) { if (forwardHeader.contains(",")) { forwardHeader = StringUtils.split(forwardHeader, ',')[0].trim(); } try { InetAddress.getByName(forwardHeader); return forwardHeader; } catch (UnknownHostException e) { } } } return getRealRemoteAddr(); } @Inject  AbstractContext(
            BodyParserEngineManager bodyParserEngineManager,
            FlashScope flashScope,
            NinjaProperties ninjaProperties,
            Session session,
            Validation validation,
            Injector injector,
            ParamParsers paramParsers); @Override void setRoute(Route route); @Override Route getRoute(); @Override Validation getValidation(); @Deprecated @Override FlashScope getFlashCookie(); @Override FlashScope getFlashScope(); @Deprecated @Override Session getSessionCookie(); @Override Session getSession(); @Override String getContextPath(); @Override String getRequestPath(); @Override String getRemoteAddr(); @Override T getAttribute(String name, Class<T> clazz); @Override String getPathParameter(String key); @Override String getPathParameterEncoded(String key); @Override Integer getPathParameterAsInteger(String key); @Override String getParameter(String key, String defaultValue); @Override Integer getParameterAsInteger(String key); @Override Integer getParameterAsInteger(String key, Integer defaultValue); @Override T getParameterAs(String key, Class<T> clazz); @Override T getParameterAs(String key, Class<T> clazz, T defaultValue); @Override T parseBody(Class<T> classOfT); @Override String getCookieValue(String name); @Override void unsetCookie(Cookie cookie); @Override void asyncRequestComplete(); @Override ResponseStreams finalizeHeadersWithoutFlashAndSessionCookie(Result result); @Override ResponseStreams finalizeHeaders(Result result); @Override String getAcceptContentType(); @Override String getAcceptEncoding(); @Override String getAcceptLanguage(); @Override String getAcceptCharset(); @Override boolean isRequestJson(); @Override boolean isRequestXml();  }
@Test public void getRemoteAddr() { AbstractContextImpl context = spy(abstractContext); doReturn("1.1.1.1").when(context).getRealRemoteAddr(); assertThat(context.getRemoteAddr(), is("1.1.1.1")); }
@Test public void getRemoteAddrUsesXForwardHeader() { AbstractContextImpl context = spy(abstractContext); when(ninjaProperties.getBooleanWithDefault(Context.NINJA_PROPERTIES_X_FORWARDED_FOR, false)).thenReturn(Boolean.TRUE); doReturn("2.2.2.2").when(context).getHeader(Context.X_FORWARD_HEADER); assertThat(context.getRemoteAddr(), is("2.2.2.2")); }
@Test public void getRemoteAddrParsesXForwardedForIfMoreThanOneHostPresent() { AbstractContextImpl context = spy(abstractContext); when(ninjaProperties.getBooleanWithDefault(Context.NINJA_PROPERTIES_X_FORWARDED_FOR, false)).thenReturn(Boolean.TRUE); doReturn("192.168.1.1, 192.168.1.2, 192.168.1.3").when(context).getHeader(Context.X_FORWARD_HEADER); assertThat(context.getRemoteAddr(), is("192.168.1.1")); }