/src/test/java/org/scribe/utils/URLUtilsTest.java

https://github.com/nile/scribe-java · Java · 179 lines · 148 code · 30 blank · 1 comment · 1 complexity · 6f86d2fc08d49dbd2c3ff8bca567c232 MD5 · raw file

  1. package org.scribe.utils;
  2. import static org.junit.Assert.*;
  3. import java.util.*;
  4. import org.junit.*;
  5. public class URLUtilsTest
  6. {
  7. @Test
  8. public void shouldPercentEncodeMap()
  9. {
  10. Map<String, String> params = new LinkedHashMap<String, String>();
  11. params.put("key", "value");
  12. params.put("key with spaces", "value with spaces");
  13. params.put("&symbols!", "#!");
  14. String expected = "key=value&key+with+spaces=value+with+spaces&%26symbols%21=%23%21";
  15. assertEquals(expected, URLUtils.formURLEncodeMap(params));
  16. }
  17. @Test
  18. public void shouldReturnEmptyStringForEmptyMap()
  19. {
  20. Map<String, String> params = new LinkedHashMap<String, String>();
  21. String expected = "";
  22. assertEquals(expected, URLUtils.formURLEncodeMap(params));
  23. }
  24. @Test
  25. public void shouldFormURLEncodeMapWithMissingValues()
  26. {
  27. Map<String, String> params = new LinkedHashMap<String, String>();
  28. params.put("key", "value");
  29. params.put("key with spaces", null);
  30. String expected = "key=value&key+with+spaces";
  31. assertEquals(expected, URLUtils.formURLEncodeMap(params));
  32. }
  33. @Test
  34. public void shouldPercentEncodeString()
  35. {
  36. String toEncode = "this is a test &^";
  37. String expected = "this%20is%20a%20test%20%26%5E";
  38. assertEquals(expected, URLUtils.percentEncode(toEncode));
  39. }
  40. @Test
  41. public void shouldFormURLEncodeString()
  42. {
  43. String toEncode = "this is a test &^";
  44. String expected = "this+is+a+test+%26%5E";
  45. assertEquals(expected, URLUtils.formURLEncode(toEncode));
  46. }
  47. @Test
  48. public void shouldFormURLDecodeString()
  49. {
  50. String toDecode = "this+is+a+test+%26%5E";
  51. String expected = "this is a test &^";
  52. assertEquals(expected, URLUtils.formURLDecode(toDecode));
  53. }
  54. @Test
  55. public void shouldPercentEncodeAllSpecialCharacters()
  56. {
  57. String plain = "!*'();:@&=+$,/?#[]";
  58. String encoded = "%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D";
  59. assertEquals(encoded, URLUtils.percentEncode(plain));
  60. assertEquals(plain, URLUtils.formURLDecode(encoded));
  61. }
  62. @Test
  63. public void shouldNotPercentEncodeReservedCharacters()
  64. {
  65. String plain = "abcde123456-._~";
  66. String encoded = plain;
  67. assertEquals(encoded, URLUtils.percentEncode(plain));
  68. }
  69. @Test(expected = IllegalArgumentException.class)
  70. public void shouldThrowExceptionIfMapIsNull()
  71. {
  72. Map<String, String> nullMap = null;
  73. URLUtils.formURLEncodeMap(nullMap);
  74. }
  75. @Test(expected = IllegalArgumentException.class)
  76. public void shouldThrowExceptionIfStringToEncodeIsNull()
  77. {
  78. String toEncode = null;
  79. URLUtils.percentEncode(toEncode);
  80. }
  81. @Test(expected = IllegalArgumentException.class)
  82. public void shouldThrowExceptionIfStringToDecodeIsNull()
  83. {
  84. String toDecode = null;
  85. URLUtils.formURLDecode(toDecode);
  86. }
  87. @Test(expected = IllegalArgumentException.class)
  88. public void shouldThrowExceptionWhenAppendingNullMapToQuerystring()
  89. {
  90. String url = "http://www.example.com";
  91. Map<String, String> nullMap = null;
  92. URLUtils.appendParametersToQueryString(url, nullMap);
  93. }
  94. @Test
  95. public void shouldAppendNothingToQuerystringIfGivenEmptyMap()
  96. {
  97. String url = "http://www.example.com";
  98. Map<String, String> emptyMap = new HashMap<String, String>();
  99. String newUrl = URLUtils.appendParametersToQueryString(url, emptyMap);
  100. Assert.assertEquals(url, newUrl);
  101. }
  102. @Test
  103. public void shouldAppendParametersToSimpleUrl()
  104. {
  105. String url = "http://www.example.com";
  106. String expectedUrl = "http://www.example.com?param1=value1&param2=value+with+spaces";
  107. Map<String, String> params = new HashMap<String, String>();
  108. params.put("param1", "value1");
  109. params.put("param2", "value with spaces");
  110. url = URLUtils.appendParametersToQueryString(url, params);
  111. Assert.assertEquals(url, expectedUrl);
  112. }
  113. @Test
  114. public void shouldAppendParametersToUrlWithQuerystring()
  115. {
  116. String url = "http://www.example.com?already=present";
  117. String expectedUrl = "http://www.example.com?already=present&param1=value1&param2=value+with+spaces";
  118. Map<String, String> params = new HashMap<String, String>();
  119. params.put("param1", "value1");
  120. params.put("param2", "value with spaces");
  121. url = URLUtils.appendParametersToQueryString(url, params);
  122. Assert.assertEquals(url, expectedUrl);
  123. }
  124. @Test
  125. public void shouldPercentEncodePlusSymbol()
  126. {
  127. String plain = "7aEP+jNAwvjc0mjhqg0nuXPf";
  128. String encoded = "7aEP%2BjNAwvjc0mjhqg0nuXPf";
  129. Assert.assertEquals(encoded, URLUtils.percentEncode(plain));
  130. }
  131. @Test
  132. public void shouldURLDecodePlusSymbol()
  133. {
  134. String encoded = "oauth_verifier=7aEP%2BjNAwvjc0mjhqg0nuXPf";
  135. String expected = "oauth_verifier=7aEP+jNAwvjc0mjhqg0nuXPf";
  136. Assert.assertEquals(expected, URLUtils.formURLDecode(encoded));
  137. }
  138. @Test
  139. public void shouldPercentEncodeCorrectlyTwitterCodingExamples()
  140. {
  141. // These tests are part of the Twitter dev examples here -> https://dev.twitter.com/docs/auth/percent-encoding-parameters
  142. String sources[] = {"Ladies + Gentlemen", "An encoded string!", "Dogs, Cats & Mice"};
  143. String encoded[] = {"Ladies%20%2B%20Gentlemen", "An%20encoded%20string%21", "Dogs%2C%20Cats%20%26%20Mice"};
  144. for(int i = 0; i < sources.length; i++)
  145. {
  146. Assert.assertEquals(encoded[i], URLUtils.percentEncode(sources[i]));
  147. }
  148. }
  149. }