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.