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

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

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 4.0.4
    • Fix Version/s: 4.1 RC1
    • Component/s: Test
    • Labels:
    • Last commented by a User:
      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

              Assignee:
              rstoya05-aop Rossen Stoyanchev
              Reporter:
              csdoc Gena Makhomed
              Last updater:
              Spring Issues Spring Issues
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since last comment:
                3 years, 1 week, 4 days ago