OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/macros.h" | 5 #include "base/macros.h" |
6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
7 #include "url/gurl.h" | 7 #include "url/gurl.h" |
8 #include "url/url_canon.h" | 8 #include "url/url_canon.h" |
9 #include "url/url_test_utils.h" | 9 #include "url/url_test_utils.h" |
10 | 10 |
(...skipping 27 matching lines...) Expand all Loading... |
38 } // namespace | 38 } // namespace |
39 | 39 |
40 // Different types of URLs should be handled differently, and handed off to | 40 // Different types of URLs should be handled differently, and handed off to |
41 // different canonicalizers. | 41 // different canonicalizers. |
42 TEST(GURLTest, Types) { | 42 TEST(GURLTest, Types) { |
43 // URLs with unknown schemes should be treated as path URLs, even when they | 43 // URLs with unknown schemes should be treated as path URLs, even when they |
44 // have things like "://". | 44 // have things like "://". |
45 EXPECT_EQ("something:///HOSTNAME.com/", | 45 EXPECT_EQ("something:///HOSTNAME.com/", |
46 TypesTestCase("something:///HOSTNAME.com/")); | 46 TypesTestCase("something:///HOSTNAME.com/")); |
47 | 47 |
48 // In the reverse, known schemes should always trigger standard URL handling. | 48 // Conversely, URLs with known schemes should always trigger standard URL |
| 49 // handling. |
49 EXPECT_EQ("http://hostname.com/", TypesTestCase("http:HOSTNAME.com")); | 50 EXPECT_EQ("http://hostname.com/", TypesTestCase("http:HOSTNAME.com")); |
50 EXPECT_EQ("http://hostname.com/", TypesTestCase("http:/HOSTNAME.com")); | 51 EXPECT_EQ("http://hostname.com/", TypesTestCase("http:/HOSTNAME.com")); |
51 EXPECT_EQ("http://hostname.com/", TypesTestCase("http://HOSTNAME.com")); | 52 EXPECT_EQ("http://hostname.com/", TypesTestCase("http://HOSTNAME.com")); |
52 EXPECT_EQ("http://hostname.com/", TypesTestCase("http:///HOSTNAME.com")); | 53 EXPECT_EQ("http://hostname.com/", TypesTestCase("http:///HOSTNAME.com")); |
53 | 54 |
54 #ifdef WIN32 | 55 #ifdef WIN32 |
55 // URLs that look like absolute Windows drive specs. | 56 // URLs that look like Windows absolute path specs. |
56 EXPECT_EQ("file:///C:/foo.txt", TypesTestCase("c:\\foo.txt")); | 57 EXPECT_EQ("file:///C:/foo.txt", TypesTestCase("c:\\foo.txt")); |
57 EXPECT_EQ("file:///Z:/foo.txt", TypesTestCase("Z|foo.txt")); | 58 EXPECT_EQ("file:///Z:/foo.txt", TypesTestCase("Z|foo.txt")); |
58 EXPECT_EQ("file://server/foo.txt", TypesTestCase("\\\\server\\foo.txt")); | 59 EXPECT_EQ("file://server/foo.txt", TypesTestCase("\\\\server\\foo.txt")); |
59 EXPECT_EQ("file://server/foo.txt", TypesTestCase("//server/foo.txt")); | 60 EXPECT_EQ("file://server/foo.txt", TypesTestCase("//server/foo.txt")); |
60 #endif | 61 #endif |
61 } | 62 } |
62 | 63 |
63 // Test the basic creation and querying of components in a GURL. We assume | 64 // Test the basic creation and querying of components in a GURL. We assume that |
64 // the parser is already tested and works, so we are mostly interested if the | 65 // the parser is already tested and works, so we are mostly interested if the |
65 // object does the right thing with the results. | 66 // object does the right thing with the results. |
66 TEST(GURLTest, Components) { | 67 TEST(GURLTest, Components) { |
67 GURL url(WStringToUTF16(L"http://user:pass@google.com:99/foo;bar?q=a#ref")); | 68 GURL url(WStringToUTF16(L"http://user:pass@google.com:99/foo;bar?q=a#ref")); |
68 EXPECT_TRUE(url.is_valid()); | 69 EXPECT_TRUE(url.is_valid()); |
69 EXPECT_TRUE(url.SchemeIs("http")); | 70 EXPECT_TRUE(url.SchemeIs("http")); |
70 EXPECT_FALSE(url.SchemeIsFile()); | 71 EXPECT_FALSE(url.SchemeIsFile()); |
71 | 72 |
72 // This is the narrow version of the URL, which should match the wide input. | 73 // This is the narrow version of the URL, which should match the wide input. |
73 EXPECT_EQ("http://user:pass@google.com:99/foo;bar?q=a#ref", url.spec()); | 74 EXPECT_EQ("http://user:pass@google.com:99/foo;bar?q=a#ref", url.spec()); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 EXPECT_EQ("", invalid2.username()); | 169 EXPECT_EQ("", invalid2.username()); |
169 EXPECT_EQ("", invalid2.password()); | 170 EXPECT_EQ("", invalid2.password()); |
170 EXPECT_EQ("", invalid2.host()); | 171 EXPECT_EQ("", invalid2.host()); |
171 EXPECT_EQ("", invalid2.port()); | 172 EXPECT_EQ("", invalid2.port()); |
172 EXPECT_EQ(PORT_UNSPECIFIED, invalid2.IntPort()); | 173 EXPECT_EQ(PORT_UNSPECIFIED, invalid2.IntPort()); |
173 EXPECT_EQ("", invalid2.path()); | 174 EXPECT_EQ("", invalid2.path()); |
174 EXPECT_EQ("", invalid2.query()); | 175 EXPECT_EQ("", invalid2.query()); |
175 EXPECT_EQ("", invalid2.ref()); | 176 EXPECT_EQ("", invalid2.ref()); |
176 } | 177 } |
177 | 178 |
178 // This is a regression test for http://crbug.com/309975 . | 179 // This is a regression test for http://crbug.com/309975. |
179 TEST(GURLTest, SelfAssign) { | 180 TEST(GURLTest, SelfAssign) { |
180 GURL a("filesystem:http://example.com/temporary/"); | 181 GURL a("filesystem:http://example.com/temporary/"); |
181 // This should not crash. | 182 // This should not crash. |
182 a = a; | 183 a = a; |
183 } | 184 } |
184 | 185 |
185 TEST(GURLTest, CopyFileSystem) { | 186 TEST(GURLTest, CopyFileSystem) { |
186 GURL url(WStringToUTF16(L"filesystem:https://user:pass@google.com:99/t/foo;bar
?q=a#ref")); | 187 GURL url(WStringToUTF16(L"filesystem:https://user:pass@google.com:99/t/foo;bar
?q=a#ref")); |
187 | 188 |
188 GURL url2(url); | 189 GURL url2(url); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 "http://google.com:12three45", | 239 "http://google.com:12three45", |
239 "path", | 240 "path", |
240 }; | 241 }; |
241 for (size_t i = 0; i < arraysize(invalid_cases); i++) { | 242 for (size_t i = 0; i < arraysize(invalid_cases); i++) { |
242 EXPECT_FALSE(GURL(invalid_cases[i]).is_valid()) | 243 EXPECT_FALSE(GURL(invalid_cases[i]).is_valid()) |
243 << "Case: " << invalid_cases[i]; | 244 << "Case: " << invalid_cases[i]; |
244 } | 245 } |
245 } | 246 } |
246 | 247 |
247 TEST(GURLTest, ExtraSlashesBeforeAuthority) { | 248 TEST(GURLTest, ExtraSlashesBeforeAuthority) { |
248 // According to RFC3986, the hier-part for URI with an authority must use only | 249 // According to RFC3986, the hierarchical part for URI with an authority |
249 // two slashes, GURL intentionally just ignores slashes more than 2 and parses | 250 // must use only two slashes; GURL intentionally just ignores extra slashes |
250 // the following part as an authority. | 251 // if there are more than 2, and parses the following part as an authority. |
251 GURL url("http:///host"); | 252 GURL url("http:///host"); |
252 EXPECT_EQ("host", url.host()); | 253 EXPECT_EQ("host", url.host()); |
253 EXPECT_EQ("/", url.path()); | 254 EXPECT_EQ("/", url.path()); |
254 } | 255 } |
255 | 256 |
256 // Given an invalid URL, we should still get most of the components. | 257 // Given an invalid URL, we should still get most of the components. |
257 TEST(GURLTest, ComponentGettersWorkEvenForInvalidURL) { | 258 TEST(GURLTest, ComponentGettersWorkEvenForInvalidURL) { |
258 GURL url("http:google.com:foo"); | 259 GURL url("http:google.com:foo"); |
259 EXPECT_FALSE(url.is_valid()); | 260 EXPECT_FALSE(url.is_valid()); |
260 EXPECT_EQ("http://google.com:foo/", url.possibly_invalid_spec()); | 261 EXPECT_EQ("http://google.com:foo/", url.possibly_invalid_spec()); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 }; | 372 }; |
372 | 373 |
373 for (size_t i = 0; i < arraysize(cases); i++) { | 374 for (size_t i = 0; i < arraysize(cases); i++) { |
374 GURL url(cases[i].input); | 375 GURL url(cases[i].input); |
375 GURL empty_path = url.GetWithEmptyPath(); | 376 GURL empty_path = url.GetWithEmptyPath(); |
376 EXPECT_EQ(cases[i].expected, empty_path.spec()); | 377 EXPECT_EQ(cases[i].expected, empty_path.spec()); |
377 } | 378 } |
378 } | 379 } |
379 | 380 |
380 TEST(GURLTest, Replacements) { | 381 TEST(GURLTest, Replacements) { |
381 // The url canonicalizer replacement test will handle most of these case. | 382 // The URL canonicalizer replacement test will handle most of these case. |
382 // The most important thing to do here is to check that the proper | 383 // The most important thing to do here is to check that the proper |
383 // canonicalizer gets called based on the scheme of the input. | 384 // canonicalizer gets called based on the scheme of the input. |
384 struct ReplaceCase { | 385 struct ReplaceCase { |
385 const char* base; | 386 const char* base; |
386 const char* scheme; | 387 const char* scheme; |
387 const char* username; | 388 const char* username; |
388 const char* password; | 389 const char* password; |
389 const char* host; | 390 const char* host; |
390 const char* port; | 391 const char* port; |
391 const char* path; | 392 const char* path; |
392 const char* query; | 393 const char* query; |
393 const char* ref; | 394 const char* ref; |
394 const char* expected; | 395 const char* expected; |
395 } replace_cases[] = { | 396 } replace_cases[] = { |
396 {"http://www.google.com/foo/bar.html?foo#bar", NULL, NULL, NULL, NULL, NULL,
"/", "", "", "http://www.google.com/"}, | 397 {"http://www.google.com/foo/bar.html?foo#bar", NULL, NULL, NULL, NULL, NULL,
"/", "", "", "http://www.google.com/"}, |
397 {"http://www.google.com/foo/bar.html?foo#bar", "javascript", "", "", "", "",
"window.open('foo');", "", "", "javascript:window.open('foo');"}, | 398 {"http://www.google.com/foo/bar.html?foo#bar", "javascript", "", "", "", "",
"window.open('foo');", "", "", "javascript:window.open('foo');"}, |
398 {"file:///C:/foo/bar.txt", "http", NULL, NULL, "www.google.com", "99", "/foo
","search", "ref", "http://www.google.com:99/foo?search#ref"}, | 399 {"file:///C:/foo/bar.txt", "http", NULL, NULL, "www.google.com", "99", "/foo
", "search", "ref", "http://www.google.com:99/foo?search#ref"}, |
399 #ifdef WIN32 | 400 #ifdef WIN32 |
400 {"http://www.google.com/foo/bar.html?foo#bar", "file", "", "", "", "", "c:\\
", "", "", "file:///C:/"}, | 401 {"http://www.google.com/foo/bar.html?foo#bar", "file", "", "", "", "", "c:\\
", "", "", "file:///C:/"}, |
401 #endif | 402 #endif |
402 {"filesystem:http://www.google.com/foo/bar.html?foo#bar", NULL, NULL, NULL,
NULL, NULL, "/", "", "", "filesystem:http://www.google.com/foo/"}, | 403 {"filesystem:http://www.google.com/foo/bar.html?foo#bar", NULL, NULL, NULL,
NULL, NULL, "/", "", "", "filesystem:http://www.google.com/foo/"}, |
403 }; | 404 }; |
404 | 405 |
405 for (size_t i = 0; i < arraysize(replace_cases); i++) { | 406 for (size_t i = 0; i < arraysize(replace_cases); i++) { |
406 const ReplaceCase& cur = replace_cases[i]; | 407 const ReplaceCase& cur = replace_cases[i]; |
407 GURL url(cur.base); | 408 GURL url(cur.base); |
408 GURL::Replacements repl; | 409 GURL::Replacements repl; |
(...skipping 19 matching lines...) Expand all Loading... |
428 GURL url(" data: one ? two # three "); | 429 GURL url(" data: one ? two # three "); |
429 | 430 |
430 // By default the trailing whitespace will have been stripped. | 431 // By default the trailing whitespace will have been stripped. |
431 EXPECT_EQ("data: one ? two # three", url.spec()); | 432 EXPECT_EQ("data: one ? two # three", url.spec()); |
432 GURL::Replacements repl; | 433 GURL::Replacements repl; |
433 repl.ClearRef(); | 434 repl.ClearRef(); |
434 GURL url_no_ref = url.ReplaceComponents(repl); | 435 GURL url_no_ref = url.ReplaceComponents(repl); |
435 | 436 |
436 EXPECT_EQ("data: one ? two ", url_no_ref.spec()); | 437 EXPECT_EQ("data: one ? two ", url_no_ref.spec()); |
437 | 438 |
438 // Importing a parsed url via this constructor overload will retain trailing | 439 // Importing a parsed URL via this constructor overload will retain trailing |
439 // whitespace. | 440 // whitespace. |
440 GURL import_url(url_no_ref.spec(), | 441 GURL import_url(url_no_ref.spec(), |
441 url_no_ref.parsed_for_possibly_invalid_spec(), | 442 url_no_ref.parsed_for_possibly_invalid_spec(), |
442 url_no_ref.is_valid()); | 443 url_no_ref.is_valid()); |
443 EXPECT_EQ(url_no_ref, import_url); | 444 EXPECT_EQ(url_no_ref, import_url); |
444 EXPECT_EQ(import_url.query(), " two "); | 445 EXPECT_EQ(import_url.query(), " two "); |
445 } | 446 } |
446 | 447 |
447 TEST(GURLTest, PathForRequest) { | 448 TEST(GURLTest, PathForRequest) { |
448 struct TestCase { | 449 struct TestCase { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 {"", "", ""}, | 555 {"", "", ""}, |
555 }; | 556 }; |
556 for (size_t i = 0; i < arraysize(cases); i++) { | 557 for (size_t i = 0; i < arraysize(cases); i++) { |
557 GURL url(cases[i].input); | 558 GURL url(cases[i].input); |
558 EXPECT_EQ(cases[i].expected_host, url.host()); | 559 EXPECT_EQ(cases[i].expected_host, url.host()); |
559 EXPECT_EQ(cases[i].expected_plainhost, url.HostNoBrackets()); | 560 EXPECT_EQ(cases[i].expected_plainhost, url.HostNoBrackets()); |
560 } | 561 } |
561 } | 562 } |
562 | 563 |
563 TEST(GURLTest, DomainIs) { | 564 TEST(GURLTest, DomainIs) { |
564 const char google_domain[] = "google.com"; | 565 GURL url_1("http://google.com/foo"); |
| 566 EXPECT_TRUE(url_1.DomainIs("google.com")); |
565 | 567 |
566 GURL url_1("http://www.google.com:99/foo"); | 568 // Subdomain and port are ignored. |
567 EXPECT_TRUE(url_1.DomainIs(google_domain)); | 569 GURL url_2("http://www.google.com:99/foo"); |
| 570 EXPECT_TRUE(url_2.DomainIs("google.com")); |
568 | 571 |
569 GURL url_2("http://google.com:99/foo"); | 572 // Different top-level domain. |
570 EXPECT_TRUE(url_2.DomainIs(google_domain)); | 573 GURL url_3("http://www.google.com.cn/foo"); |
| 574 EXPECT_FALSE(url_3.DomainIs("google.com")); |
571 | 575 |
572 GURL url_3("http://google.com./foo"); | 576 // Different host name. |
573 EXPECT_TRUE(url_3.DomainIs(google_domain)); | 577 GURL url_4("http://www.iamnotgoogle.com/foo"); |
| 578 EXPECT_FALSE(url_4.DomainIs("google.com")); |
574 | 579 |
575 GURL url_4("http://google.com/foo"); | 580 // The input must be lower-cased otherwise DomainIs returns false. |
576 EXPECT_FALSE(url_4.DomainIs("google.com.")); | 581 GURL url_5("http://www.google.com/foo"); |
| 582 EXPECT_FALSE(url_5.DomainIs("Google.com")); |
577 | 583 |
578 GURL url_5("http://google.com./foo"); | 584 // If the URL is invalid, DomainIs returns false. |
579 EXPECT_TRUE(url_5.DomainIs("google.com.")); | 585 GURL invalid_url("google.com"); |
| 586 EXPECT_FALSE(invalid_url.is_valid()); |
| 587 EXPECT_FALSE(invalid_url.DomainIs("google.com")); |
| 588 } |
580 | 589 |
581 GURL url_6("http://www.google.com./foo"); | 590 TEST(GURLTest, DomainIsTerminatingDotBehavior) { |
582 EXPECT_TRUE(url_6.DomainIs(".com.")); | 591 // If the host part ends with a dot, it matches input domains |
| 592 // with or without a dot. |
| 593 GURL url_with_dot("http://www.google.com./foo"); |
| 594 EXPECT_TRUE(url_with_dot.DomainIs("google.com")); |
| 595 EXPECT_TRUE(url_with_dot.DomainIs("google.com.")); |
| 596 EXPECT_TRUE(url_with_dot.DomainIs(".com")); |
| 597 EXPECT_TRUE(url_with_dot.DomainIs(".com.")); |
583 | 598 |
584 GURL url_7("http://www.balabala.com/foo"); | 599 // But, if the host name doesn't end with a dot and the input |
585 EXPECT_FALSE(url_7.DomainIs(google_domain)); | 600 // domain does, then it's considered to not match. |
| 601 GURL url_without_dot("http://google.com/foo"); |
| 602 EXPECT_FALSE(url_without_dot.DomainIs("google.com.")); |
586 | 603 |
587 GURL url_8("http://www.google.com.cn/foo"); | 604 // If the URL ends with two dots, it doesn't match. |
588 EXPECT_FALSE(url_8.DomainIs(google_domain)); | 605 GURL url_with_two_dots("http://www.google.com../foo"); |
| 606 EXPECT_FALSE(url_with_two_dots.DomainIs("google.com")); |
| 607 } |
589 | 608 |
590 GURL url_9("http://www.iamnotgoogle.com/foo"); | 609 TEST(GURLTest, DomainIsWithFilesystemScheme) { |
591 EXPECT_FALSE(url_9.DomainIs(google_domain)); | 610 GURL url_1("filesystem:http://www.google.com:99/foo/"); |
| 611 EXPECT_TRUE(url_1.DomainIs("google.com")); |
592 | 612 |
593 GURL url_10("http://www.iamnotgoogle.com../foo"); | 613 GURL url_2("filesystem:http://www.iamnotgoogle.com/foo/"); |
594 EXPECT_FALSE(url_10.DomainIs(".com")); | 614 EXPECT_FALSE(url_2.DomainIs("google.com")); |
595 | |
596 GURL url_11("filesystem:http://www.google.com:99/foo/"); | |
597 EXPECT_TRUE(url_11.DomainIs(google_domain)); | |
598 | |
599 GURL url_12("filesystem:http://www.iamnotgoogle.com/foo/"); | |
600 EXPECT_FALSE(url_12.DomainIs(google_domain)); | |
601 } | 615 } |
602 | 616 |
603 // Newlines should be stripped from inputs. | 617 // Newlines should be stripped from inputs. |
604 TEST(GURLTest, Newlines) { | 618 TEST(GURLTest, Newlines) { |
605 // Constructor. | 619 // Constructor. |
606 GURL url_1(" \t ht\ntp://\twww.goo\rgle.com/as\ndf \n "); | 620 GURL url_1(" \t ht\ntp://\twww.goo\rgle.com/as\ndf \n "); |
607 EXPECT_EQ("http://www.google.com/asdf", url_1.spec()); | 621 EXPECT_EQ("http://www.google.com/asdf", url_1.spec()); |
608 | 622 |
609 // Relative path resolver. | 623 // Relative path resolver. |
610 GURL url_2 = url_1.Resolve(" \n /fo\to\r "); | 624 GURL url_2 = url_1.Resolve(" \n /fo\to\r "); |
(...skipping 24 matching lines...) Expand all Loading... |
635 EXPECT_TRUE(GURL("wss://bar/").SchemeIsWSOrWSS()); | 649 EXPECT_TRUE(GURL("wss://bar/").SchemeIsWSOrWSS()); |
636 EXPECT_FALSE(GURL("http://bar/").SchemeIsWSOrWSS()); | 650 EXPECT_FALSE(GURL("http://bar/").SchemeIsWSOrWSS()); |
637 } | 651 } |
638 | 652 |
639 TEST(GURLTest, SchemeIsBlob) { | 653 TEST(GURLTest, SchemeIsBlob) { |
640 EXPECT_TRUE(GURL("BLOB://BAR/").SchemeIsBlob()); | 654 EXPECT_TRUE(GURL("BLOB://BAR/").SchemeIsBlob()); |
641 EXPECT_TRUE(GURL("blob://bar/").SchemeIsBlob()); | 655 EXPECT_TRUE(GURL("blob://bar/").SchemeIsBlob()); |
642 EXPECT_FALSE(GURL("http://bar/").SchemeIsBlob()); | 656 EXPECT_FALSE(GURL("http://bar/").SchemeIsBlob()); |
643 } | 657 } |
644 | 658 |
| 659 TEST(GURLTest, ContentAndPathForNonStandardURLs) { |
| 660 struct TestCase { |
| 661 const char* url; |
| 662 const char* expected; |
| 663 } cases[] = { |
| 664 {"null", ""}, |
| 665 {"not-a-standard-scheme:this is arbitrary content", |
| 666 "this is arbitrary content"}, |
| 667 {"view-source:http://example.com/path", "http://example.com/path"}, |
| 668 {"blob:http://example.com/GUID", "http://example.com/GUID"}, |
| 669 {"blob://http://example.com/GUID", "//http://example.com/GUID"}, |
| 670 {"blob:http://user:password@example.com/GUID", |
| 671 "http://user:password@example.com/GUID"}, |
| 672 |
| 673 // TODO(mkwst): This seems like a bug. https://crbug.com/513600 |
| 674 {"filesystem:http://example.com/path", "/"}, |
| 675 }; |
| 676 |
| 677 for (const auto& test : cases) { |
| 678 GURL url(test.url); |
| 679 EXPECT_EQ(test.expected, url.path()) << test.url; |
| 680 EXPECT_EQ(test.expected, url.GetContent()) << test.url; |
| 681 } |
| 682 } |
| 683 |
645 } // namespace url | 684 } // namespace url |
OLD | NEW |