Skip to content

Introduce MockPart in spring-test [SPR-14252] #18825

@spring-projects-issues

Description

@spring-projects-issues

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 in spring-test based on the existing org.springframework.mock.web.test.MockPart in spring-web, potentially implementing write() and delete() as well.

Reference URL: http://stackoverflow.com/questions/37000457/setting-request-parts-in-spring-test-mvc

Issue Links:

Referenced from: commits c8f98ec

1 votes, 4 watchers

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions