Skip to content

Commit

Permalink
WebRequest.getParameters(): code cleanup and sync with HttpWebConnect…
Browse files Browse the repository at this point in the history
…ion (#836)
  • Loading branch information
rbri committed Aug 6, 2024
1 parent 5a77acc commit 96566d1
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 71 deletions.
37 changes: 17 additions & 20 deletions src/main/java/org/htmlunit/HttpWebConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,19 +293,20 @@ private HttpUriRequest makeHttpMethod(final WebRequest webRequest, final HttpCli
final HttpRequestBase httpMethod = buildHttpMethod(webRequest.getHttpMethod(), uri);
setProxy(httpMethod, webRequest);

// POST, PUT and PATCH
if (httpMethod instanceof HttpEntityEnclosingRequest) {
// developer note:
// this has to be in sync with org.htmlunit.WebRequest.getRequestParameters()
// developer note:
// this has to be in sync with org.htmlunit.WebRequest.getRequestParameters()

// POST, PUT, PATCH, DELETE, OPTIONS
if ((httpMethod instanceof HttpEntityEnclosingRequest)
&& (httpMethod instanceof HttpPost
|| httpMethod instanceof HttpPut
|| httpMethod instanceof HttpPatch
|| httpMethod instanceof org.htmlunit.httpclient.HttpDelete
|| httpMethod instanceof org.htmlunit.httpclient.HttpOptions)) {

final HttpEntityEnclosingRequest method = (HttpEntityEnclosingRequest) httpMethod;

if (webRequest.getEncodingType() == FormEncodingType.URL_ENCODED
&& (method instanceof HttpPost
|| method instanceof HttpPatch
|| method instanceof HttpPut
|| method instanceof org.htmlunit.httpclient.HttpDelete
|| method instanceof org.htmlunit.httpclient.HttpOptions)) {
if (FormEncodingType.URL_ENCODED == webRequest.getEncodingType()) {
if (webRequest.getRequestBody() == null) {
final List<NameValuePair> pairs = webRequest.getRequestParameters();
final String query = HttpUtils.toQueryFormFields(pairs, charset);
Expand All @@ -329,12 +330,7 @@ private HttpUriRequest makeHttpMethod(final WebRequest webRequest, final HttpCli
method.setEntity(urlEncodedEntity);
}
}
else if (webRequest.getEncodingType() == FormEncodingType.TEXT_PLAIN
&& (method instanceof HttpPost
|| method instanceof HttpPatch
|| method instanceof HttpPut
|| method instanceof org.htmlunit.httpclient.HttpDelete
|| method instanceof org.htmlunit.httpclient.HttpOptions)) {
else if (FormEncodingType.TEXT_PLAIN == webRequest.getEncodingType()) {
if (webRequest.getRequestBody() == null) {
final StringBuilder body = new StringBuilder();
for (final NameValuePair pair : webRequest.getRequestParameters()) {
Expand Down Expand Up @@ -370,17 +366,18 @@ else if (FormEncodingType.MULTIPART == webRequest.getEncodingType()) {
}
method.setEntity(builder.build());
}
else { // for instance a PATCH request
else {
// for instance a PATCH request
final String body = webRequest.getRequestBody();
if (body != null) {
method.setEntity(new StringEntity(body, charset));
}
}
}
else {
// this is the case for GET as well as TRACE, DELETE, OPTIONS and HEAD
if (!webRequest.getRequestParameters().isEmpty()) {
final List<NameValuePair> pairs = webRequest.getRequestParameters();
// GET, TRACE, HEAD
final List<NameValuePair> pairs = webRequest.getRequestParameters();
if (!pairs.isEmpty()) {
final String query = HttpUtils.toQueryFormFields(pairs, charset);
uri = UrlUtils.toURI(url, query);
httpMethod.setURI(uri);
Expand Down
110 changes: 62 additions & 48 deletions src/main/java/org/htmlunit/WebRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,70 +363,84 @@ public List<NameValuePair> getParameters() {
// the spring org.springframework.test.web.servlet.htmlunitHtmlUnitRequestBuilder uses
// this method and is sensitive to all the details of the current implementation.

if (HttpMethod.POST != getHttpMethod() && HttpMethod.PUT != getHttpMethod()
&& HttpMethod.PATCH != getHttpMethod()) {

final List<NameValuePair> allParameters = new ArrayList<>();
allParameters.addAll(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));

// a bit strange but in case of GET, TRACE, DELETE, OPTIONS and HEAD
// HttpWebConnection.makeHttpMethod() moves the parameters up to the query
// to reflect this we have to take the parameters into account even if this
// looks wrong for GET requests
if (!getRequestParameters().isEmpty()) {
return normalize(getRequestParameters());
}

return normalize(allParameters);
}

if (getEncodingType() == FormEncodingType.URL_ENCODED
&& (HttpMethod.POST == getHttpMethod() || HttpMethod.PUT == getHttpMethod())) {
if (getRequestBody() == null) {
// POST, PUT, PATCH, DELETE, OPTIONS
final HttpMethod httpMethod = getHttpMethod();
if (httpMethod == HttpMethod.POST
|| httpMethod == HttpMethod.PUT
|| httpMethod == HttpMethod.PATCH
|| httpMethod == HttpMethod.DELETE
|| httpMethod == HttpMethod.OPTIONS) {

if (FormEncodingType.URL_ENCODED == getEncodingType()) {
if (getRequestBody() == null) {
final List<NameValuePair> allParameters = new ArrayList<>();
allParameters.addAll(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));

// for PATCH/DELETE/OPTIONS request browsers are sending the body
// but the servlet API does not get it
if (httpMethod != HttpMethod.PATCH
&& httpMethod != HttpMethod.DELETE
&& httpMethod != HttpMethod.OPTIONS) {
allParameters.addAll(getRequestParameters());
}

return normalize(allParameters);
}

// getRequestParameters and getRequestBody are mutually exclusive
final List<NameValuePair> allParameters = new ArrayList<>();
allParameters.addAll(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
allParameters.addAll(getRequestParameters());

// for PATCH/DELETE/OPTIONS request browsers are sending the body
// but the servlet API does not get it
if (httpMethod != HttpMethod.PATCH
&& httpMethod != HttpMethod.DELETE
&& httpMethod != HttpMethod.OPTIONS) {
allParameters.addAll(HttpUtils.parseUrlQuery(getRequestBody(), getCharset()));
}

return normalize(allParameters);
}

// 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 (FormEncodingType.TEXT_PLAIN == getEncodingType()) {
if (getRequestBody() == null) {
final List<NameValuePair> allParameters = new ArrayList<>();
allParameters.addAll(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));

// the servlet api ignores the parameters
// allParameters.addAll(getRequestParameters());

return normalize(allParameters);
}

if (getEncodingType() == FormEncodingType.TEXT_PLAIN
&& (HttpMethod.POST == getHttpMethod() || HttpMethod.PUT == getHttpMethod())) {
if (getRequestBody() == null) {
return normalize(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
}

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

allParameters.addAll(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
// the servlet api ignores the parameters
// allParameters.addAll(getRequestParameters());

// the servlet api ignores these parameters but to make spring happy we include them
allParameters.addAll(getRequestParameters());

return normalize(allParameters);
}

return normalize(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
}

if ((getEncodingType() == FormEncodingType.URL_ENCODED || getEncodingType() == FormEncodingType.TEXT_PLAIN)
&& HttpMethod.PATCH == getHttpMethod()) {
return normalize(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
return Collections.emptyList();
}

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

allParameters.addAll(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));

// the servlet api ignores these parameters but to make spring happy we include them
allParameters.addAll(getRequestParameters());
// GET, TRACE, HEAD

return normalize(allParameters);
// a bit strange but
// HttpWebConnection.makeHttpMethod() moves the parameters up to the query
// to reflect this we have to take the parameters into account even if this
// looks wrong for GET requests
if (!getRequestParameters().isEmpty()) {
return normalize(getRequestParameters());
}

// for instance a PUT or PATCH request
return Collections.emptyList();
return normalize(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
}

private static List<NameValuePair> normalize(final List<NameValuePair> pairs) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/htmlunit/WebRequest2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static Collection<Object[]> data() throws Exception {
{null, emptyParameters, oneParameter, emptyValueParameter, sameAsInQueryParameter,
sameKeyAsInQueryParameter, sameKeyDifferentValuesParameter};

final String[] bodies = {"", "a=b", "a=b&c=d", "a=", "a", "a=b&a=d"};
final String[] bodies = {"null", "", "a=b", "a=b&c=d", "a=", "a", "a=b&a=d"};

for (final HttpMethod method : methods) {
for (final String query : queries) {
Expand Down Expand Up @@ -198,7 +198,7 @@ public void test() throws Exception {
request.setHttpMethod(httpMethod_);
request.setEncodingType(encoding_);

if (body_ == null) {
if ("null".equals(body_)) {
if (parameter_ != null) {
request.setRequestParameters(parameter_.getPairs());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ private void testSendBody(final String method, final boolean bodyIncluded) throw
+ " body.append('x', 'body');\n"
+ " xhr.send(body);\n"
+ " }\n"
+ "</script></head><body onload='test()'></body></html>";
+ "</script></head>\n"
+ "<body onload='test()'></body></html>";

final MockWebConnection mockWebConnection = getMockWebConnection();
mockWebConnection.setResponse(WebTestCase.URL_FIRST, html);
Expand Down

0 comments on commit 96566d1

Please sign in to comment.