1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.fileupload.util.mime;
18
19 import static org.junit.Assert.assertEquals;
20
21 import java.io.UnsupportedEncodingException;
22
23 import org.junit.Test;
24
25
26
27
28
29
30
31 public final class MimeUtilityTestCase {
32
33 @Test
34 public void noNeedToDecode() throws Exception {
35 assertEncoded("abc", "abc");
36 }
37
38 @Test
39 public void decodeUtf8QuotedPrintableEncoded() throws Exception {
40 assertEncoded(" h\u00e9! \u00e0\u00e8\u00f4u !!!", "=?UTF-8?Q?_h=C3=A9!_=C3=A0=C3=A8=C3=B4u_!!!?=");
41 }
42
43 @Test
44 public void decodeUtf8Base64Encoded() throws Exception {
45 assertEncoded(" h\u00e9! \u00e0\u00e8\u00f4u !!!", "=?UTF-8?B?IGjDqSEgw6DDqMO0dSAhISE=?=");
46 }
47
48 @Test
49 public void decodeIso88591Base64Encoded() throws Exception {
50 assertEncoded("If you can read this you understand the example.",
51 "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n");
52 }
53
54 @Test
55 public void decodeIso88591Base64EncodedWithWhiteSpace() throws Exception {
56 assertEncoded("If you can read this you understand the example.",
57 "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\t \r\n =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n");
58 }
59
60 private static void assertEncoded(String expected, String encoded) throws Exception {
61 assertEquals(expected, MimeUtility.decodeText(encoded));
62 }
63
64 @Test(expected=UnsupportedEncodingException.class)
65 public void decodeInvalidEncoding() throws Exception {
66 MimeUtility.decodeText("=?invalid?B?xyz-?=");
67 }
68 }