From 70a217c4d6bd4ab6cf5602f845612ce55c02bcfe Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Mon, 25 Jun 2012 18:58:17 +0000 Subject: [PATCH] Add tests for charset String version of parse git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1353699 13f79535-47bb-0310-9956-ffa450edef68 --- .../client/utils/TestURLEncodedUtils.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/httpclient/src/test/java/org/apache/http/client/utils/TestURLEncodedUtils.java b/httpclient/src/test/java/org/apache/http/client/utils/TestURLEncodedUtils.java index ee774804c..2934b5d63 100644 --- a/httpclient/src/test/java/org/apache/http/client/utils/TestURLEncodedUtils.java +++ b/httpclient/src/test/java/org/apache/http/client/utils/TestURLEncodedUtils.java @@ -27,6 +27,7 @@ package org.apache.http.client.utils; +import java.net.URI; import java.util.ArrayList; import java.util.List; @@ -96,6 +97,61 @@ public class TestURLEncodedUtils { assertNameValuePair(result.get(0), "price", "10 \u20AC"); } + @Test + public void testParseURLCodedContentString () throws Exception { + List result; + + result = parseString(""); + Assert.assertTrue(result.isEmpty()); + + result = parseString("Name0"); + Assert.assertEquals(1, result.size()); + assertNameValuePair(result.get(0), "Name0", null); + + result = parseString("Name1=Value1"); + Assert.assertEquals(1, result.size()); + assertNameValuePair(result.get(0), "Name1", "Value1"); + + result = parseString("Name2="); + Assert.assertEquals(1, result.size()); + assertNameValuePair(result.get(0), "Name2", ""); + + result = parseString("Name3"); + Assert.assertEquals(1, result.size()); + assertNameValuePair(result.get(0), "Name3", null); + + result = parseString("Name4=Value%204%21"); + Assert.assertEquals(1, result.size()); + assertNameValuePair(result.get(0), "Name4", "Value 4!"); + + result = parseString("Name4=Value%2B4%21"); + Assert.assertEquals(1, result.size()); + assertNameValuePair(result.get(0), "Name4", "Value+4!"); + + result = parseString("Name4=Value%204%21%20%214"); + Assert.assertEquals(1, result.size()); + assertNameValuePair(result.get(0), "Name4", "Value 4! !4"); + + result = parseString("Name5=aaa&Name6=bbb"); + Assert.assertEquals(2, result.size()); + assertNameValuePair(result.get(0), "Name5", "aaa"); + assertNameValuePair(result.get(1), "Name6", "bbb"); + + result = parseString("Name7=aaa&Name7=b%2Cb&Name7=ccc"); + Assert.assertEquals(3, result.size()); + assertNameValuePair(result.get(0), "Name7", "aaa"); + assertNameValuePair(result.get(1), "Name7", "b,b"); + assertNameValuePair(result.get(2), "Name7", "ccc"); + + result = parseString("Name8=xx%2C%20%20yy%20%20%2Czz"); + Assert.assertEquals(1, result.size()); + assertNameValuePair(result.get(0), "Name8", "xx, yy ,zz"); + + result = parseString("price=10%20%E2%82%AC"); + Assert.assertEquals(1, result.size()); + assertNameValuePair(result.get(0), "price", "10 \u20AC"); + } + @Test public void testParseInvalidURLCodedContent () throws Exception { List result; @@ -310,6 +366,10 @@ public class TestURLEncodedUtils { return URLEncodedUtils.parse(params, Consts.UTF_8); } + private List parseString (final String uri) throws Exception { + return URLEncodedUtils.parse(new URI("?"+uri), "UTF-8"); + } + private static void assertNameValuePair ( final NameValuePair parameter, final String expectedName,