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.http.client.utils;
29  
30  import java.net.URI;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import org.apache.http.Consts;
35  import org.apache.http.NameValuePair;
36  import org.apache.http.entity.ContentType;
37  import org.apache.http.entity.StringEntity;
38  import org.apache.http.message.BasicNameValuePair;
39  import org.apache.http.protocol.HTTP;
40  import org.junit.Assert;
41  import org.junit.Test;
42  
43  public class TestURLEncodedUtils {
44  
45      @Test
46      public void testParseURLCodedContent() throws Exception {
47          List <NameValuePair> result;
48  
49          result = parse("");
50          Assert.assertTrue(result.isEmpty());
51  
52          result = parse("Name0");
53          Assert.assertEquals(1, result.size());
54          assertNameValuePair(result.get(0), "Name0", null);
55  
56          result = parse("Name1=Value1");
57          Assert.assertEquals(1, result.size());
58          assertNameValuePair(result.get(0), "Name1", "Value1");
59  
60          result = parse("Name2=");
61          Assert.assertEquals(1, result.size());
62          assertNameValuePair(result.get(0), "Name2", "");
63  
64          result = parse("Name3");
65          Assert.assertEquals(1, result.size());
66          assertNameValuePair(result.get(0), "Name3", null);
67  
68          result = parse("Name4=Value%204%21");
69          Assert.assertEquals(1, result.size());
70          assertNameValuePair(result.get(0), "Name4", "Value 4!");
71  
72          result = parse("Name4=Value%2B4%21");
73          Assert.assertEquals(1, result.size());
74          assertNameValuePair(result.get(0), "Name4", "Value+4!");
75  
76          result = parse("Name4=Value%204%21%20%214");
77          Assert.assertEquals(1, result.size());
78          assertNameValuePair(result.get(0), "Name4", "Value 4! !4");
79  
80          result = parse("Name5=aaa&Name6=bbb");
81          Assert.assertEquals(2, result.size());
82          assertNameValuePair(result.get(0), "Name5", "aaa");
83          assertNameValuePair(result.get(1), "Name6", "bbb");
84  
85          result = parse("Name7=aaa&Name7=b%2Cb&Name7=ccc");
86          Assert.assertEquals(3, result.size());
87          assertNameValuePair(result.get(0), "Name7", "aaa");
88          assertNameValuePair(result.get(1), "Name7", "b,b");
89          assertNameValuePair(result.get(2), "Name7", "ccc");
90  
91          result = parse("Name8=xx%2C%20%20yy%20%20%2Czz");
92          Assert.assertEquals(1, result.size());
93          assertNameValuePair(result.get(0), "Name8", "xx,  yy  ,zz");
94  
95          result = parse("price=10%20%E2%82%AC");
96          Assert.assertEquals(1, result.size());
97          assertNameValuePair(result.get(0), "price", "10 \u20AC");
98      }
99  
100     @Test
101     public void testParseURLCodedContentString() throws Exception {
102         List <NameValuePair> result;
103 
104         result = parseString("");
105         Assert.assertTrue(result.isEmpty());
106 
107         result = parseString("Name0");
108         Assert.assertEquals(1, result.size());
109         assertNameValuePair(result.get(0), "Name0", null);
110 
111         result = parseString("Name1=Value1");
112         Assert.assertEquals(1, result.size());
113         assertNameValuePair(result.get(0), "Name1", "Value1");
114 
115         result = parseString("Name2=");
116         Assert.assertEquals(1, result.size());
117         assertNameValuePair(result.get(0), "Name2", "");
118 
119         result = parseString("Name3");
120         Assert.assertEquals(1, result.size());
121         assertNameValuePair(result.get(0), "Name3", null);
122 
123         result = parseString("Name4=Value%204%21");
124         Assert.assertEquals(1, result.size());
125         assertNameValuePair(result.get(0), "Name4", "Value 4!");
126 
127         result = parseString("Name4=Value%2B4%21");
128         Assert.assertEquals(1, result.size());
129         assertNameValuePair(result.get(0), "Name4", "Value+4!");
130 
131         result = parseString("Name4=Value%204%21%20%214");
132         Assert.assertEquals(1, result.size());
133         assertNameValuePair(result.get(0), "Name4", "Value 4! !4");
134 
135         result = parseString("Name5=aaa&Name6=bbb");
136         Assert.assertEquals(2, result.size());
137         assertNameValuePair(result.get(0), "Name5", "aaa");
138         assertNameValuePair(result.get(1), "Name6", "bbb");
139 
140         result = parseString("Name7=aaa&Name7=b%2Cb&Name7=ccc");
141         Assert.assertEquals(3, result.size());
142         assertNameValuePair(result.get(0), "Name7", "aaa");
143         assertNameValuePair(result.get(1), "Name7", "b,b");
144         assertNameValuePair(result.get(2), "Name7", "ccc");
145 
146         result = parseString("Name8=xx%2C%20%20yy%20%20%2Czz");
147         Assert.assertEquals(1, result.size());
148         assertNameValuePair(result.get(0), "Name8", "xx,  yy  ,zz");
149 
150         result = parseString("price=10%20%E2%82%AC");
151         Assert.assertEquals(1, result.size());
152         assertNameValuePair(result.get(0), "price", "10 \u20AC");
153     }
154 
155     @Test
156     public void testParseInvalidURLCodedContent() throws Exception {
157         List <NameValuePair> result;
158 
159         result = parse("name=%");
160         Assert.assertEquals(1, result.size());
161         assertNameValuePair(result.get(0), "name", "%");
162 
163         result = parse("name=%a");
164         Assert.assertEquals(1, result.size());
165         assertNameValuePair(result.get(0), "name", "%a");
166 
167         result = parse("name=%wa%20");
168         Assert.assertEquals(1, result.size());
169         assertNameValuePair(result.get(0), "name", "%wa ");
170     }
171 
172     @Test
173     public void testParseEntity() throws Exception {
174         final StringEntity entity = new StringEntity("Name1=Value1");
175 
176         entity.setContentType(URLEncodedUtils.CONTENT_TYPE);
177         final List <NameValuePair> result = URLEncodedUtils.parse(entity);
178         Assert.assertEquals(1, result.size());
179         assertNameValuePair(result.get(0), "Name1", "Value1");
180 
181         entity.setContentType("text/test");
182         Assert.assertTrue(URLEncodedUtils.parse(entity).isEmpty());
183     }
184 
185     private static final int SWISS_GERMAN_HELLO [] = {
186         0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
187     };
188 
189     private static final int RUSSIAN_HELLO [] = {
190         0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438,
191         0x432, 0x435, 0x442
192     };
193 
194     private static String constructString(final int [] unicodeChars) {
195         final StringBuffer buffer = new StringBuffer();
196         if (unicodeChars != null) {
197             for (final int unicodeChar : unicodeChars) {
198                 buffer.append((char)unicodeChar);
199             }
200         }
201         return buffer.toString();
202     }
203 
204     @Test
205     public void testParseUTF8Entity() throws Exception {
206         final String ru_hello = constructString(RUSSIAN_HELLO);
207         final String ch_hello = constructString(SWISS_GERMAN_HELLO);
208         final List <NameValuePair> parameters = new ArrayList<NameValuePair>();
209         parameters.add(new BasicNameValuePair("russian", ru_hello));
210         parameters.add(new BasicNameValuePair("swiss", ch_hello));
211 
212         final String s = URLEncodedUtils.format(parameters, Consts.UTF_8);
213 
214         Assert.assertEquals("russian=%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82" +
215                 "&swiss=Gr%C3%BCezi_z%C3%A4m%C3%A4", s);
216 
217         final StringEntity entity = new StringEntity(s, ContentType.create(
218                 URLEncodedUtils.CONTENT_TYPE, Consts.UTF_8));
219         final List <NameValuePair> result = URLEncodedUtils.parse(entity);
220         Assert.assertEquals(2, result.size());
221         assertNameValuePair(result.get(0), "russian", ru_hello);
222         assertNameValuePair(result.get(1), "swiss", ch_hello);
223     }
224 
225     @Test
226     public void testParseUTF8Ampersand1String() throws Exception {
227         final String ru_hello = constructString(RUSSIAN_HELLO);
228         final String ch_hello = constructString(SWISS_GERMAN_HELLO);
229         final List <NameValuePair> parameters = new ArrayList<NameValuePair>();
230         parameters.add(new BasicNameValuePair("russian", ru_hello));
231         parameters.add(new BasicNameValuePair("swiss", ch_hello));
232 
233         final String s = URLEncodedUtils.format(parameters, Consts.UTF_8);
234 
235         final List <NameValuePair> result = URLEncodedUtils.parse(s, Consts.UTF_8);
236         Assert.assertEquals(2, result.size());
237         assertNameValuePair(result.get(0), "russian", ru_hello);
238         assertNameValuePair(result.get(1), "swiss", ch_hello);
239     }
240 
241     @Test
242     public void testParseUTF8Ampersand2String() throws Exception {
243         testParseUTF8String('&');
244     }
245 
246     @Test
247     public void testParseUTF8SemicolonString() throws Exception {
248         testParseUTF8String(';');
249     }
250 
251     private void testParseUTF8String(final char parameterSeparator) throws Exception {
252         final String ru_hello = constructString(RUSSIAN_HELLO);
253         final String ch_hello = constructString(SWISS_GERMAN_HELLO);
254         final List <NameValuePair> parameters = new ArrayList<NameValuePair>();
255         parameters.add(new BasicNameValuePair("russian", ru_hello));
256         parameters.add(new BasicNameValuePair("swiss", ch_hello));
257 
258         final String s = URLEncodedUtils.format(parameters, parameterSeparator, Consts.UTF_8);
259 
260         final List <NameValuePair> result1 = URLEncodedUtils.parse(s, Consts.UTF_8);
261         Assert.assertEquals(2, result1.size());
262         assertNameValuePair(result1.get(0), "russian", ru_hello);
263         assertNameValuePair(result1.get(1), "swiss", ch_hello);
264 
265         final List <NameValuePair> result2 = URLEncodedUtils.parse(s, Consts.UTF_8, parameterSeparator);
266         Assert.assertEquals(result1, result2);
267     }
268 
269     @Test
270     public void testParseEntityDefaultContentType() throws Exception {
271         final String ch_hello = constructString(SWISS_GERMAN_HELLO);
272         final String us_hello = "hi there";
273         final List <NameValuePair> parameters = new ArrayList<NameValuePair>();
274         parameters.add(new BasicNameValuePair("english", us_hello));
275         parameters.add(new BasicNameValuePair("swiss", ch_hello));
276 
277         final String s = URLEncodedUtils.format(parameters, HTTP.DEF_CONTENT_CHARSET);
278 
279         Assert.assertEquals("english=hi+there&swiss=Gr%FCezi_z%E4m%E4", s);
280 
281         final StringEntity entity = new StringEntity(s, ContentType.create(
282                 URLEncodedUtils.CONTENT_TYPE, HTTP.DEF_CONTENT_CHARSET));
283         final List <NameValuePair> result = URLEncodedUtils.parse(entity);
284         Assert.assertEquals(2, result.size());
285         assertNameValuePair(result.get(0), "english", us_hello);
286         assertNameValuePair(result.get(1), "swiss", ch_hello);
287     }
288 
289     @Test
290     public void testIsEncoded() throws Exception {
291         final StringEntity entity = new StringEntity("...");
292 
293         entity.setContentType(URLEncodedUtils.CONTENT_TYPE);
294         Assert.assertTrue(URLEncodedUtils.isEncoded(entity));
295 
296         entity.setContentType(URLEncodedUtils.CONTENT_TYPE + "; charset=US-ASCII");
297         Assert.assertTrue(URLEncodedUtils.isEncoded(entity));
298 
299         entity.setContentType("text/test");
300         Assert.assertFalse(URLEncodedUtils.isEncoded(entity));
301     }
302 
303     @Test
304     public void testFormat() throws Exception {
305         final List <NameValuePair> params = new ArrayList <NameValuePair>();
306         Assert.assertEquals(0, URLEncodedUtils.format(params, Consts.ASCII).length());
307 
308         params.clear();
309         params.add(new BasicNameValuePair("Name0", null));
310         Assert.assertEquals("Name0", URLEncodedUtils.format(params, Consts.ASCII));
311 
312         params.clear();
313         params.add(new BasicNameValuePair("Name1", "Value1"));
314         Assert.assertEquals("Name1=Value1", URLEncodedUtils.format(params, Consts.ASCII));
315 
316         params.clear();
317         params.add(new BasicNameValuePair("Name2", ""));
318         Assert.assertEquals("Name2=", URLEncodedUtils.format(params, Consts.ASCII));
319 
320         params.clear();
321         params.add(new BasicNameValuePair("Name4", "Value 4&"));
322         Assert.assertEquals("Name4=Value+4%26", URLEncodedUtils.format(params, Consts.ASCII));
323 
324         params.clear();
325         params.add(new BasicNameValuePair("Name4", "Value+4&"));
326         Assert.assertEquals("Name4=Value%2B4%26", URLEncodedUtils.format(params, Consts.ASCII));
327 
328         params.clear();
329         params.add(new BasicNameValuePair("Name4", "Value 4& =4"));
330         Assert.assertEquals("Name4=Value+4%26+%3D4", URLEncodedUtils.format(params, Consts.ASCII));
331 
332         params.clear();
333         params.add(new BasicNameValuePair("Name5", "aaa"));
334         params.add(new BasicNameValuePair("Name6", "bbb"));
335         Assert.assertEquals("Name5=aaa&Name6=bbb", URLEncodedUtils.format(params, Consts.ASCII));
336 
337         params.clear();
338         params.add(new BasicNameValuePair("Name7", "aaa"));
339         params.add(new BasicNameValuePair("Name7", "b,b"));
340         params.add(new BasicNameValuePair("Name7", "ccc"));
341         Assert.assertEquals("Name7=aaa&Name7=b%2Cb&Name7=ccc", URLEncodedUtils.format(params, Consts.ASCII));
342         Assert.assertEquals("Name7=aaa&Name7=b%2Cb&Name7=ccc", URLEncodedUtils.format(params, '&', Consts.ASCII));
343         Assert.assertEquals("Name7=aaa;Name7=b%2Cb;Name7=ccc", URLEncodedUtils.format(params, ';', Consts.ASCII));
344 
345         params.clear();
346         params.add(new BasicNameValuePair("Name8", "xx,  yy  ,zz"));
347         Assert.assertEquals("Name8=xx%2C++yy++%2Czz", URLEncodedUtils.format(params, Consts.ASCII));
348     }
349 
350     @Test
351     public void testFormatString() throws Exception { // as above, using String
352         final List <NameValuePair> params = new ArrayList <NameValuePair>();
353         Assert.assertEquals(0, URLEncodedUtils.format(params, "US-ASCII").length());
354 
355         params.clear();
356         params.add(new BasicNameValuePair("Name0", null));
357         Assert.assertEquals("Name0", URLEncodedUtils.format(params, "US-ASCII"));
358 
359         params.clear();
360         params.add(new BasicNameValuePair("Name1", "Value1"));
361         Assert.assertEquals("Name1=Value1", URLEncodedUtils.format(params, "US-ASCII"));
362 
363         params.clear();
364         params.add(new BasicNameValuePair("Name2", ""));
365         Assert.assertEquals("Name2=", URLEncodedUtils.format(params, "US-ASCII"));
366 
367         params.clear();
368         params.add(new BasicNameValuePair("Name4", "Value 4&"));
369         Assert.assertEquals("Name4=Value+4%26", URLEncodedUtils.format(params, "US-ASCII"));
370 
371         params.clear();
372         params.add(new BasicNameValuePair("Name4", "Value+4&"));
373         Assert.assertEquals("Name4=Value%2B4%26", URLEncodedUtils.format(params, "US-ASCII"));
374 
375         params.clear();
376         params.add(new BasicNameValuePair("Name4", "Value 4& =4"));
377         Assert.assertEquals("Name4=Value+4%26+%3D4", URLEncodedUtils.format(params, "US-ASCII"));
378 
379         params.clear();
380         params.add(new BasicNameValuePair("Name5", "aaa"));
381         params.add(new BasicNameValuePair("Name6", "bbb"));
382         Assert.assertEquals("Name5=aaa&Name6=bbb", URLEncodedUtils.format(params, "US-ASCII"));
383 
384         params.clear();
385         params.add(new BasicNameValuePair("Name7", "aaa"));
386         params.add(new BasicNameValuePair("Name7", "b,b"));
387         params.add(new BasicNameValuePair("Name7", "ccc"));
388         Assert.assertEquals("Name7=aaa&Name7=b%2Cb&Name7=ccc", URLEncodedUtils.format(params, "US-ASCII"));
389 
390         params.clear();
391         params.add(new BasicNameValuePair("Name8", "xx,  yy  ,zz"));
392         Assert.assertEquals("Name8=xx%2C++yy++%2Czz", URLEncodedUtils.format(params, "US-ASCII"));
393     }
394 
395     private List <NameValuePair> parse (final String params) {
396         return URLEncodedUtils.parse(params, Consts.UTF_8);
397     }
398 
399     private List <NameValuePair> parseString (final String uri) throws Exception {
400         return URLEncodedUtils.parse(new URI("?"+uri), "UTF-8");
401     }
402 
403     private static void assertNameValuePair (
404             final NameValuePair parameter,
405             final String expectedName,
406             final String expectedValue) {
407         Assert.assertEquals(parameter.getName(), expectedName);
408         Assert.assertEquals(parameter.getValue(), expectedValue);
409     }
410 
411 }