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