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