Skip to content

Commit 1acc1c3

Browse files
committed
Polish ContentCachingResponseWrapper[Tests]
1 parent 6ad75bd commit 1acc1c3

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

spring-web/src/main/java/org/springframework/web/util/ContentCachingResponseWrapper.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
/**
3939
* {@link javax.servlet.http.HttpServletResponse} wrapper that caches all content written to
4040
* the {@linkplain #getOutputStream() output stream} and {@linkplain #getWriter() writer},
41-
* and allows this content to be retrieved via a {@link #getContentAsByteArray() byte array}.
41+
* and allows this content to be retrieved via a {@linkplain #getContentAsByteArray() byte array}.
4242
*
4343
* <p>Used e.g. by {@link org.springframework.web.filter.ShallowEtagHeaderFilter}.
4444
* Note: As of Spring Framework 5.0, this wrapper is built on the Servlet 3.1 API.
@@ -122,9 +122,16 @@ public PrintWriter getWriter() throws IOException {
122122
return this.writer;
123123
}
124124

125+
/**
126+
* This method neither flushes content to the client nor commits the underlying
127+
* response, since the content has not yet been copied to the response.
128+
* <p>Invoke {@link #copyBodyToResponse()} to copy the cached body content to
129+
* the wrapped response object and flush its buffer.
130+
* @see javax.servlet.ServletResponseWrapper#flushBuffer()
131+
*/
125132
@Override
126133
public void flushBuffer() throws IOException {
127-
// do not flush the underlying response as the content has not been copied to it yet
134+
// no-op
128135
}
129136

130137
@Override
@@ -142,15 +149,11 @@ public void setContentLengthLong(long len) {
142149
throw new IllegalArgumentException("Content-Length exceeds ContentCachingResponseWrapper's maximum (" +
143150
Integer.MAX_VALUE + "): " + len);
144151
}
145-
int lenInt = (int) len;
146-
if (lenInt > this.content.size()) {
147-
this.content.resize(lenInt);
148-
}
149-
this.contentLength = lenInt;
152+
setContentLength((int) len);
150153
}
151154

152155
@Override
153-
public void setContentType(String type) {
156+
public void setContentType(@Nullable String type) {
154157
this.contentType = type;
155158
}
156159

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,55 +16,69 @@
1616

1717
package org.springframework.web.filter;
1818

19-
import java.nio.charset.StandardCharsets;
20-
2119
import javax.servlet.http.HttpServletResponse;
2220

2321
import org.junit.jupiter.api.Test;
2422

25-
import org.springframework.http.HttpHeaders;
2623
import org.springframework.util.FileCopyUtils;
2724
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
2825
import org.springframework.web.util.ContentCachingResponseWrapper;
2926

27+
import static java.nio.charset.StandardCharsets.UTF_8;
3028
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.springframework.http.HttpHeaders.CONTENT_LENGTH;
30+
import static org.springframework.http.HttpHeaders.TRANSFER_ENCODING;
3131

3232
/**
3333
* Unit tests for {@link ContentCachingResponseWrapper}.
34+
*
3435
* @author Rossen Stoyanchev
3536
*/
3637
public class ContentCachingResponseWrapperTests {
3738

3839
@Test
3940
void copyBodyToResponse() throws Exception {
40-
byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8);
41+
byte[] responseBody = "Hello World".getBytes(UTF_8);
4142
MockHttpServletResponse response = new MockHttpServletResponse();
4243

4344
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
44-
responseWrapper.setStatus(HttpServletResponse.SC_OK);
45+
responseWrapper.setStatus(HttpServletResponse.SC_CREATED);
4546
FileCopyUtils.copy(responseBody, responseWrapper.getOutputStream());
4647
responseWrapper.copyBodyToResponse();
4748

48-
assertThat(response.getStatus()).isEqualTo(200);
49-
assertThat(response.getContentLength() > 0).isTrue();
49+
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_CREATED);
50+
assertThat(response.getContentLength()).isGreaterThan(0);
5051
assertThat(response.getContentAsByteArray()).isEqualTo(responseBody);
5152
}
5253

5354
@Test
5455
void copyBodyToResponseWithTransferEncoding() throws Exception {
55-
byte[] responseBody = "6\r\nHello 5\r\nWorld0\r\n\r\n".getBytes(StandardCharsets.UTF_8);
56+
byte[] responseBody = "6\r\nHello 5\r\nWorld0\r\n\r\n".getBytes(UTF_8);
5657
MockHttpServletResponse response = new MockHttpServletResponse();
5758

5859
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
59-
responseWrapper.setStatus(HttpServletResponse.SC_OK);
60-
responseWrapper.setHeader(HttpHeaders.TRANSFER_ENCODING, "chunked");
60+
responseWrapper.setStatus(HttpServletResponse.SC_CREATED);
61+
responseWrapper.setHeader(TRANSFER_ENCODING, "chunked");
6162
FileCopyUtils.copy(responseBody, responseWrapper.getOutputStream());
6263
responseWrapper.copyBodyToResponse();
6364

64-
assertThat(response.getStatus()).isEqualTo(200);
65-
assertThat(response.getHeader(HttpHeaders.TRANSFER_ENCODING)).isEqualTo("chunked");
66-
assertThat(response.getHeader(HttpHeaders.CONTENT_LENGTH)).isNull();
65+
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_CREATED);
66+
assertHeader(response, TRANSFER_ENCODING, "chunked");
67+
assertHeader(response, CONTENT_LENGTH, null);
6768
assertThat(response.getContentAsByteArray()).isEqualTo(responseBody);
6869
}
6970

71+
private void assertHeader(HttpServletResponse response, String header, String value) {
72+
if (value == null) {
73+
assertThat(response.containsHeader(header)).as(header).isFalse();
74+
assertThat(response.getHeader(header)).as(header).isNull();
75+
assertThat(response.getHeaders(header)).as(header).isEmpty();
76+
}
77+
else {
78+
assertThat(response.containsHeader(header)).as(header).isTrue();
79+
assertThat(response.getHeader(header)).as(header).isEqualTo(value);
80+
assertThat(response.getHeaders(header)).as(header).containsExactly(value);
81+
}
82+
}
83+
7084
}

0 commit comments

Comments
 (0)