-
-
Notifications
You must be signed in to change notification settings - Fork 181
Description
Okay I finally found some time to look into this.
You're indeed correct that the error seems to stem from the MockMvcWebClientBuilder
from Spring. To be honest I'm not to familiar with the MockMvcWebClientBuilder
. The problem seems to be what we're seeing here:
The FormData / RequestBody is not correctly put in to the @RequestParam
parameters in the Controller.
Therefore the request fails with 400 Bad Request
.
I'm having some difficulties debugging the Spring test code ... however I think the issue comes from HtmlUnitRequestBuilder.content()
:
This one takes the request body without any escaping and passes it to the controller:
private void content(MockHttpServletRequest request, Charset charset) {
String requestBody = this.webRequest.getRequestBody();
if (requestBody == null) {
return;
}
request.setContent(requestBody.getBytes(charset)); //requestBody = 'subject test message test'
//requestBody should be: 'subject=subject+test&message=message+test' since it's form encoded.
}
To get to the results I've transformed the project to use SpringBoot since it was easier for me to find out what's actually happening and where the error potentially is. I hope that it's running with gradle ... since I needed to convert it to a maven project because of my gradle issue 👼
The project is here: https://github.com/twendelmuth/spring-test-htmlunit
Added tests that should illustrate what's wrong: https://github.com/twendelmuth/spring-test-htmlunit/tree/master/src/test/java/spring/test/htmlunit/controller
And more specific: https://github.com/twendelmuth/spring-test-htmlunit/blob/master/src/test/java/spring/test/htmlunit/controller/WelcomeControllerMockMvc.java#L37 - this is what's happening in the end in your test.
I hope this helps :-)
Originally posted by @twendelmuth in #223 (comment)