16
16
17
17
package org .springframework .web .filter ;
18
18
19
- import java .util .function .BiConsumer ;
20
19
import java .util .stream .Stream ;
21
20
22
21
import javax .servlet .http .HttpServletResponse ;
23
22
23
+ import org .junit .jupiter .api .Named ;
24
24
import org .junit .jupiter .api .Test ;
25
25
import org .junit .jupiter .params .ParameterizedTest ;
26
- import org .junit .jupiter .params .provider .Arguments ;
27
26
import org .junit .jupiter .params .provider .MethodSource ;
28
27
29
28
import org .springframework .http .MediaType ;
34
33
import static java .nio .charset .StandardCharsets .UTF_8 ;
35
34
import static org .assertj .core .api .Assertions .assertThat ;
36
35
import static org .junit .jupiter .api .Named .named ;
37
- import static org .junit .jupiter .params .provider .Arguments .arguments ;
38
36
import static org .springframework .http .HttpHeaders .CONTENT_LENGTH ;
39
37
import static org .springframework .http .HttpHeaders .CONTENT_TYPE ;
40
38
import static org .springframework .http .HttpHeaders .TRANSFER_ENCODING ;
45
43
* @author Rossen Stoyanchev
46
44
* @author Sam Brannen
47
45
*/
48
- public class ContentCachingResponseWrapperTests {
46
+ class ContentCachingResponseWrapperTests {
49
47
50
48
@ Test
51
49
void copyBodyToResponse () throws Exception {
@@ -121,39 +119,83 @@ void copyBodyToResponseWithPresetHeaders() throws Exception {
121
119
}
122
120
123
121
@ ParameterizedTest (name = "[{index}] {0}" )
124
- @ MethodSource ("setContentTypeFunctions " )
125
- void copyBodyToResponseWithOverridingHeaders ( BiConsumer < HttpServletResponse , String > setContentType ) throws Exception {
122
+ @ MethodSource ("setContentLengthFunctions " )
123
+ void copyBodyToResponseWithOverridingContentLength ( SetContentLength setContentLength ) throws Exception {
126
124
byte [] responseBody = "Hello World" .getBytes (UTF_8 );
127
125
int responseLength = responseBody .length ;
128
126
int originalContentLength = 11 ;
129
127
int overridingContentLength = 22 ;
130
- String originalContentType = MediaType .TEXT_PLAIN_VALUE ;
131
- String overridingContentType = MediaType .APPLICATION_JSON_VALUE ;
132
128
133
129
MockHttpServletResponse response = new MockHttpServletResponse ();
134
130
response .setContentLength (originalContentLength );
135
- response .setContentType (originalContentType );
136
131
137
132
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper (response );
138
- responseWrapper .setStatus (HttpServletResponse .SC_CREATED );
139
133
responseWrapper .setContentLength (overridingContentLength );
140
- setContentType .accept (responseWrapper , overridingContentType );
141
134
142
- assertThat (responseWrapper .getStatus ()).isEqualTo (HttpServletResponse .SC_CREATED );
135
+ setContentLength .invoke (responseWrapper , overridingContentLength );
136
+
143
137
assertThat (responseWrapper .getContentSize ()).isZero ();
144
- assertThat (responseWrapper .getHeaderNames ()).containsExactlyInAnyOrder (CONTENT_TYPE , CONTENT_LENGTH );
138
+ assertThat (responseWrapper .getHeaderNames ()).containsExactlyInAnyOrder (CONTENT_LENGTH );
145
139
146
140
assertHeader (response , CONTENT_LENGTH , originalContentLength );
147
141
assertHeader (responseWrapper , CONTENT_LENGTH , overridingContentLength );
142
+
143
+ FileCopyUtils .copy (responseBody , responseWrapper .getOutputStream ());
144
+ assertThat (responseWrapper .getContentSize ()).isEqualTo (responseLength );
145
+
146
+ responseWrapper .copyBodyToResponse ();
147
+
148
+ assertThat (responseWrapper .getContentSize ()).isZero ();
149
+ assertThat (responseWrapper .getHeaderNames ()).containsExactlyInAnyOrder (CONTENT_LENGTH );
150
+
151
+ assertHeader (response , CONTENT_LENGTH , responseLength );
152
+ assertHeader (responseWrapper , CONTENT_LENGTH , responseLength );
153
+
154
+ assertThat (response .getContentLength ()).isEqualTo (responseLength );
155
+ assertThat (response .getContentAsByteArray ()).isEqualTo (responseBody );
156
+ assertThat (response .getHeaderNames ()).containsExactlyInAnyOrder (CONTENT_LENGTH );
157
+ }
158
+
159
+ private static Stream <Named <SetContentLength >> setContentLengthFunctions () {
160
+ return Stream .of (
161
+ named ("setContentLength()" , HttpServletResponse ::setContentLength ),
162
+ named ("setContentLengthLong()" , HttpServletResponse ::setContentLengthLong ),
163
+ named ("setIntHeader()" , (response , contentLength ) -> response .setIntHeader (CONTENT_LENGTH , contentLength )),
164
+ named ("addIntHeader()" , (response , contentLength ) -> response .addIntHeader (CONTENT_LENGTH , contentLength )),
165
+ named ("setHeader()" , (response , contentLength ) -> response .setHeader (CONTENT_LENGTH , "" + contentLength )),
166
+ named ("addHeader()" , (response , contentLength ) -> response .addHeader (CONTENT_LENGTH , "" + contentLength ))
167
+ );
168
+ }
169
+
170
+ @ ParameterizedTest (name = "[{index}] {0}" )
171
+ @ MethodSource ("setContentTypeFunctions" )
172
+ void copyBodyToResponseWithOverridingContentType (SetContentType setContentType ) throws Exception {
173
+ byte [] responseBody = "Hello World" .getBytes (UTF_8 );
174
+ int responseLength = responseBody .length ;
175
+ String originalContentType = MediaType .TEXT_PLAIN_VALUE ;
176
+ String overridingContentType = MediaType .APPLICATION_JSON_VALUE ;
177
+
178
+ MockHttpServletResponse response = new MockHttpServletResponse ();
179
+ response .setContentType (originalContentType );
180
+
181
+ ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper (response );
182
+
148
183
assertContentTypeHeader (response , originalContentType );
184
+ assertContentTypeHeader (responseWrapper , originalContentType );
185
+
186
+ setContentType .invoke (responseWrapper , overridingContentType );
187
+
188
+ assertThat (responseWrapper .getContentSize ()).isZero ();
189
+ assertThat (responseWrapper .getHeaderNames ()).containsExactlyInAnyOrder (CONTENT_TYPE );
190
+
191
+ assertContentTypeHeader (response , overridingContentType );
149
192
assertContentTypeHeader (responseWrapper , overridingContentType );
150
193
151
194
FileCopyUtils .copy (responseBody , responseWrapper .getOutputStream ());
152
195
assertThat (responseWrapper .getContentSize ()).isEqualTo (responseLength );
153
196
154
197
responseWrapper .copyBodyToResponse ();
155
198
156
- assertThat (responseWrapper .getStatus ()).isEqualTo (HttpServletResponse .SC_CREATED );
157
199
assertThat (responseWrapper .getContentSize ()).isZero ();
158
200
assertThat (responseWrapper .getHeaderNames ()).containsExactlyInAnyOrder (CONTENT_TYPE , CONTENT_LENGTH );
159
201
@@ -162,24 +204,19 @@ void copyBodyToResponseWithOverridingHeaders(BiConsumer<HttpServletResponse, Str
162
204
assertContentTypeHeader (response , overridingContentType );
163
205
assertContentTypeHeader (responseWrapper , overridingContentType );
164
206
165
- assertThat (response .getStatus ()).isEqualTo (HttpServletResponse .SC_CREATED );
166
207
assertThat (response .getContentLength ()).isEqualTo (responseLength );
167
208
assertThat (response .getContentAsByteArray ()).isEqualTo (responseBody );
168
209
assertThat (response .getHeaderNames ()).containsExactlyInAnyOrder (CONTENT_TYPE , CONTENT_LENGTH );
169
210
}
170
211
171
- private static Stream <Arguments > setContentTypeFunctions () {
212
+ private static Stream <Named < SetContentType > > setContentTypeFunctions () {
172
213
return Stream .of (
173
- namedArguments ("setContentType()" , HttpServletResponse ::setContentType ),
174
- namedArguments ("setHeader()" , (response , contentType ) -> response .setHeader (CONTENT_TYPE , contentType )),
175
- namedArguments ("addHeader()" , (response , contentType ) -> response .addHeader (CONTENT_TYPE , contentType ))
214
+ named ("setContentType()" , HttpServletResponse ::setContentType ),
215
+ named ("setHeader()" , (response , contentType ) -> response .setHeader (CONTENT_TYPE , contentType )),
216
+ named ("addHeader()" , (response , contentType ) -> response .addHeader (CONTENT_TYPE , contentType ))
176
217
);
177
218
}
178
219
179
- private static Arguments namedArguments (String name , BiConsumer <HttpServletResponse , String > setContentTypeFunction ) {
180
- return arguments (named (name , setContentTypeFunction ));
181
- }
182
-
183
220
@ Test
184
221
void copyBodyToResponseWithTransferEncoding () throws Exception {
185
222
byte [] responseBody = "6\r \n Hello 5\r \n World0\r \n \r \n " .getBytes (UTF_8 );
@@ -219,4 +256,15 @@ private void assertContentTypeHeader(HttpServletResponse response, String conten
219
256
assertThat (response .getContentType ()).as (CONTENT_TYPE ).isEqualTo (contentType );
220
257
}
221
258
259
+
260
+ @ FunctionalInterface
261
+ private interface SetContentLength {
262
+ void invoke (HttpServletResponse response , int contentLength );
263
+ }
264
+
265
+ @ FunctionalInterface
266
+ private interface SetContentType {
267
+ void invoke (HttpServletResponse response , String contentType );
268
+ }
269
+
222
270
}
0 commit comments