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.
18
18
19
19
import java .io .IOException ;
20
20
import java .net .URI ;
21
+ import java .net .URLDecoder ;
21
22
import java .nio .charset .StandardCharsets ;
22
23
import java .util .List ;
23
24
@@ -182,19 +183,15 @@ void getFormBody() throws IOException {
182
183
mockRequest .addParameter ("name 2" , "value 2+1" , "value 2+2" );
183
184
mockRequest .addParameter ("name 3" , (String ) null );
184
185
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 );
186
+ assertFormContent ("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3" );
188
187
}
189
188
190
189
@ Test
191
190
void getEmptyFormBody () throws IOException {
192
191
mockRequest .setContentType ("application/x-www-form-urlencoded; charset=UTF-8" );
193
192
mockRequest .setMethod ("POST" );
194
193
195
- byte [] result = FileCopyUtils .copyToByteArray (request .getBody ());
196
- byte [] content = "" .getBytes (StandardCharsets .UTF_8 );
197
- assertThat (result ).as ("Invalid content returned" ).isEqualTo (content );
194
+ assertFormContent ("" );
198
195
}
199
196
200
197
@ Test // gh-31327
@@ -206,9 +203,7 @@ void getFormBodyWhenQueryParamsAlsoPresent() throws IOException {
206
203
mockRequest .setContent ("foo=bar" .getBytes (StandardCharsets .UTF_8 ));
207
204
mockRequest .addHeader ("Content-Length" , 7 );
208
205
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 );
206
+ assertFormContent ("foo=bar" );
212
207
}
213
208
214
209
@ Test // gh-32471
@@ -219,9 +214,25 @@ void getFormBodyWhenNotEncodedCharactersPresent() throws IOException {
219
214
mockRequest .addParameter ("lastName" , "Test@er" );
220
215
mockRequest .addHeader ("Content-Length" , 26 );
221
216
217
+ int contentLength = assertFormContent ("name=Test&lastName=Test%40er" );
218
+ assertThat (request .getHeaders ().getContentLength ()).isEqualTo (contentLength );
219
+ }
220
+
221
+ @ Test // gh-34675
222
+ void getFormBodyWithNotUtf8Charset () throws IOException {
223
+ String charset = "windows-1251" ;
224
+ mockRequest .setContentType ("application/x-www-form-urlencoded; charset=" + charset );
225
+ mockRequest .setMethod ("POST" );
226
+ mockRequest .addParameter ("x" , URLDecoder .decode ("%e0%e0%e0" , charset ));
227
+
228
+ assertFormContent ("x=%E0%E0%E0" );
229
+ }
230
+
231
+ private int assertFormContent (String expected ) throws IOException {
222
232
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 );
233
+ byte [] content = expected .getBytes (StandardCharsets .UTF_8 );
234
+ assertThat (result ).as ("Invalid content returned" ).isEqualTo (content );
235
+ return result .length ;
225
236
}
226
237
227
238
@ Test
0 commit comments