Skip to content

Commit 5e08a88

Browse files
committed
Upgrade Servlet mock classes to Servlet 6.1
This commit upgrades our Mock Servlet classes for Servlet 6.1 support: * the read/write `ByteBuffer` variants for `ServletInputStream` and `ServletOutputStream` were not added as the default implementation matches well the testing use case. * Implement the session accessor with a simple lambda. Our mocks do not simulate the scheduling of request/response processing on different threads. * Ensure that the response content length can only be written before the response is committed. Calling those methods after commit is a no-op, per specification. Closes gh-33749
1 parent 3b65506 commit 5e08a88

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,10 @@ public String getContentAsString(Charset fallbackCharset) throws UnsupportedEnco
319319

320320
@Override
321321
public void setContentLength(int contentLength) {
322-
this.contentLength = contentLength;
323-
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true);
322+
if (!this.committed) {
323+
this.contentLength = contentLength;
324+
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true);
325+
}
324326
}
325327

326328
/**
@@ -334,8 +336,10 @@ public int getContentLength() {
334336

335337
@Override
336338
public void setContentLengthLong(long contentLength) {
337-
this.contentLength = contentLength;
338-
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true);
339+
if (!this.committed) {
340+
this.contentLength = contentLength;
341+
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true);
342+
}
339343
}
340344

341345
public long getContentLengthLong() {

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
* @author Vedran Pavic
4646
* @since 1.0.2
4747
*/
48-
@SuppressWarnings("deprecation")
4948
public class MockHttpSession implements HttpSession {
5049

5150
/**
@@ -240,6 +239,12 @@ public boolean isNew() {
240239
return this.isNew;
241240
}
242241

242+
@Override
243+
public Accessor getAccessor() {
244+
return sessionConsumer -> sessionConsumer.accept(MockHttpSession.this);
245+
}
246+
247+
243248
/**
244249
* Serialize the attributes of this session into an object that can be
245250
* turned into a byte array with standard Java serialization.

spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,10 @@ public String getContentAsString(Charset fallbackCharset) throws UnsupportedEnco
319319

320320
@Override
321321
public void setContentLength(int contentLength) {
322-
this.contentLength = contentLength;
323-
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true);
322+
if (!this.committed) {
323+
this.contentLength = contentLength;
324+
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true);
325+
}
324326
}
325327

326328
/**
@@ -334,8 +336,10 @@ public int getContentLength() {
334336

335337
@Override
336338
public void setContentLengthLong(long contentLength) {
337-
this.contentLength = contentLength;
338-
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true);
339+
if (!this.committed) {
340+
this.contentLength = contentLength;
341+
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true);
342+
}
339343
}
340344

341345
public long getContentLengthLong() {
@@ -636,7 +640,7 @@ public void sendRedirect(String url) throws IOException {
636640
sendRedirect(url, HttpServletResponse.SC_MOVED_TEMPORARILY, true);
637641
}
638642

639-
// @Override - on Servlet 6.1
643+
@Override
640644
public void sendRedirect(String url, int sc, boolean clearBuffer) throws IOException {
641645
Assert.state(!isCommitted(), "Cannot send redirect - response is already committed");
642646
Assert.notNull(url, "Redirect URL must not be null");

spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpSession.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
* @author Vedran Pavic
4646
* @since 1.0.2
4747
*/
48-
@SuppressWarnings("deprecation")
4948
public class MockHttpSession implements HttpSession {
5049

5150
/**
@@ -239,6 +238,11 @@ public boolean isNew() {
239238
return this.isNew;
240239
}
241240

241+
@Override
242+
public Accessor getAccessor() {
243+
return sessionConsumer -> sessionConsumer.accept(MockHttpSession.this);
244+
}
245+
242246
/**
243247
* Serialize the attributes of this session into an object that can be
244248
* turned into a byte array with standard Java serialization.

0 commit comments

Comments
 (0)