Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-11764

MockHttpServletRequest should not require setContent for non-null getInputStream() result

This issue belongs to an archived project. You can view it, but you can't modify it. Learn more

    XMLWordPrintable

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Complete
    • 4.0.4
    • 4.1 RC1
    • Test
    • true

    Description

      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.

      Attachments

        Issue Links

          Activity

            People

              rstoya05-aop Rossen Stoyanchev
              csdoc Gena Makhomed
              Archiver:
              tmarshall Trevor Marshall
              Spring Issues Spring Issues

              Dates

                Created:
                Updated:
                Resolved:
                Archived:
                4 years, 20 weeks, 2 days ago