From 5e08a88219dd5aaa2f3a3bf4ff36c7b2bcd6ed37 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 22 Nov 2024 17:07:44 +0100 Subject: [PATCH] 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 --- .../mock/web/MockHttpServletResponse.java | 12 ++++++++---- .../springframework/mock/web/MockHttpSession.java | 7 ++++++- .../servlet/MockHttpServletResponse.java | 14 +++++++++----- .../web/testfixture/servlet/MockHttpSession.java | 6 +++++- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java index 592e216763a..18df83dbf6a 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -319,8 +319,10 @@ public class MockHttpServletResponse implements HttpServletResponse { @Override public void setContentLength(int contentLength) { - this.contentLength = contentLength; - doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true); + if (!this.committed) { + this.contentLength = contentLength; + doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true); + } } /** @@ -334,8 +336,10 @@ public class MockHttpServletResponse implements HttpServletResponse { @Override public void setContentLengthLong(long contentLength) { - this.contentLength = contentLength; - doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true); + if (!this.committed) { + this.contentLength = contentLength; + doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true); + } } public long getContentLengthLong() { diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java index 7b10ea96ae3..1881d802bc9 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java @@ -45,7 +45,6 @@ import org.springframework.util.Assert; * @author Vedran Pavic * @since 1.0.2 */ -@SuppressWarnings("deprecation") public class MockHttpSession implements HttpSession { /** @@ -240,6 +239,12 @@ public class MockHttpSession implements HttpSession { return this.isNew; } + @Override + public Accessor getAccessor() { + return sessionConsumer -> sessionConsumer.accept(MockHttpSession.this); + } + + /** * Serialize the attributes of this session into an object that can be * turned into a byte array with standard Java serialization. diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java index cbcff8f5736..c51560554e3 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java @@ -319,8 +319,10 @@ public class MockHttpServletResponse implements HttpServletResponse { @Override public void setContentLength(int contentLength) { - this.contentLength = contentLength; - doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true); + if (!this.committed) { + this.contentLength = contentLength; + doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true); + } } /** @@ -334,8 +336,10 @@ public class MockHttpServletResponse implements HttpServletResponse { @Override public void setContentLengthLong(long contentLength) { - this.contentLength = contentLength; - doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true); + if (!this.committed) { + this.contentLength = contentLength; + doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true); + } } public long getContentLengthLong() { @@ -636,7 +640,7 @@ public class MockHttpServletResponse implements HttpServletResponse { sendRedirect(url, HttpServletResponse.SC_MOVED_TEMPORARILY, true); } - // @Override - on Servlet 6.1 + @Override public void sendRedirect(String url, int sc, boolean clearBuffer) throws IOException { Assert.state(!isCommitted(), "Cannot send redirect - response is already committed"); Assert.notNull(url, "Redirect URL must not be null"); diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpSession.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpSession.java index 0711d388469..0ec6fae4b8e 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpSession.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpSession.java @@ -45,7 +45,6 @@ import org.springframework.util.Assert; * @author Vedran Pavic * @since 1.0.2 */ -@SuppressWarnings("deprecation") public class MockHttpSession implements HttpSession { /** @@ -239,6 +238,11 @@ public class MockHttpSession implements HttpSession { return this.isNew; } + @Override + public Accessor getAccessor() { + return sessionConsumer -> sessionConsumer.accept(MockHttpSession.this); + } + /** * Serialize the attributes of this session into an object that can be * turned into a byte array with standard Java serialization.