-
Notifications
You must be signed in to change notification settings - Fork 38.5k
MockHttpServletRequest InputStream has been made static in gh-29125 #29901
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
Comments
After looking into the behavior here, it looks like this change fixed an actual issue. This aligns In light of that, I'm closing this issue as this works as expected. Thanks for your report! |
Hi ok. That might be, but could you explain how I'm supposed align with the expected behavior if I'm testing something like this:
The 2nd tests fails with the mentioned
Maybe you could tell me how I'm supposed to do this differently if every Best regards |
Sorry, I initially tried to replicate the static behavior by using different requests and it worked fine; obviously my unit test was flawed. Thanks to your code snippet I managed to reproduce the issue. I'm reopening this and re-scheduling this for 6.0.5. |
Ah ok, thank you. I just uploaded a working (broken) test snippet. Maybe It's of use: |
Affects: 6.0.4
Hi,
since the upgrade to 6.x I have some MockMvc Tests that fail if the content ist
null
. This also happens for Spring Boot Health Actuators.The tests fail with an HttpMessageNotReadableException caused by a
java.io.IOException: Stream closed
I went deep in to the debugger to find the reason:
Pull-Request spring-projects/spring-data-mongodb#4160 replaced
StreamUtils.emptyInput()
withInputStream.nullInputStream()
forprivate static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM = new DelegatingServletInputStream(InputStream.nullInputStream());
see: 0770d86#diff-1a707902c32bc4ce99156e838462185a17d27d3a8c686a879894597981563124L104
EMPTY_SERVLET_INPUT_STREAM
is used every time the content of a call is null:spring-framework/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java
Line 519 in e1010a1
The Problem is, that the old
StreamUtils.emptyInput()
was a simplenew ByteArrayInputStream(new byte[0]);
which could be read and closed multiple times. The newInputStream.nullInputStream()
handles more like a normal stream and can't be read again if closed.I think the problem result from the fact that
public ServletInputStream getInputStream()
uses the static EMPTY_SERVLET_INPUT_STREAM (every time the same instance) instead of callingnew DelegatingServletInputStream(InputStream.nullInputStream());
I don't know the logic behind the caching and context creation but a fix could be in the MockHttpServletRequest getInputStream():
There might be some more side effects that I don't know off.
Best regards
Daniel
The text was updated successfully, but these errors were encountered: