16
16
17
17
package org .springframework .http ;
18
18
19
- import java .lang .reflect .Method ;
20
- import java .nio .charset .Charset ;
21
19
import java .nio .charset .StandardCharsets ;
22
20
import java .time .ZonedDateTime ;
23
21
import java .time .format .DateTimeFormatter ;
24
22
25
23
import org .junit .jupiter .api .Test ;
26
24
27
- import org .springframework .util .ReflectionUtils ;
28
-
29
25
import static org .assertj .core .api .Assertions .assertThat ;
30
26
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
31
27
@@ -74,6 +70,30 @@ public void parseEncodedFilename() {
74
70
.build ());
75
71
}
76
72
73
+ @ Test
74
+ public void parseEncodedFilenameWithoutCharset () {
75
+ assertThat (parse ("form-data; name=\" name\" ; filename*=test.txt" ))
76
+ .isEqualTo (ContentDisposition .builder ("form-data" )
77
+ .name ("name" )
78
+ .filename ("test.txt" )
79
+ .build ());
80
+ }
81
+
82
+ @ Test
83
+ public void parseEncodedFilenameWithInvalidCharset () {
84
+ assertThatIllegalArgumentException ()
85
+ .isThrownBy (() -> parse ("form-data; name=\" name\" ; filename*=UTF-16''test.txt" ));
86
+ }
87
+
88
+ @ Test
89
+ public void parseEncodedFilenameWithInvalidName () {
90
+ assertThatIllegalArgumentException ()
91
+ .isThrownBy (() -> parse ("form-data; name=\" name\" ; filename*=UTF-8''%A" ));
92
+
93
+ assertThatIllegalArgumentException ()
94
+ .isThrownBy (() -> parse ("form-data; name=\" name\" ; filename*=UTF-8''%A.txt" ));
95
+ }
96
+
77
97
@ Test // gh-23077
78
98
public void parseWithEscapedQuote () {
79
99
assertThat (parse ("form-data; name=\" file\" ; filename=\" \\ \" The Twilight Zone\\ \" .txt\" ; size=123" ))
@@ -147,7 +167,7 @@ private static ContentDisposition parse(String input) {
147
167
148
168
149
169
@ Test
150
- public void headerValue () {
170
+ public void format () {
151
171
assertThat (
152
172
ContentDisposition .builder ("form-data" )
153
173
.name ("foo" )
@@ -158,7 +178,7 @@ public void headerValue() {
158
178
}
159
179
160
180
@ Test
161
- public void headerValueWithEncodedFilename () {
181
+ public void formatWithEncodedFilename () {
162
182
assertThat (
163
183
ContentDisposition .builder ("form-data" )
164
184
.name ("name" )
@@ -167,67 +187,25 @@ public void headerValueWithEncodedFilename() {
167
187
.isEqualTo ("form-data; name=\" name\" ; filename*=UTF-8''%E4%B8%AD%E6%96%87.txt" );
168
188
}
169
189
170
- @ Test // SPR-14547
171
- public void encodeHeaderFieldParam () {
172
- Method encode = ReflectionUtils .findMethod (ContentDisposition .class ,
173
- "encodeHeaderFieldParam" , String .class , Charset .class );
174
- ReflectionUtils .makeAccessible (encode );
175
-
176
- String result = (String )ReflectionUtils .invokeMethod (encode , null , "test.txt" ,
177
- StandardCharsets .US_ASCII );
178
- assertThat (result ).isEqualTo ("test.txt" );
179
-
180
- result = (String )ReflectionUtils .invokeMethod (encode , null , "中文.txt" , StandardCharsets .UTF_8 );
181
- assertThat (result ).isEqualTo ("UTF-8''%E4%B8%AD%E6%96%87.txt" );
182
- }
183
-
184
190
@ Test
185
- public void encodeHeaderFieldParamInvalidCharset () {
186
- Method encode = ReflectionUtils .findMethod (ContentDisposition .class ,
187
- "encodeHeaderFieldParam" , String .class , Charset .class );
188
- ReflectionUtils .makeAccessible (encode );
189
- assertThatIllegalArgumentException ().isThrownBy (() ->
190
- ReflectionUtils .invokeMethod (encode , null , "test" , StandardCharsets .UTF_16 ));
191
- }
192
-
193
- @ Test // SPR-14408
194
- public void decodeHeaderFieldParam () {
195
- Method decode = ReflectionUtils .findMethod (ContentDisposition .class ,
196
- "decodeHeaderFieldParam" , String .class );
197
- ReflectionUtils .makeAccessible (decode );
198
-
199
- String result = (String )ReflectionUtils .invokeMethod (decode , null , "test.txt" );
200
- assertThat (result ).isEqualTo ("test.txt" );
201
-
202
- result = (String )ReflectionUtils .invokeMethod (decode , null , "UTF-8''%E4%B8%AD%E6%96%87.txt" );
203
- assertThat (result ).isEqualTo ("中文.txt" );
204
- }
205
-
206
- @ Test
207
- public void decodeHeaderFieldParamInvalidCharset () {
208
- Method decode = ReflectionUtils .findMethod (ContentDisposition .class ,
209
- "decodeHeaderFieldParam" , String .class );
210
- ReflectionUtils .makeAccessible (decode );
211
- assertThatIllegalArgumentException ().isThrownBy (() ->
212
- ReflectionUtils .invokeMethod (decode , null , "UTF-16''test" ));
213
- }
214
-
215
- @ Test
216
- public void decodeHeaderFieldParamShortInvalidEncodedFilename () {
217
- Method decode = ReflectionUtils .findMethod (ContentDisposition .class ,
218
- "decodeHeaderFieldParam" , String .class );
219
- ReflectionUtils .makeAccessible (decode );
220
- assertThatIllegalArgumentException ().isThrownBy (() ->
221
- ReflectionUtils .invokeMethod (decode , null , "UTF-8''%A" ));
191
+ public void formatWithEncodedFilenameUsingUsAscii () {
192
+ assertThat (
193
+ ContentDisposition .builder ("form-data" )
194
+ .name ("name" )
195
+ .filename ("test.txt" , StandardCharsets .US_ASCII )
196
+ .build ()
197
+ .toString ())
198
+ .isEqualTo ("form-data; name=\" name\" ; filename=\" test.txt\" " );
222
199
}
223
200
224
201
@ Test
225
- public void decodeHeaderFieldParamLongerInvalidEncodedFilename () {
226
- Method decode = ReflectionUtils .findMethod (ContentDisposition .class ,
227
- "decodeHeaderFieldParam" , String .class );
228
- ReflectionUtils .makeAccessible (decode );
202
+ public void formatWithEncodedFilenameUsingInvalidCharset () {
229
203
assertThatIllegalArgumentException ().isThrownBy (() ->
230
- ReflectionUtils .invokeMethod (decode , null , "UTF-8''%A.txt" ));
204
+ ContentDisposition .builder ("form-data" )
205
+ .name ("name" )
206
+ .filename ("test.txt" , StandardCharsets .UTF_16 )
207
+ .build ()
208
+ .toString ());
231
209
}
232
210
233
211
}
0 commit comments