Skip to content

Commit

Permalink
add test for PUT and PATCH (issue #836)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Jul 31, 2024
1 parent f86a869 commit e1224d1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/htmlunit/WebRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public List<NameValuePair> getParameters() {
}

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

Expand Down
64 changes: 52 additions & 12 deletions src/test/java/org/htmlunit/WebRequest2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.htmlunit;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
Expand Down Expand Up @@ -96,8 +97,8 @@ public void setup() throws Exception {
public static Collection<Object[]> data() throws Exception {
final List<Object[]> data = new ArrayList<>();

final HttpMethod[] methods = {HttpMethod.OPTIONS, HttpMethod.GET, /* HttpMethod.HEAD,*/ HttpMethod.POST,
HttpMethod.PUT, HttpMethod.DELETE /* , HttpMethod.TRACE, HttpMethod.PATCH */};
final HttpMethod[] methods = {HttpMethod.OPTIONS, HttpMethod.GET, HttpMethod.HEAD, HttpMethod.POST,
HttpMethod.PUT, HttpMethod.DELETE, /*HttpMethod.TRACE,*/ HttpMethod.PATCH};
final String[] queries = {"", "?a=b", "?a=b&c=d", "?a=", "?a", "?", "?a=b&a=d"};
final FormEncodingType[] encodings =
{FormEncodingType.URL_ENCODED, FormEncodingType.MULTIPART, FormEncodingType.TEXT_PLAIN};
Expand Down Expand Up @@ -197,6 +198,12 @@ public void test() throws Exception {

// calculate expectation from bounce servlet
String expectedContent = ((TextPage) page).getContent();

if (HttpMethod.HEAD.equals(request.getHttpMethod())) {
assertEquals(0, expectedContent.length());
expectedContent = InspectServlet.PARAMETERS_;
}

assertTrue(expectedContent.startsWith("Parameters: \n"));
expectedContent = expectedContent.substring("Parameters: \n".length()).trim();

Expand Down Expand Up @@ -233,6 +240,20 @@ public void test() throws Exception {
*/
public static class InspectServlet extends HttpServlet {

/** Hack. */
public static String PARAMETERS_;

@Override
protected void service(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
if ("patch".equalsIgnoreCase(req.getMethod())) {
doPatch(req, resp);
return;
}

super.service(req, resp);
}

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
Expand All @@ -242,7 +263,8 @@ protected void doGet(final HttpServletRequest req, final HttpServletResponse res
@Override
protected void doHead(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
bounce(req, resp);
// head does not deliver a body
bounceToStatic(req, resp);
}

@Override
Expand Down Expand Up @@ -275,17 +297,35 @@ protected void doTrace(final HttpServletRequest req, final HttpServletResponse r
bounce(req, resp);
}

protected void doPatch(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
bounce(req, resp);
}

private static void bounce(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
try (Writer writer = resp.getWriter()) {
writer.write("Parameters: \n");
for (final String key : req.getParameterMap().keySet()) {
final String val = req.getParameter(key);
if (val == null) {
writer.write(" '" + key + "': '-null-'\n");
}
else {
writer.write(" '" + key + "': '" + val + "'\n");
}
bounce(writer, req, resp);
}
}

private static void bounceToStatic(final HttpServletRequest req, final HttpServletResponse resp)
throws IOException {
try (StringWriter writer = new StringWriter()) {
bounce(writer, req, resp);
PARAMETERS_ = writer.toString();
}
}

private static void bounce(final Writer writer,
final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
writer.write("Parameters: \n");
for (final String key : req.getParameterMap().keySet()) {
final String val = req.getParameter(key);
if (val == null) {
writer.write(" '" + key + "': '-null-'\n");
}
else {
writer.write(" '" + key + "': '" + val + "'\n");
}
}
}
Expand Down

0 comments on commit e1224d1

Please sign in to comment.