|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2016 the original author or authors. |
| 2 | + * Copyright 2002-2020 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.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.http;
|
18 | 18 |
|
| 19 | +import org.hamcrest.Matchers; |
| 20 | +import org.junit.Test; |
| 21 | + |
19 | 22 | import java.net.URI;
|
20 | 23 | import java.net.URISyntaxException;
|
21 | 24 | import java.nio.charset.Charset;
|
| 25 | +import java.nio.charset.StandardCharsets; |
22 | 26 | import java.util.ArrayList;
|
23 | 27 | import java.util.Arrays;
|
24 | 28 | import java.util.Calendar;
|
|
28 | 32 | import java.util.List;
|
29 | 33 | import java.util.Locale;
|
30 | 34 | import java.util.TimeZone;
|
| 35 | +import java.util.function.BiConsumer; |
31 | 36 |
|
32 |
| -import org.hamcrest.Matchers; |
33 |
| -import org.junit.Test; |
34 |
| - |
35 |
| -import static org.hamcrest.Matchers.*; |
36 |
| -import static org.junit.Assert.*; |
| 37 | +import static org.hamcrest.Matchers.is; |
| 38 | +import static org.junit.Assert.assertEquals; |
| 39 | +import static org.junit.Assert.assertFalse; |
| 40 | +import static org.junit.Assert.assertNull; |
| 41 | +import static org.junit.Assert.assertThat; |
| 42 | +import static org.junit.Assert.assertTrue; |
| 43 | +import static org.junit.Assert.fail; |
37 | 44 |
|
38 | 45 | /**
|
39 | 46 | * Unit tests for {@link org.springframework.http.HttpHeaders}.
|
@@ -311,21 +318,70 @@ public void cacheControlAllValues() {
|
311 | 318 | assertThat(headers.getCacheControl(), is("max-age=1000, public, s-maxage=1000"));
|
312 | 319 | }
|
313 | 320 |
|
314 |
| - @SuppressWarnings("deprecation") |
315 | 321 | @Test
|
316 | 322 | public void contentDisposition() {
|
317 | 323 | headers.setContentDispositionFormData("name", null);
|
318 | 324 | assertEquals("Invalid Content-Disposition header", "form-data; name=\"name\"",
|
319 | 325 | headers.getFirst("Content-Disposition"));
|
320 | 326 |
|
321 |
| - headers.setContentDispositionFormData("name", "filename"); |
322 |
| - assertEquals("Invalid Content-Disposition header", "form-data; name=\"name\"; filename=\"filename\"", |
| 327 | + headers.setContentDispositionFormData("name", "foo.txt"); |
| 328 | + assertEquals("Invalid Content-Disposition header", "form-data; name=\"name\"; filename=\"foo.txt\"", |
323 | 329 | headers.getFirst("Content-Disposition"));
|
| 330 | + } |
324 | 331 |
|
325 |
| - headers.setContentDispositionFormData("name", "中文.txt", Charset.forName("UTF-8")); |
| 332 | + @SuppressWarnings("deprecation") |
| 333 | + @Test // SPR-14547 |
| 334 | + public void contentDispositionWithCharset() { |
| 335 | + |
| 336 | + headers.setContentDispositionFormData("name", "foo.txt", StandardCharsets.US_ASCII); |
| 337 | + assertEquals("Invalid Content-Disposition header", "form-data; name=\"name\"; filename=\"foo.txt\"", |
| 338 | + headers.getFirst("Content-Disposition")); |
| 339 | + |
| 340 | + headers.setContentDispositionFormData("name", "中文.txt", StandardCharsets.UTF_8); |
326 | 341 | assertEquals("Invalid Content-Disposition header",
|
327 | 342 | "form-data; name=\"name\"; filename*=UTF-8''%E4%B8%AD%E6%96%87.txt",
|
328 | 343 | headers.getFirst("Content-Disposition"));
|
| 344 | + |
| 345 | + try { |
| 346 | + headers.setContentDispositionFormData("name", "foo.txt", StandardCharsets.UTF_16); |
| 347 | + fail(); |
| 348 | + } |
| 349 | + catch (IllegalArgumentException ex) { |
| 350 | + // expected |
| 351 | + } |
| 352 | + } |
| 353 | + |
| 354 | + @Test // gh-24580 |
| 355 | + public void contentDispositionWithFilenameWithQuotes() { |
| 356 | + BiConsumer<String, String> tester = (filenameIn, filenameOut) -> { |
| 357 | + headers.setContentDispositionFormData("name", filenameIn); |
| 358 | + assertEquals("form-data; name=\"name\"; filename=\"" + filenameOut + "\"", |
| 359 | + headers.getFirst("Content-Disposition")); |
| 360 | + }; |
| 361 | + |
| 362 | + tester.accept("foo.txt", "foo.txt"); |
| 363 | + |
| 364 | + String filename = "\"foo.txt"; |
| 365 | + tester.accept(filename, "\\" + filename); |
| 366 | + |
| 367 | + filename = "\\\"foo.txt"; |
| 368 | + tester.accept(filename, filename); |
| 369 | + |
| 370 | + filename = "\\\\\"foo.txt"; |
| 371 | + tester.accept(filename, "\\" + filename); |
| 372 | + |
| 373 | + filename = "\\\\\\\"foo.txt"; |
| 374 | + tester.accept(filename, filename); |
| 375 | + |
| 376 | + filename = "\\\\\\\\\"foo.txt"; |
| 377 | + tester.accept(filename, "\\" + filename); |
| 378 | + |
| 379 | + tester.accept("\"\"foo.txt", "\\\"\\\"foo.txt"); |
| 380 | + tester.accept("\"\"\"foo.txt", "\\\"\\\"\\\"foo.txt"); |
| 381 | + |
| 382 | + tester.accept("foo.txt\\", "foo.txt"); |
| 383 | + tester.accept("foo.txt\\\\", "foo.txt\\\\"); |
| 384 | + tester.accept("foo.txt\\\\\\", "foo.txt\\\\"); |
329 | 385 | }
|
330 | 386 |
|
331 | 387 | @Test // SPR-11917
|
@@ -409,19 +465,4 @@ public void accessControlRequestMethod() {
|
409 | 465 | headers.setAccessControlRequestMethod(HttpMethod.POST);
|
410 | 466 | assertEquals(HttpMethod.POST, headers.getAccessControlRequestMethod());
|
411 | 467 | }
|
412 |
| - |
413 |
| - @Test // SPR-14547 |
414 |
| - public void encodeHeaderFieldParam() { |
415 |
| - String result = HttpHeaders.encodeHeaderFieldParam("test.txt", Charset.forName("US-ASCII")); |
416 |
| - assertEquals("test.txt", result); |
417 |
| - |
418 |
| - result = HttpHeaders.encodeHeaderFieldParam("中文.txt", Charset.forName("UTF-8")); |
419 |
| - assertEquals("UTF-8''%E4%B8%AD%E6%96%87.txt", result); |
420 |
| - } |
421 |
| - |
422 |
| - @Test(expected = IllegalArgumentException.class) |
423 |
| - public void encodeHeaderFieldParamInvalidCharset() { |
424 |
| - HttpHeaders.encodeHeaderFieldParam("test", Charset.forName("UTF-16")); |
425 |
| - } |
426 |
| - |
427 | 468 | }
|
0 commit comments