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  
32  import java.nio.charset.StandardCharsets;
33  import java.util.Arrays;
34  import java.util.List;
35  
36  import org.apache.hc.core5.http.NameValuePair;
37  import org.apache.hc.core5.http.NameValuePairListMatcher;
38  import org.apache.hc.core5.http.message.BasicNameValuePair;
39  import org.hamcrest.CoreMatchers;
40  import org.junit.jupiter.api.Test;
41  
42  public class TestWWWFormCodec {
43  
44      private static final String CH_HELLO = "\u0047\u0072\u00FC\u0065\u007A\u0069\u005F\u007A\u00E4\u006D\u00E4";
45      private static final String RU_HELLO = "\u0412\u0441\u0435\u043C\u005F\u043F\u0440\u0438\u0432\u0435\u0442";
46  
47      private static List<NameValuePair> parse(final String params) {
48          return WWWFormCodec.parse(params, StandardCharsets.UTF_8);
49      }
50  
51      @Test
52      public void testParse() throws Exception {
53          assertThat(parse(""), NameValuePairListMatcher.isEmpty());
54          assertThat(parse("Name0"),
55                  NameValuePairListMatcher.equalsTo(new BasicNameValuePair("Name0", null)));
56          assertThat(parse("Name1=Value1"),
57                  NameValuePairListMatcher.equalsTo(new BasicNameValuePair("Name1", "Value1")));
58          assertThat(parse("Name2="),
59                  NameValuePairListMatcher.equalsTo(new BasicNameValuePair("Name2", "")));
60          assertThat(parse(" Name3  "),
61                  NameValuePairListMatcher.equalsTo(new BasicNameValuePair("Name3", null)));
62          assertThat(parse("Name4=Value%204%21"),
63                  NameValuePairListMatcher.equalsTo(new BasicNameValuePair("Name4", "Value 4!")));
64          assertThat(parse("Name4=Value%2B4%21"),
65                  NameValuePairListMatcher.equalsTo(new BasicNameValuePair("Name4", "Value+4!")));
66          assertThat(parse("Name4=Value%204%21%20%214"),
67                  NameValuePairListMatcher.equalsTo(new BasicNameValuePair("Name4", "Value 4! !4")));
68          assertThat(parse("Name5=aaa&Name6=bbb"),
69                  NameValuePairListMatcher.equalsTo(
70                          new BasicNameValuePair("Name5", "aaa"),
71                          new BasicNameValuePair("Name6", "bbb")));
72          assertThat(parse("Name7=aaa&Name7=b%2Cb&Name7=ccc"),
73                  NameValuePairListMatcher.equalsTo(
74                          new BasicNameValuePair("Name7", "aaa"),
75                          new BasicNameValuePair("Name7", "b,b"),
76                          new BasicNameValuePair("Name7", "ccc")));
77          assertThat(parse("Name8=xx%2C%20%20yy%20%20%2Czz"),
78                  NameValuePairListMatcher.equalsTo(new BasicNameValuePair("Name8", "xx,  yy  ,zz")));
79          assertThat(parse("price=10%20%E2%82%AC"),
80                  NameValuePairListMatcher.equalsTo(new BasicNameValuePair("price", "10 \u20AC")));
81          assertThat(parse("a=b\"c&d=e"),
82                  NameValuePairListMatcher.equalsTo(
83                          new BasicNameValuePair("a", "b\"c"),
84                          new BasicNameValuePair("d", "e")));
85          assertThat(parse("russian=" + PercentCodec.encode(RU_HELLO, StandardCharsets.UTF_8) +
86                          "&swiss=" + PercentCodec.encode(CH_HELLO, StandardCharsets.UTF_8)),
87                  NameValuePairListMatcher.equalsTo(
88                          new BasicNameValuePair("russian", RU_HELLO),
89                          new BasicNameValuePair("swiss", CH_HELLO)));
90      }
91  
92      private static String format(final NameValuePair... nvps) {
93          return WWWFormCodec.format(Arrays.asList(nvps), StandardCharsets.UTF_8);
94      }
95  
96      @Test
97      public void testFormat() throws Exception {
98          assertThat(format(new BasicNameValuePair("Name0", null)), CoreMatchers.equalTo("Name0"));
99          assertThat(format(new BasicNameValuePair("Name1", "Value1")), CoreMatchers.equalTo("Name1=Value1"));
100         assertThat(format(new BasicNameValuePair("Name2", "")), CoreMatchers.equalTo("Name2="));
101         assertThat(format(new BasicNameValuePair("Name4", "Value 4&")),
102                 CoreMatchers.equalTo("Name4=Value+4%26"));
103         assertThat(format(new BasicNameValuePair("Name4", "Value+4&")),
104                 CoreMatchers.equalTo("Name4=Value%2B4%26"));
105         assertThat(format(new BasicNameValuePair("Name4", "Value 4& =4")),
106                 CoreMatchers.equalTo("Name4=Value+4%26+%3D4"));
107         assertThat(format(
108                 new BasicNameValuePair("Name5", "aaa"),
109                 new BasicNameValuePair("Name6", "bbb")), CoreMatchers.equalTo("Name5=aaa&Name6=bbb"));
110         assertThat(format(
111                 new BasicNameValuePair("Name7", "aaa"),
112                 new BasicNameValuePair("Name7", "b,b"),
113                 new BasicNameValuePair("Name7", "ccc")
114         ), CoreMatchers.equalTo("Name7=aaa&Name7=b%2Cb&Name7=ccc"));
115         assertThat(format(new BasicNameValuePair("Name8", "xx,  yy  ,zz")),
116                 CoreMatchers.equalTo("Name8=xx%2C++yy++%2Czz"));
117         assertThat(format(
118                 new BasicNameValuePair("russian", RU_HELLO),
119                 new BasicNameValuePair("swiss", CH_HELLO)),
120                 CoreMatchers.equalTo("russian=" + PercentCodec.encode(RU_HELLO, StandardCharsets.UTF_8) +
121                         "&swiss=" + PercentCodec.encode(CH_HELLO, StandardCharsets.UTF_8)));
122     }
123 
124 }