Bug in both spring boot 3.4.0-M1 and 3.3.2 (spring-test 6.2.0-M6 and 6.1.11).
When submitting a form in a real browser the query string is included in the parameters.
This allow me to write @PostMapping(params = "form=1")
to detect which form was submitted.
It works in production, but not a unittest.
When using HtmlUnit in a test annotated with @SpringBootTest
this fails.
HtmlUnitRequestBuilder.params
reads this.webRequest.getParameters()
, but WebRequest.getParameters
ignores query string parameters for post.
I've tested this in spring boot 3.4.0-M1 as I hoped the switch to a newer htmlunit version would have fixed it. The bug also exists in 3.3.2 and probably all previous versions.
I've put together a minimal testcase here: https://github.com/Crydust/testcasehtmlunit
Comment From: snicoll
I had a look to the sample and I am not sure why this was reported here. By your own admission, HtmlUnit ignores the request parameters for a POST.
Can you clarify?
Comment From: Crydust
Good question and it took me a moment to find a way to prove htmlunit is innocent.
I think the problem is in org.springframework.test.web.servlet.htmlunit.HtmlUnitRequestBuilder
.
It works when I use htmlunit in a standalone test without @SpringBootTest
.
To show you what I mean you'd need to run the spring boot project here (don't forget to pull latest commit because I fixed a problem with sessions): https://github.com/Crydust/testcasehtmlunit
Then run the tests from this project: https://github.com/Crydust/testcasehtmlunit2
Comment From: rstoyanchev
@Crydust, testcasehtmlunit2 connects to a live server. All that shows is that Servlet containers prepare Servlet request parameters as expected by the Servlet spec. In the MockMvc HtmlUnit integration, we only aim to adapt HtmlUnit's WebRequest
to MockHttpServletRequest
and use the exposed getParameters
API for access to request parameters without the need to parse the body or the query ourselves.
Some time ago, we had a long back and forth under #28240 on a similar case. As a result of that HtmlUnit exposed WebRequest.getParameters()
and we simplified our code in dd1e6b94125794aec412388682c3ff91217c08e3, and that includes not having to parse the query string any more.
Please, reach out to the HtmlUnit project to ask why they don't parse the query string in this particular case.
Comment From: rbri
Will have a look at this and hopefully we find together a solution... Thanks for nailing down the problem.