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 java.nio.charset.StandardCharsets;
31  
32  import org.hamcrest.CoreMatchers;
33  import org.hamcrest.MatcherAssert;
34  import org.junit.Test;
35  
36  /**
37   * Unit tests for {@link PercentCodec}.
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  }