Skip to content

Commit

Permalink
Combine query with request body parameters
Browse files Browse the repository at this point in the history
This way the return value of `org.htmlunit.WebRequest.getParameters` will better resemble that of a servlet api.
  • Loading branch information
Crydust committed Jul 30, 2024
1 parent ef6b166 commit 599a2ba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/main/java/org/htmlunit/WebRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,11 @@ public List<NameValuePair> getParameters() {
return normalize(getRequestParameters());
}

return normalize(HttpUtils.parseUrlQuery(getRequestBody(), getCharset()));
// getRequestParameters and getRequestBody are mutually exclusive
List<NameValuePair> allParameters = new ArrayList<>();

Check failure on line 371 in src/main/java/org/htmlunit/WebRequest.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Local variable 'allParameters' could be declared final Raw Output: {"locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/WebRequest.java"},"region":{"endColumn":46,"endLine":371,"startColumn":33,"startLine":371}}}],"message":{"text":"Local variable 'allParameters' could be declared final"},"ruleId":"LocalVariableCouldBeFinal","ruleIndex":59}

Check failure on line 371 in src/main/java/org/htmlunit/WebRequest.java

View workflow job for this annotation

GitHub Actions / CheckStyle

[checkstyle] reported by reviewdog 🐶 Variable 'allParameters' should be declared final. Raw Output: /home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/WebRequest.java:371:33: error: Variable 'allParameters' should be declared final. (com.puppycrawl.tools.checkstyle.checks.coding.FinalLocalVariableCheck)
allParameters.addAll(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
allParameters.addAll(HttpUtils.parseUrlQuery(getRequestBody(), getCharset()));
return normalize(allParameters);
}

if (getEncodingType() == FormEncodingType.TEXT_PLAIN && HttpMethod.POST == getHttpMethod()) {
Expand All @@ -379,7 +383,10 @@ public List<NameValuePair> getParameters() {
}

if (FormEncodingType.MULTIPART == getEncodingType()) {
return normalize(getRequestParameters());
List<NameValuePair> allParameters = new ArrayList<>();

Check failure on line 386 in src/main/java/org/htmlunit/WebRequest.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Local variable 'allParameters' could be declared final Raw Output: {"locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/WebRequest.java"},"region":{"endColumn":46,"endLine":386,"startColumn":33,"startLine":386}}}],"message":{"text":"Local variable 'allParameters' could be declared final"},"ruleId":"LocalVariableCouldBeFinal","ruleIndex":59}

Check failure on line 386 in src/main/java/org/htmlunit/WebRequest.java

View workflow job for this annotation

GitHub Actions / CheckStyle

[checkstyle] reported by reviewdog 🐶 Variable 'allParameters' should be declared final. Raw Output: /home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/WebRequest.java:386:33: error: Variable 'allParameters' should be declared final. (com.puppycrawl.tools.checkstyle.checks.coding.FinalLocalVariableCheck)
allParameters.addAll(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
allParameters.addAll(getRequestParameters());
return normalize(allParameters);
}

// for instance a PUT or PATCH request
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/org/htmlunit/WebRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;

Check failure on line 25 in src/test/java/org/htmlunit/WebRequestTest.java

View workflow job for this annotation

GitHub Actions / CheckStyle

[checkstyle] reported by reviewdog 🐶 Unused import - java.util.Arrays. Raw Output: /home/runner/work/htmlunit/htmlunit/src/test/java/org/htmlunit/WebRequestTest.java:25:8: error: Unused import - java.util.Arrays. (com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck)
import java.util.Collections;
import java.util.List;

import org.apache.http.auth.BasicUserPrincipal;
Expand Down Expand Up @@ -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<NameValuePair> parameters = request.getParameters();

Check failure on line 410 in src/test/java/org/htmlunit/WebRequestTest.java

View workflow job for this annotation

GitHub Actions / CheckStyle

[checkstyle] reported by reviewdog 🐶 Variable 'parameters' should be declared final. Raw Output: /home/runner/work/htmlunit/htmlunit/src/test/java/org/htmlunit/WebRequestTest.java:410:29: error: Variable 'parameters' should be declared final. (com.puppycrawl.tools.checkstyle.checks.coding.FinalLocalVariableCheck)

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<NameValuePair> parameters = request.getParameters();

Check failure on line 427 in src/test/java/org/htmlunit/WebRequestTest.java

View workflow job for this annotation

GitHub Actions / CheckStyle

[checkstyle] reported by reviewdog 🐶 Variable 'parameters' should be declared final. Raw Output: /home/runner/work/htmlunit/htmlunit/src/test/java/org/htmlunit/WebRequestTest.java:427:29: error: Variable 'parameters' should be declared final. (com.puppycrawl.tools.checkstyle.checks.coding.FinalLocalVariableCheck)

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
*/
Expand Down

0 comments on commit 599a2ba

Please sign in to comment.