Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include "ios/chrome/common/x_callback_url.h" | |
| 6 | |
| 7 #include "base/strings/stringprintf.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace { | |
| 11 | |
| 12 struct XCallbackURLEncodeTestCase { | |
| 13 const char* scheme; | |
| 14 const char* action; | |
| 15 GURL success_url; | |
| 16 GURL error_url; | |
| 17 GURL cancel_url; | |
| 18 std::map<std::string, std::string> parameters; | |
| 19 | |
| 20 const char* expected; | |
| 21 }; | |
| 22 | |
| 23 TEST(XCallbackURLTest, IsXCallbackURL) { | |
| 24 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.
| |
| 25 | |
| 26 EXPECT_FALSE(IsXCallbackURL(GURL())); | |
| 27 EXPECT_FALSE(IsXCallbackURL(GURL("https://www.google.com"))); | |
| 28 } | |
| 29 | |
| 30 TEST(XCallbackURLTest, URLWithScheme) { | |
| 31 const XCallbackURLEncodeTestCase test_cases[] = { | |
| 32 { | |
| 33 "chrome", | |
| 34 nullptr, | |
| 35 GURL(), | |
| 36 GURL(), | |
| 37 GURL(), | |
| 38 {}, | |
| 39 | |
| 40 "chrome://x-callback-url/", | |
| 41 }, | |
| 42 { | |
| 43 "chrome", | |
| 44 "command", | |
| 45 GURL(), | |
| 46 GURL(), | |
| 47 GURL(), | |
| 48 {}, | |
| 49 | |
| 50 "chrome://x-callback-url/command", | |
| 51 }, | |
| 52 { | |
| 53 "chrome", | |
| 54 "command", | |
| 55 GURL("chrome://callback/?success=1"), | |
| 56 GURL("chrome://callback/?success=0"), | |
| 57 GURL("chrome://callback/?cancelled=1"), | |
| 58 {}, | |
| 59 | |
| 60 "chrome://x-callback-url/" | |
| 61 "command?x-success=chrome%3A%2F%2Fcallback%2F%" | |
| 62 "3Fsuccess%3D1&x-error=chrome%3A%2F%2Fcallback%2F%" | |
| 63 "3Fsuccess%3D0&x-cancel=chrome%3A%2F%2Fcallback%2F%" | |
| 64 "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.
| |
| 65 }, | |
| 66 { | |
| 67 "chrome", | |
| 68 "command", | |
| 69 GURL(), | |
| 70 GURL(), | |
| 71 GURL(), | |
| 72 {{"foo", "bar baz"}, {"qux", ""}}, | |
| 73 | |
| 74 "chrome://x-callback-url/command?foo=bar+baz&qux=", | |
| 75 }, | |
| 76 { | |
| 77 "chrome", | |
| 78 "command", | |
| 79 GURL("chrome://callback/?success=1"), | |
| 80 GURL("chrome://callback/?success=0"), | |
| 81 GURL("chrome://callback/?cancelled=1"), | |
| 82 {{"foo", "bar baz"}, {"qux", ""}}, | |
| 83 | |
| 84 "chrome://x-callback-url/" | |
| 85 "command?x-success=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D1&x-" | |
| 86 "error=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D0&x-cancel=chrome%" | |
| 87 "3A%2F%2Fcallback%2F%3Fcancelled%3D1&foo=bar+baz&qux=", | |
| 88 }, | |
| 89 }; | |
| 90 | |
| 91 for (size_t i = 0; i < arraysize(test_cases); ++i) { | |
| 92 const XCallbackURLEncodeTestCase& test_case = test_cases[i]; | |
| 93 const GURL x_callback_url = CreateXCallbackURLWithParameters( | |
| 94 test_case.scheme, test_case.action, test_case.success_url, | |
| 95 test_case.error_url, test_case.cancel_url, test_case.parameters); | |
| 96 EXPECT_EQ(test_case.expected, x_callback_url.spec()); | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 struct XCallbackURLDecodeTestCase { | |
| 101 GURL x_callback_url; | |
| 102 | |
| 103 std::map<std::string, std::string> expected; | |
| 104 }; | |
| 105 | |
| 106 TEST(XCallbackURLTest, QueryParameters) { | |
| 107 const XCallbackURLDecodeTestCase test_cases[] = { | |
| 108 { | |
| 109 GURL("chrome://x-callback-url/"), | |
| 110 | |
| 111 {}, | |
| 112 }, | |
| 113 { | |
| 114 GURL("chrome://x-callback-url/command"), | |
| 115 | |
| 116 {}, | |
| 117 }, | |
| 118 { | |
| 119 GURL("chrome://x-callback-url/" | |
| 120 "command?x-success=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D1&x-" | |
| 121 "error=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D0&x-cancel=" | |
| 122 "chrome%3A%2F%2Fcallback%2F%3Fcancelled%3D1"), | |
| 123 | |
| 124 {{"x-success", "chrome://callback/?success=1"}, | |
| 125 {"x-error", "chrome://callback/?success=0"}, | |
| 126 {"x-cancel", "chrome://callback/?cancelled=1"}}, | |
| 127 }, | |
| 128 { | |
| 129 GURL("chrome://x-callback-url/command?foo=bar+baz&qux="), | |
| 130 | |
| 131 {{"foo", "bar baz"}, {"qux", ""}}, | |
| 132 }, | |
| 133 { | |
| 134 GURL("chrome://x-callback-url/" | |
| 135 "command?x-success=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D1&x-" | |
| 136 "error=chrome%3A%2F%2Fcallback%2F%3Fsuccess%3D0&x-cancel=" | |
| 137 "chrome%3A%2F%2Fcallback%2F%3Fcancelled%3D1&foo=bar+baz&qux="), | |
| 138 | |
| 139 {{"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.
| |
| 140 {"x-error", "chrome://callback/?success=0"}, | |
| 141 {"x-cancel", "chrome://callback/?cancelled=1"}, | |
| 142 {"foo", "bar baz"}, | |
| 143 {"qux", ""}}, | |
| 144 }, | |
| 145 }; | |
| 146 | |
| 147 for (size_t i = 0; i < arraysize(test_cases); ++i) { | |
| 148 const XCallbackURLDecodeTestCase& test_case = test_cases[i]; | |
| 149 const std::map<std::string, std::string> parameters = | |
| 150 ExtractQueryParametersFromXCallbackURL(test_case.x_callback_url); | |
| 151 EXPECT_EQ(test_case.expected, parameters); | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 } // namespace | |
| OLD | NEW |