-
Notifications
You must be signed in to change notification settings - Fork 37.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MockHttpServletRequest should not require setContent for non-null getInputStream() result [SPR-11764] #16386
Comments
Sam Brannen commented This is potentially related to #16382. |
Rossen Stoyanchev commented The Servlet API documentation doesn't seem very clear on that. The Javadoc has no mention on what should be returned if there is no body (see here). This is a long standing intentional behavior of MockHttpServletRequest so it's certainly not a bug. It is also quite reasonable IMO to return null if there is no body. You will need to update you wrapper to guard against null. |
Gena Makhomed commented See javadoc for other methods, for example, ServletRequest.getCharacterEncoding()
Documentation of getInputStream() very clean about return value:
So, getInputStream() method not allowed to return null in any case, even in case of one-byte or zero-byte request body.
Incompatibility with Servlet api - is bug, in any case.
No reason at all. Servlet container does not read request body before passing it to servlet, and Servlet API not allow to return null instead ServletInputStream object - And you can see javadoc for any other method: ServletRequest P.S. Do you know about SpecialCase pattern from Martin Fowler book? This pattern already used in many places in Spring Framework and Java EE API. Other name of this pattern is Null Object pattern. |
Rossen Stoyanchev commented Alright personally I prefer to see it explicitly mentioned either way but you have a point about how null return value is documented in the Servlet API. I am re-opening to consider a fix. |
Juergen Hoeller commented There is a subtle additional notion: We're returning -1 from getContentLength() if no content has been set, indicating "unknown length" (as per the Servlet API javadoc). We're only doing that if no content has been set (the default). Preserving that, we can't simply use an empty byte array as default content, which would otherwise be a worthwhile option. Instead, we'd have to locally return an empty ServletInputStream in the no-content case. Note that simply calling setContent(new byte[0]) on a custom MockHttpServletRequest instance also solves the problem, since that will lead to an empty ServletInputStream getting returned... Arguably, the current impl simply requires a setContent call to happen (even if just with empty content) if you'd like to access an InputStream or Reader - and what's being asked for here is easier setup for the no-content case, with no need to call setContent. Juergen |
Rossen Stoyanchev commented
This is something we can do in MockHttpServletRequestBuilder so it becomes the default for MockMvc. That said returning an empty ServletInputStream seems a safe option as well. It is unlikely that existing tests depend on getInputStream returning null (asserts on the request would be unusual). |
Rossen Stoyanchev commented Commit c269d2. |
Gena Makhomed opened SPR-11764 and commented
Yet another filter, and unit test failed with exception
Filter code fragment:
HttpRequestWrapper
code fragment: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:
The text was updated successfully, but these errors were encountered: