[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame^] | 1 | // Copyright 2007 Google Inc. All Rights Reserved. |
| 2 | // Author: [email protected] (Brett Wilson) |
| 3 | |
| 4 | #include "googleurl/src/gurl.h" |
| 5 | #include "googleurl/src/url_canon.h" |
| 6 | #include "googleurl/src/url_test_utils.h" |
| 7 | #include "testing/gtest/include/gtest/gtest.h" |
| 8 | |
| 9 | // Some implementations of base/basictypes.h may define ARRAYSIZE. |
| 10 | // If it's not defined, we define it to the ARRAYSIZE_UNSAFE macro |
| 11 | // which is in our version of basictypes.h. |
| 12 | #ifndef ARRAYSIZE |
| 13 | #define ARRAYSIZE ARRAYSIZE_UNSAFE |
| 14 | #endif |
| 15 | |
| 16 | using url_test_utils::WStringToUTF16; |
| 17 | using url_test_utils::ConvertUTF8ToUTF16; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | template<typename CHAR> |
| 22 | void SetupReplacement(void (url_canon::Replacements<CHAR>::*func)(const CHAR*, |
| 23 | const url_parse::Component&), |
| 24 | url_canon::Replacements<CHAR>* replacements, |
| 25 | const CHAR* str) { |
| 26 | if (str) { |
| 27 | url_parse::Component comp; |
| 28 | if (str[0]) |
| 29 | comp.len = static_cast<int>(strlen(str)); |
| 30 | (replacements->*func)(str, comp); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // Returns the canonicalized string for the given URL string for the |
| 35 | // GURLTest.Types test. |
| 36 | std::string TypesTestCase(const char* src) { |
| 37 | GURL gurl(src); |
| 38 | return gurl.possibly_invalid_spec(); |
| 39 | } |
| 40 | |
| 41 | } // namespace |
| 42 | |
| 43 | // Different types of URLs should be handled differently by url_util, and |
| 44 | // handed off to different canonicalizers. |
| 45 | TEST(GURLTest, Types) { |
| 46 | // URLs with unknown schemes should be treated as path URLs, even when they |
| 47 | // have things like "://". |
| 48 | EXPECT_EQ("something:///HOSTNAME.com/", |
| 49 | TypesTestCase("something:///HOSTNAME.com/")); |
| 50 | |
| 51 | // In the reverse, known schemes should always trigger standard URL handling. |
| 52 | EXPECT_EQ("http://hostname.com/", TypesTestCase("http:HOSTNAME.com")); |
| 53 | EXPECT_EQ("http://hostname.com/", TypesTestCase("http:/HOSTNAME.com")); |
| 54 | EXPECT_EQ("http://hostname.com/", TypesTestCase("http://HOSTNAME.com")); |
| 55 | EXPECT_EQ("http://hostname.com/", TypesTestCase("http:///HOSTNAME.com")); |
| 56 | |
| 57 | #ifdef WIN32 |
| 58 | // URLs that look like absolute Windows drive specs. |
| 59 | EXPECT_EQ("file:///C:/foo.txt", TypesTestCase("c:\\foo.txt")); |
| 60 | EXPECT_EQ("file:///Z:/foo.txt", TypesTestCase("Z|foo.txt")); |
| 61 | EXPECT_EQ("file://server/foo.txt", TypesTestCase("\\\\server\\foo.txt")); |
| 62 | EXPECT_EQ("file://server/foo.txt", TypesTestCase("//server/foo.txt")); |
| 63 | #endif |
| 64 | } |
| 65 | |
| 66 | // Test the basic creation and querying of components in a GURL. We assume |
| 67 | // the parser is already tested and works, so we are mostly interested if the |
| 68 | // object does the right thing with the results. |
| 69 | TEST(GURLTest, Components) { |
| 70 | GURL url(WStringToUTF16(L"http://user:[email protected]:99/foo;bar?q=a#ref")); |
| 71 | EXPECT_TRUE(url.is_valid()); |
| 72 | EXPECT_TRUE(url.SchemeIs("http")); |
| 73 | EXPECT_FALSE(url.SchemeIsFile()); |
| 74 | |
| 75 | // This is the narrow version of the URL, which should match the wide input. |
| 76 | EXPECT_EQ("http://user:[email protected]:99/foo;bar?q=a#ref", url.spec()); |
| 77 | |
| 78 | EXPECT_EQ("http", url.scheme()); |
| 79 | EXPECT_EQ("user", url.username()); |
| 80 | EXPECT_EQ("pass", url.password()); |
| 81 | EXPECT_EQ("google.com", url.host()); |
| 82 | EXPECT_EQ("99", url.port()); |
| 83 | EXPECT_EQ(99, url.IntPort()); |
| 84 | EXPECT_EQ("/foo;bar", url.path()); |
| 85 | EXPECT_EQ("q=a", url.query()); |
| 86 | EXPECT_EQ("ref", url.ref()); |
| 87 | } |
| 88 | |
| 89 | TEST(GURLTest, Empty) { |
| 90 | GURL url; |
| 91 | EXPECT_FALSE(url.is_valid()); |
| 92 | EXPECT_EQ("", url.spec()); |
| 93 | |
| 94 | EXPECT_EQ("", url.scheme()); |
| 95 | EXPECT_EQ("", url.username()); |
| 96 | EXPECT_EQ("", url.password()); |
| 97 | EXPECT_EQ("", url.host()); |
| 98 | EXPECT_EQ("", url.port()); |
| 99 | EXPECT_EQ(url_parse::PORT_UNSPECIFIED, url.IntPort()); |
| 100 | EXPECT_EQ("", url.path()); |
| 101 | EXPECT_EQ("", url.query()); |
| 102 | EXPECT_EQ("", url.ref()); |
| 103 | } |
| 104 | |
| 105 | TEST(GURLTest, Copy) { |
| 106 | GURL url(WStringToUTF16(L"http://user:[email protected]:99/foo;bar?q=a#ref")); |
| 107 | |
| 108 | GURL url2(url); |
| 109 | EXPECT_TRUE(url2.is_valid()); |
| 110 | |
| 111 | EXPECT_EQ("http://user:[email protected]:99/foo;bar?q=a#ref", url2.spec()); |
| 112 | EXPECT_EQ("http", url2.scheme()); |
| 113 | EXPECT_EQ("user", url2.username()); |
| 114 | EXPECT_EQ("pass", url2.password()); |
| 115 | EXPECT_EQ("google.com", url2.host()); |
| 116 | EXPECT_EQ("99", url2.port()); |
| 117 | EXPECT_EQ(99, url2.IntPort()); |
| 118 | EXPECT_EQ("/foo;bar", url2.path()); |
| 119 | EXPECT_EQ("q=a", url2.query()); |
| 120 | EXPECT_EQ("ref", url2.ref()); |
| 121 | |
| 122 | // Copying of invalid URL should be invalid |
| 123 | GURL invalid; |
| 124 | GURL invalid2(invalid); |
| 125 | EXPECT_FALSE(invalid2.is_valid()); |
| 126 | EXPECT_EQ("", invalid2.spec()); |
| 127 | EXPECT_EQ("", invalid2.scheme()); |
| 128 | EXPECT_EQ("", invalid2.username()); |
| 129 | EXPECT_EQ("", invalid2.password()); |
| 130 | EXPECT_EQ("", invalid2.host()); |
| 131 | EXPECT_EQ("", invalid2.port()); |
| 132 | EXPECT_EQ(url_parse::PORT_UNSPECIFIED, invalid2.IntPort()); |
| 133 | EXPECT_EQ("", invalid2.path()); |
| 134 | EXPECT_EQ("", invalid2.query()); |
| 135 | EXPECT_EQ("", invalid2.ref()); |
| 136 | } |
| 137 | |
| 138 | TEST(GURLTest, CopyFileSystem) { |
| 139 | GURL url(WStringToUTF16(L"filesystem:https://user:[email protected]:99/t/foo;bar?q=a#ref")); |
| 140 | |
| 141 | GURL url2(url); |
| 142 | EXPECT_TRUE(url2.is_valid()); |
| 143 | |
| 144 | EXPECT_EQ("filesystem:https://user:[email protected]:99/t/foo;bar?q=a#ref", url2.spec()); |
| 145 | EXPECT_EQ("filesystem", url2.scheme()); |
| 146 | EXPECT_EQ("", url2.username()); |
| 147 | EXPECT_EQ("", url2.password()); |
| 148 | EXPECT_EQ("", url2.host()); |
| 149 | EXPECT_EQ("", url2.port()); |
| 150 | EXPECT_EQ(url_parse::PORT_UNSPECIFIED, url2.IntPort()); |
| 151 | EXPECT_EQ("/foo;bar", url2.path()); |
| 152 | EXPECT_EQ("q=a", url2.query()); |
| 153 | EXPECT_EQ("ref", url2.ref()); |
| 154 | |
| 155 | const GURL* inner = url2.inner_url(); |
| 156 | ASSERT_TRUE(inner); |
| 157 | EXPECT_EQ("https", inner->scheme()); |
| 158 | EXPECT_EQ("user", inner->username()); |
| 159 | EXPECT_EQ("pass", inner->password()); |
| 160 | EXPECT_EQ("google.com", inner->host()); |
| 161 | EXPECT_EQ("99", inner->port()); |
| 162 | EXPECT_EQ(99, inner->IntPort()); |
| 163 | EXPECT_EQ("/t", inner->path()); |
| 164 | EXPECT_EQ("", inner->query()); |
| 165 | EXPECT_EQ("", inner->ref()); |
| 166 | } |
| 167 | |
| 168 | // Given an invalid URL, we should still get most of the components. |
| 169 | TEST(GURLTest, Invalid) { |
| 170 | GURL url("http:google.com:foo"); |
| 171 | EXPECT_FALSE(url.is_valid()); |
| 172 | EXPECT_EQ("http://google.com:foo/", url.possibly_invalid_spec()); |
| 173 | |
| 174 | EXPECT_EQ("http", url.scheme()); |
| 175 | EXPECT_EQ("", url.username()); |
| 176 | EXPECT_EQ("", url.password()); |
| 177 | EXPECT_EQ("google.com", url.host()); |
| 178 | EXPECT_EQ("foo", url.port()); |
| 179 | EXPECT_EQ(url_parse::PORT_INVALID, url.IntPort()); |
| 180 | EXPECT_EQ("/", url.path()); |
| 181 | EXPECT_EQ("", url.query()); |
| 182 | EXPECT_EQ("", url.ref()); |
| 183 | } |
| 184 | |
| 185 | TEST(GURLTest, Resolve) { |
| 186 | // The tricky cases for relative URL resolving are tested in the |
| 187 | // canonicalizer unit test. Here, we just test that the GURL integration |
| 188 | // works properly. |
| 189 | struct ResolveCase { |
| 190 | const char* base; |
| 191 | const char* relative; |
| 192 | bool expected_valid; |
| 193 | const char* expected; |
| 194 | } resolve_cases[] = { |
| 195 | {"http://www.google.com/", "foo.html", true, "http://www.google.com/foo.html"}, |
| 196 | {"http://www.google.com/", "http://images.google.com/foo.html", true, "http://images.google.com/foo.html"}, |
| 197 | {"http://www.google.com/blah/bloo?c#d", "../../../hello/./world.html?a#b", true, "http://www.google.com/hello/world.html?a#b"}, |
| 198 | {"http://www.google.com/foo#bar", "#com", true, "http://www.google.com/foo#com"}, |
| 199 | {"http://www.google.com/", "Https:images.google.com", true, "https://images.google.com/"}, |
| 200 | // A non-standard base can be replaced with a standard absolute URL. |
| 201 | {"data:blahblah", "http://google.com/", true, "http://google.com/"}, |
| 202 | {"data:blahblah", "http:google.com", true, "http://google.com/"}, |
| 203 | // Filesystem URLs have different paths to test. |
| 204 | {"filesystem:http://www.google.com/type/", "foo.html", true, "filesystem:http://www.google.com/type/foo.html"}, |
| 205 | {"filesystem:http://www.google.com/type/", "../foo.html", true, "filesystem:http://www.google.com/type/foo.html"}, |
| 206 | }; |
| 207 | |
| 208 | for (size_t i = 0; i < ARRAYSIZE(resolve_cases); i++) { |
| 209 | // 8-bit code path. |
| 210 | GURL input(resolve_cases[i].base); |
| 211 | GURL output = input.Resolve(resolve_cases[i].relative); |
| 212 | EXPECT_EQ(resolve_cases[i].expected_valid, output.is_valid()) << i; |
| 213 | EXPECT_EQ(resolve_cases[i].expected, output.spec()) << i; |
| 214 | EXPECT_EQ(output.SchemeIsFileSystem(), output.inner_url() != NULL); |
| 215 | |
| 216 | // Wide code path. |
| 217 | GURL inputw(ConvertUTF8ToUTF16(resolve_cases[i].base)); |
| 218 | GURL outputw = |
| 219 | input.Resolve(ConvertUTF8ToUTF16(resolve_cases[i].relative)); |
| 220 | EXPECT_EQ(resolve_cases[i].expected_valid, outputw.is_valid()) << i; |
| 221 | EXPECT_EQ(resolve_cases[i].expected, outputw.spec()) << i; |
| 222 | EXPECT_EQ(outputw.SchemeIsFileSystem(), outputw.inner_url() != NULL); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | TEST(GURLTest, GetOrigin) { |
| 227 | struct TestCase { |
| 228 | const char* input; |
| 229 | const char* expected; |
| 230 | } cases[] = { |
| 231 | {"http://www.google.com", "http://www.google.com/"}, |
| 232 | {"javascript:window.alert(\"hello,world\");", ""}, |
| 233 | {"http://user:[email protected]:21/blah#baz", "http://www.google.com:21/"}, |
| 234 | {"http://[email protected]", "http://www.google.com/"}, |
| 235 | {"http://:[email protected]", "http://www.google.com/"}, |
| 236 | {"http://:@www.google.com", "http://www.google.com/"}, |
| 237 | {"filesystem:http://www.google.com/temp/foo?q#b", "http://www.google.com/"}, |
| 238 | {"filesystem:http://user:[email protected]:21/blah#baz", "http://google.com:21/"}, |
| 239 | }; |
| 240 | for (size_t i = 0; i < ARRAYSIZE(cases); i++) { |
| 241 | GURL url(cases[i].input); |
| 242 | GURL origin = url.GetOrigin(); |
| 243 | EXPECT_EQ(cases[i].expected, origin.spec()); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | TEST(GURLTest, GetWithEmptyPath) { |
| 248 | struct TestCase { |
| 249 | const char* input; |
| 250 | const char* expected; |
| 251 | } cases[] = { |
| 252 | {"http://www.google.com", "http://www.google.com/"}, |
| 253 | {"javascript:window.alert(\"hello, world\");", ""}, |
| 254 | {"http://www.google.com/foo/bar.html?baz=22", "http://www.google.com/"}, |
| 255 | {"filesystem:http://www.google.com/temporary/bar.html?baz=22", "filesystem:http://www.google.com/temporary/"}, |
| 256 | {"filesystem:file:///temporary/bar.html?baz=22", "filesystem:file:///temporary/"}, |
| 257 | }; |
| 258 | |
| 259 | for (size_t i = 0; i < ARRAYSIZE(cases); i++) { |
| 260 | GURL url(cases[i].input); |
| 261 | GURL empty_path = url.GetWithEmptyPath(); |
| 262 | EXPECT_EQ(cases[i].expected, empty_path.spec()); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | TEST(GURLTest, Replacements) { |
| 267 | // The url canonicalizer replacement test will handle most of these case. |
| 268 | // The most important thing to do here is to check that the proper |
| 269 | // canonicalizer gets called based on the scheme of the input. |
| 270 | struct ReplaceCase { |
| 271 | const char* base; |
| 272 | const char* scheme; |
| 273 | const char* username; |
| 274 | const char* password; |
| 275 | const char* host; |
| 276 | const char* port; |
| 277 | const char* path; |
| 278 | const char* query; |
| 279 | const char* ref; |
| 280 | const char* expected; |
| 281 | } replace_cases[] = { |
| 282 | {"http://www.google.com/foo/bar.html?foo#bar", NULL, NULL, NULL, NULL, NULL, "/", "", "", "http://www.google.com/"}, |
| 283 | {"http://www.google.com/foo/bar.html?foo#bar", "javascript", "", "", "", "", "window.open('foo');", "", "", "javascript:window.open('foo');"}, |
| 284 | {"file:///C:/foo/bar.txt", "http", NULL, NULL, "www.google.com", "99", "/foo","search", "ref", "http://www.google.com:99/foo?search#ref"}, |
| 285 | #ifdef WIN32 |
| 286 | {"http://www.google.com/foo/bar.html?foo#bar", "file", "", "", "", "", "c:\\", "", "", "file:///C:/"}, |
| 287 | #endif |
| 288 | {"filesystem:http://www.google.com/foo/bar.html?foo#bar", NULL, NULL, NULL, NULL, NULL, "/", "", "", "filesystem:http://www.google.com/foo/"}, |
| 289 | }; |
| 290 | |
| 291 | for (size_t i = 0; i < ARRAYSIZE(replace_cases); i++) { |
| 292 | const ReplaceCase& cur = replace_cases[i]; |
| 293 | GURL url(cur.base); |
| 294 | GURL::Replacements repl; |
| 295 | SetupReplacement(&GURL::Replacements::SetScheme, &repl, cur.scheme); |
| 296 | SetupReplacement(&GURL::Replacements::SetUsername, &repl, cur.username); |
| 297 | SetupReplacement(&GURL::Replacements::SetPassword, &repl, cur.password); |
| 298 | SetupReplacement(&GURL::Replacements::SetHost, &repl, cur.host); |
| 299 | SetupReplacement(&GURL::Replacements::SetPort, &repl, cur.port); |
| 300 | SetupReplacement(&GURL::Replacements::SetPath, &repl, cur.path); |
| 301 | SetupReplacement(&GURL::Replacements::SetQuery, &repl, cur.query); |
| 302 | SetupReplacement(&GURL::Replacements::SetRef, &repl, cur.ref); |
| 303 | GURL output = url.ReplaceComponents(repl); |
| 304 | |
| 305 | EXPECT_EQ(replace_cases[i].expected, output.spec()); |
| 306 | EXPECT_EQ(output.SchemeIsFileSystem(), output.inner_url() != NULL); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | TEST(GURLTest, PathForRequest) { |
| 311 | struct TestCase { |
| 312 | const char* input; |
| 313 | const char* expected; |
| 314 | const char* inner_expected; |
| 315 | } cases[] = { |
| 316 | {"http://www.google.com", "/", NULL}, |
| 317 | {"http://www.google.com/", "/", NULL}, |
| 318 | {"http://www.google.com/foo/bar.html?baz=22", "/foo/bar.html?baz=22", NULL}, |
| 319 | {"http://www.google.com/foo/bar.html#ref", "/foo/bar.html", NULL}, |
| 320 | {"http://www.google.com/foo/bar.html?query#ref", "/foo/bar.html?query", NULL}, |
| 321 | {"filesystem:http://www.google.com/temporary/foo/bar.html?query#ref", "/foo/bar.html?query", "/temporary"}, |
| 322 | {"filesystem:http://www.google.com/temporary/foo/bar.html?query", "/foo/bar.html?query", "/temporary"}, |
| 323 | }; |
| 324 | |
| 325 | for (size_t i = 0; i < ARRAYSIZE(cases); i++) { |
| 326 | GURL url(cases[i].input); |
| 327 | std::string path_request = url.PathForRequest(); |
| 328 | EXPECT_EQ(cases[i].expected, path_request); |
| 329 | EXPECT_EQ(cases[i].inner_expected == NULL, url.inner_url() == NULL); |
| 330 | if (url.inner_url() && cases[i].inner_expected) |
| 331 | EXPECT_EQ(cases[i].inner_expected, url.inner_url()->PathForRequest()); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | TEST(GURLTest, EffectiveIntPort) { |
| 336 | struct PortTest { |
| 337 | const char* spec; |
| 338 | int expected_int_port; |
| 339 | } port_tests[] = { |
| 340 | // http |
| 341 | {"http://www.google.com/", 80}, |
| 342 | {"http://www.google.com:80/", 80}, |
| 343 | {"http://www.google.com:443/", 443}, |
| 344 | |
| 345 | // https |
| 346 | {"https://www.google.com/", 443}, |
| 347 | {"https://www.google.com:443/", 443}, |
| 348 | {"https://www.google.com:80/", 80}, |
| 349 | |
| 350 | // ftp |
| 351 | {"ftp://www.google.com/", 21}, |
| 352 | {"ftp://www.google.com:21/", 21}, |
| 353 | {"ftp://www.google.com:80/", 80}, |
| 354 | |
| 355 | // gopher |
| 356 | {"gopher://www.google.com/", 70}, |
| 357 | {"gopher://www.google.com:70/", 70}, |
| 358 | {"gopher://www.google.com:80/", 80}, |
| 359 | |
| 360 | // file - no port |
| 361 | {"file://www.google.com/", url_parse::PORT_UNSPECIFIED}, |
| 362 | {"file://www.google.com:443/", url_parse::PORT_UNSPECIFIED}, |
| 363 | |
| 364 | // data - no port |
| 365 | {"data:www.google.com:90", url_parse::PORT_UNSPECIFIED}, |
| 366 | {"data:www.google.com", url_parse::PORT_UNSPECIFIED}, |
| 367 | |
| 368 | // filesystem - no port |
| 369 | {"filesystem:http://www.google.com:90/t/foo", url_parse::PORT_UNSPECIFIED}, |
| 370 | {"filesystem:file:///t/foo", url_parse::PORT_UNSPECIFIED}, |
| 371 | }; |
| 372 | |
| 373 | for (size_t i = 0; i < ARRAYSIZE(port_tests); i++) { |
| 374 | GURL url(port_tests[i].spec); |
| 375 | EXPECT_EQ(port_tests[i].expected_int_port, url.EffectiveIntPort()); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | TEST(GURLTest, IPAddress) { |
| 380 | struct IPTest { |
| 381 | const char* spec; |
| 382 | bool expected_ip; |
| 383 | } ip_tests[] = { |
| 384 | {"http://www.google.com/", false}, |
| 385 | {"http://192.168.9.1/", true}, |
| 386 | {"http://192.168.9.1.2/", false}, |
| 387 | {"http://192.168.m.1/", false}, |
| 388 | {"http://2001:db8::1/", false}, |
| 389 | {"http://[2001:db8::1]/", true}, |
| 390 | {"", false}, |
| 391 | {"some random input!", false}, |
| 392 | }; |
| 393 | |
| 394 | for (size_t i = 0; i < ARRAYSIZE(ip_tests); i++) { |
| 395 | GURL url(ip_tests[i].spec); |
| 396 | EXPECT_EQ(ip_tests[i].expected_ip, url.HostIsIPAddress()); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | TEST(GURLTest, HostNoBrackets) { |
| 401 | struct TestCase { |
| 402 | const char* input; |
| 403 | const char* expected_host; |
| 404 | const char* expected_plainhost; |
| 405 | } cases[] = { |
| 406 | {"http://www.google.com", "www.google.com", "www.google.com"}, |
| 407 | {"http://[2001:db8::1]/", "[2001:db8::1]", "2001:db8::1"}, |
| 408 | {"http://[::]/", "[::]", "::"}, |
| 409 | |
| 410 | // Don't require a valid URL, but don't crash either. |
| 411 | {"http://[]/", "[]", ""}, |
| 412 | {"http://[x]/", "[x]", "x"}, |
| 413 | {"http://[x/", "[x", "[x"}, |
| 414 | {"http://x]/", "x]", "x]"}, |
| 415 | {"http://[/", "[", "["}, |
| 416 | {"http://]/", "]", "]"}, |
| 417 | {"", "", ""}, |
| 418 | }; |
| 419 | for (size_t i = 0; i < ARRAYSIZE(cases); i++) { |
| 420 | GURL url(cases[i].input); |
| 421 | EXPECT_EQ(cases[i].expected_host, url.host()); |
| 422 | EXPECT_EQ(cases[i].expected_plainhost, url.HostNoBrackets()); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | TEST(GURLTest, DomainIs) { |
| 427 | const char google_domain[] = "google.com"; |
| 428 | |
| 429 | GURL url_1("http://www.google.com:99/foo"); |
| 430 | EXPECT_TRUE(url_1.DomainIs(google_domain)); |
| 431 | |
| 432 | GURL url_2("http://google.com:99/foo"); |
| 433 | EXPECT_TRUE(url_2.DomainIs(google_domain)); |
| 434 | |
| 435 | GURL url_3("http://google.com./foo"); |
| 436 | EXPECT_TRUE(url_3.DomainIs(google_domain)); |
| 437 | |
| 438 | GURL url_4("http://google.com/foo"); |
| 439 | EXPECT_FALSE(url_4.DomainIs("google.com.")); |
| 440 | |
| 441 | GURL url_5("http://google.com./foo"); |
| 442 | EXPECT_TRUE(url_5.DomainIs("google.com.")); |
| 443 | |
| 444 | GURL url_6("http://www.google.com./foo"); |
| 445 | EXPECT_TRUE(url_6.DomainIs(".com.")); |
| 446 | |
| 447 | GURL url_7("http://www.balabala.com/foo"); |
| 448 | EXPECT_FALSE(url_7.DomainIs(google_domain)); |
| 449 | |
| 450 | GURL url_8("http://www.google.com.cn/foo"); |
| 451 | EXPECT_FALSE(url_8.DomainIs(google_domain)); |
| 452 | |
| 453 | GURL url_9("http://www.iamnotgoogle.com/foo"); |
| 454 | EXPECT_FALSE(url_9.DomainIs(google_domain)); |
| 455 | |
| 456 | GURL url_10("http://www.iamnotgoogle.com../foo"); |
| 457 | EXPECT_FALSE(url_10.DomainIs(".com")); |
| 458 | |
| 459 | GURL url_11("filesystem:http://www.google.com:99/foo/"); |
| 460 | EXPECT_TRUE(url_11.DomainIs(google_domain)); |
| 461 | |
| 462 | GURL url_12("filesystem:http://www.iamnotgoogle.com/foo/"); |
| 463 | EXPECT_FALSE(url_12.DomainIs(google_domain)); |
| 464 | } |
| 465 | |
| 466 | // Newlines should be stripped from inputs. |
| 467 | TEST(GURLTest, Newlines) { |
| 468 | // Constructor. |
| 469 | GURL url_1(" \t ht\ntp://\twww.goo\rgle.com/as\ndf \n "); |
| 470 | EXPECT_EQ("http://www.google.com/asdf", url_1.spec()); |
| 471 | |
| 472 | // Relative path resolver. |
| 473 | GURL url_2 = url_1.Resolve(" \n /fo\to\r "); |
| 474 | EXPECT_EQ("http://www.google.com/foo", url_2.spec()); |
| 475 | |
| 476 | // Note that newlines are NOT stripped from ReplaceComponents. |
| 477 | } |
| 478 | |
| 479 | TEST(GURLTest, IsStandard) { |
| 480 | GURL a("http:foo/bar"); |
| 481 | EXPECT_TRUE(a.IsStandard()); |
| 482 | |
| 483 | GURL b("foo:bar/baz"); |
| 484 | EXPECT_FALSE(b.IsStandard()); |
| 485 | |
| 486 | GURL c("foo://bar/baz"); |
| 487 | EXPECT_FALSE(c.IsStandard()); |
| 488 | } |