Skip to content

MockHttpServletRequest should not require setContent for non-null getInputStream() result [SPR-11764] #16386

@spring-projects-issues

Description

@spring-projects-issues

Gena Makhomed opened SPR-11764 and commented

Yet another filter, and unit test failed with exception

java.lang.IllegalArgumentException: No InputStream specified
	at org.springframework.util.Assert.notNull(Assert.java:112)
	at org.springframework.util.StreamUtils.copy(StreamUtils.java:118)
	at org.springframework.util.StreamUtils.copyToByteArray(StreamUtils.java:56)
	at com.pb.ivrcgate.util.filter.HttpRequestWrapper.<init>(HttpRequestWrapper.java:27)
	at com.pb.ivrcgate.util.filter.HttpDumperFilter.doFilterInternal(HttpDumperFilter.java:25)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:137)
	at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:141)
	at com.pb.ivrcgate.controller.GateControllerTest.badUri(GateControllerTest.java:40)

Filter code fragment:

public class HttpDumperFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        try {
            HttpRequestWrapper request = new HttpRequestWrapper(httpServletRequest);
            HttpResponseWrapper response = new HttpResponseWrapper(httpServletResponse);
            filterChain.doFilter(request, response);
            // ...
        } catch (Exception ex) {
            logger.error("", ex);
            throw ex;
        }
    }
    // ...
}

HttpRequestWrapper code fragment:

public class HttpRequestWrapper extends HttpServletRequestWrapper {

    private final byte[] content;

    public HttpRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        this.content = StreamUtils.copyToByteArray(request.getInputStream()); // line #27
    }
    // ...
}

In compliance with Servlet API and documentation - MockMvc must return object of class ServletInputStream, and can't return null.

For example, tomcat return ServletInputStream object even for GET requests, and never return null from getInputStream() method.

Looks like this is bug in MockMvc.


Affects: 4.0.4

Issue Links:

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions