Skip to content

Commit 3b93b16

Browse files
committed
Add QUERY test with form urlencoded body
1 parent 05d0e37 commit 3b93b16

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

test/jakarta/servlet/http/TestHttpServlet.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,49 @@ public boolean isResponseBodyOK() {
303303
}
304304

305305

306+
@Test
307+
public void testQueryParameters() throws Exception {
308+
Tomcat tomcat = getTomcatInstance();
309+
310+
// Configure connector to parse body parameters for QUERY
311+
tomcat.getConnector().setParseBodyMethods("POST,QUERY");
312+
313+
// No file system docBase required
314+
StandardContext ctx = (StandardContext) getProgrammaticRootContext();
315+
316+
// Map the test Servlet
317+
ParamsServlet servlet = new ParamsServlet();
318+
Tomcat.addServlet(ctx, "servlet", servlet);
319+
ctx.addServletMappingDecoded("/", "servlet");
320+
321+
tomcat.start();
322+
323+
SimpleHttpClient client = new SimpleHttpClient() {
324+
@Override
325+
public boolean isResponseBodyOK() {
326+
return true;
327+
}
328+
};
329+
client.setPort(getPort());
330+
client.setRequest(new String[] {
331+
"QUERY /?baz=123 HTTP/1.1" + CRLF +
332+
"Host: localhost:" + getPort() + CRLF +
333+
"Content-Type: application/x-www-form-urlencoded" + CRLF +
334+
"Content-Length: 15" + CRLF +
335+
"Connection: close" + CRLF +
336+
CRLF +
337+
"foo=abc&bar=xyz"
338+
});
339+
client.connect();
340+
client.sendRequest();
341+
client.readResponse(true);
342+
343+
Assert.assertTrue(client.isResponse200());
344+
Assert.assertEquals("abc,xyz,123", client.getResponseBody());
345+
}
346+
347+
348+
306349

307350
private void doTestDoOptions(Servlet servlet, String expectedAllow) throws Exception {
308351
Tomcat tomcat = getTomcatInstance();
@@ -673,5 +716,19 @@ protected void doQuery(HttpServletRequest req, HttpServletResponse resp) throws
673716
doGet(req, resp);
674717
}
675718
}
719+
720+
721+
private static class ParamsServlet extends HttpServlet {
722+
723+
private static final long serialVersionUID = 1L;
724+
725+
@Override
726+
protected void doQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
727+
resp.setContentType("text/plain");
728+
resp.setCharacterEncoding("UTF-8");
729+
PrintWriter pw = resp.getWriter();
730+
pw.print(req.getParameter("foo") + "," + req.getParameter("bar") + "," + req.getParameter("baz"));
731+
}
732+
}
676733
}
677734

0 commit comments

Comments
 (0)