diff --git a/rollbar-web/src/main/java/com/rollbar/web/provider/RequestProvider.java b/rollbar-web/src/main/java/com/rollbar/web/provider/RequestProvider.java index fcd2f101..4787f368 100755 --- a/rollbar-web/src/main/java/com/rollbar/web/provider/RequestProvider.java +++ b/rollbar-web/src/main/java/com/rollbar/web/provider/RequestProvider.java @@ -58,6 +58,7 @@ public Request provide() { .method(method(req)) .headers(headers(req)) .get(getParams(req)) + .post(postParams(req)) .queryString(queryString(req)) .userIp(userIp(req)) .build(); @@ -140,21 +141,42 @@ private static Map headers(HttpServletRequest request) { private static Map> getParams(HttpServletRequest request) { if ("GET".equalsIgnoreCase(request.getMethod())) { - Map> params = new HashMap<>(); + return params(request); + } + + return null; + } - Map paramNames = request.getParameterMap(); - for (Entry param : paramNames.entrySet()) { - if (param.getValue() != null && param.getValue().length > 0) { - params.put(param.getKey(), asList(param.getValue())); + private static Map postParams(HttpServletRequest request) { + if ("POST".equalsIgnoreCase(request.getMethod())) { + Map> params = params(request); + Map postParams = new HashMap<>(); + for (Entry> entry : params.entrySet()) { + if (entry.getValue() != null && entry.getValue().size() == 1) { + postParams.put(entry.getKey(), entry.getValue().get(0)); + } else { + postParams.put(entry.getKey(), entry.getValue()); } } - - return params; + return postParams; } return null; } + private static Map> params(HttpServletRequest request) { + Map> params = new HashMap<>(); + + Map paramNames = request.getParameterMap(); + for (Entry param : paramNames.entrySet()) { + if (param.getValue() != null && param.getValue().length > 0) { + params.put(param.getKey(), asList(param.getValue())); + } + } + + return params; + } + private static String queryString(HttpServletRequest request) { return request.getQueryString(); } diff --git a/rollbar-web/src/test/java/com/rollbar/web/provider/RequestProviderTest.java b/rollbar-web/src/test/java/com/rollbar/web/provider/RequestProviderTest.java index 0327c456..7a9813df 100644 --- a/rollbar-web/src/test/java/com/rollbar/web/provider/RequestProviderTest.java +++ b/rollbar-web/src/test/java/com/rollbar/web/provider/RequestProviderTest.java @@ -35,17 +35,10 @@ public class RequestProviderTest { + "application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"); } - static final Map REQUEST_GET_PARAMS = new HashMap<>(); + static final Map REQUEST_PARAMS = new HashMap<>(); static { - REQUEST_GET_PARAMS.put("param1", new String[]{"value1", "value2"}); - REQUEST_GET_PARAMS.put("param2", new String[]{"value3"}); - } - - static final Map> EXPECTED_GET_PARAMS = new HashMap<>(); - static { - for(String paramName : REQUEST_GET_PARAMS.keySet()) { - EXPECTED_GET_PARAMS.put(paramName, asList(REQUEST_GET_PARAMS.get(paramName))); - } + REQUEST_PARAMS.put("param1", new String[]{"value1", "value2"}); + REQUEST_PARAMS.put("param2", new String[]{"value3"}); } static final String REMOTE_ADDRESS = "127.0.0.1"; @@ -66,12 +59,10 @@ public class RequestProviderTest { @Before public void setUp() { when(request.getRequestURL()).thenReturn(REQUEST_URL); - when(request.getMethod()).thenReturn(METHOD); when(request.getHeaderNames()).thenReturn(enumeration(HEADERS.keySet())); for(String headerName : HEADERS.keySet()) { when(request.getHeader(headerName)).thenReturn(HEADERS.get(headerName)); } - when(request.getParameterMap()).thenReturn(REQUEST_GET_PARAMS); when(request.getQueryString()).thenReturn(QUERYSTRING); when(request.getRemoteAddr()).thenReturn(REMOTE_ADDRESS); @@ -85,16 +76,43 @@ public void setUp() { @Test public void shouldRetrieveTheRequest() { + when(request.getMethod()).thenReturn(METHOD); + when(request.getParameterMap()).thenReturn(REQUEST_PARAMS); + + Map> expectedGetParams = new HashMap<>(); + for(String paramName : REQUEST_PARAMS.keySet()) { + expectedGetParams.put(paramName, asList(REQUEST_PARAMS.get(paramName))); + } + 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.getGet(), is(expectedGetParams)); assertThat(result.getQueryString(), is(QUERYSTRING)); assertThat(result.getUserIp(), is(REMOTE_ADDRESS)); } + @Test + public void shouldTrackPostParams() { + when(request.getMethod()).thenReturn("POST"); + when(request.getParameterMap()).thenReturn(REQUEST_PARAMS); + + Map expectedPostParams = new HashMap<>(); + for(String paramName : REQUEST_PARAMS.keySet()) { + String[] values = REQUEST_PARAMS.get(paramName); + if (values.length == 1) { + expectedPostParams.put(paramName, values[0]); + } else { + expectedPostParams.put(paramName, asList(values)); + } + } + + Request result = sut.provide(); + assertThat(result.getPost(), is(expectedPostParams)); + } + @Test public void shouldRetrieveTheRemoteAddressUsingHeader() { String userIpHeaderName = "X-FORWARDED-FOR";