Skip to content

Commit

Permalink
Implemented: Add unit tests for ‘UtilHttp#getParameterMap’
Browse files Browse the repository at this point in the history
(OFBIZ-11138)

Adapt slightly the implementation to make mocking easier.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1863397 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Jul 19, 2019
1 parent e576b5e commit 1d3d3be
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,13 @@ public static Map<String, Object> getParameterMap(HttpServletRequest request, Se
Map<String, Object> paramMap = new HashMap<>();

// add all the actual HTTP request parameters
Enumeration<String> e = UtilGenerics.cast(request.getParameterNames());
while (e.hasMoreElements()) {
String name = e.nextElement();
Map<String, String[]> origParams = request.getParameterMap();
origParams.forEach((name, paramArr) -> {
if (nameSet != null && (onlyIncludeOrSkipPrim ^ nameSet.contains(name))) {
continue;
return;
}

Object value = null;
String[] paramArr = request.getParameterValues(name);
if (paramArr != null) {
if (paramArr.length > 1) {
value = Arrays.asList(paramArr);
Expand All @@ -160,7 +158,7 @@ public static Map<String, Object> getParameterMap(HttpServletRequest request, Se
}
}
paramMap.put(name, value);
}
});

paramMap.putAll(getPathInfoOnlyParameterMap(request.getPathInfo(), nameSet, onlyIncludeOrSkipPrim));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,27 @@
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.HttpMethod;

import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

public class UtilHttpTest {
private HttpServletRequest req;

@Before
public void setup() {
req = Mockito.mock(HttpServletRequest.class);
}

@Test
public void basicGetPathInfoOnlyParameterMap() {
Expand Down Expand Up @@ -61,4 +74,50 @@ public void filteredGetPathInfoOnlyParameterMap() {
assertThat(getPathInfoOnlyParameterMap("/~foo=1/~bar=2", UtilMisc.toSet("foo"), true),
allOf(hasEntry("foo", "1"), not(hasEntry("bar", "2"))));
}

@Test
public void basicGetParameterMap() {
when(req.getParameterMap()).thenReturn(UtilMisc.toMap(
"foo", new String[] {"1"},
"bar", new String[] {"2", "3"}));
when(req.getPathInfo()).thenReturn("/foo");
assertThat(UtilHttp.getParameterMap(req), Matchers.<Map<String, Object>>allOf(
hasEntry("foo", "1"),
hasEntry("bar", Arrays.asList("2", "3"))));
}

@Test
public void pathInfoOverrideGetParameterMap() {
when(req.getParameterMap()).thenReturn(UtilMisc.toMap(
"foo", new String[] {"1"},
"bar", new String[] {"2"}));
when(req.getPathInfo()).thenReturn("/foo/~bar=3");
assertThat(UtilHttp.getParameterMap(req), Matchers.<Map<String, Object>>allOf(
hasEntry("foo", "1"),
hasEntry("bar", "3")));
}

@Test
public void emptyParameterMap() {
when(req.getParameterMap()).thenReturn(Collections.emptyMap());
when(req.getPathInfo()).thenReturn("/foo/bar");
when(req.getMethod()).thenReturn(HttpMethod.POST);
UtilHttp.getParameterMap(req);
// Check that multi-part arguments are looked up
Mockito.verify(req).getContentType();
}

@Test
public void filteredGetParameterMap() {
when(req.getParameterMap()).thenReturn(UtilMisc.toMap(
"foo", new String[] {"1"},
"bar", new String[] {"2", "3"}));
when(req.getPathInfo()).thenReturn("/foo");
assertThat(UtilHttp.getParameterMap(req, UtilMisc.toSet("bar"), false), Matchers.<Map<String, Object>>allOf(
hasEntry("foo", "1"),
not(hasEntry("bar", Arrays.asList("2", "3")))));
assertThat(UtilHttp.getParameterMap(req, UtilMisc.toSet("bar"), true), Matchers.<Map<String, Object>>allOf(
not(hasEntry("foo", "1")),
hasEntry("bar", Arrays.asList("2", "3"))));
}
}

0 comments on commit 1d3d3be

Please sign in to comment.