spring-projects / spring-projects/spring-framework

Issue/Question: RequestPart and HttpExchange not working together in Tests

Open
#35,416 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: web status: feedback-provided status: waiting-for-triage
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with MockMvcClientHttpRequestFactory and run the tests in the linked multipartfile-example repository. Trace how the @HttpExchange client request is built and how MockMultipartFile parts are represented, then verify the request reaches the endpoint with its multipart data when tested through MockMvc.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.