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
This commit is contained in:
Brian Clozel
2024-11-22 17:07:44 +01:00
parent 3b65506c13
commit 5e08a88219
4 changed files with 28 additions and 11 deletions

View File

@@ -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() {

View File

@@ -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.

View File

@@ -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");

View File

@@ -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.