Stephen Liang opened SPR-13979 and commented
Overview
The params method in HtmlUnitRequestBuilder does not check if a parameter contains a File payload, which results in a File input box to get its value (the path to the file) and submit it as a key/value rather than the payload being uploaded as multipart form data.
For Reference
https://github.com/spring-projects/spring-framework/blob/master/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java#L361
webRequest.getRequestParameters() is a KeyDataPair which contains a field called fileObject_. This field should be recognized and used in place of the value_ field which is the name of the field, rather than the content of the file (when the parameter is has a file).
To Reproduce
- Create a simple file upload form, e.g.:
<html><head></head><body>
<form enctype='multipart/form-data' action='"/upload"' method='POST'>
<input type='file' name='image' />
<input type='submit' id='clickMe'>
</form>
</body>
</html>
- Create a html unit test that sends the file path to the "file" input type
- Click on the submit button
Expected Result: The file contents are read and sent as form data Actual Result: The file's name is sent as a key/value pair
Affects: 4.2 GA
0 votes, 5 watchers
Comment From: spring-projects-issues
Rossen Stoyanchev commented
Indeed for file uploads we'd have to create a MockMultipartHttpServletRequest in HtmlUnitRequestBuilder.
Comment From: spring-projects-issues
Rob Winch commented
Stephen Liang Thanks for the report! Any chance you would be interested in contributing a Pull Request?
Comment From: djkirby
Any update on this? Would be nice to have this functionality 🤞
Comment From: sbrannen
I've added the ideal-for-contribution label so that someone from the community can pick this one up.
Comment From: govi20
Would like to take it up.
Comment From: sbrannen
@govi20, great!
Feel free to submit a PR.
If for some reason you cannot get to it, please post back here so that we know you're not actively working on it.
Comment From: kobaeugenea
Hi. Created PR for this issue https://github.com/spring-projects/spring-framework/pull/24926
Comment From: sbrannen
Superseded by #24926