Skip to content

MockHttpServletRequest.getReader, getInputStream should each return the same object on repeat calls [SPR-16505] #21048

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

  1. The content retrieval methods (getReader and getInputStream) cannot be used in combination. (See MockHttpServletRequest shouldn't allow calls to both getReader and getInputStream [SPR-16499] #21042.)
  2. Each content retrieval method (getReader or getInputStream) 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:

Referenced from: pull request #1689, and commits 3fc8ec4

0 votes, 5 watchers

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions