Skip to content

Commit

Permalink
Merge pull request #243 from rollbar/basoko/track-request-parameters-…
Browse files Browse the repository at this point in the history
…with-every-method

Add all request parameters for post request method
  • Loading branch information
basoko authored Dec 10, 2020
2 parents 1ad54e2 + e0eeb0a commit 09c4351
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -140,21 +141,42 @@ private static Map<String, String> headers(HttpServletRequest request) {

private static Map<String, List<String>> getParams(HttpServletRequest request) {
if ("GET".equalsIgnoreCase(request.getMethod())) {
Map<String, List<String>> params = new HashMap<>();
return params(request);
}

return null;
}

Map<String, String[]> paramNames = request.getParameterMap();
for (Entry<String, String[]> param : paramNames.entrySet()) {
if (param.getValue() != null && param.getValue().length > 0) {
params.put(param.getKey(), asList(param.getValue()));
private static Map<String, Object> postParams(HttpServletRequest request) {
if ("POST".equalsIgnoreCase(request.getMethod())) {
Map<String, List<String>> params = params(request);
Map<String, Object> postParams = new HashMap<>();
for (Entry<String, List<String>> 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<String, List<String>> params(HttpServletRequest request) {
Map<String, List<String>> params = new HashMap<>();

Map<String, String[]> paramNames = request.getParameterMap();
for (Entry<String, String[]> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,10 @@ public class RequestProviderTest {
+ "application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
}

static final Map<String, String[]> REQUEST_GET_PARAMS = new HashMap<>();
static final Map<String, String[]> 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<String, List<String>> 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";
Expand All @@ -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);

Expand All @@ -85,16 +76,43 @@ public void setUp() {

@Test
public void shouldRetrieveTheRequest() {
when(request.getMethod()).thenReturn(METHOD);
when(request.getParameterMap()).thenReturn(REQUEST_PARAMS);

Map<String, List<String>> 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<String, Object> 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";
Expand Down

0 comments on commit 09c4351

Please sign in to comment.