Chromium Code Reviews| Index: ios/chrome/common/x_callback_url_unittest.cc |
| diff --git a/ios/chrome/common/x_callback_url_unittest.cc b/ios/chrome/common/x_callback_url_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9857e336486ecf76485a9b4a0521155dbe24494a |
| --- /dev/null |
| +++ b/ios/chrome/common/x_callback_url_unittest.cc |
| @@ -0,0 +1,155 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ios/chrome/common/x_callback_url.h" |
| + |
| +#include "base/strings/stringprintf.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +struct XCallbackURLEncodeTestCase { |
| + const char* scheme; |
| + const char* action; |
| + GURL success_url; |
| + GURL error_url; |
| + GURL cancel_url; |
| + std::map<std::string, std::string> parameters; |
| + |
| + const char* expected; |
| +}; |
| + |
| +TEST(XCallbackURLTest, IsXCallbackURL) { |
| + EXPECT_TRUE(IsXCallbackURL(GURL("chrome://x-callback-url"))); |
|
mef
2016/11/07 18:42:10
Maybe add another test to show that scheme: isn't
sdefresne
2016/11/08 07:20:43
Done.
|
| + |
| + EXPECT_FALSE(IsXCallbackURL(GURL())); |
| + EXPECT_FALSE(IsXCallbackURL(GURL("https://www.google.com"))); |
| +} |
| + |
| +TEST(XCallbackURLTest, URLWithScheme) { |
| + const XCallbackURLEncodeTestCase test_cases[] = { |
| + { |
| + "chrome", |
| + nullptr, |
| + GURL(), |
| + GURL(), |
| + GURL(), |
| + {}, |
| + |
| + "chrome://x-callback-url/", |
| + }, |
| + { |
| + "chrome", |
| + "command", |
| + GURL(), |
| + GURL(), |
| + GURL(), |
| + {}, |
| + |
| + "chrome://x-callback-url/command", |
| + }, |
| + { |
| + "chrome", |
| + "command", |
| + GURL("chrome://callback/?success=1"), |
| + GURL("chrome://callback/?success=0"), |
| + GURL("chrome://callback/?cancelled=1"), |
| + {}, |
| + |
| + "chrome://x-callback-url/" |
| + "command?x-success=chrome%3A%2F%2Fcallback%2F%" |
| + "3Fsuccess%3D1&x-error=chrome%3A%2F%2Fcallback%2F%" |
| + "3Fsuccess%3D0&x-cancel=chrome%3A%2F%2Fcallback%2F%" |
| + "3Fcancelled%3D1", |
|
rohitrao (ping after 24h)
2016/11/07 17:58:17
Nit: could you break all of these lines before the
sdefresne
2016/11/08 07:20:44
Done.
|
| + }, |
| + { |
| + "chrome", |
| + "command", |
| + GURL(), |
| + GURL(), |
| + GURL(), |
| + {{"foo", "bar baz"}, {"qux", ""}}, |
| + |
| + "chrome://x-callback-url/command?foo=bar+baz&qux=", |
| + }, |
| + { |
| + "chrome", |
| + "command", |
| + GURL("chrome://callback/?success=1"), |
| + GURL("chrome://callback/?success=0"), |
| + GURL("chrome://callback/?cancelled=1"), |
| + {{"foo", "bar baz"}, {"qux", ""}}, |
| + |
| + "chrome://x-callback-url/" |
| + "command?x-success=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D1&x-" |
| + "error=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D0&x-cancel=chrome%" |
| + "3A%2F%2Fcallback%2F%3Fcancelled%3D1&foo=bar+baz&qux=", |
| + }, |
| + }; |
| + |
| + for (size_t i = 0; i < arraysize(test_cases); ++i) { |
| + const XCallbackURLEncodeTestCase& test_case = test_cases[i]; |
| + const GURL x_callback_url = CreateXCallbackURLWithParameters( |
| + test_case.scheme, test_case.action, test_case.success_url, |
| + test_case.error_url, test_case.cancel_url, test_case.parameters); |
| + EXPECT_EQ(test_case.expected, x_callback_url.spec()); |
| + } |
| +} |
| + |
| +struct XCallbackURLDecodeTestCase { |
| + GURL x_callback_url; |
| + |
| + std::map<std::string, std::string> expected; |
| +}; |
| + |
| +TEST(XCallbackURLTest, QueryParameters) { |
| + const XCallbackURLDecodeTestCase test_cases[] = { |
| + { |
| + GURL("chrome://x-callback-url/"), |
| + |
| + {}, |
| + }, |
| + { |
| + GURL("chrome://x-callback-url/command"), |
| + |
| + {}, |
| + }, |
| + { |
| + GURL("chrome://x-callback-url/" |
| + "command?x-success=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D1&x-" |
| + "error=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D0&x-cancel=" |
| + "chrome%3A%2F%2Fcallback%2F%3Fcancelled%3D1"), |
| + |
| + {{"x-success", "chrome://callback/?success=1"}, |
| + {"x-error", "chrome://callback/?success=0"}, |
| + {"x-cancel", "chrome://callback/?cancelled=1"}}, |
| + }, |
| + { |
| + GURL("chrome://x-callback-url/command?foo=bar+baz&qux="), |
| + |
| + {{"foo", "bar baz"}, {"qux", ""}}, |
| + }, |
| + { |
| + GURL("chrome://x-callback-url/" |
| + "command?x-success=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D1&x-" |
| + "error=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D0&x-cancel=" |
| + "chrome%3A%2F%2Fcallback%2F%3Fcancelled%3D1&foo=bar+baz&qux="), |
| + |
| + {{"x-success", "chrome://callback/?success=1"}, |
|
rohitrao (ping after 24h)
2016/11/07 17:58:17
Thoughts on adding another test case which has "%2
sdefresne
2016/11/08 07:20:43
Done.
|
| + {"x-error", "chrome://callback/?success=0"}, |
| + {"x-cancel", "chrome://callback/?cancelled=1"}, |
| + {"foo", "bar baz"}, |
| + {"qux", ""}}, |
| + }, |
| + }; |
| + |
| + for (size_t i = 0; i < arraysize(test_cases); ++i) { |
| + const XCallbackURLDecodeTestCase& test_case = test_cases[i]; |
| + const std::map<std::string, std::string> parameters = |
| + ExtractQueryParametersFromXCallbackURL(test_case.x_callback_url); |
| + EXPECT_EQ(test_case.expected, parameters); |
| + } |
| +} |
| + |
| +} // namespace |