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 java.nio.charset.StandardCharsets;
31
32 import org.hamcrest.CoreMatchers;
33 import org.hamcrest.MatcherAssert;
34 import org.junit.Test;
35
36
37
38
39 public class TestPercentCodec {
40
41 @Test
42 public void testCoding() {
43 final StringBuilder buf = new StringBuilder();
44 PercentCodec.encode(buf, "blah!", StandardCharsets.UTF_8);
45 PercentCodec.encode(buf, " ~ ", StandardCharsets.UTF_8);
46 PercentCodec.encode(buf, "huh?", StandardCharsets.UTF_8);
47 MatcherAssert.assertThat(buf.toString(), CoreMatchers.equalTo("blah%21%20~%20huh%3F"));
48 }
49
50 @Test
51 public void testDecoding() {
52 MatcherAssert.assertThat(PercentCodec.decode("blah%21%20~%20huh%3F", StandardCharsets.UTF_8),
53 CoreMatchers.equalTo("blah! ~ huh?"));
54 MatcherAssert.assertThat(PercentCodec.decode("blah%21+~%20huh%3F", StandardCharsets.UTF_8),
55 CoreMatchers.equalTo("blah!+~ huh?"));
56 MatcherAssert.assertThat(PercentCodec.decode("blah%21+~%20huh%3F", StandardCharsets.UTF_8, true),
57 CoreMatchers.equalTo("blah! ~ huh?"));
58 }
59
60 @Test
61 public void testDecodingPartialContent() {
62 MatcherAssert.assertThat(PercentCodec.decode("blah%21%20%", StandardCharsets.UTF_8),
63 CoreMatchers.equalTo("blah! %"));
64 MatcherAssert.assertThat(PercentCodec.decode("blah%21%20%a", StandardCharsets.UTF_8),
65 CoreMatchers.equalTo("blah! %a"));
66 MatcherAssert.assertThat(PercentCodec.decode("blah%21%20%wa", StandardCharsets.UTF_8),
67 CoreMatchers.equalTo("blah! %wa"));
68 }
69
70 }