Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required request part 'file' is not present error with MockMvc multipart tests #26265

Closed
sudhirtumati opened this issue Dec 13, 2020 · 5 comments
Labels
status: duplicate A duplicate of another issue

Comments

@sudhirtumati
Copy link

Spring boot version: 2.4.1

Test:

@Test
@WithMockUser(roles = "APP_OWNER")
void given_application_owner_then_applicationUpload_should_throw_403() throws Exception {
	MockMultipartFile file = new MockMultipartFile("file", "some content".getBytes());
	mockMvc.perform(
			multipart("/applications/actions/upload").file(file).header("Content-Type", "multipart/form-data"))
			.andExpect(status().isForbidden());
}

Controller:

@PostMapping(value = "/actions/upload",
		consumes = { MediaType.MULTIPART_FORM_DATA_VALUE, "multipart/form-data;charset=UTF-8" })
@RolesAllowed("ADMIN")
public List<ApplicationUploadStatusDTO> uploadApplications(@RequestParam("file") MultipartFile file) {
	// Do something here
}

Expected result: 403 error
Actual result: 400

MockHttpServletResponse:
           Status = 400
    Error message = Required request part 'file' is not present
          Headers = [X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

Same code is working as expected with spring-boot 2.4.0

@wilkinsona
Copy link
Member

@sudhirtumati Thanks for the report. To help us to identify the cause of the change in behaviour, could you please provide a complete yet minimal sample that reproduces the problem? You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

@sudhirtumati
Copy link
Author

@wilkinsona Sample application is available at https://github.com/sudhirtumati/spring_boot_issue_24483_sample

main branch - uses spring boot 2.4.0 and test pass successfully
feature/upgrade-spring-boot-version branch uses spring boot 2.4.1 and test fails

@wilkinsona
Copy link
Member

Thanks for the sample. I've reproduced the problem and it appears to be caused by the changes made for #26166. We'll transfer this issue to the Framework team so that they can take a look.

@bclozel bclozel transferred this issue from spring-projects/spring-boot Dec 14, 2020
@bclozel
Copy link
Member

bclozel commented Dec 14, 2020

I’m closing this as a duplicate of #26261

@bclozel bclozel closed this as completed Dec 14, 2020
@bclozel bclozel added the status: duplicate A duplicate of another issue label Dec 14, 2020
@rstoyanchev
Copy link
Contributor

Yes it is a duplicate of #26261 indeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants