/twitter-core/src/test/java/com/twitter/sdk/android/core/internal/network/UrlUtilsTest.java

https://github.com/twitter/twitter-kit-android · Java · 121 lines · 88 code · 17 blank · 16 comment · 1 complexity · 561c71ff4b2a1dc35640174c0f3a30d1 MD5 · raw file

  1. /*
  2. * Copyright (C) 2015 Twitter, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package com.twitter.sdk.android.core.internal.network;
  18. import org.junit.Test;
  19. import org.junit.runner.RunWith;
  20. import org.robolectric.RobolectricTestRunner;
  21. import java.net.URI;
  22. import java.util.TreeMap;
  23. import static org.junit.Assert.assertEquals;
  24. import static org.junit.Assert.assertNotNull;
  25. @RunWith(RobolectricTestRunner.class)
  26. public class UrlUtilsTest {
  27. private static final String[] ORIGINAL_STRINGS = new String[]{
  28. "Ladies + Gentlemen",
  29. "An encoded string!",
  30. "Dogs, Cats & Mice",
  31. "☃",
  32. "~`!@#$%^&*()+=,<.>?/",
  33. "-._~"
  34. };
  35. private static final String[] PERCENT_ENCODED_STRINGS = new String[]{
  36. "Ladies%20%2B%20Gentlemen",
  37. "An%20encoded%20string%21",
  38. "Dogs%2C%20Cats%20%26%20Mice",
  39. "%E2%98%83",
  40. "~%60%21%40%23%24%25%5E%26%2A%28%29%2B%3D%2C%3C.%3E%3F%2F",
  41. "-._~"
  42. };
  43. private static final String QUERY_PARAMS = "plainParam=1&&emptyParam&decodedParam=%2B2me&";
  44. private static final String PLAIN_PARAM_KEY = "plainParam";
  45. private static final String PLAIN_PARAM_VALUE = "1";
  46. private static final String EMPTY_PARAM_KEY = "emptyParam";
  47. private static final String EMPTY_PARAM_VALUE = "";
  48. private static final String DECODED_PARAM_KEY = "decodedParam";
  49. private static final String DECODED_PARAM_VALUE_PLAIN = "%2B2me";
  50. private static final String DECODED_PARAM_VALUE_DECODED = "+2me";
  51. private static final URI URI_WITH_PARAMS = URI.create("http://test.com?" + QUERY_PARAMS);
  52. @Test
  53. public void testPercentEncode() {
  54. int i = 0;
  55. for (String s : ORIGINAL_STRINGS) {
  56. assertEquals(PERCENT_ENCODED_STRINGS[i], UrlUtils.percentEncode(s));
  57. i += 1;
  58. }
  59. }
  60. @Test
  61. public void testGetQueryParams_nullDecode() {
  62. final TreeMap<String, String> params = UrlUtils.getQueryParams(EMPTY_PARAM_VALUE, true);
  63. assertNotNull(params);
  64. assertEquals(0, params.size());
  65. }
  66. @Test
  67. public void testGetQueryParams_nullNotDecode() {
  68. final TreeMap<String, String> params = UrlUtils.getQueryParams(EMPTY_PARAM_VALUE, false);
  69. assertNotNull(params);
  70. assertEquals(0, params.size());
  71. }
  72. @Test
  73. public void testGetQueryParams_allParamsDecode() {
  74. final TreeMap<String, String> params = UrlUtils.getQueryParams(QUERY_PARAMS, true);
  75. assertDecodedValue(params);
  76. }
  77. @Test
  78. public void testGetQueryParams_allParamsNotDecode() {
  79. final TreeMap<String, String> params = UrlUtils.getQueryParams(QUERY_PARAMS, false);
  80. assertNotDecodedValue(params);
  81. }
  82. @Test
  83. public void testURIParams_allParamsDecode() {
  84. final TreeMap<String, String> params = UrlUtils.getQueryParams(URI_WITH_PARAMS, true);
  85. assertDecodedValue(params);
  86. }
  87. @Test
  88. public void testURIParams_allParamsNotDecode() {
  89. final TreeMap<String, String> params = UrlUtils.getQueryParams(URI_WITH_PARAMS, false);
  90. assertNotDecodedValue(params);
  91. }
  92. private void assertNotDecodedValue(final TreeMap<String, String> params) {
  93. assertEquals(3, params.size());
  94. assertEquals(PLAIN_PARAM_VALUE, params.get(PLAIN_PARAM_KEY));
  95. assertEquals(EMPTY_PARAM_VALUE, params.get(EMPTY_PARAM_KEY));
  96. assertEquals(DECODED_PARAM_VALUE_PLAIN, params.get(DECODED_PARAM_KEY));
  97. }
  98. private void assertDecodedValue(final TreeMap<String, String> params) {
  99. assertEquals(3, params.size());
  100. assertEquals(PLAIN_PARAM_VALUE, params.get(PLAIN_PARAM_KEY));
  101. assertEquals(EMPTY_PARAM_VALUE, params.get(EMPTY_PARAM_KEY));
  102. assertEquals(DECODED_PARAM_VALUE_DECODED, params.get(DECODED_PARAM_KEY));
  103. }
  104. }