1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.fileupload2.core;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.io.UnsupportedEncodingException;
25
26 import org.junit.jupiter.api.Test;
27
28
29
30
31
32
33 public final class RFC2231UtilityTestCase {
34
35 private static void assertEncoded(final String expected, final String encoded) throws Exception {
36 assertEquals(expected, RFC2231Utils.decodeText(encoded));
37 }
38
39 @Test
40 public void testDecodeInvalidEncoding() throws Exception {
41 assertThrows(UnsupportedEncodingException.class, () -> RFC2231Utils.decodeText("abc'en'hello"));
42 }
43
44 @Test
45 public void testDecodeIso88591() throws Exception {
46 assertEncoded("\u00A3 rate", "iso-8859-1'en'%A3%20rate");
47 }
48
49 @Test
50 public void testDecodeUtf8() throws Exception {
51 assertEncoded("\u00a3 \u0061\u006e\u0064 \u20ac \u0072\u0061\u0074\u0065\u0073", "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates");
52 }
53
54 @Test
55 public void testHasEncodedValue() {
56 final var nameWithAsteriskAtEnd = "paramname*";
57 assertTrue(RFC2231Utils.hasEncodedValue(nameWithAsteriskAtEnd));
58
59 final var nameWithAsteriskNotAtEnd = "param*name";
60 assertFalse(RFC2231Utils.hasEncodedValue(nameWithAsteriskNotAtEnd));
61
62 final var nameWithoutAsterisk = "paramname";
63 assertFalse(RFC2231Utils.hasEncodedValue(nameWithoutAsterisk));
64 }
65
66 @Test
67 public void testNoNeedToDecode() throws Exception {
68 assertEncoded("abc", "abc");
69 }
70
71 @Test
72 public void testStripDelimiter() {
73 final var nameWithAsteriskAtEnd = "paramname*";
74 assertEquals("paramname", RFC2231Utils.stripDelimiter(nameWithAsteriskAtEnd));
75
76 final var nameWithAsteriskNotAtEnd = "param*name";
77 assertEquals("param*name", RFC2231Utils.stripDelimiter(nameWithAsteriskNotAtEnd));
78
79 final var nameWithTwoAsterisks = "param*name*";
80 assertEquals("param*name", RFC2231Utils.stripDelimiter(nameWithTwoAsterisks));
81
82 final var nameWithoutAsterisk = "paramname";
83 assertEquals("paramname", RFC2231Utils.stripDelimiter(nameWithoutAsterisk));
84 }
85 }