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:
@@ -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() {
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user