Polish ContentCachingResponseWrapper[Tests]

This commit is contained in:
Sam Brannen
2024-02-24 13:34:48 +01:00
parent 3cc64968b9
commit 8787381892
2 changed files with 21 additions and 18 deletions

View File

@@ -38,7 +38,7 @@ import org.springframework.util.FastByteArrayOutputStream;
/**
* {@link jakarta.servlet.http.HttpServletResponse} wrapper that caches all content written to
* the {@linkplain #getOutputStream() output stream} and {@linkplain #getWriter() writer},
* and allows this content to be retrieved via a {@link #getContentAsByteArray() byte array}.
* and allows this content to be retrieved via a {@linkplain #getContentAsByteArray() byte array}.
*
* <p>Used e.g. by {@link org.springframework.web.filter.ShallowEtagHeaderFilter}.
*
@@ -120,9 +120,16 @@ public class ContentCachingResponseWrapper extends HttpServletResponseWrapper {
return this.writer;
}
/**
* This method neither flushes content to the client nor commits the underlying
* response, since the content has not yet been copied to the response.
* <p>Invoke {@link #copyBodyToResponse()} to copy the cached body content to
* the wrapped response object and flush its buffer.
* @see jakarta.servlet.ServletResponseWrapper#flushBuffer()
*/
@Override
public void flushBuffer() throws IOException {
// do not flush the underlying response as the content has not been copied to it yet
// no-op
}
@Override
@@ -139,15 +146,11 @@ public class ContentCachingResponseWrapper extends HttpServletResponseWrapper {
throw new IllegalArgumentException("Content-Length exceeds ContentCachingResponseWrapper's maximum (" +
Integer.MAX_VALUE + "): " + len);
}
int lenInt = (int) len;
if (lenInt > this.content.size()) {
this.content.resize(lenInt);
}
this.contentLength = lenInt;
setContentLength((int) len);
}
@Override
public void setContentType(String type) {
public void setContentType(@Nullable String type) {
this.contentType = type;
}

View File

@@ -16,17 +16,17 @@
package org.springframework.web.filter;
import java.nio.charset.StandardCharsets;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import org.springframework.web.util.ContentCachingResponseWrapper;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpHeaders.CONTENT_LENGTH;
import static org.springframework.http.HttpHeaders.TRANSFER_ENCODING;
/**
* Tests for {@link ContentCachingResponseWrapper}.
@@ -37,7 +37,7 @@ class ContentCachingResponseWrapperTests {
@Test
void copyBodyToResponse() throws Exception {
byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8);
byte[] responseBody = "Hello World".getBytes(UTF_8);
MockHttpServletResponse response = new MockHttpServletResponse();
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
@@ -45,25 +45,25 @@ class ContentCachingResponseWrapperTests {
FileCopyUtils.copy(responseBody, responseWrapper.getOutputStream());
responseWrapper.copyBodyToResponse();
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
assertThat(response.getContentLength()).isGreaterThan(0);
assertThat(response.getContentAsByteArray()).isEqualTo(responseBody);
}
@Test
void copyBodyToResponseWithTransferEncoding() throws Exception {
byte[] responseBody = "6\r\nHello 5\r\nWorld0\r\n\r\n".getBytes(StandardCharsets.UTF_8);
byte[] responseBody = "6\r\nHello 5\r\nWorld0\r\n\r\n".getBytes(UTF_8);
MockHttpServletResponse response = new MockHttpServletResponse();
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
responseWrapper.setStatus(HttpServletResponse.SC_OK);
responseWrapper.setHeader(HttpHeaders.TRANSFER_ENCODING, "chunked");
responseWrapper.setHeader(TRANSFER_ENCODING, "chunked");
FileCopyUtils.copy(responseBody, responseWrapper.getOutputStream());
responseWrapper.copyBodyToResponse();
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getHeader(HttpHeaders.TRANSFER_ENCODING)).isEqualTo("chunked");
assertThat(response.getHeader(HttpHeaders.CONTENT_LENGTH)).isNull();
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
assertThat(response.getHeader(TRANSFER_ENCODING)).isEqualTo("chunked");
assertThat(response.getHeader(CONTENT_LENGTH)).isNull();
assertThat(response.getContentAsByteArray()).isEqualTo(responseBody);
}