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
3 // found in the LICENSE file.
7 #include "base/macros.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 #include "url/url_canon.h"
12 #include "url/url_test_utils.h"
18 template<typename CHAR>
19 void SetupReplacement(
20 void (Replacements<CHAR>::*func)(const CHAR*, const Component&),
21 Replacements<CHAR>* replacements,
26 comp.len = static_cast<int>(strlen(str));
27 (replacements->*func)(str, comp);
31 // Returns the canonicalized string for the given URL string for the
32 // GURLTest.Types test.
33 std::string TypesTestCase(const char* src) {
35 return gurl.possibly_invalid_spec();
40 // Different types of URLs should be handled differently, and handed off to
41 // different canonicalizers.
42 TEST(GURLTest, Types) {
43 // URLs with unknown schemes should be treated as path URLs, even when they
44 // have things like "://".
45 EXPECT_EQ("something:///HOSTNAME.com/",
46 TypesTestCase("something:///HOSTNAME.com/"));
48 // Conversely, URLs with known schemes should always trigger standard URL
50 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"));
53 EXPECT_EQ("http://hostname.com/", TypesTestCase("http:///HOSTNAME.com"));
56 // URLs that look like Windows absolute path specs.
57 EXPECT_EQ("file:///C:/foo.txt", TypesTestCase("c:\\foo.txt"));
58 EXPECT_EQ("file:///Z:/foo.txt", TypesTestCase("Z|foo.txt"));
59 EXPECT_EQ("file://server/foo.txt", TypesTestCase("\\\\server\\foo.txt"));
60 EXPECT_EQ("file://server/foo.txt", TypesTestCase("//server/foo.txt"));
64 // Test the basic creation and querying of components in a GURL. We assume that
65 // the parser is already tested and works, so we are mostly interested if the
66 // object does the right thing with the results.
67 TEST(GURLTest, Components) {
68 GURL empty_url(base::UTF8ToUTF16(""));
69 EXPECT_TRUE(empty_url.is_empty());
70 EXPECT_FALSE(empty_url.is_valid());
72 GURL url(base::UTF8ToUTF16("http://user:pass@google.com:99/foo;bar?q=a#ref"));
73 EXPECT_FALSE(url.is_empty());
74 EXPECT_TRUE(url.is_valid());
75 EXPECT_TRUE(url.SchemeIs("http"));
76 EXPECT_FALSE(url.SchemeIsFile());
78 // This is the narrow version of the URL, which should match the wide input.
79 EXPECT_EQ("http://user:pass@google.com:99/foo;bar?q=a#ref", url.spec());
81 EXPECT_EQ("http", url.scheme());
82 EXPECT_EQ("user", url.username());
83 EXPECT_EQ("pass", url.password());
84 EXPECT_EQ("google.com", url.host());
85 EXPECT_EQ("99", url.port());
86 EXPECT_EQ(99, url.IntPort());
87 EXPECT_EQ("/foo;bar", url.path());
88 EXPECT_EQ("q=a", url.query());
89 EXPECT_EQ("ref", url.ref());
91 // Test parsing userinfo with special characters.
92 GURL url_special_pass("http://user:%40!$&'()*+,;=:@google.com:12345");
93 EXPECT_TRUE(url_special_pass.is_valid());
94 // GURL canonicalizes some delimiters.
95 EXPECT_EQ("%40!$&%27()*+,%3B%3D%3A", url_special_pass.password());
96 EXPECT_EQ("google.com", url_special_pass.host());
97 EXPECT_EQ("12345", url_special_pass.port());
100 TEST(GURLTest, Empty) {
102 EXPECT_FALSE(url.is_valid());
103 EXPECT_EQ("", url.spec());
105 EXPECT_EQ("", url.scheme());
106 EXPECT_EQ("", url.username());
107 EXPECT_EQ("", url.password());
108 EXPECT_EQ("", url.host());
109 EXPECT_EQ("", url.port());
110 EXPECT_EQ(PORT_UNSPECIFIED, url.IntPort());
111 EXPECT_EQ("", url.path());
112 EXPECT_EQ("", url.query());
113 EXPECT_EQ("", url.ref());
116 TEST(GURLTest, Copy) {
117 GURL url(base::UTF8ToUTF16(
118 "http://user:pass@google.com:99/foo;bar?q=a#ref"));
121 EXPECT_TRUE(url2.is_valid());
123 EXPECT_EQ("http://user:pass@google.com:99/foo;bar?q=a#ref", url2.spec());
124 EXPECT_EQ("http", url2.scheme());
125 EXPECT_EQ("user", url2.username());
126 EXPECT_EQ("pass", url2.password());
127 EXPECT_EQ("google.com", url2.host());
128 EXPECT_EQ("99", url2.port());
129 EXPECT_EQ(99, url2.IntPort());
130 EXPECT_EQ("/foo;bar", url2.path());
131 EXPECT_EQ("q=a", url2.query());
132 EXPECT_EQ("ref", url2.ref());
134 // Copying of invalid URL should be invalid
136 GURL invalid2(invalid);
137 EXPECT_FALSE(invalid2.is_valid());
138 EXPECT_EQ("", invalid2.spec());
139 EXPECT_EQ("", invalid2.scheme());
140 EXPECT_EQ("", invalid2.username());
141 EXPECT_EQ("", invalid2.password());
142 EXPECT_EQ("", invalid2.host());
143 EXPECT_EQ("", invalid2.port());
144 EXPECT_EQ(PORT_UNSPECIFIED, invalid2.IntPort());
145 EXPECT_EQ("", invalid2.path());
146 EXPECT_EQ("", invalid2.query());
147 EXPECT_EQ("", invalid2.ref());
150 TEST(GURLTest, Assign) {
151 GURL url(base::UTF8ToUTF16(
152 "http://user:pass@google.com:99/foo;bar?q=a#ref"));
156 EXPECT_TRUE(url2.is_valid());
158 EXPECT_EQ("http://user:pass@google.com:99/foo;bar?q=a#ref", url2.spec());
159 EXPECT_EQ("http", url2.scheme());
160 EXPECT_EQ("user", url2.username());
161 EXPECT_EQ("pass", url2.password());
162 EXPECT_EQ("google.com", url2.host());
163 EXPECT_EQ("99", url2.port());
164 EXPECT_EQ(99, url2.IntPort());
165 EXPECT_EQ("/foo;bar", url2.path());
166 EXPECT_EQ("q=a", url2.query());
167 EXPECT_EQ("ref", url2.ref());
169 // Assignment of invalid URL should be invalid
173 EXPECT_FALSE(invalid2.is_valid());
174 EXPECT_EQ("", invalid2.spec());
175 EXPECT_EQ("", invalid2.scheme());
176 EXPECT_EQ("", invalid2.username());
177 EXPECT_EQ("", invalid2.password());
178 EXPECT_EQ("", invalid2.host());
179 EXPECT_EQ("", invalid2.port());
180 EXPECT_EQ(PORT_UNSPECIFIED, invalid2.IntPort());
181 EXPECT_EQ("", invalid2.path());
182 EXPECT_EQ("", invalid2.query());
183 EXPECT_EQ("", invalid2.ref());
186 // This is a regression test for http://crbug.com/309975.
187 TEST(GURLTest, SelfAssign) {
188 GURL a("filesystem:http://example.com/temporary/");
189 // This should not crash.
190 a = *&a; // The *& defeats Clang's -Wself-assign warning.
193 TEST(GURLTest, CopyFileSystem) {
194 GURL url(base::UTF8ToUTF16(
195 "filesystem:https://user:pass@google.com:99/t/foo;bar?q=a#ref"));
198 EXPECT_TRUE(url2.is_valid());
200 EXPECT_EQ("filesystem:https://google.com:99/t/foo;bar?q=a#ref", url2.spec());
201 EXPECT_EQ("filesystem", url2.scheme());
202 EXPECT_EQ("", url2.username());
203 EXPECT_EQ("", url2.password());
204 EXPECT_EQ("", url2.host());
205 EXPECT_EQ("", url2.port());
206 EXPECT_EQ(PORT_UNSPECIFIED, url2.IntPort());
207 EXPECT_EQ("/foo;bar", url2.path());
208 EXPECT_EQ("q=a", url2.query());
209 EXPECT_EQ("ref", url2.ref());
211 const GURL* inner = url2.inner_url();
213 EXPECT_EQ("https", inner->scheme());
214 EXPECT_EQ("", inner->username());
215 EXPECT_EQ("", inner->password());
216 EXPECT_EQ("google.com", inner->host());
217 EXPECT_EQ("99", inner->port());
218 EXPECT_EQ(99, inner->IntPort());
219 EXPECT_EQ("/t", inner->path());
220 EXPECT_EQ("", inner->query());
221 EXPECT_EQ("", inner->ref());
224 TEST(GURLTest, IsValid) {
225 const char* valid_cases[] = {
227 "unknown://google.com",
228 "http://user:pass@google.com",
229 "http://google.com:12345",
230 "http://google.com/path",
231 "http://google.com//path",
232 "http://google.com?k=v#fragment",
233 "http://user:pass@google.com:12345/path?k=v#fragment",
237 for (size_t i = 0; i < arraysize(valid_cases); i++) {
238 EXPECT_TRUE(GURL(valid_cases[i]).is_valid())
239 << "Case: " << valid_cases[i];
242 const char* invalid_cases[] = {
244 "http:://google.com",
246 "http://google.com:12three45",
250 for (size_t i = 0; i < arraysize(invalid_cases); i++) {
251 EXPECT_FALSE(GURL(invalid_cases[i]).is_valid())
252 << "Case: " << invalid_cases[i];
256 TEST(GURLTest, ExtraSlashesBeforeAuthority) {
257 // According to RFC3986, the hierarchical part for URI with an authority
258 // must use only two slashes; GURL intentionally just ignores extra slashes
259 // if there are more than 2, and parses the following part as an authority.
260 GURL url("http:///host");
261 EXPECT_EQ("host", url.host());
262 EXPECT_EQ("/", url.path());
265 // Given an invalid URL, we should still get most of the components.
266 TEST(GURLTest, ComponentGettersWorkEvenForInvalidURL) {
267 GURL url("http:google.com:foo");
268 EXPECT_FALSE(url.is_valid());
269 EXPECT_EQ("http://google.com:foo/", url.possibly_invalid_spec());
271 EXPECT_EQ("http", url.scheme());
272 EXPECT_EQ("", url.username());
273 EXPECT_EQ("", url.password());
274 EXPECT_EQ("google.com", url.host());
275 EXPECT_EQ("foo", url.port());
276 EXPECT_EQ(PORT_INVALID, url.IntPort());
277 EXPECT_EQ("/", url.path());
278 EXPECT_EQ("", url.query());
279 EXPECT_EQ("", url.ref());
282 TEST(GURLTest, Resolve) {
283 // The tricky cases for relative URL resolving are tested in the
284 // canonicalizer unit test. Here, we just test that the GURL integration
288 const char* relative;
290 const char* expected;
291 } resolve_cases[] = {
292 {"http://www.google.com/", "foo.html", true, "http://www.google.com/foo.html"},
293 {"http://www.google.com/foo/", "bar", true, "http://www.google.com/foo/bar"},
294 {"http://www.google.com/foo/", "/bar", true, "http://www.google.com/bar"},
295 {"http://www.google.com/foo", "bar", true, "http://www.google.com/bar"},
296 {"http://www.google.com/", "http://images.google.com/foo.html", true, "http://images.google.com/foo.html"},
297 {"http://www.google.com/", "http://images.\tgoogle.\ncom/\rfoo.html", true, "http://images.google.com/foo.html"},
298 {"http://www.google.com/blah/bloo?c#d", "../../../hello/./world.html?a#b", true, "http://www.google.com/hello/world.html?a#b"},
299 {"http://www.google.com/foo#bar", "#com", true, "http://www.google.com/foo#com"},
300 {"http://www.google.com/", "Https:images.google.com", true, "https://images.google.com/"},
301 // A non-standard base can be replaced with a standard absolute URL.
302 {"data:blahblah", "http://google.com/", true, "http://google.com/"},
303 {"data:blahblah", "http:google.com", true, "http://google.com/"},
304 // Filesystem URLs have different paths to test.
305 {"filesystem:http://www.google.com/type/", "foo.html", true, "filesystem:http://www.google.com/type/foo.html"},
306 {"filesystem:http://www.google.com/type/", "../foo.html", true, "filesystem:http://www.google.com/type/foo.html"},
309 for (size_t i = 0; i < arraysize(resolve_cases); i++) {
311 GURL input(resolve_cases[i].base);
312 GURL output = input.Resolve(resolve_cases[i].relative);
313 EXPECT_EQ(resolve_cases[i].expected_valid, output.is_valid()) << i;
314 EXPECT_EQ(resolve_cases[i].expected, output.spec()) << i;
315 EXPECT_EQ(output.SchemeIsFileSystem(), output.inner_url() != NULL);
318 GURL inputw(base::UTF8ToUTF16(resolve_cases[i].base));
320 input.Resolve(base::UTF8ToUTF16(resolve_cases[i].relative));
321 EXPECT_EQ(resolve_cases[i].expected_valid, outputw.is_valid()) << i;
322 EXPECT_EQ(resolve_cases[i].expected, outputw.spec()) << i;
323 EXPECT_EQ(outputw.SchemeIsFileSystem(), outputw.inner_url() != NULL);
327 TEST(GURLTest, GetOrigin) {
330 const char* expected;
332 {"http://www.google.com", "http://www.google.com/"},
333 {"javascript:window.alert(\"hello,world\");", ""},
334 {"http://user:pass@www.google.com:21/blah#baz", "http://www.google.com:21/"},
335 {"http://user@www.google.com", "http://www.google.com/"},
336 {"http://:pass@www.google.com", "http://www.google.com/"},
337 {"http://:@www.google.com", "http://www.google.com/"},
338 {"filesystem:http://www.google.com/temp/foo?q#b", "http://www.google.com/"},
339 {"filesystem:http://user:pass@google.com:21/blah#baz", "http://google.com:21/"},
341 for (size_t i = 0; i < arraysize(cases); i++) {
342 GURL url(cases[i].input);
343 GURL origin = url.GetOrigin();
344 EXPECT_EQ(cases[i].expected, origin.spec());
348 TEST(GURLTest, GetAsReferrer) {
351 const char* expected;
353 {"http://www.google.com", "http://www.google.com/"},
354 {"http://user:pass@www.google.com:21/blah#baz", "http://www.google.com:21/blah"},
355 {"http://user@www.google.com", "http://www.google.com/"},
356 {"http://:pass@www.google.com", "http://www.google.com/"},
357 {"http://:@www.google.com", "http://www.google.com/"},
358 {"http://www.google.com/temp/foo?q#b", "http://www.google.com/temp/foo?q"},
360 {"unknown-scheme://foo.html", ""},
361 {"file:///tmp/test.html", ""},
362 {"https://www.google.com", "https://www.google.com/"},
364 for (size_t i = 0; i < arraysize(cases); i++) {
365 GURL url(cases[i].input);
366 GURL origin = url.GetAsReferrer();
367 EXPECT_EQ(cases[i].expected, origin.spec());
371 TEST(GURLTest, GetWithEmptyPath) {
374 const char* expected;
376 {"http://www.google.com", "http://www.google.com/"},
377 {"javascript:window.alert(\"hello, world\");", ""},
378 {"http://www.google.com/foo/bar.html?baz=22", "http://www.google.com/"},
379 {"filesystem:http://www.google.com/temporary/bar.html?baz=22", "filesystem:http://www.google.com/temporary/"},
380 {"filesystem:file:///temporary/bar.html?baz=22", "filesystem:file:///temporary/"},
383 for (size_t i = 0; i < arraysize(cases); i++) {
384 GURL url(cases[i].input);
385 GURL empty_path = url.GetWithEmptyPath();
386 EXPECT_EQ(cases[i].expected, empty_path.spec());
390 TEST(GURLTest, GetWithoutFilename) {
393 const char* expected;
395 // Common Standard URLs.
396 {"https://www.google.com", "https://www.google.com/"},
397 {"https://www.google.com/", "https://www.google.com/"},
398 {"https://www.google.com/maps.htm", "https://www.google.com/"},
399 {"https://www.google.com/maps/", "https://www.google.com/maps/"},
400 {"https://www.google.com/index.html", "https://www.google.com/"},
401 {"https://www.google.com/index.html?q=maps", "https://www.google.com/"},
402 {"https://www.google.com/index.html#maps/", "https://www.google.com/"},
403 {"https://foo:bar@www.google.com/maps.htm", "https://foo:bar@www.google.com/"},
404 {"https://www.google.com/maps/au/index.html", "https://www.google.com/maps/au/"},
405 {"https://www.google.com/maps/au/north", "https://www.google.com/maps/au/"},
406 {"https://www.google.com/maps/au/north/", "https://www.google.com/maps/au/north/"},
407 {"https://www.google.com/maps/au/index.html?q=maps#fragment/", "https://www.google.com/maps/au/"},
408 {"http://www.google.com:8000/maps/au/index.html?q=maps#fragment/", "http://www.google.com:8000/maps/au/"},
409 {"https://www.google.com/maps/au/north/?q=maps#fragment", "https://www.google.com/maps/au/north/"},
410 {"https://www.google.com/maps/au/north?q=maps#fragment", "https://www.google.com/maps/au/"},
411 // Less common standard URLs.
412 {"filesystem:http://www.google.com/temporary/bar.html?baz=22", "filesystem:http://www.google.com/temporary/"},
413 {"file:///temporary/bar.html?baz=22","file:///temporary/"},
414 {"ftp://foo/test/index.html", "ftp://foo/test/"},
415 {"gopher://foo/test/index.html", "gopher://foo/test/"},
416 {"ws://foo/test/index.html", "ws://foo/test/"},
417 // Non-standard, hierarchical URLs.
418 {"chrome://foo/bar.html", "chrome://foo/"},
419 {"httpa://foo/test/index.html", "httpa://foo/test/"},
420 // Non-standard, non-hierarchical URLs.
421 {"blob:https://foo.bar/test/index.html", ""},
424 {"scheme:opaque_data", ""},
429 for (size_t i = 0; i < arraysize(cases); i++) {
430 GURL url(cases[i].input);
431 GURL without_filename = url.GetWithoutFilename();
432 EXPECT_EQ(cases[i].expected, without_filename.spec()) << i;
436 TEST(GURLTest, Replacements) {
437 // The URL canonicalizer replacement test will handle most of these case.
438 // The most important thing to do here is to check that the proper
439 // canonicalizer gets called based on the scheme of the input.
443 const char* username;
444 const char* password;
450 const char* expected;
451 } replace_cases[] = {
452 {"http://www.google.com/foo/bar.html?foo#bar", NULL, NULL, NULL, NULL,
453 NULL, "/", "", "", "http://www.google.com/"},
454 {"http://www.google.com/foo/bar.html?foo#bar", "javascript", "", "", "",
455 "", "window.open('foo');", "", "", "javascript:window.open('foo');"},
456 {"file:///C:/foo/bar.txt", "http", NULL, NULL, "www.google.com", "99",
457 "/foo", "search", "ref", "http://www.google.com:99/foo?search#ref"},
459 {"http://www.google.com/foo/bar.html?foo#bar", "file", "", "", "", "",
460 "c:\\", "", "", "file:///C:/"},
462 {"filesystem:http://www.google.com/foo/bar.html?foo#bar", NULL, NULL,
463 NULL, NULL, NULL, "/", "", "", "filesystem:http://www.google.com/foo/"},
464 // Lengthen the URL instead of shortening it, to test creation of
466 {"filesystem:http://www.google.com/foo/", NULL, NULL, NULL, NULL, NULL,
467 "bar.html", "foo", "bar",
468 "filesystem:http://www.google.com/foo/bar.html?foo#bar"},
471 for (size_t i = 0; i < arraysize(replace_cases); i++) {
472 const ReplaceCase& cur = replace_cases[i];
474 GURL::Replacements repl;
475 SetupReplacement(&GURL::Replacements::SetScheme, &repl, cur.scheme);
476 SetupReplacement(&GURL::Replacements::SetUsername, &repl, cur.username);
477 SetupReplacement(&GURL::Replacements::SetPassword, &repl, cur.password);
478 SetupReplacement(&GURL::Replacements::SetHost, &repl, cur.host);
479 SetupReplacement(&GURL::Replacements::SetPort, &repl, cur.port);
480 SetupReplacement(&GURL::Replacements::SetPath, &repl, cur.path);
481 SetupReplacement(&GURL::Replacements::SetQuery, &repl, cur.query);
482 SetupReplacement(&GURL::Replacements::SetRef, &repl, cur.ref);
483 GURL output = url.ReplaceComponents(repl);
485 EXPECT_EQ(replace_cases[i].expected, output.spec());
487 EXPECT_EQ(output.SchemeIsFileSystem(), output.inner_url() != NULL);
488 if (output.SchemeIsFileSystem()) {
489 // TODO(mmenke): inner_url()->spec() is currently the same as the spec()
490 // for the GURL itself. This should be fixed.
491 // See https://crbug.com/619596
492 EXPECT_EQ(replace_cases[i].expected, output.inner_url()->spec());
497 TEST(GURLTest, ClearFragmentOnDataUrl) {
498 // http://crbug.com/291747 - a data URL may legitimately have trailing
499 // whitespace in the spec after the ref is cleared. Test this does not trigger
500 // the Parsed importing validation DCHECK in GURL.
501 GURL url(" data: one ? two # three ");
503 // By default the trailing whitespace will have been stripped.
504 EXPECT_EQ("data: one ? two # three", url.spec());
505 GURL::Replacements repl;
507 GURL url_no_ref = url.ReplaceComponents(repl);
509 EXPECT_EQ("data: one ? two ", url_no_ref.spec());
511 // Importing a parsed URL via this constructor overload will retain trailing
513 GURL import_url(url_no_ref.spec(),
514 url_no_ref.parsed_for_possibly_invalid_spec(),
515 url_no_ref.is_valid());
516 EXPECT_EQ(url_no_ref, import_url);
517 EXPECT_EQ(import_url.query(), " two ");
520 TEST(GURLTest, PathForRequest) {
523 const char* expected;
524 const char* inner_expected;
526 {"http://www.google.com", "/", NULL},
527 {"http://www.google.com/", "/", NULL},
528 {"http://www.google.com/foo/bar.html?baz=22", "/foo/bar.html?baz=22", NULL},
529 {"http://www.google.com/foo/bar.html#ref", "/foo/bar.html", NULL},
530 {"http://www.google.com/foo/bar.html?query#ref", "/foo/bar.html?query", NULL},
531 {"filesystem:http://www.google.com/temporary/foo/bar.html?query#ref", "/foo/bar.html?query", "/temporary"},
532 {"filesystem:http://www.google.com/temporary/foo/bar.html?query", "/foo/bar.html?query", "/temporary"},
535 for (size_t i = 0; i < arraysize(cases); i++) {
536 GURL url(cases[i].input);
537 std::string path_request = url.PathForRequest();
538 EXPECT_EQ(cases[i].expected, path_request);
539 EXPECT_EQ(cases[i].inner_expected == NULL, url.inner_url() == NULL);
540 if (url.inner_url() && cases[i].inner_expected)
541 EXPECT_EQ(cases[i].inner_expected, url.inner_url()->PathForRequest());
545 TEST(GURLTest, EffectiveIntPort) {
548 int expected_int_port;
551 {"http://www.google.com/", 80},
552 {"http://www.google.com:80/", 80},
553 {"http://www.google.com:443/", 443},
556 {"https://www.google.com/", 443},
557 {"https://www.google.com:443/", 443},
558 {"https://www.google.com:80/", 80},
561 {"ftp://www.google.com/", 21},
562 {"ftp://www.google.com:21/", 21},
563 {"ftp://www.google.com:80/", 80},
566 {"gopher://www.google.com/", 70},
567 {"gopher://www.google.com:70/", 70},
568 {"gopher://www.google.com:80/", 80},
571 {"file://www.google.com/", PORT_UNSPECIFIED},
572 {"file://www.google.com:443/", PORT_UNSPECIFIED},
575 {"data:www.google.com:90", PORT_UNSPECIFIED},
576 {"data:www.google.com", PORT_UNSPECIFIED},
578 // filesystem - no port
579 {"filesystem:http://www.google.com:90/t/foo", PORT_UNSPECIFIED},
580 {"filesystem:file:///t/foo", PORT_UNSPECIFIED},
583 for (size_t i = 0; i < arraysize(port_tests); i++) {
584 GURL url(port_tests[i].spec);
585 EXPECT_EQ(port_tests[i].expected_int_port, url.EffectiveIntPort());
589 TEST(GURLTest, IPAddress) {
594 {"http://www.google.com/", false},
595 {"http://192.168.9.1/", true},
596 {"http://192.168.9.1.2/", false},
597 {"http://192.168.m.1/", false},
598 {"http://2001:db8::1/", false},
599 {"http://[2001:db8::1]/", true},
601 {"some random input!", false},
604 for (size_t i = 0; i < arraysize(ip_tests); i++) {
605 GURL url(ip_tests[i].spec);
606 EXPECT_EQ(ip_tests[i].expected_ip, url.HostIsIPAddress());
610 TEST(GURLTest, HostNoBrackets) {
613 const char* expected_host;
614 const char* expected_plainhost;
616 {"http://www.google.com", "www.google.com", "www.google.com"},
617 {"http://[2001:db8::1]/", "[2001:db8::1]", "2001:db8::1"},
618 {"http://[::]/", "[::]", "::"},
620 // Don't require a valid URL, but don't crash either.
621 {"http://[]/", "[]", ""},
622 {"http://[x]/", "[x]", "x"},
623 {"http://[x/", "[x", "[x"},
624 {"http://x]/", "x]", "x]"},
625 {"http://[/", "[", "["},
626 {"http://]/", "]", "]"},
629 for (size_t i = 0; i < arraysize(cases); i++) {
630 GURL url(cases[i].input);
631 EXPECT_EQ(cases[i].expected_host, url.host());
632 EXPECT_EQ(cases[i].expected_plainhost, url.HostNoBrackets());
633 EXPECT_EQ(cases[i].expected_plainhost, url.HostNoBracketsPiece());
637 TEST(GURLTest, DomainIs) {
638 GURL url_1("http://google.com/foo");
639 EXPECT_TRUE(url_1.DomainIs("google.com"));
641 // Subdomain and port are ignored.
642 GURL url_2("http://www.google.com:99/foo");
643 EXPECT_TRUE(url_2.DomainIs("google.com"));
645 // Different top-level domain.
646 GURL url_3("http://www.google.com.cn/foo");
647 EXPECT_FALSE(url_3.DomainIs("google.com"));
649 // Different host name.
650 GURL url_4("http://www.iamnotgoogle.com/foo");
651 EXPECT_FALSE(url_4.DomainIs("google.com"));
653 // The input must be lower-cased otherwise DomainIs returns false.
654 GURL url_5("http://www.google.com/foo");
655 EXPECT_FALSE(url_5.DomainIs("Google.com"));
657 // If the URL is invalid, DomainIs returns false.
658 GURL invalid_url("google.com");
659 EXPECT_FALSE(invalid_url.is_valid());
660 EXPECT_FALSE(invalid_url.DomainIs("google.com"));
662 GURL url_with_escape_chars("https://www.,.test");
663 EXPECT_TRUE(url_with_escape_chars.is_valid());
664 EXPECT_EQ(url_with_escape_chars.host(), "www.%2C.test");
665 EXPECT_TRUE(url_with_escape_chars.DomainIs("%2C.test"));
668 TEST(GURLTest, DomainIsTerminatingDotBehavior) {
669 // If the host part ends with a dot, it matches input domains
670 // with or without a dot.
671 GURL url_with_dot("http://www.google.com./foo");
672 EXPECT_TRUE(url_with_dot.DomainIs("google.com"));
673 EXPECT_TRUE(url_with_dot.DomainIs("google.com."));
674 EXPECT_TRUE(url_with_dot.DomainIs(".com"));
675 EXPECT_TRUE(url_with_dot.DomainIs(".com."));
677 // But, if the host name doesn't end with a dot and the input
678 // domain does, then it's considered to not match.
679 GURL url_without_dot("http://google.com/foo");
680 EXPECT_FALSE(url_without_dot.DomainIs("google.com."));
682 // If the URL ends with two dots, it doesn't match.
683 GURL url_with_two_dots("http://www.google.com../foo");
684 EXPECT_FALSE(url_with_two_dots.DomainIs("google.com"));
687 TEST(GURLTest, DomainIsWithFilesystemScheme) {
688 GURL url_1("filesystem:http://www.google.com:99/foo/");
689 EXPECT_TRUE(url_1.DomainIs("google.com"));
691 GURL url_2("filesystem:http://www.iamnotgoogle.com/foo/");
692 EXPECT_FALSE(url_2.DomainIs("google.com"));
695 // Newlines should be stripped from inputs.
696 TEST(GURLTest, Newlines) {
698 GURL url_1(" \t ht\ntp://\twww.goo\rgle.com/as\ndf \n ");
699 EXPECT_EQ("http://www.google.com/asdf", url_1.spec());
701 url_1.parsed_for_possibly_invalid_spec().potentially_dangling_markup);
703 // Relative path resolver.
704 GURL url_2 = url_1.Resolve(" \n /fo\to\r ");
705 EXPECT_EQ("http://www.google.com/foo", url_2.spec());
707 url_2.parsed_for_possibly_invalid_spec().potentially_dangling_markup);
710 GURL url_3(" \t ht\ntp://\twww.goo\rgle.com/as\ndf< \n ");
711 EXPECT_EQ("http://www.google.com/asdf%3C", url_3.spec());
713 url_3.parsed_for_possibly_invalid_spec().potentially_dangling_markup);
715 // Relative path resolver.
716 GURL url_4 = url_1.Resolve(" \n /fo\to<\r ");
717 EXPECT_EQ("http://www.google.com/foo%3C", url_4.spec());
719 url_4.parsed_for_possibly_invalid_spec().potentially_dangling_markup);
721 // Note that newlines are NOT stripped from ReplaceComponents.
724 TEST(GURLTest, IsStandard) {
725 GURL a("http:foo/bar");
726 EXPECT_TRUE(a.IsStandard());
728 GURL b("foo:bar/baz");
729 EXPECT_FALSE(b.IsStandard());
731 GURL c("foo://bar/baz");
732 EXPECT_FALSE(c.IsStandard());
734 GURL d("cid:bar@baz");
735 EXPECT_FALSE(d.IsStandard());
738 TEST(GURLTest, SchemeIsHTTPOrHTTPS) {
739 EXPECT_TRUE(GURL("http://bar/").SchemeIsHTTPOrHTTPS());
740 EXPECT_TRUE(GURL("HTTPS://BAR").SchemeIsHTTPOrHTTPS());
741 EXPECT_FALSE(GURL("ftp://bar/").SchemeIsHTTPOrHTTPS());
744 TEST(GURLTest, SchemeIsWSOrWSS) {
745 EXPECT_TRUE(GURL("WS://BAR/").SchemeIsWSOrWSS());
746 EXPECT_TRUE(GURL("wss://bar/").SchemeIsWSOrWSS());
747 EXPECT_FALSE(GURL("http://bar/").SchemeIsWSOrWSS());
750 TEST(GURLTest, SchemeIsCryptographic) {
751 EXPECT_TRUE(GURL("https://foo.bar.com/").SchemeIsCryptographic());
752 EXPECT_TRUE(GURL("HTTPS://foo.bar.com/").SchemeIsCryptographic());
753 EXPECT_TRUE(GURL("HtTpS://foo.bar.com/").SchemeIsCryptographic());
755 EXPECT_TRUE(GURL("wss://foo.bar.com/").SchemeIsCryptographic());
756 EXPECT_TRUE(GURL("WSS://foo.bar.com/").SchemeIsCryptographic());
757 EXPECT_TRUE(GURL("WsS://foo.bar.com/").SchemeIsCryptographic());
759 EXPECT_FALSE(GURL("http://foo.bar.com/").SchemeIsCryptographic());
760 EXPECT_FALSE(GURL("ws://foo.bar.com/").SchemeIsCryptographic());
763 TEST(GURLTest, SchemeIsBlob) {
764 EXPECT_TRUE(GURL("BLOB://BAR/").SchemeIsBlob());
765 EXPECT_TRUE(GURL("blob://bar/").SchemeIsBlob());
766 EXPECT_FALSE(GURL("http://bar/").SchemeIsBlob());
769 TEST(GURLTest, ContentAndPathForNonStandardURLs) {
772 const char* expected;
775 {"not-a-standard-scheme:this is arbitrary content",
776 "this is arbitrary content"},
777 {"view-source:http://example.com/path", "http://example.com/path"},
778 {"blob:http://example.com/GUID", "http://example.com/GUID"},
779 {"blob://http://example.com/GUID", "//http://example.com/GUID"},
780 {"blob:http://user:password@example.com/GUID",
781 "http://user:password@example.com/GUID"},
783 // TODO(mkwst): This seems like a bug. https://crbug.com/513600
784 {"filesystem:http://example.com/path", "/"},
787 for (const auto& test : cases) {
789 EXPECT_EQ(test.expected, url.path()) << test.url;
790 EXPECT_EQ(test.expected, url.GetContent()) << test.url;
794 TEST(GURLTest, IsAboutBlank) {
795 const std::string kAboutBlankUrls[] = {"about:blank", "about:blank?foo",
797 "about:blank?foo#foo"};
798 for (const auto& url : kAboutBlankUrls)
799 EXPECT_TRUE(GURL(url).IsAboutBlank()) << url;
801 const std::string kNotAboutBlankUrls[] = {
802 "http:blank", "about:blan", "about://blank",
803 "about:blank/foo", "about://:8000/blank", "about://foo:foo@/blank",
804 "foo@about:blank", "foo:bar@about:blank", "about:blank:8000"};
805 for (const auto& url : kNotAboutBlankUrls)
806 EXPECT_FALSE(GURL(url).IsAboutBlank()) << url;
809 TEST(GURLTest, EqualsIgnoringRef) {
816 {"http://a.com", "http://a.com", true},
817 {"http://a.com", "http://b.com", false},
820 {"http://a.com#foo", "http://a.com#foo", true},
821 {"http://a.com#foo", "http://b.com#foo", false},
824 {"http://a.com#foo", "http://a.com#bar", true},
825 {"http://a.com#foo", "http://b.com#bar", false},
827 // One has a ref, the other doesn't.
828 {"http://a.com#foo", "http://a.com", true},
829 {"http://a.com#foo", "http://b.com", false},
832 {"http://a.com#", "http://a.com#", true},
833 {"http://a.com#", "http://a.com", true},
835 // URLs that differ only by their last character.
836 {"http://aaa", "http://aab", false},
837 {"http://aaa#foo", "http://aab#foo", false},
839 // Different size of the part before the ref.
840 {"http://123#a", "http://123456#a", false},
843 {"blob:http://a.com#foo", "blob:http://a.com#foo", true},
844 {"blob:http://a.com#foo", "blob:http://a.com#bar", true},
845 {"blob:http://a.com#foo", "blob:http://b.com#bar", false},
848 {"filesystem:http://a.com#foo", "filesystem:http://a.com#foo", true},
849 {"filesystem:http://a.com#foo", "filesystem:http://a.com#bar", true},
850 {"filesystem:http://a.com#foo", "filesystem:http://b.com#bar", false},
853 {"data:text/html,a#foo", "data:text/html,a#bar", true},
854 {"data:text/html,a#foo", "data:text/html,a#foo", true},
855 {"data:text/html,a#foo", "data:text/html,b#foo", false},
858 for (const auto& test_case : kTestCases) {
859 SCOPED_TRACE(testing::Message()
861 << "url_a = " << test_case.url_a << std::endl
862 << "url_b = " << test_case.url_b << std::endl);
864 EXPECT_EQ(test_case.are_equals,
865 GURL(test_case.url_a).EqualsIgnoringRef(GURL(test_case.url_b)));
867 EXPECT_EQ(test_case.are_equals,
868 GURL(test_case.url_b).EqualsIgnoringRef(GURL(test_case.url_a)));
872 TEST(GURLTest, DebugAlias) {
873 GURL url("https://foo.com/bar");
874 DEBUG_ALIAS_FOR_GURL(url_debug_alias, url);
875 EXPECT_STREQ("https://foo.com/bar", url_debug_alias);