Browse Source

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
pull/33950/head
Brian Clozel 1 year ago
parent
commit
5e08a88219
  1. 12
      spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java
  2. 7
      spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java
  3. 14
      spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java
  4. 6
      spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpSession.java

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

@ -319,8 +319,10 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Override @Override
public void setContentLength(int contentLength) { public void setContentLength(int contentLength) {
this.contentLength = contentLength; if (!this.committed) {
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true); this.contentLength = contentLength;
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true);
}
} }
/** /**
@ -334,8 +336,10 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Override @Override
public void setContentLengthLong(long contentLength) { public void setContentLengthLong(long contentLength) {
this.contentLength = contentLength; if (!this.committed) {
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true); this.contentLength = contentLength;
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true);
}
} }
public long getContentLengthLong() { public long getContentLengthLong() {

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

@ -45,7 +45,6 @@ import org.springframework.util.Assert;
* @author Vedran Pavic * @author Vedran Pavic
* @since 1.0.2 * @since 1.0.2
*/ */
@SuppressWarnings("deprecation")
public class MockHttpSession implements HttpSession { public class MockHttpSession implements HttpSession {
/** /**
@ -240,6 +239,12 @@ public class MockHttpSession implements HttpSession {
return this.isNew; 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 * Serialize the attributes of this session into an object that can be
* turned into a byte array with standard Java serialization. * turned into a byte array with standard Java serialization.

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

@ -319,8 +319,10 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Override @Override
public void setContentLength(int contentLength) { public void setContentLength(int contentLength) {
this.contentLength = contentLength; if (!this.committed) {
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true); this.contentLength = contentLength;
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true);
}
} }
/** /**
@ -334,8 +336,10 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Override @Override
public void setContentLengthLong(long contentLength) { public void setContentLengthLong(long contentLength) {
this.contentLength = contentLength; if (!this.committed) {
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true); this.contentLength = contentLength;
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true);
}
} }
public long getContentLengthLong() { public long getContentLengthLong() {
@ -636,7 +640,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
sendRedirect(url, HttpServletResponse.SC_MOVED_TEMPORARILY, true); sendRedirect(url, HttpServletResponse.SC_MOVED_TEMPORARILY, true);
} }
// @Override - on Servlet 6.1 @Override
public void sendRedirect(String url, int sc, boolean clearBuffer) throws IOException { public void sendRedirect(String url, int sc, boolean clearBuffer) throws IOException {
Assert.state(!isCommitted(), "Cannot send redirect - response is already committed"); Assert.state(!isCommitted(), "Cannot send redirect - response is already committed");
Assert.notNull(url, "Redirect URL must not be null"); Assert.notNull(url, "Redirect URL must not be null");

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

@ -45,7 +45,6 @@ import org.springframework.util.Assert;
* @author Vedran Pavic * @author Vedran Pavic
* @since 1.0.2 * @since 1.0.2
*/ */
@SuppressWarnings("deprecation")
public class MockHttpSession implements HttpSession { public class MockHttpSession implements HttpSession {
/** /**
@ -239,6 +238,11 @@ public class MockHttpSession implements HttpSession {
return this.isNew; 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 * Serialize the attributes of this session into an object that can be
* turned into a byte array with standard Java serialization. * turned into a byte array with standard Java serialization.

Loading…
Cancel
Save