1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.net;
29
30 import static org.hamcrest.MatcherAssert.assertThat;
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32
33 import java.nio.charset.StandardCharsets;
34 import java.util.stream.Stream;
35
36 import org.hamcrest.CoreMatchers;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.params.ParameterizedTest;
39 import org.junit.jupiter.params.provider.MethodSource;
40
41
42
43
44 public class TestPercentCodec {
45
46 @Test
47 public void testCoding() {
48 final StringBuilder buf = new StringBuilder();
49 PercentCodec.encode(buf, "blah!", StandardCharsets.UTF_8);
50 PercentCodec.encode(buf, " ~ ", StandardCharsets.UTF_8);
51 PercentCodec.encode(buf, "huh?", StandardCharsets.UTF_8);
52 assertThat(buf.toString(), CoreMatchers.equalTo("blah%21%20~%20huh%3F"));
53 }
54
55 @Test
56 public void testDecoding() {
57 assertThat(PercentCodec.decode("blah%21%20~%20huh%3F", StandardCharsets.UTF_8),
58 CoreMatchers.equalTo("blah! ~ huh?"));
59 assertThat(PercentCodec.decode("blah%21+~%20huh%3F", StandardCharsets.UTF_8),
60 CoreMatchers.equalTo("blah!+~ huh?"));
61 assertThat(PercentCodec.decode("blah%21+~%20huh%3F", StandardCharsets.UTF_8, true),
62 CoreMatchers.equalTo("blah! ~ huh?"));
63 }
64
65 @Test
66 public void testDecodingPartialContent() {
67 assertThat(PercentCodec.decode("blah%21%20%", StandardCharsets.UTF_8),
68 CoreMatchers.equalTo("blah! %"));
69 assertThat(PercentCodec.decode("blah%21%20%a", StandardCharsets.UTF_8),
70 CoreMatchers.equalTo("blah! %a"));
71 assertThat(PercentCodec.decode("blah%21%20%wa", StandardCharsets.UTF_8),
72 CoreMatchers.equalTo("blah! %wa"));
73 }
74
75 @ParameterizedTest
76 @MethodSource("params")
77 public void testRfc5987EncodingDecoding(final String input, final String expected) {
78 assertEquals(expected, PercentCodec.RFC5987.encode(input));
79 assertEquals(input, PercentCodec.RFC5987.decode(expected));
80 }
81
82 static Stream<Object[]> params() {
83 return Stream.of(
84 new Object[]{"foo-ä-€.html", "foo-%C3%A4-%E2%82%AC.html"},
85 new Object[]{"世界ーファイル 2.jpg", "%E4%B8%96%E7%95%8C%E3%83%BC%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%202.jpg"},
86 new Object[]{"foo.jpg", "foo.jpg"},
87 new Object[]{"simple", "simple"},
88 new Object[]{"reserved/chars?", "reserved%2Fchars%3F"},
89 new Object[]{"", ""},
90 new Object[]{"space test", "space%20test"},
91 new Object[]{"ümlaut", "%C3%BCmlaut"}
92 );
93 }
94
95 @Test
96 public void verifyRfc5987EncodingandDecoding() {
97 final String s = "!\"$£%^&*()_-+={[}]:@~;'#,./<>?\\|✓éèæðŃœ";
98 assertThat(PercentCodec.RFC5987.decode(PercentCodec.RFC5987.encode(s)), CoreMatchers.equalTo(s));
99 }
100
101 }