1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -182,19 +182,15 @@ void getFormBody() throws IOException {
182
182
mockRequest .addParameter ("name 2" , "value 2+1" , "value 2+2" );
183
183
mockRequest .addParameter ("name 3" , (String ) null );
184
184
185
- byte [] result = FileCopyUtils .copyToByteArray (request .getBody ());
186
- byte [] content = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3" .getBytes (StandardCharsets .UTF_8 );
187
- assertThat (result ).as ("Invalid content returned" ).isEqualTo (content );
185
+ assertFormContent ("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3" );
188
186
}
189
187
190
188
@ Test
191
189
void getEmptyFormBody () throws IOException {
192
190
mockRequest .setContentType ("application/x-www-form-urlencoded; charset=UTF-8" );
193
191
mockRequest .setMethod ("POST" );
194
192
195
- byte [] result = FileCopyUtils .copyToByteArray (request .getBody ());
196
- byte [] content = "" .getBytes (StandardCharsets .UTF_8 );
197
- assertThat (result ).as ("Invalid content returned" ).isEqualTo (content );
193
+ assertFormContent ("" );
198
194
}
199
195
200
196
@ Test // gh-31327
@@ -206,9 +202,7 @@ void getFormBodyWhenQueryParamsAlsoPresent() throws IOException {
206
202
mockRequest .setContent ("foo=bar" .getBytes (StandardCharsets .UTF_8 ));
207
203
mockRequest .addHeader ("Content-Length" , 7 );
208
204
209
- byte [] result = FileCopyUtils .copyToByteArray (request .getBody ());
210
- byte [] content = "foo=bar" .getBytes (StandardCharsets .UTF_8 );
211
- assertThat (result ).as ("Invalid content returned" ).isEqualTo (content );
205
+ assertFormContent ("foo=bar" );
212
206
}
213
207
214
208
@ Test // gh-32471
@@ -219,9 +213,15 @@ void getFormBodyWhenNotEncodedCharactersPresent() throws IOException {
219
213
mockRequest .addParameter ("lastName" , "Test@er" );
220
214
mockRequest .addHeader ("Content-Length" , 26 );
221
215
216
+ int contentLength = assertFormContent ("name=Test&lastName=Test%40er" );
217
+ assertThat (request .getHeaders ().getContentLength ()).isEqualTo (contentLength );
218
+ }
219
+
220
+ private int assertFormContent (String expected ) throws IOException {
222
221
byte [] result = FileCopyUtils .copyToByteArray (request .getBody ());
223
- assertThat (result ).isEqualTo ("name=Test&lastName=Test%40er" .getBytes (StandardCharsets .UTF_8 ));
224
- assertThat (request .getHeaders ().getContentLength ()).isEqualTo (result .length );
222
+ byte [] content = expected .getBytes (StandardCharsets .UTF_8 );
223
+ assertThat (result ).as ("Invalid content returned" ).isEqualTo (content );
224
+ return result .length ;
225
225
}
226
226
227
227
@ Test
0 commit comments