x_callback_url_unittest.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ios/chrome/common/x_callback_url.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. #include "testing/platform_test.h"
  7. namespace {
  8. struct XCallbackURLEncodeTestCase {
  9. const char* scheme;
  10. const char* action;
  11. GURL success_url;
  12. GURL error_url;
  13. GURL cancel_url;
  14. std::map<std::string, std::string> parameters;
  15. const char* expected;
  16. };
  17. using XCallbackURLTest = PlatformTest;
  18. TEST_F(XCallbackURLTest, IsXCallbackURL) {
  19. EXPECT_TRUE(IsXCallbackURL(GURL("chrome://x-callback-url")));
  20. EXPECT_TRUE(IsXCallbackURL(GURL("https://x-callback-url")));
  21. EXPECT_TRUE(IsXCallbackURL(GURL("exotic-scheme://x-callback-url")));
  22. EXPECT_TRUE(IsXCallbackURL(GURL("chrome://x-callback-url/action")));
  23. EXPECT_TRUE(IsXCallbackURL(GURL("https://x-callback-url/action")));
  24. EXPECT_TRUE(IsXCallbackURL(GURL("exotic-scheme://x-callback-url/action")));
  25. EXPECT_FALSE(IsXCallbackURL(GURL()));
  26. EXPECT_FALSE(IsXCallbackURL(GURL("chrome://version")));
  27. EXPECT_FALSE(IsXCallbackURL(GURL("https://www.google.com")));
  28. }
  29. TEST_F(XCallbackURLTest, URLWithScheme) {
  30. const XCallbackURLEncodeTestCase test_cases[] = {
  31. {
  32. "chrome",
  33. "",
  34. GURL(),
  35. GURL(),
  36. GURL(),
  37. {},
  38. "chrome://x-callback-url/",
  39. },
  40. {
  41. "chrome",
  42. "command",
  43. GURL(),
  44. GURL(),
  45. GURL(),
  46. {},
  47. "chrome://x-callback-url/command",
  48. },
  49. {
  50. "chrome",
  51. "command",
  52. GURL("chrome://callback/?success=1"),
  53. GURL("chrome://callback/?success=0"),
  54. GURL("chrome://callback/?cancelled=1"),
  55. {},
  56. "chrome://x-callback-url/"
  57. "command?x-success=chrome%3A%2F%2Fcallback%2F"
  58. "%3Fsuccess%3D1&x-error=chrome%3A%2F%2Fcallback%2F"
  59. "%3Fsuccess%3D0&x-cancel=chrome%3A%2F%2Fcallback%2F"
  60. "%3Fcancelled%3D1",
  61. },
  62. {
  63. "chrome",
  64. "command",
  65. GURL(),
  66. GURL(),
  67. GURL(),
  68. {{"foo", "bar baz"}, {"qux", ""}},
  69. "chrome://x-callback-url/command?foo=bar+baz&qux=",
  70. },
  71. {
  72. "chrome",
  73. "command",
  74. GURL("chrome://callback/?success=1"),
  75. GURL("chrome://callback/?success=0"),
  76. GURL("chrome://callback/?cancelled=1"),
  77. {{"foo", "bar baz"}, {"qux", ""}},
  78. "chrome://x-callback-url/"
  79. "command?x-success=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D1"
  80. "&x-error=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D0&x-cancel=chrome"
  81. "%3A%2F%2Fcallback%2F%3Fcancelled%3D1&foo=bar+baz&qux=",
  82. },
  83. {
  84. "chrome",
  85. "command",
  86. GURL("chrome://path/with%20spaces"),
  87. GURL(),
  88. GURL(),
  89. {},
  90. "chrome://x-callback-url/command?x-success="
  91. "chrome%3A%2F%2Fpath%2Fwith%2520spaces",
  92. },
  93. };
  94. for (size_t i = 0; i < std::size(test_cases); ++i) {
  95. const XCallbackURLEncodeTestCase& test_case = test_cases[i];
  96. const GURL x_callback_url = CreateXCallbackURLWithParameters(
  97. test_case.scheme, test_case.action, test_case.success_url,
  98. test_case.error_url, test_case.cancel_url, test_case.parameters);
  99. EXPECT_EQ(test_case.expected, x_callback_url.spec());
  100. }
  101. }
  102. struct XCallbackURLDecodeTestCase {
  103. GURL x_callback_url;
  104. std::map<std::string, std::string> expected;
  105. };
  106. TEST_F(XCallbackURLTest, QueryParameters) {
  107. const XCallbackURLDecodeTestCase test_cases[] = {
  108. {
  109. GURL("chrome://x-callback-url/"),
  110. {},
  111. },
  112. {
  113. GURL("chrome://x-callback-url/command"),
  114. {},
  115. },
  116. {
  117. GURL("chrome://x-callback-url/"
  118. "command?x-success=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D1"
  119. "&x-error=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D0&x-cancel="
  120. "chrome%3A%2F%2Fcallback%2F%3Fcancelled%3D1"),
  121. {{"x-success", "chrome://callback/?success=1"},
  122. {"x-error", "chrome://callback/?success=0"},
  123. {"x-cancel", "chrome://callback/?cancelled=1"}},
  124. },
  125. {
  126. GURL("chrome://x-callback-url/command?foo=bar+baz&qux="),
  127. {{"foo", "bar baz"}, {"qux", ""}},
  128. },
  129. {
  130. GURL("chrome://x-callback-url/"
  131. "command?x-success=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D1"
  132. "&x-error=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D0&x-cancel="
  133. "chrome%3A%2F%2Fcallback%2F%3Fcancelled%3D1&foo=bar+baz&qux="),
  134. {{"x-success", "chrome://callback/?success=1"},
  135. {"x-error", "chrome://callback/?success=0"},
  136. {"x-cancel", "chrome://callback/?cancelled=1"},
  137. {"foo", "bar baz"},
  138. {"qux", ""}},
  139. },
  140. {
  141. GURL("chrome://x-callback-url/command?x-success="
  142. "chrome%3A%2F%2Fpath%2Fwith%2520spaces"),
  143. {{"x-success", "chrome://path/with%20spaces"}},
  144. },
  145. };
  146. for (size_t i = 0; i < std::size(test_cases); ++i) {
  147. const XCallbackURLDecodeTestCase& test_case = test_cases[i];
  148. const std::map<std::string, std::string> parameters =
  149. ExtractQueryParametersFromXCallbackURL(test_case.x_callback_url);
  150. EXPECT_EQ(test_case.expected, parameters);
  151. }
  152. }
  153. } // namespace