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.assertThrows;
21
22 import java.io.UnsupportedEncodingException;
23
24 import org.junit.jupiter.api.Test;
25
26
27
28
29 public final class MimeUtilityTestCase {
30
31 private static void assertEncoded(final String expected, final String encoded) throws Exception {
32 assertEquals(expected, MimeUtils.decodeText(encoded));
33 }
34
35 @Test
36 public void testDecodeInvalidEncoding() {
37 assertThrows(UnsupportedEncodingException.class, () -> MimeUtils.decodeText("=?invalid?B?xyz-?="));
38 }
39
40 @Test
41 public void testDecodeIso88591Base64Encoded() throws Exception {
42 assertEncoded("If you can read this you understand the example.",
43 "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= " + "=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n");
44 }
45
46 @Test
47 public void testDecodeIso88591Base64EncodedWithWhiteSpace() throws Exception {
48 assertEncoded("If you can read this you understand the example.",
49 "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\t \r\n =?ISO-8859-" + "2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n");
50 }
51
52 @Test
53 public void testDecodeUtf8Base64Encoded() throws Exception {
54 assertEncoded(" h\u00e9! \u00e0\u00e8\u00f4u !!!", "=?UTF-8?B?IGjDqSEgw6DDqMO0dSAhISE=?=");
55 }
56
57 @Test
58 public void testDecodeUtf8QuotedPrintableEncoded() throws Exception {
59 assertEncoded(" h\u00e9! \u00e0\u00e8\u00f4u !!!", "=?UTF-8?Q?_h=C3=A9!_=C3=A0=C3=A8=C3=B4u_!!!?=");
60 }
61
62 @Test
63 public void testNoNeedToDecode() throws Exception {
64 assertEncoded("abc", "abc");
65 }
66 }