Description
Av Pinzur opened SPR-16505 and commented
A de facto rule of thumb for the Servlet paradigm is that a ServletRequest
's body content can only be read once. This implies at least a couple limitations:
- The content retrieval methods (
getReader
andgetInputStream
) cannot be used in combination. (See MockHttpServletRequest shouldn't allow calls to both getReader and getInputStream [SPR-16499] #21042.) - Each content retrieval method (
getReader
orgetInputStream
) will read the body only once.
This latter limitation is the subject of this ticket. Although unlike #1, this behavior is not explicitly specified in the interface documentation, major Servlet implementations appear to concur in returning the same object reference for successive calls to either getReader
or getInputStream
. This means that it's effectively a bug for application code to attempt to read the body contents twice from two such separate calls.
However, the current implementation of MockHttpServletRequest
constructs a fresh object each time one of these methods is called. Revising this implementation to retain the returned reader or stream reference for successive calls will better reflect real-world implementations and enhance the framework's value by allowing developers to catch one more class of bugs prior to deploying to an actual container.
Affects: 4.3.14, 5.0.3
Issue Links:
- MockHttpServletRequest shouldn't allow calls to both getReader and getInputStream [SPR-16499] #21042 MockHttpServletRequest shouldn't allow calls to both getReader and getInputStream
- MockHttpServletRequest doesn't reset InputStream/Reader on setContent [SPR-17373] #21906 MockHttpServletRequest doesn't reset InputStream/Reader on setContent
Referenced from: pull request #1689, and commits 3fc8ec4
0 votes, 5 watchers