Skip to content

Commit 5fd73f0

Browse files
committed
Fix MockHttpServletRequest InputStream for empty body
Prior to this commit, a recent change applied in gh-29125 changed the behavior of `MockHttpServletRequest` instances. In case of an empty request body, the returned `InputStream` would be static and could not be reused across requests. This could result in `java.io.IOException: Stream closed` exceptions if a previous request was read. This commit ensures that a new instance of an empty stream is returned for each request instance. Fixes gh-29901
1 parent 16937c7 commit 5fd73f0

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -101,9 +101,6 @@ public class MockHttpServletRequest implements HttpServletRequest {
101101

102102
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
103103

104-
private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM =
105-
new DelegatingServletInputStream(InputStream.nullInputStream());
106-
107104
private static final BufferedReader EMPTY_BUFFERED_READER =
108105
new BufferedReader(new StringReader(""));
109106

@@ -513,12 +510,12 @@ public ServletInputStream getInputStream() {
513510
}
514511
else if (this.reader != null) {
515512
throw new IllegalStateException(
516-
"Cannot call getInputStream() after getReader() has already been called for the current request") ;
513+
"Cannot call getInputStream() after getReader() has already been called for the current request");
517514
}
518515

519516
this.inputStream = (this.content != null ?
520517
new DelegatingServletInputStream(new ByteArrayInputStream(this.content)) :
521-
EMPTY_SERVLET_INPUT_STREAM);
518+
new DelegatingServletInputStream(InputStream.nullInputStream()));
522519
return this.inputStream;
523520
}
524521

spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ void setContentAndGetInputStream() throws IOException {
8282
assertThat(StreamUtils.copyToString(request.getInputStream(), Charset.defaultCharset())).isEqualTo("body");
8383
}
8484

85+
@Test
86+
void readEmptyInputStreamWorksAcrossRequests() throws IOException {
87+
MockHttpServletRequest firstRequest = new MockHttpServletRequest();
88+
firstRequest.getInputStream().readAllBytes();
89+
firstRequest.getInputStream().close();
90+
91+
MockHttpServletRequest secondRequest = new MockHttpServletRequest();
92+
secondRequest.getInputStream().readAllBytes();
93+
secondRequest.getInputStream().close();
94+
}
95+
8596
@Test
8697
void setContentAndGetReader() throws IOException {
8798
byte[] bytes = "body".getBytes(Charset.defaultCharset());

0 commit comments

Comments
 (0)