123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- // 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 "testing/gtest/include/gtest/gtest.h"
- #include "testing/platform_test.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;
- };
- using XCallbackURLTest = PlatformTest;
- TEST_F(XCallbackURLTest, IsXCallbackURL) {
- EXPECT_TRUE(IsXCallbackURL(GURL("chrome://x-callback-url")));
- EXPECT_TRUE(IsXCallbackURL(GURL("https://x-callback-url")));
- EXPECT_TRUE(IsXCallbackURL(GURL("exotic-scheme://x-callback-url")));
- EXPECT_TRUE(IsXCallbackURL(GURL("chrome://x-callback-url/action")));
- EXPECT_TRUE(IsXCallbackURL(GURL("https://x-callback-url/action")));
- EXPECT_TRUE(IsXCallbackURL(GURL("exotic-scheme://x-callback-url/action")));
- EXPECT_FALSE(IsXCallbackURL(GURL()));
- EXPECT_FALSE(IsXCallbackURL(GURL("chrome://version")));
- EXPECT_FALSE(IsXCallbackURL(GURL("https://www.google.com")));
- }
- TEST_F(XCallbackURLTest, URLWithScheme) {
- const XCallbackURLEncodeTestCase test_cases[] = {
- {
- "chrome",
- "",
- 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",
- },
- {
- "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=",
- },
- {
- "chrome",
- "command",
- GURL("chrome://path/with%20spaces"),
- GURL(),
- GURL(),
- {},
- "chrome://x-callback-url/command?x-success="
- "chrome%3A%2F%2Fpath%2Fwith%2520spaces",
- },
- };
- for (size_t i = 0; i < std::size(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_F(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"},
- {"x-error", "chrome://callback/?success=0"},
- {"x-cancel", "chrome://callback/?cancelled=1"},
- {"foo", "bar baz"},
- {"qux", ""}},
- },
- {
- GURL("chrome://x-callback-url/command?x-success="
- "chrome%3A%2F%2Fpath%2Fwith%2520spaces"),
- {{"x-success", "chrome://path/with%20spaces"}},
- },
- };
- for (size_t i = 0; i < std::size(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
|