-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Description
Sam Brannen opened SPR-14252 and commented
Status Quo
Spring already has a mock for javax.servlet.http.Part
in the test source folder in spring-web
; however, there is currently no counterpart in the spring-test
module for public consumption.
Further Analysis
As pointed out by a user on Stack Overflow, it is currently not possible to mock Part
on your own due to the following:
In
RequestPartServletServerHttpRequest
I run into this:this.headers = this.multipartRequest.getMultipartHeaders(this.partName); if (this.headers == null) { throw new MissingServletRequestPartException(partName); }
It is therefore likely that we will need to modify MockMultipartHttpServletRequest
as well. In particular, the contract for Spring's MultipartHttpServletRequest.getMultipartHeaders()
states the following.
Return the headers associated with the specified part of the multipart request.
If the underlying implementation supports access to headers, then all headers are returned. Otherwise, the returned headers will include a 'Content-Type' header at the very least.
However, MockMultipartHttpServletRequest.getMultipartHeaders()
in fact returns null
if the content type for the part cannot be retrieved. This is because Spring's MultipartRequest.getMultipartContentType()
specifies that it will return the associated content type, or null
if not defined. Thus, there is definitely a conflict between these two contracts. Most importantly, MockMultipartHttpServletRequest.getMultipartContentType()
only supports look-ups for MultipartFile
and disregards any Part
that might be present.
Deliverables
- Introduce a mock for
javax.servlet.http.Part
inspring-test
based on the existingorg.springframework.mock.web.test.MockPart
inspring-web
, potentially implementingwrite()
anddelete()
as well.
Reference URL: http://stackoverflow.com/questions/37000457/setting-request-parts-in-spring-test-mvc
Issue Links:
- Support for MockPart in Spring MVC Test [SPR-14253] #18826 Support for MockPart in Spring MVC Test ("is depended on by")
- Backport s/s/m/j/o/s/m/w/MockPart.java from 5.x to 4.3.x [SPR-15854] #20409 Backport s/s/m/j/o/s/m/w/MockPart.java from 5.x to 4.3.x
Referenced from: commits c8f98ec
1 votes, 4 watchers