First I want to say thank you for all the work that you are doing!
I am not pretty sure if this is an issue or something that I am doing wrong on my side.
I am using @HttpExchange
+ @PostEchange
in addition to @RequestPart
for defining HTTP endpoints. When running the application and testing calling the endpoint manually everything works as expected but when I am trying to create a test for the application the multipart
functionality stops working.
@Autowired
private MultipartfileExampleApplication.OperationsClient operationsClient;
/*
* =====>> This is not working
*/
@Test
void file() {
var multipartFile = new MockMultipartFile("file", "hello.txt", "text/plain", "Hello World".getBytes());
var message = operationsClient.multipart(multipartFile);
Assertions.assertEquals("Hey are here with a multipart request", message);
}
Reproducible repo
Run the tests in the following repo
https://github.com/Dam14n/multipartfile-example
Research
Again I am considering that what I did was expected. So, there is a real issue that can be solved but If I did something wrong the next suggestion does not make any sense.
In MockMvcClientHttpRequestFactory
the request is always created in this way:
MockHttpServletResponse servletResponse = this.mockMvc
.perform(request(httpMethod, uri).content(requestBody).headers(requestHeaders))
.andReturn()
.getResponse();
I also debugged the internal logic and my findings were that this never take into consideration the files/parts configuration of the request.
So, it would be good to have something like (pseudo-code)
var request = request(httpMethod, uri);
if (request is multipart){
request = multipart(uri).file(files coming from somewhere??)
}
var finalRequest = request.content(requestBody).headers(requestHeaders)
MockHttpServletResponse servletResponse = this.mockMvc
.perform(finalRequest)
.andReturn()
.getResponse();
FYI: This was just a suggestion based on my findings which can be 100% a wrong suggestion due to I dont have 100% knowledge of every class/functionality inside Spring boot.
Again thank you Spring team!
Comment From: rstoyanchev
The link to the example repo does not work, please correct it.
Comment From: Dam14n
Hi @rstoyanchev I didnt realize that I created it as a prívate repository. I just make it public. Sorry