Added formURLEncode/Decode methods

This commit is contained in:
Pablo Fernandez committed 2011-03-13 10:21:39 -03:00
1 parent 70358ad651
commit e0e8df98d3
2 files changed
+79 -34

No files matched your search

@@ -16,7 +16,7 @@ public class URLUtilsTest
params.put("key with spaces", "value with spaces");
params.put("&symbols!", "#!");
String expected = "key=value&key%20with%20spaces=value%20with%20spaces&%26symbols%21=%23%21";
String expected = "key=value&key+with+spaces=value+with+spaces&%26symbols%21=%23%21";
assertEquals(expected, URLUtils.formURLEncodeMap(params));
}
@@ -28,6 +28,17 @@ public class URLUtilsTest
assertEquals(expected, URLUtils.formURLEncodeMap(params));
}
@Test
public void shouldFormURLEncodeMapWithMissingValues()
{
Map<String, String> params = new LinkedHashMap<String, String>();
params.put("key", "value");
params.put("key with spaces", null);
String expected = "key=value&key+with+spaces";
assertEquals(expected, URLUtils.formURLEncodeMap(params));
}
@Test
public void shouldPercentEncodeString()
{
@@ -37,24 +48,32 @@ public class URLUtilsTest
}
@Test
public void shouldPercentDecodeString()
public void shouldFormURLEncodeString()
{
String toDecode = "this+is+a+test+%26%5E";
String expected = "this is a test &^";
assertEquals(expected, URLUtils.percentDecode(toDecode));
String toEncode = "this is a test &^";
String expected = "this+is+a+test+%26%5E";
assertEquals(expected, URLUtils.formURLEncode(toEncode));
}
@Test
public void shouldEncodeAllSpecialCharacters()
public void shouldFormURLDecodeString()
{
String toDecode = "this+is+a+test+%26%5E";
String expected = "this is a test &^";
assertEquals(expected, URLUtils.formURLDecode(toDecode));
}
@Test
public void shouldPercentEncodeAllSpecialCharacters()
{
String plain = "!*'();:@&=+$,/?#[]";
String encoded = "%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D";
assertEquals(encoded, URLUtils.percentEncode(plain));
assertEquals(plain, URLUtils.percentDecode(encoded));
assertEquals(plain, URLUtils.formURLDecode(encoded));
}
@Test
public void shouldNotEncodeReservedCharacters()
public void shouldNotPercentEncodeReservedCharacters()
{
String plain = "abcde123456-._~";
String encoded = plain;
@@ -79,7 +98,7 @@ public class URLUtilsTest
public void shouldThrowExceptionIfStringToDecodeIsNull()
{
String toDecode = null;
URLUtils.percentDecode(toDecode);
URLUtils.formURLDecode(toDecode);
}
@Test(expected = IllegalArgumentException.class)
@@ -103,7 +122,7 @@ public class URLUtilsTest
public void shouldAppendParametersToSimpleUrl()
{
String url = "http://www.example.com";
String expectedUrl = "http://www.example.com?param1=value1&param2=value%20with%20spaces";
String expectedUrl = "http://www.example.com?param1=value1&param2=value+with+spaces";
Map<String, String> params = new HashMap<String, String>();
params.put("param1", "value1");
@@ -117,7 +136,7 @@ public class URLUtilsTest
public void shouldAppendParametersToUrlWithQuerystring()
{
String url = "http://www.example.com?already=present";
String expectedUrl = "http://www.example.com?already=present&param1=value1&param2=value%20with%20spaces";
String expectedUrl = "http://www.example.com?already=present&param1=value1&param2=value+with+spaces";
Map<String, String> params = new HashMap<String, String>();
params.put("param1", "value1");
@@ -128,7 +147,7 @@ public class URLUtilsTest
}
@Test
public void shouldEncodePlusSymbol()
public void shouldPercentEncodePlusSymbol()
{
String plain = "7aEP+jNAwvjc0mjhqg0nuXPf";
String encoded = "7aEP%2BjNAwvjc0mjhqg0nuXPf";
@@ -137,11 +156,11 @@ public class URLUtilsTest
}
@Test
public void shouldDecodePlusSymbol()
public void shouldURLDecodePlusSymbol()
{
String encoded = "oauth_verifier=7aEP%2BjNAwvjc0mjhqg0nuXPf";
String expected = "oauth_verifier=7aEP+jNAwvjc0mjhqg0nuXPf";
Assert.assertEquals(expected, URLUtils.percentDecode(encoded));
Assert.assertEquals(expected, URLUtils.formURLDecode(encoded));
}
}