RequestProvider implements Provider<Request> { @Override public Request provide() { HttpServletRequest req = RollbarRequestListener.getServletRequest(); if (req != null) { Request request = new Request.Builder() .url(url(req)) .method(method(req)) .headers(headers(req)) .get(getParams(req)) .queryString(queryString(req)) .userIp(userIp(req)) .build(); return request; } return null; } RequestProvider(Builder builder); @Override Request provide(); static final String CAPTURE_IP_ANONYMIZE; static final String CAPTURE_IP_NONE; }
@Test public void shouldRetrieveTheRequest() { Request result = sut.provide(); assertThat(result.getUrl(), is(REQUEST_URL.toString())); assertThat(result.getMethod(), is(METHOD)); assertThat(result.getHeaders(), is(HEADERS)); assertThat(result.getGet(), is(EXPECTED_GET_PARAMS)); assertThat(result.getQueryString(), is(QUERYSTRING)); assertThat(result.getUserIp(), is(REMOTE_ADDRESS)); }
@Test public void shouldRetrieveTheRemoteAddressUsingHeader() { String userIpHeaderName = "X-FORWARDED-FOR"; RequestProvider sut = new RequestProvider.Builder() .userIpHeaderName(userIpHeaderName) .build(); String remoteAddr = "192.168.1.1"; when(request.getHeader(userIpHeaderName)).thenReturn(remoteAddr); Request result = sut.provide(); assertThat(result.getUserIp(), is(remoteAddr)); }
@Test public void shouldRetrieveTheRemoteAddressUsingRequestRemoteAddress() { String userIpHeaderName = ""; RequestProvider sut = new RequestProvider.Builder() .userIpHeaderName(userIpHeaderName) .build(); String remoteAddr = "192.168.1.1"; when(request.getRemoteAddr()).thenReturn(remoteAddr); Request result = sut.provide(); assertThat(result.getUserIp(), is(remoteAddr)); }
@Test public void shouldRetrieveTheRemoteAddressUsingRequestRemoteAddressAndAnonymize() { String userIpHeaderName = ""; RequestProvider sut = new RequestProvider.Builder() .userIpHeaderName(userIpHeaderName) .captureIp("anonymize") .build(); String remoteAddr = "192.168.1.1"; String remoteAddrAnon = "192.168.1.0"; when(request.getRemoteAddr()).thenReturn(remoteAddr); Request result = sut.provide(); assertThat(result.getUserIp(), is(remoteAddrAnon)); }
@Test public void shouldRetrieveTheRemoteAddressUsingRequestRemoteAddressAndNotCaptureIfCaptureIpIsNone() { String userIpHeaderName = ""; RequestProvider sut = new RequestProvider.Builder() .userIpHeaderName(userIpHeaderName) .captureIp("none") .build(); String remoteAddr = "192.168.1.1"; when(request.getRemoteAddr()).thenReturn(remoteAddr); Request result = sut.provide(); assertNull(result.getUserIp()); }