Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine query with request body parameters #836

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -53,7 +53,7 @@
* @author Michael Lueck
* @author Lai Quang Duong
*/
public class WebRequest implements Serializable {

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

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Too many fields Raw Output: {"locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/WebRequest.java"},"region":{"endColumn":2,"endLine":681,"startColumn":49,"startLine":56}}}],"message":{"text":"Too many fields"},"ruleId":"TooManyFields","ruleIndex":0}

public enum HttpHint {
/** Force to include the charset. */
Expand Down Expand Up @@ -367,7 +367,11 @@
return normalize(getRequestParameters());
}

return normalize(HttpUtils.parseUrlQuery(getRequestBody(), getCharset()));
// getRequestParameters and getRequestBody are mutually exclusive
final List<NameValuePair> 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()) {
Expand All @@ -379,7 +383,10 @@
}

if (FormEncodingType.MULTIPART == getEncodingType()) {
return normalize(getRequestParameters());
final List<NameValuePair> allParameters = new ArrayList<>();
allParameters.addAll(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
allParameters.addAll(getRequestParameters());
return normalize(allParameters);
}

// for instance a PUT or PATCH request
Expand Down
38 changes: 38 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,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;
Expand Down Expand Up @@ -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<NameValuePair> 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<NameValuePair> 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
*/
Expand Down