Docs: theme_creation_guide.md
[chromium-blink-merge.git] / url / gurl_unittest.cc
blob18aa2aea96c6db8607bd405402d767ee19b916ff
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.
5 #include "base/macros.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "url/gurl.h"
8 #include "url/url_canon.h"
9 #include "url/url_test_utils.h"
11 namespace url {
13 using test_utils::WStringToUTF16;
14 using test_utils::ConvertUTF8ToUTF16;
16 namespace {
18 template<typename CHAR>
19 void SetupReplacement(
20 void (Replacements<CHAR>::*func)(const CHAR*, const Component&),
21 Replacements<CHAR>* replacements,
22 const CHAR* str) {
23 if (str) {
24 Component comp;
25 if (str[0])
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) {
34 GURL gurl(src);
35 return gurl.possibly_invalid_spec();
38 } // namespace
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
49 // handling.
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"));
55 #ifdef WIN32
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"));
61 #endif
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 url(WStringToUTF16(L"http://user:pass@google.com:99/foo;bar?q=a#ref"));
69 EXPECT_TRUE(url.is_valid());
70 EXPECT_TRUE(url.SchemeIs("http"));
71 EXPECT_FALSE(url.SchemeIsFile());
73 // This is the narrow version of the URL, which should match the wide input.
74 EXPECT_EQ("http://user:pass@google.com:99/foo;bar?q=a#ref", url.spec());
76 EXPECT_EQ("http", url.scheme());
77 EXPECT_EQ("user", url.username());
78 EXPECT_EQ("pass", url.password());
79 EXPECT_EQ("google.com", url.host());
80 EXPECT_EQ("99", url.port());
81 EXPECT_EQ(99, url.IntPort());
82 EXPECT_EQ("/foo;bar", url.path());
83 EXPECT_EQ("q=a", url.query());
84 EXPECT_EQ("ref", url.ref());
86 // Test parsing userinfo with special characters.
87 GURL url_special_pass("http://user:%40!$&'()*+,;=:@google.com:12345");
88 EXPECT_TRUE(url_special_pass.is_valid());
89 // GURL canonicalizes some delimiters.
90 EXPECT_EQ("%40!$&%27()*+,%3B%3D%3A", url_special_pass.password());
91 EXPECT_EQ("google.com", url_special_pass.host());
92 EXPECT_EQ("12345", url_special_pass.port());
95 TEST(GURLTest, Empty) {
96 GURL url;
97 EXPECT_FALSE(url.is_valid());
98 EXPECT_EQ("", url.spec());
100 EXPECT_EQ("", url.scheme());
101 EXPECT_EQ("", url.username());
102 EXPECT_EQ("", url.password());
103 EXPECT_EQ("", url.host());
104 EXPECT_EQ("", url.port());
105 EXPECT_EQ(PORT_UNSPECIFIED, url.IntPort());
106 EXPECT_EQ("", url.path());
107 EXPECT_EQ("", url.query());
108 EXPECT_EQ("", url.ref());
111 TEST(GURLTest, Copy) {
112 GURL url(WStringToUTF16(L"http://user:pass@google.com:99/foo;bar?q=a#ref"));
114 GURL url2(url);
115 EXPECT_TRUE(url2.is_valid());
117 EXPECT_EQ("http://user:pass@google.com:99/foo;bar?q=a#ref", url2.spec());
118 EXPECT_EQ("http", url2.scheme());
119 EXPECT_EQ("user", url2.username());
120 EXPECT_EQ("pass", url2.password());
121 EXPECT_EQ("google.com", url2.host());
122 EXPECT_EQ("99", url2.port());
123 EXPECT_EQ(99, url2.IntPort());
124 EXPECT_EQ("/foo;bar", url2.path());
125 EXPECT_EQ("q=a", url2.query());
126 EXPECT_EQ("ref", url2.ref());
128 // Copying of invalid URL should be invalid
129 GURL invalid;
130 GURL invalid2(invalid);
131 EXPECT_FALSE(invalid2.is_valid());
132 EXPECT_EQ("", invalid2.spec());
133 EXPECT_EQ("", invalid2.scheme());
134 EXPECT_EQ("", invalid2.username());
135 EXPECT_EQ("", invalid2.password());
136 EXPECT_EQ("", invalid2.host());
137 EXPECT_EQ("", invalid2.port());
138 EXPECT_EQ(PORT_UNSPECIFIED, invalid2.IntPort());
139 EXPECT_EQ("", invalid2.path());
140 EXPECT_EQ("", invalid2.query());
141 EXPECT_EQ("", invalid2.ref());
144 TEST(GURLTest, Assign) {
145 GURL url(WStringToUTF16(L"http://user:pass@google.com:99/foo;bar?q=a#ref"));
147 GURL url2;
148 url2 = url;
149 EXPECT_TRUE(url2.is_valid());
151 EXPECT_EQ("http://user:pass@google.com:99/foo;bar?q=a#ref", url2.spec());
152 EXPECT_EQ("http", url2.scheme());
153 EXPECT_EQ("user", url2.username());
154 EXPECT_EQ("pass", url2.password());
155 EXPECT_EQ("google.com", url2.host());
156 EXPECT_EQ("99", url2.port());
157 EXPECT_EQ(99, url2.IntPort());
158 EXPECT_EQ("/foo;bar", url2.path());
159 EXPECT_EQ("q=a", url2.query());
160 EXPECT_EQ("ref", url2.ref());
162 // Assignment of invalid URL should be invalid
163 GURL invalid;
164 GURL invalid2;
165 invalid2 = invalid;
166 EXPECT_FALSE(invalid2.is_valid());
167 EXPECT_EQ("", invalid2.spec());
168 EXPECT_EQ("", invalid2.scheme());
169 EXPECT_EQ("", invalid2.username());
170 EXPECT_EQ("", invalid2.password());
171 EXPECT_EQ("", invalid2.host());
172 EXPECT_EQ("", invalid2.port());
173 EXPECT_EQ(PORT_UNSPECIFIED, invalid2.IntPort());
174 EXPECT_EQ("", invalid2.path());
175 EXPECT_EQ("", invalid2.query());
176 EXPECT_EQ("", invalid2.ref());
179 // This is a regression test for http://crbug.com/309975.
180 TEST(GURLTest, SelfAssign) {
181 GURL a("filesystem:http://example.com/temporary/");
182 // This should not crash.
183 a = a;
186 TEST(GURLTest, CopyFileSystem) {
187 GURL url(WStringToUTF16(L"filesystem:https://user:pass@google.com:99/t/foo;bar?q=a#ref"));
189 GURL url2(url);
190 EXPECT_TRUE(url2.is_valid());
192 EXPECT_EQ("filesystem:https://user:pass@google.com:99/t/foo;bar?q=a#ref", url2.spec());
193 EXPECT_EQ("filesystem", url2.scheme());
194 EXPECT_EQ("", url2.username());
195 EXPECT_EQ("", url2.password());
196 EXPECT_EQ("", url2.host());
197 EXPECT_EQ("", url2.port());
198 EXPECT_EQ(PORT_UNSPECIFIED, url2.IntPort());
199 EXPECT_EQ("/foo;bar", url2.path());
200 EXPECT_EQ("q=a", url2.query());
201 EXPECT_EQ("ref", url2.ref());
203 const GURL* inner = url2.inner_url();
204 ASSERT_TRUE(inner);
205 EXPECT_EQ("https", inner->scheme());
206 EXPECT_EQ("user", inner->username());
207 EXPECT_EQ("pass", inner->password());
208 EXPECT_EQ("google.com", inner->host());
209 EXPECT_EQ("99", inner->port());
210 EXPECT_EQ(99, inner->IntPort());
211 EXPECT_EQ("/t", inner->path());
212 EXPECT_EQ("", inner->query());
213 EXPECT_EQ("", inner->ref());
216 TEST(GURLTest, IsValid) {
217 const char* valid_cases[] = {
218 "http://google.com",
219 "unknown://google.com",
220 "http://user:pass@google.com",
221 "http://google.com:12345",
222 "http://google.com/path",
223 "http://google.com//path",
224 "http://google.com?k=v#fragment",
225 "http://user:pass@google.com:12345/path?k=v#fragment",
226 "http:/path",
227 "http:path",
228 "://google.com",
230 for (size_t i = 0; i < arraysize(valid_cases); i++) {
231 EXPECT_TRUE(GURL(valid_cases[i]).is_valid())
232 << "Case: " << valid_cases[i];
235 const char* invalid_cases[] = {
236 "http://?k=v",
237 "http:://google.com",
238 "http//google.com",
239 "http://google.com:12three45",
240 "path",
242 for (size_t i = 0; i < arraysize(invalid_cases); i++) {
243 EXPECT_FALSE(GURL(invalid_cases[i]).is_valid())
244 << "Case: " << invalid_cases[i];
248 TEST(GURLTest, ExtraSlashesBeforeAuthority) {
249 // According to RFC3986, the hierarchical part for URI with an authority
250 // must use only two slashes; GURL intentionally just ignores extra slashes
251 // if there are more than 2, and parses the following part as an authority.
252 GURL url("http:///host");
253 EXPECT_EQ("host", url.host());
254 EXPECT_EQ("/", url.path());
257 // Given an invalid URL, we should still get most of the components.
258 TEST(GURLTest, ComponentGettersWorkEvenForInvalidURL) {
259 GURL url("http:google.com:foo");
260 EXPECT_FALSE(url.is_valid());
261 EXPECT_EQ("http://google.com:foo/", url.possibly_invalid_spec());
263 EXPECT_EQ("http", url.scheme());
264 EXPECT_EQ("", url.username());
265 EXPECT_EQ("", url.password());
266 EXPECT_EQ("google.com", url.host());
267 EXPECT_EQ("foo", url.port());
268 EXPECT_EQ(PORT_INVALID, url.IntPort());
269 EXPECT_EQ("/", url.path());
270 EXPECT_EQ("", url.query());
271 EXPECT_EQ("", url.ref());
274 TEST(GURLTest, Resolve) {
275 // The tricky cases for relative URL resolving are tested in the
276 // canonicalizer unit test. Here, we just test that the GURL integration
277 // works properly.
278 struct ResolveCase {
279 const char* base;
280 const char* relative;
281 bool expected_valid;
282 const char* expected;
283 } resolve_cases[] = {
284 {"http://www.google.com/", "foo.html", true, "http://www.google.com/foo.html"},
285 {"http://www.google.com/foo/", "bar", true, "http://www.google.com/foo/bar"},
286 {"http://www.google.com/foo/", "/bar", true, "http://www.google.com/bar"},
287 {"http://www.google.com/foo", "bar", true, "http://www.google.com/bar"},
288 {"http://www.google.com/", "http://images.google.com/foo.html", true, "http://images.google.com/foo.html"},
289 {"http://www.google.com/blah/bloo?c#d", "../../../hello/./world.html?a#b", true, "http://www.google.com/hello/world.html?a#b"},
290 {"http://www.google.com/foo#bar", "#com", true, "http://www.google.com/foo#com"},
291 {"http://www.google.com/", "Https:images.google.com", true, "https://images.google.com/"},
292 // A non-standard base can be replaced with a standard absolute URL.
293 {"data:blahblah", "http://google.com/", true, "http://google.com/"},
294 {"data:blahblah", "http:google.com", true, "http://google.com/"},
295 // Filesystem URLs have different paths to test.
296 {"filesystem:http://www.google.com/type/", "foo.html", true, "filesystem:http://www.google.com/type/foo.html"},
297 {"filesystem:http://www.google.com/type/", "../foo.html", true, "filesystem:http://www.google.com/type/foo.html"},
300 for (size_t i = 0; i < arraysize(resolve_cases); i++) {
301 // 8-bit code path.
302 GURL input(resolve_cases[i].base);
303 GURL output = input.Resolve(resolve_cases[i].relative);
304 EXPECT_EQ(resolve_cases[i].expected_valid, output.is_valid()) << i;
305 EXPECT_EQ(resolve_cases[i].expected, output.spec()) << i;
306 EXPECT_EQ(output.SchemeIsFileSystem(), output.inner_url() != NULL);
308 // Wide code path.
309 GURL inputw(ConvertUTF8ToUTF16(resolve_cases[i].base));
310 GURL outputw =
311 input.Resolve(ConvertUTF8ToUTF16(resolve_cases[i].relative));
312 EXPECT_EQ(resolve_cases[i].expected_valid, outputw.is_valid()) << i;
313 EXPECT_EQ(resolve_cases[i].expected, outputw.spec()) << i;
314 EXPECT_EQ(outputw.SchemeIsFileSystem(), outputw.inner_url() != NULL);
318 TEST(GURLTest, GetOrigin) {
319 struct TestCase {
320 const char* input;
321 const char* expected;
322 } cases[] = {
323 {"http://www.google.com", "http://www.google.com/"},
324 {"javascript:window.alert(\"hello,world\");", ""},
325 {"http://user:pass@www.google.com:21/blah#baz", "http://www.google.com:21/"},
326 {"http://user@www.google.com", "http://www.google.com/"},
327 {"http://:pass@www.google.com", "http://www.google.com/"},
328 {"http://:@www.google.com", "http://www.google.com/"},
329 {"filesystem:http://www.google.com/temp/foo?q#b", "http://www.google.com/"},
330 {"filesystem:http://user:pass@google.com:21/blah#baz", "http://google.com:21/"},
332 for (size_t i = 0; i < arraysize(cases); i++) {
333 GURL url(cases[i].input);
334 GURL origin = url.GetOrigin();
335 EXPECT_EQ(cases[i].expected, origin.spec());
339 TEST(GURLTest, GetAsReferrer) {
340 struct TestCase {
341 const char* input;
342 const char* expected;
343 } cases[] = {
344 {"http://www.google.com", "http://www.google.com/"},
345 {"http://user:pass@www.google.com:21/blah#baz", "http://www.google.com:21/blah"},
346 {"http://user@www.google.com", "http://www.google.com/"},
347 {"http://:pass@www.google.com", "http://www.google.com/"},
348 {"http://:@www.google.com", "http://www.google.com/"},
349 {"http://www.google.com/temp/foo?q#b", "http://www.google.com/temp/foo?q"},
350 {"not a url", ""},
351 {"unknown-scheme://foo.html", ""},
352 {"file:///tmp/test.html", ""},
353 {"https://www.google.com", "https://www.google.com/"},
355 for (size_t i = 0; i < arraysize(cases); i++) {
356 GURL url(cases[i].input);
357 GURL origin = url.GetAsReferrer();
358 EXPECT_EQ(cases[i].expected, origin.spec());
362 TEST(GURLTest, GetWithEmptyPath) {
363 struct TestCase {
364 const char* input;
365 const char* expected;
366 } cases[] = {
367 {"http://www.google.com", "http://www.google.com/"},
368 {"javascript:window.alert(\"hello, world\");", ""},
369 {"http://www.google.com/foo/bar.html?baz=22", "http://www.google.com/"},
370 {"filesystem:http://www.google.com/temporary/bar.html?baz=22", "filesystem:http://www.google.com/temporary/"},
371 {"filesystem:file:///temporary/bar.html?baz=22", "filesystem:file:///temporary/"},
374 for (size_t i = 0; i < arraysize(cases); i++) {
375 GURL url(cases[i].input);
376 GURL empty_path = url.GetWithEmptyPath();
377 EXPECT_EQ(cases[i].expected, empty_path.spec());
381 TEST(GURLTest, Replacements) {
382 // The URL canonicalizer replacement test will handle most of these case.
383 // The most important thing to do here is to check that the proper
384 // canonicalizer gets called based on the scheme of the input.
385 struct ReplaceCase {
386 const char* base;
387 const char* scheme;
388 const char* username;
389 const char* password;
390 const char* host;
391 const char* port;
392 const char* path;
393 const char* query;
394 const char* ref;
395 const char* expected;
396 } replace_cases[] = {
397 {"http://www.google.com/foo/bar.html?foo#bar", NULL, NULL, NULL, NULL, NULL, "/", "", "", "http://www.google.com/"},
398 {"http://www.google.com/foo/bar.html?foo#bar", "javascript", "", "", "", "", "window.open('foo');", "", "", "javascript:window.open('foo');"},
399 {"file:///C:/foo/bar.txt", "http", NULL, NULL, "www.google.com", "99", "/foo", "search", "ref", "http://www.google.com:99/foo?search#ref"},
400 #ifdef WIN32
401 {"http://www.google.com/foo/bar.html?foo#bar", "file", "", "", "", "", "c:\\", "", "", "file:///C:/"},
402 #endif
403 {"filesystem:http://www.google.com/foo/bar.html?foo#bar", NULL, NULL, NULL, NULL, NULL, "/", "", "", "filesystem:http://www.google.com/foo/"},
406 for (size_t i = 0; i < arraysize(replace_cases); i++) {
407 const ReplaceCase& cur = replace_cases[i];
408 GURL url(cur.base);
409 GURL::Replacements repl;
410 SetupReplacement(&GURL::Replacements::SetScheme, &repl, cur.scheme);
411 SetupReplacement(&GURL::Replacements::SetUsername, &repl, cur.username);
412 SetupReplacement(&GURL::Replacements::SetPassword, &repl, cur.password);
413 SetupReplacement(&GURL::Replacements::SetHost, &repl, cur.host);
414 SetupReplacement(&GURL::Replacements::SetPort, &repl, cur.port);
415 SetupReplacement(&GURL::Replacements::SetPath, &repl, cur.path);
416 SetupReplacement(&GURL::Replacements::SetQuery, &repl, cur.query);
417 SetupReplacement(&GURL::Replacements::SetRef, &repl, cur.ref);
418 GURL output = url.ReplaceComponents(repl);
420 EXPECT_EQ(replace_cases[i].expected, output.spec());
421 EXPECT_EQ(output.SchemeIsFileSystem(), output.inner_url() != NULL);
425 TEST(GURLTest, ClearFragmentOnDataUrl) {
426 // http://crbug.com/291747 - a data URL may legitimately have trailing
427 // whitespace in the spec after the ref is cleared. Test this does not trigger
428 // the Parsed importing validation DCHECK in GURL.
429 GURL url(" data: one ? two # three ");
431 // By default the trailing whitespace will have been stripped.
432 EXPECT_EQ("data: one ? two # three", url.spec());
433 GURL::Replacements repl;
434 repl.ClearRef();
435 GURL url_no_ref = url.ReplaceComponents(repl);
437 EXPECT_EQ("data: one ? two ", url_no_ref.spec());
439 // Importing a parsed URL via this constructor overload will retain trailing
440 // whitespace.
441 GURL import_url(url_no_ref.spec(),
442 url_no_ref.parsed_for_possibly_invalid_spec(),
443 url_no_ref.is_valid());
444 EXPECT_EQ(url_no_ref, import_url);
445 EXPECT_EQ(import_url.query(), " two ");
448 TEST(GURLTest, PathForRequest) {
449 struct TestCase {
450 const char* input;
451 const char* expected;
452 const char* inner_expected;
453 } cases[] = {
454 {"http://www.google.com", "/", NULL},
455 {"http://www.google.com/", "/", NULL},
456 {"http://www.google.com/foo/bar.html?baz=22", "/foo/bar.html?baz=22", NULL},
457 {"http://www.google.com/foo/bar.html#ref", "/foo/bar.html", NULL},
458 {"http://www.google.com/foo/bar.html?query#ref", "/foo/bar.html?query", NULL},
459 {"filesystem:http://www.google.com/temporary/foo/bar.html?query#ref", "/foo/bar.html?query", "/temporary"},
460 {"filesystem:http://www.google.com/temporary/foo/bar.html?query", "/foo/bar.html?query", "/temporary"},
463 for (size_t i = 0; i < arraysize(cases); i++) {
464 GURL url(cases[i].input);
465 std::string path_request = url.PathForRequest();
466 EXPECT_EQ(cases[i].expected, path_request);
467 EXPECT_EQ(cases[i].inner_expected == NULL, url.inner_url() == NULL);
468 if (url.inner_url() && cases[i].inner_expected)
469 EXPECT_EQ(cases[i].inner_expected, url.inner_url()->PathForRequest());
473 TEST(GURLTest, EffectiveIntPort) {
474 struct PortTest {
475 const char* spec;
476 int expected_int_port;
477 } port_tests[] = {
478 // http
479 {"http://www.google.com/", 80},
480 {"http://www.google.com:80/", 80},
481 {"http://www.google.com:443/", 443},
483 // https
484 {"https://www.google.com/", 443},
485 {"https://www.google.com:443/", 443},
486 {"https://www.google.com:80/", 80},
488 // ftp
489 {"ftp://www.google.com/", 21},
490 {"ftp://www.google.com:21/", 21},
491 {"ftp://www.google.com:80/", 80},
493 // gopher
494 {"gopher://www.google.com/", 70},
495 {"gopher://www.google.com:70/", 70},
496 {"gopher://www.google.com:80/", 80},
498 // file - no port
499 {"file://www.google.com/", PORT_UNSPECIFIED},
500 {"file://www.google.com:443/", PORT_UNSPECIFIED},
502 // data - no port
503 {"data:www.google.com:90", PORT_UNSPECIFIED},
504 {"data:www.google.com", PORT_UNSPECIFIED},
506 // filesystem - no port
507 {"filesystem:http://www.google.com:90/t/foo", PORT_UNSPECIFIED},
508 {"filesystem:file:///t/foo", PORT_UNSPECIFIED},
511 for (size_t i = 0; i < arraysize(port_tests); i++) {
512 GURL url(port_tests[i].spec);
513 EXPECT_EQ(port_tests[i].expected_int_port, url.EffectiveIntPort());
517 TEST(GURLTest, IPAddress) {
518 struct IPTest {
519 const char* spec;
520 bool expected_ip;
521 } ip_tests[] = {
522 {"http://www.google.com/", false},
523 {"http://192.168.9.1/", true},
524 {"http://192.168.9.1.2/", false},
525 {"http://192.168.m.1/", false},
526 {"http://2001:db8::1/", false},
527 {"http://[2001:db8::1]/", true},
528 {"", false},
529 {"some random input!", false},
532 for (size_t i = 0; i < arraysize(ip_tests); i++) {
533 GURL url(ip_tests[i].spec);
534 EXPECT_EQ(ip_tests[i].expected_ip, url.HostIsIPAddress());
538 TEST(GURLTest, HostNoBrackets) {
539 struct TestCase {
540 const char* input;
541 const char* expected_host;
542 const char* expected_plainhost;
543 } cases[] = {
544 {"http://www.google.com", "www.google.com", "www.google.com"},
545 {"http://[2001:db8::1]/", "[2001:db8::1]", "2001:db8::1"},
546 {"http://[::]/", "[::]", "::"},
548 // Don't require a valid URL, but don't crash either.
549 {"http://[]/", "[]", ""},
550 {"http://[x]/", "[x]", "x"},
551 {"http://[x/", "[x", "[x"},
552 {"http://x]/", "x]", "x]"},
553 {"http://[/", "[", "["},
554 {"http://]/", "]", "]"},
555 {"", "", ""},
557 for (size_t i = 0; i < arraysize(cases); i++) {
558 GURL url(cases[i].input);
559 EXPECT_EQ(cases[i].expected_host, url.host());
560 EXPECT_EQ(cases[i].expected_plainhost, url.HostNoBrackets());
564 TEST(GURLTest, DomainIs) {
565 GURL url_1("http://google.com/foo");
566 EXPECT_TRUE(url_1.DomainIs("google.com"));
568 // Subdomain and port are ignored.
569 GURL url_2("http://www.google.com:99/foo");
570 EXPECT_TRUE(url_2.DomainIs("google.com"));
572 // Different top-level domain.
573 GURL url_3("http://www.google.com.cn/foo");
574 EXPECT_FALSE(url_3.DomainIs("google.com"));
576 // Different host name.
577 GURL url_4("http://www.iamnotgoogle.com/foo");
578 EXPECT_FALSE(url_4.DomainIs("google.com"));
580 // The input must be lower-cased otherwise DomainIs returns false.
581 GURL url_5("http://www.google.com/foo");
582 EXPECT_FALSE(url_5.DomainIs("Google.com"));
584 // If the URL is invalid, DomainIs returns false.
585 GURL invalid_url("google.com");
586 EXPECT_FALSE(invalid_url.is_valid());
587 EXPECT_FALSE(invalid_url.DomainIs("google.com"));
590 TEST(GURLTest, DomainIsTerminatingDotBehavior) {
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."));
599 // But, if the host name doesn't end with a dot and the input
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."));
604 // If the URL ends with two dots, it doesn't match.
605 GURL url_with_two_dots("http://www.google.com../foo");
606 EXPECT_FALSE(url_with_two_dots.DomainIs("google.com"));
609 TEST(GURLTest, DomainIsWithFilesystemScheme) {
610 GURL url_1("filesystem:http://www.google.com:99/foo/");
611 EXPECT_TRUE(url_1.DomainIs("google.com"));
613 GURL url_2("filesystem:http://www.iamnotgoogle.com/foo/");
614 EXPECT_FALSE(url_2.DomainIs("google.com"));
617 // Newlines should be stripped from inputs.
618 TEST(GURLTest, Newlines) {
619 // Constructor.
620 GURL url_1(" \t ht\ntp://\twww.goo\rgle.com/as\ndf \n ");
621 EXPECT_EQ("http://www.google.com/asdf", url_1.spec());
623 // Relative path resolver.
624 GURL url_2 = url_1.Resolve(" \n /fo\to\r ");
625 EXPECT_EQ("http://www.google.com/foo", url_2.spec());
627 // Note that newlines are NOT stripped from ReplaceComponents.
630 TEST(GURLTest, IsStandard) {
631 GURL a("http:foo/bar");
632 EXPECT_TRUE(a.IsStandard());
634 GURL b("foo:bar/baz");
635 EXPECT_FALSE(b.IsStandard());
637 GURL c("foo://bar/baz");
638 EXPECT_FALSE(c.IsStandard());
641 TEST(GURLTest, SchemeIsHTTPOrHTTPS) {
642 EXPECT_TRUE(GURL("http://bar/").SchemeIsHTTPOrHTTPS());
643 EXPECT_TRUE(GURL("HTTPS://BAR").SchemeIsHTTPOrHTTPS());
644 EXPECT_FALSE(GURL("ftp://bar/").SchemeIsHTTPOrHTTPS());
647 TEST(GURLTest, SchemeIsWSOrWSS) {
648 EXPECT_TRUE(GURL("WS://BAR/").SchemeIsWSOrWSS());
649 EXPECT_TRUE(GURL("wss://bar/").SchemeIsWSOrWSS());
650 EXPECT_FALSE(GURL("http://bar/").SchemeIsWSOrWSS());
653 TEST(GURLTest, SchemeIsBlob) {
654 EXPECT_TRUE(GURL("BLOB://BAR/").SchemeIsBlob());
655 EXPECT_TRUE(GURL("blob://bar/").SchemeIsBlob());
656 EXPECT_FALSE(GURL("http://bar/").SchemeIsBlob());
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"},
673 // TODO(mkwst): This seems like a bug. https://crbug.com/513600
674 {"filesystem:http://example.com/path", "/"},
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;
684 } // namespace url