From 599a2ba2b8846408558430c7f17ab1e778e59872 Mon Sep 17 00:00:00 2001 From: Kristof Neirynck Date: Sun, 28 Jul 2024 18:55:44 +0200 Subject: [PATCH] Combine query with request body parameters This way the return value of `org.htmlunit.WebRequest.getParameters` will better resemble that of a servlet api. --- src/main/java/org/htmlunit/WebRequest.java | 11 +++++- .../java/org/htmlunit/WebRequestTest.java | 39 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/htmlunit/WebRequest.java b/src/main/java/org/htmlunit/WebRequest.java index e75011eee4d..4e08361f85f 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 + 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()); + 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 cb9b90199f3..0be6bc84d32 100644 --- a/src/test/java/org/htmlunit/WebRequestTest.java +++ b/src/test/java/org/htmlunit/WebRequestTest.java @@ -22,6 +22,8 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.apache.http.auth.BasicUserPrincipal; @@ -394,6 +396,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"); + + 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"))); + + 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 */