Skip to content

Commit f33fa53

Browse files
committed
file parameters are back for the result of WebRequest.getParameters() (#836)
1 parent 116e938 commit f33fa53

2 files changed

Lines changed: 41 additions & 9 deletions

File tree

src/main/java/org/htmlunit/WebRequest.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -343,20 +343,25 @@ public void setEncodingType(final FormEncodingType encodingType) {
343343
}
344344

345345
/**
346-
* Retrieves the request parameters used. Similar to the servlet api function
346+
* <p>Retrieves the request parameters used. Similar to the servlet api function
347347
* getParameterMap() this works depending on the request type and collects the
348-
* url parameters and the body stuff.
349-
* The value is also normalized - null is converted to an empty string.
350-
* In contrast to the servlet api this creates a separate KeyValuePair for every
348+
* url parameters and the body stuff.<br>
349+
* The value is also normalized - null is converted to an empty string.</p>
350+
* <p>In contrast to the servlet api this creates a separate KeyValuePair for every
351351
* parameter. This means that pairs with the same name can be part of the list. The
352-
* servlet api will return a string[] as value for the key in this case.
352+
* servlet api will return a string[] as value for the key in this case.<br>
353+
* Additionally this method includes also the uploaded files for multipart post
354+
* requests.</p>
353355
*
354356
* @return the request parameters to use
355357
*/
356358
public List<NameValuePair> getParameters() {
357359
// developer note:
358-
// this has to be in sync with
359-
// org.htmlunit.HttpWebConnection.makeHttpMethod(WebRequest, HttpClientBuilder)
360+
// this has to be in sync with org.htmlunit.HttpWebConnection.makeHttpMethod(WebRequest, HttpClientBuilder)
361+
362+
// developer note:
363+
// the spring org.springframework.test.web.servlet.htmlunitHtmlUnitRequestBuilder uses
364+
// this method and is sensitive to all the details of the current implementation.
360365

361366
if (HttpMethod.POST != getHttpMethod() && HttpMethod.PUT != getHttpMethod()
362367
&& HttpMethod.PATCH != getHttpMethod()) {
@@ -402,9 +407,11 @@ public List<NameValuePair> getParameters() {
402407

403408
if (FormEncodingType.MULTIPART == getEncodingType()) {
404409
final List<NameValuePair> allParameters = new ArrayList<>();
410+
411+
// the servlet api ignores these parameters but to make spring happy we include them
412+
allParameters.addAll(getRequestParameters());
413+
405414
allParameters.addAll(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
406-
// the servlet api ignores the parameters
407-
// allParameters.addAll(getRequestParameters());
408415
return normalize(allParameters);
409416
}
410417

src/test/java/org/htmlunit/WebRequest2Test.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
import javax.servlet.http.HttpServletRequest;
3232
import javax.servlet.http.HttpServletResponse;
3333

34+
import org.apache.commons.fileupload.FileItem;
35+
import org.apache.commons.fileupload.FileUploadBase;
36+
import org.apache.commons.fileupload.FileUploadException;
37+
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
38+
import org.apache.commons.fileupload.servlet.ServletFileUpload;
39+
import org.apache.commons.fileupload.servlet.ServletRequestContext;
3440
import org.apache.commons.lang3.StringUtils;
3541
import org.htmlunit.junit.BrowserParameterizedRunner;
3642
import org.htmlunit.junit.BrowserParameterizedRunner.Default;
@@ -354,6 +360,25 @@ private static void bounceToStatic(final HttpServletRequest req, final HttpServl
354360
private static void bounce(final Writer writer,
355361
final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
356362
writer.write("Parameters: \n");
363+
364+
// different from the servlet api spring also uses the file upload information
365+
try {
366+
// if (ServletFileUpload.isMultipartContent(req)) {
367+
// ignore the post request check
368+
if (FileUploadBase.isMultipartContent(new ServletRequestContext(req))) {
369+
final DiskFileItemFactory factory = new DiskFileItemFactory();
370+
371+
final ServletFileUpload upload = new ServletFileUpload(factory);
372+
final List<FileItem> items = upload.parseRequest(req);
373+
for (final FileItem fileItem : items) {
374+
writer.write(" '" + fileItem.getFieldName() + "': '" + fileItem.getString() + "'\n");
375+
}
376+
}
377+
}
378+
catch (final FileUploadException e) {
379+
throw new IOException(e);
380+
}
381+
357382
// use only getParameterMap() here because we like to have the same behavior
358383
for (final Map.Entry<String, String[]> entry : req.getParameterMap().entrySet()) {
359384
if (entry.getValue() == null) {

0 commit comments

Comments
 (0)