diff --git a/src/main/java/org/htmlunit/WebRequest.java b/src/main/java/org/htmlunit/WebRequest.java index e75011eee4..9524ae32c5 100644 --- a/src/main/java/org/htmlunit/WebRequest.java +++ b/src/main/java/org/htmlunit/WebRequest.java @@ -367,7 +367,11 @@ public List getParameters() { return normalize(getRequestParameters()); } - return normalize(HttpUtils.parseUrlQuery(getRequestBody(), getCharset())); + // getRequestParameters and getRequestBody are mutually exclusive + final List allParameters = new ArrayList<>(); + allParameters.addAll(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset())); + allParameters.addAll(HttpUtils.parseUrlQuery(getRequestBody(), getCharset())); + return normalize(allParameters); } if (getEncodingType() == FormEncodingType.TEXT_PLAIN && HttpMethod.POST == getHttpMethod()) { @@ -379,7 +383,10 @@ public List getParameters() { } if (FormEncodingType.MULTIPART == getEncodingType()) { - return normalize(getRequestParameters()); + final List allParameters = new ArrayList<>(); + allParameters.addAll(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset())); + allParameters.addAll(getRequestParameters()); + return normalize(allParameters); } // for instance a PUT or PATCH request diff --git a/src/test/java/org/htmlunit/WebRequestTest.java b/src/test/java/org/htmlunit/WebRequestTest.java index cb9b90199f..d816d2362b 100644 --- a/src/test/java/org/htmlunit/WebRequestTest.java +++ b/src/test/java/org/htmlunit/WebRequestTest.java @@ -22,6 +22,7 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.apache.http.auth.BasicUserPrincipal; @@ -394,6 +395,43 @@ public void getParametersFromUrlEncodedBodyPost() throws Exception { assertEquals("u", request.getParameters().get(0).getValue()); } + /** + * @throws Exception if the test fails + */ + @Test + public void getParametersFromQueryAndUrlEncodedBodyPost() throws Exception { + final URL url = new URL("http://localhost/test?a=b"); + final WebRequest request = new WebRequest(url); + request.setHttpMethod(HttpMethod.POST); + request.setEncodingType(FormEncodingType.URL_ENCODED); + request.setRequestBody("c=d"); + + final List parameters = request.getParameters(); + + assertEquals(2, parameters.size()); + assertEquals("a", parameters.get(0).getName()); + assertEquals("b", parameters.get(0).getValue()); + assertEquals("c", parameters.get(1).getName()); + assertEquals("d", parameters.get(1).getValue()); + } + + @Test + public void getParametersFromQueryAndUrlEncodedBodyPostWhenEncodingTypeIsMultipart() throws Exception { + final URL url = new URL("http://localhost/test?a=b"); + final WebRequest request = new WebRequest(url); + request.setHttpMethod(HttpMethod.POST); + request.setEncodingType(FormEncodingType.MULTIPART); + request.setRequestParameters(Collections.singletonList(new NameValuePair("c", "d"))); + + final List parameters = request.getParameters(); + + assertEquals(2, parameters.size()); + assertEquals("a", parameters.get(0).getName()); + assertEquals("b", parameters.get(0).getValue()); + assertEquals("c", parameters.get(1).getName()); + assertEquals("d", parameters.get(1).getValue()); + } + /** * @throws Exception if the test fails */