View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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   * Unit tests for {@link PercentCodec}.
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"},  // Unreserved characters
88                  new Object[]{"reserved/chars?", "reserved%2Fchars%3F"},  // Reserved characters
89                  new Object[]{"", ""},  // Empty string
90                  new Object[]{"space test", "space%20test"},  // String with space
91                  new Object[]{"ümlaut", "%C3%BCmlaut"}  // Non-ASCII characters
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 }