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