Add metrics for tracking LoginDatabase::Init failure.
[chromium-blink-merge.git] / url / gurl_unittest.cc
blob788c3d896837b91ccb686d50fc9ec995e69a14c1
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 // In the reverse, known schemes should always trigger standard URL handling.
49 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"));
52 EXPECT_EQ("http://hostname.com/", TypesTestCase("http:///HOSTNAME.com"));
54 #ifdef WIN32
55 // URLs that look like absolute Windows drive specs.
56 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://server/foo.txt", TypesTestCase("\\\\server\\foo.txt"));
59 EXPECT_EQ("file://server/foo.txt", TypesTestCase("//server/foo.txt"));
60 #endif
63 // Test the basic creation and querying of components in a GURL. We assume
64 // the parser is already tested and works, so we are mostly interested if the
65 // object does the right thing with the results.
66 TEST(GURLTest, Components) {
67 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.SchemeIs("http"));
70 EXPECT_FALSE(url.SchemeIsFile());
72 // 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());
75 EXPECT_EQ("http", url.scheme());
76 EXPECT_EQ("user", url.username());
77 EXPECT_EQ("pass", url.password());
78 EXPECT_EQ("google.com", url.host());
79 EXPECT_EQ("99", url.port());
80 EXPECT_EQ(99, url.IntPort());
81 EXPECT_EQ("/foo;bar", url.path());
82 EXPECT_EQ("q=a", url.query());
83 EXPECT_EQ("ref", url.ref());
85 // Test parsing userinfo with special characters.
86 GURL url_special_pass("http://user:%40!$&'()*+,;=:@google.com:12345");
87 EXPECT_TRUE(url_special_pass.is_valid());
88 // GURL canonicalizes some delimiters.
89 EXPECT_EQ("%40!$&%27()*+,%3B%3D%3A", url_special_pass.password());
90 EXPECT_EQ("google.com", url_special_pass.host());
91 EXPECT_EQ("12345", url_special_pass.port());
94 TEST(GURLTest, Empty) {
95 GURL url;
96 EXPECT_FALSE(url.is_valid());
97 EXPECT_EQ("", url.spec());
99 EXPECT_EQ("", url.scheme());
100 EXPECT_EQ("", url.username());
101 EXPECT_EQ("", url.password());
102 EXPECT_EQ("", url.host());
103 EXPECT_EQ("", url.port());
104 EXPECT_EQ(PORT_UNSPECIFIED, url.IntPort());
105 EXPECT_EQ("", url.path());
106 EXPECT_EQ("", url.query());
107 EXPECT_EQ("", url.ref());
110 TEST(GURLTest, Copy) {
111 GURL url(WStringToUTF16(L"http://user:pass@google.com:99/foo;bar?q=a#ref"));
113 GURL url2(url);
114 EXPECT_TRUE(url2.is_valid());
116 EXPECT_EQ("http://user:pass@google.com:99/foo;bar?q=a#ref", url2.spec());
117 EXPECT_EQ("http", url2.scheme());
118 EXPECT_EQ("user", url2.username());
119 EXPECT_EQ("pass", url2.password());
120 EXPECT_EQ("google.com", url2.host());
121 EXPECT_EQ("99", url2.port());
122 EXPECT_EQ(99, url2.IntPort());
123 EXPECT_EQ("/foo;bar", url2.path());
124 EXPECT_EQ("q=a", url2.query());
125 EXPECT_EQ("ref", url2.ref());
127 // Copying of invalid URL should be invalid
128 GURL invalid;
129 GURL invalid2(invalid);
130 EXPECT_FALSE(invalid2.is_valid());
131 EXPECT_EQ("", invalid2.spec());
132 EXPECT_EQ("", invalid2.scheme());
133 EXPECT_EQ("", invalid2.username());
134 EXPECT_EQ("", invalid2.password());
135 EXPECT_EQ("", invalid2.host());
136 EXPECT_EQ("", invalid2.port());
137 EXPECT_EQ(PORT_UNSPECIFIED, invalid2.IntPort());
138 EXPECT_EQ("", invalid2.path());
139 EXPECT_EQ("", invalid2.query());
140 EXPECT_EQ("", invalid2.ref());
143 TEST(GURLTest, Assign) {
144 GURL url(WStringToUTF16(L"http://user:pass@google.com:99/foo;bar?q=a#ref"));
146 GURL url2;
147 url2 = url;
148 EXPECT_TRUE(url2.is_valid());
150 EXPECT_EQ("http://user:pass@google.com:99/foo;bar?q=a#ref", url2.spec());
151 EXPECT_EQ("http", url2.scheme());
152 EXPECT_EQ("user", url2.username());
153 EXPECT_EQ("pass", url2.password());
154 EXPECT_EQ("google.com", url2.host());
155 EXPECT_EQ("99", url2.port());
156 EXPECT_EQ(99, url2.IntPort());
157 EXPECT_EQ("/foo;bar", url2.path());
158 EXPECT_EQ("q=a", url2.query());
159 EXPECT_EQ("ref", url2.ref());
161 // Assignment of invalid URL should be invalid
162 GURL invalid;
163 GURL invalid2;
164 invalid2 = invalid;
165 EXPECT_FALSE(invalid2.is_valid());
166 EXPECT_EQ("", invalid2.spec());
167 EXPECT_EQ("", invalid2.scheme());
168 EXPECT_EQ("", invalid2.username());
169 EXPECT_EQ("", invalid2.password());
170 EXPECT_EQ("", invalid2.host());
171 EXPECT_EQ("", invalid2.port());
172 EXPECT_EQ(PORT_UNSPECIFIED, invalid2.IntPort());
173 EXPECT_EQ("", invalid2.path());
174 EXPECT_EQ("", invalid2.query());
175 EXPECT_EQ("", invalid2.ref());
178 // This is a regression test for http://crbug.com/309975 .
179 TEST(GURLTest, SelfAssign) {
180 GURL a("filesystem:http://example.com/temporary/");
181 // This should not crash.
182 a = a;
185 TEST(GURLTest, CopyFileSystem) {
186 GURL url(WStringToUTF16(L"filesystem:https://user:pass@google.com:99/t/foo;bar?q=a#ref"));
188 GURL url2(url);
189 EXPECT_TRUE(url2.is_valid());
191 EXPECT_EQ("filesystem:https://user:pass@google.com:99/t/foo;bar?q=a#ref", url2.spec());
192 EXPECT_EQ("filesystem", url2.scheme());
193 EXPECT_EQ("", url2.username());
194 EXPECT_EQ("", url2.password());
195 EXPECT_EQ("", url2.host());
196 EXPECT_EQ("", url2.port());
197 EXPECT_EQ(PORT_UNSPECIFIED, url2.IntPort());
198 EXPECT_EQ("/foo;bar", url2.path());
199 EXPECT_EQ("q=a", url2.query());
200 EXPECT_EQ("ref", url2.ref());
202 const GURL* inner = url2.inner_url();
203 ASSERT_TRUE(inner);
204 EXPECT_EQ("https", inner->scheme());
205 EXPECT_EQ("user", inner->username());
206 EXPECT_EQ("pass", inner->password());
207 EXPECT_EQ("google.com", inner->host());
208 EXPECT_EQ("99", inner->port());
209 EXPECT_EQ(99, inner->IntPort());
210 EXPECT_EQ("/t", inner->path());
211 EXPECT_EQ("", inner->query());
212 EXPECT_EQ("", inner->ref());
215 TEST(GURLTest, IsValid) {
216 const char* valid_cases[] = {
217 "http://google.com",
218 "unknown://google.com",
219 "http://user:pass@google.com",
220 "http://google.com:12345",
221 "http://google.com/path",
222 "http://google.com//path",
223 "http://google.com?k=v#fragment",
224 "http://user:pass@google.com:12345/path?k=v#fragment",
225 "http:/path",
226 "http:path",
227 "://google.com",
229 for (size_t i = 0; i < arraysize(valid_cases); i++) {
230 EXPECT_TRUE(GURL(valid_cases[i]).is_valid())
231 << "Case: " << valid_cases[i];
234 const char* invalid_cases[] = {
235 "http://?k=v",
236 "http:://google.com",
237 "http//google.com",
238 "http://google.com:12three45",
239 "path",
241 for (size_t i = 0; i < arraysize(invalid_cases); i++) {
242 EXPECT_FALSE(GURL(invalid_cases[i]).is_valid())
243 << "Case: " << invalid_cases[i];
247 TEST(GURLTest, ExtraSlashesBeforeAuthority) {
248 // According to RFC3986, the hier-part for URI with an authority must use only
249 // two slashes, GURL intentionally just ignores slashes more than 2 and parses
250 // the following part as an authority.
251 GURL url("http:///host");
252 EXPECT_EQ("host", url.host());
253 EXPECT_EQ("/", url.path());
256 // Given an invalid URL, we should still get most of the components.
257 TEST(GURLTest, ComponentGettersWorkEvenForInvalidURL) {
258 GURL url("http:google.com:foo");
259 EXPECT_FALSE(url.is_valid());
260 EXPECT_EQ("http://google.com:foo/", url.possibly_invalid_spec());
262 EXPECT_EQ("http", url.scheme());
263 EXPECT_EQ("", url.username());
264 EXPECT_EQ("", url.password());
265 EXPECT_EQ("google.com", url.host());
266 EXPECT_EQ("foo", url.port());
267 EXPECT_EQ(PORT_INVALID, url.IntPort());
268 EXPECT_EQ("/", url.path());
269 EXPECT_EQ("", url.query());
270 EXPECT_EQ("", url.ref());
273 TEST(GURLTest, Resolve) {
274 // The tricky cases for relative URL resolving are tested in the
275 // canonicalizer unit test. Here, we just test that the GURL integration
276 // works properly.
277 struct ResolveCase {
278 const char* base;
279 const char* relative;
280 bool expected_valid;
281 const char* expected;
282 } resolve_cases[] = {
283 {"http://www.google.com/", "foo.html", true, "http://www.google.com/foo.html"},
284 {"http://www.google.com/foo/", "bar", true, "http://www.google.com/foo/bar"},
285 {"http://www.google.com/foo/", "/bar", true, "http://www.google.com/bar"},
286 {"http://www.google.com/foo", "bar", true, "http://www.google.com/bar"},
287 {"http://www.google.com/", "http://images.google.com/foo.html", true, "http://images.google.com/foo.html"},
288 {"http://www.google.com/blah/bloo?c#d", "../../../hello/./world.html?a#b", true, "http://www.google.com/hello/world.html?a#b"},
289 {"http://www.google.com/foo#bar", "#com", true, "http://www.google.com/foo#com"},
290 {"http://www.google.com/", "Https:images.google.com", true, "https://images.google.com/"},
291 // A non-standard base can be replaced with a standard absolute URL.
292 {"data:blahblah", "http://google.com/", true, "http://google.com/"},
293 {"data:blahblah", "http:google.com", true, "http://google.com/"},
294 // Filesystem URLs have different paths to test.
295 {"filesystem:http://www.google.com/type/", "foo.html", true, "filesystem:http://www.google.com/type/foo.html"},
296 {"filesystem:http://www.google.com/type/", "../foo.html", true, "filesystem:http://www.google.com/type/foo.html"},
299 for (size_t i = 0; i < arraysize(resolve_cases); i++) {
300 // 8-bit code path.
301 GURL input(resolve_cases[i].base);
302 GURL output = input.Resolve(resolve_cases[i].relative);
303 EXPECT_EQ(resolve_cases[i].expected_valid, output.is_valid()) << i;
304 EXPECT_EQ(resolve_cases[i].expected, output.spec()) << i;
305 EXPECT_EQ(output.SchemeIsFileSystem(), output.inner_url() != NULL);
307 // Wide code path.
308 GURL inputw(ConvertUTF8ToUTF16(resolve_cases[i].base));
309 GURL outputw =
310 input.Resolve(ConvertUTF8ToUTF16(resolve_cases[i].relative));
311 EXPECT_EQ(resolve_cases[i].expected_valid, outputw.is_valid()) << i;
312 EXPECT_EQ(resolve_cases[i].expected, outputw.spec()) << i;
313 EXPECT_EQ(outputw.SchemeIsFileSystem(), outputw.inner_url() != NULL);
317 TEST(GURLTest, GetOrigin) {
318 struct TestCase {
319 const char* input;
320 const char* expected;
321 } cases[] = {
322 {"http://www.google.com", "http://www.google.com/"},
323 {"javascript:window.alert(\"hello,world\");", ""},
324 {"http://user:pass@www.google.com:21/blah#baz", "http://www.google.com:21/"},
325 {"http://user@www.google.com", "http://www.google.com/"},
326 {"http://:pass@www.google.com", "http://www.google.com/"},
327 {"http://:@www.google.com", "http://www.google.com/"},
328 {"filesystem:http://www.google.com/temp/foo?q#b", "http://www.google.com/"},
329 {"filesystem:http://user:pass@google.com:21/blah#baz", "http://google.com:21/"},
331 for (size_t i = 0; i < arraysize(cases); i++) {
332 GURL url(cases[i].input);
333 GURL origin = url.GetOrigin();
334 EXPECT_EQ(cases[i].expected, origin.spec());
338 TEST(GURLTest, GetAsReferrer) {
339 struct TestCase {
340 const char* input;
341 const char* expected;
342 } cases[] = {
343 {"http://www.google.com", "http://www.google.com/"},
344 {"http://user:pass@www.google.com:21/blah#baz", "http://www.google.com:21/blah"},
345 {"http://user@www.google.com", "http://www.google.com/"},
346 {"http://:pass@www.google.com", "http://www.google.com/"},
347 {"http://:@www.google.com", "http://www.google.com/"},
348 {"http://www.google.com/temp/foo?q#b", "http://www.google.com/temp/foo?q"},
349 {"not a url", ""},
350 {"unknown-scheme://foo.html", ""},
351 {"file:///tmp/test.html", ""},
352 {"https://www.google.com", "https://www.google.com/"},
354 for (size_t i = 0; i < arraysize(cases); i++) {
355 GURL url(cases[i].input);
356 GURL origin = url.GetAsReferrer();
357 EXPECT_EQ(cases[i].expected, origin.spec());
361 TEST(GURLTest, GetWithEmptyPath) {
362 struct TestCase {
363 const char* input;
364 const char* expected;
365 } cases[] = {
366 {"http://www.google.com", "http://www.google.com/"},
367 {"javascript:window.alert(\"hello, world\");", ""},
368 {"http://www.google.com/foo/bar.html?baz=22", "http://www.google.com/"},
369 {"filesystem:http://www.google.com/temporary/bar.html?baz=22", "filesystem:http://www.google.com/temporary/"},
370 {"filesystem:file:///temporary/bar.html?baz=22", "filesystem:file:///temporary/"},
373 for (size_t i = 0; i < arraysize(cases); i++) {
374 GURL url(cases[i].input);
375 GURL empty_path = url.GetWithEmptyPath();
376 EXPECT_EQ(cases[i].expected, empty_path.spec());
380 TEST(GURLTest, Replacements) {
381 // 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 // canonicalizer gets called based on the scheme of the input.
384 struct ReplaceCase {
385 const char* base;
386 const char* scheme;
387 const char* username;
388 const char* password;
389 const char* host;
390 const char* port;
391 const char* path;
392 const char* query;
393 const char* ref;
394 const char* expected;
395 } 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", "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 #ifdef WIN32
400 {"http://www.google.com/foo/bar.html?foo#bar", "file", "", "", "", "", "c:\\", "", "", "file:///C:/"},
401 #endif
402 {"filesystem:http://www.google.com/foo/bar.html?foo#bar", NULL, NULL, NULL, NULL, NULL, "/", "", "", "filesystem:http://www.google.com/foo/"},
405 for (size_t i = 0; i < arraysize(replace_cases); i++) {
406 const ReplaceCase& cur = replace_cases[i];
407 GURL url(cur.base);
408 GURL::Replacements repl;
409 SetupReplacement(&GURL::Replacements::SetScheme, &repl, cur.scheme);
410 SetupReplacement(&GURL::Replacements::SetUsername, &repl, cur.username);
411 SetupReplacement(&GURL::Replacements::SetPassword, &repl, cur.password);
412 SetupReplacement(&GURL::Replacements::SetHost, &repl, cur.host);
413 SetupReplacement(&GURL::Replacements::SetPort, &repl, cur.port);
414 SetupReplacement(&GURL::Replacements::SetPath, &repl, cur.path);
415 SetupReplacement(&GURL::Replacements::SetQuery, &repl, cur.query);
416 SetupReplacement(&GURL::Replacements::SetRef, &repl, cur.ref);
417 GURL output = url.ReplaceComponents(repl);
419 EXPECT_EQ(replace_cases[i].expected, output.spec());
420 EXPECT_EQ(output.SchemeIsFileSystem(), output.inner_url() != NULL);
424 TEST(GURLTest, ClearFragmentOnDataUrl) {
425 // http://crbug.com/291747 - a data URL may legitimately have trailing
426 // whitespace in the spec after the ref is cleared. Test this does not trigger
427 // the Parsed importing validation DCHECK in GURL.
428 GURL url(" data: one ? two # three ");
430 // By default the trailing whitespace will have been stripped.
431 EXPECT_EQ("data: one ? two # three", url.spec());
432 GURL::Replacements repl;
433 repl.ClearRef();
434 GURL url_no_ref = url.ReplaceComponents(repl);
436 EXPECT_EQ("data: one ? two ", url_no_ref.spec());
438 // Importing a parsed url via this constructor overload will retain trailing
439 // whitespace.
440 GURL import_url(url_no_ref.spec(),
441 url_no_ref.parsed_for_possibly_invalid_spec(),
442 url_no_ref.is_valid());
443 EXPECT_EQ(url_no_ref, import_url);
444 EXPECT_EQ(import_url.query(), " two ");
447 TEST(GURLTest, PathForRequest) {
448 struct TestCase {
449 const char* input;
450 const char* expected;
451 const char* inner_expected;
452 } cases[] = {
453 {"http://www.google.com", "/", NULL},
454 {"http://www.google.com/", "/", NULL},
455 {"http://www.google.com/foo/bar.html?baz=22", "/foo/bar.html?baz=22", NULL},
456 {"http://www.google.com/foo/bar.html#ref", "/foo/bar.html", NULL},
457 {"http://www.google.com/foo/bar.html?query#ref", "/foo/bar.html?query", NULL},
458 {"filesystem:http://www.google.com/temporary/foo/bar.html?query#ref", "/foo/bar.html?query", "/temporary"},
459 {"filesystem:http://www.google.com/temporary/foo/bar.html?query", "/foo/bar.html?query", "/temporary"},
462 for (size_t i = 0; i < arraysize(cases); i++) {
463 GURL url(cases[i].input);
464 std::string path_request = url.PathForRequest();
465 EXPECT_EQ(cases[i].expected, path_request);
466 EXPECT_EQ(cases[i].inner_expected == NULL, url.inner_url() == NULL);
467 if (url.inner_url() && cases[i].inner_expected)
468 EXPECT_EQ(cases[i].inner_expected, url.inner_url()->PathForRequest());
472 TEST(GURLTest, EffectiveIntPort) {
473 struct PortTest {
474 const char* spec;
475 int expected_int_port;
476 } port_tests[] = {
477 // http
478 {"http://www.google.com/", 80},
479 {"http://www.google.com:80/", 80},
480 {"http://www.google.com:443/", 443},
482 // https
483 {"https://www.google.com/", 443},
484 {"https://www.google.com:443/", 443},
485 {"https://www.google.com:80/", 80},
487 // ftp
488 {"ftp://www.google.com/", 21},
489 {"ftp://www.google.com:21/", 21},
490 {"ftp://www.google.com:80/", 80},
492 // gopher
493 {"gopher://www.google.com/", 70},
494 {"gopher://www.google.com:70/", 70},
495 {"gopher://www.google.com:80/", 80},
497 // file - no port
498 {"file://www.google.com/", PORT_UNSPECIFIED},
499 {"file://www.google.com:443/", PORT_UNSPECIFIED},
501 // data - no port
502 {"data:www.google.com:90", PORT_UNSPECIFIED},
503 {"data:www.google.com", PORT_UNSPECIFIED},
505 // filesystem - no port
506 {"filesystem:http://www.google.com:90/t/foo", PORT_UNSPECIFIED},
507 {"filesystem:file:///t/foo", PORT_UNSPECIFIED},
510 for (size_t i = 0; i < arraysize(port_tests); i++) {
511 GURL url(port_tests[i].spec);
512 EXPECT_EQ(port_tests[i].expected_int_port, url.EffectiveIntPort());
516 TEST(GURLTest, IPAddress) {
517 struct IPTest {
518 const char* spec;
519 bool expected_ip;
520 } ip_tests[] = {
521 {"http://www.google.com/", false},
522 {"http://192.168.9.1/", true},
523 {"http://192.168.9.1.2/", false},
524 {"http://192.168.m.1/", false},
525 {"http://2001:db8::1/", false},
526 {"http://[2001:db8::1]/", true},
527 {"", false},
528 {"some random input!", false},
531 for (size_t i = 0; i < arraysize(ip_tests); i++) {
532 GURL url(ip_tests[i].spec);
533 EXPECT_EQ(ip_tests[i].expected_ip, url.HostIsIPAddress());
537 TEST(GURLTest, HostNoBrackets) {
538 struct TestCase {
539 const char* input;
540 const char* expected_host;
541 const char* expected_plainhost;
542 } cases[] = {
543 {"http://www.google.com", "www.google.com", "www.google.com"},
544 {"http://[2001:db8::1]/", "[2001:db8::1]", "2001:db8::1"},
545 {"http://[::]/", "[::]", "::"},
547 // Don't require a valid URL, but don't crash either.
548 {"http://[]/", "[]", ""},
549 {"http://[x]/", "[x]", "x"},
550 {"http://[x/", "[x", "[x"},
551 {"http://x]/", "x]", "x]"},
552 {"http://[/", "[", "["},
553 {"http://]/", "]", "]"},
554 {"", "", ""},
556 for (size_t i = 0; i < arraysize(cases); i++) {
557 GURL url(cases[i].input);
558 EXPECT_EQ(cases[i].expected_host, url.host());
559 EXPECT_EQ(cases[i].expected_plainhost, url.HostNoBrackets());
563 TEST(GURLTest, DomainIs) {
564 GURL url_1("http://google.com/foo");
565 EXPECT_TRUE(url_1.DomainIs("google.com"));
567 // Subdomain and port are ignored.
568 GURL url_2("http://www.google.com:99/foo");
569 EXPECT_TRUE(url_2.DomainIs("google.com"));
571 // Different top-level domain.
572 GURL url_3("http://www.google.com.cn/foo");
573 EXPECT_FALSE(url_3.DomainIs("google.com"));
575 // Different host name.
576 GURL url_4("http://www.iamnotgoogle.com/foo");
577 EXPECT_FALSE(url_4.DomainIs("google.com"));
579 // The input must be lower-cased otherwise DomainIs returns false.
580 GURL url_5("http://www.google.com/foo");
581 EXPECT_FALSE(url_5.DomainIs("Google.com"));
583 // If the URL is invalid, DomainIs returns false.
584 GURL invalid_url("google.com");
585 EXPECT_FALSE(invalid_url.is_valid());
586 EXPECT_FALSE(invalid_url.DomainIs("google.com"));
589 TEST(GURLTest, DomainIsTerminatingDotBehavior) {
590 // If the host part ends with a dot, it matches input domains
591 // with or without a dot.
592 GURL url_with_dot("http://www.google.com./foo");
593 EXPECT_TRUE(url_with_dot.DomainIs("google.com"));
594 EXPECT_TRUE(url_with_dot.DomainIs("google.com."));
595 EXPECT_TRUE(url_with_dot.DomainIs(".com"));
596 EXPECT_TRUE(url_with_dot.DomainIs(".com."));
598 // But, if the host name doesn't end with a dot and the input
599 // domain does, then it's considered to not match.
600 GURL url_without_dot("http://google.com/foo");
601 EXPECT_FALSE(url_without_dot.DomainIs("google.com."));
603 // If the URL ends with two dots, it doesn't match.
604 GURL url_with_two_dots("http://www.google.com../foo");
605 EXPECT_FALSE(url_with_two_dots.DomainIs("google.com"));
608 TEST(GURLTest, DomainIsWithFilesystemScheme) {
609 GURL url_1("filesystem:http://www.google.com:99/foo/");
610 EXPECT_TRUE(url_1.DomainIs("google.com"));
612 GURL url_2("filesystem:http://www.iamnotgoogle.com/foo/");
613 EXPECT_FALSE(url_2.DomainIs("google.com"));
616 // Newlines should be stripped from inputs.
617 TEST(GURLTest, Newlines) {
618 // Constructor.
619 GURL url_1(" \t ht\ntp://\twww.goo\rgle.com/as\ndf \n ");
620 EXPECT_EQ("http://www.google.com/asdf", url_1.spec());
622 // Relative path resolver.
623 GURL url_2 = url_1.Resolve(" \n /fo\to\r ");
624 EXPECT_EQ("http://www.google.com/foo", url_2.spec());
626 // Note that newlines are NOT stripped from ReplaceComponents.
629 TEST(GURLTest, IsStandard) {
630 GURL a("http:foo/bar");
631 EXPECT_TRUE(a.IsStandard());
633 GURL b("foo:bar/baz");
634 EXPECT_FALSE(b.IsStandard());
636 GURL c("foo://bar/baz");
637 EXPECT_FALSE(c.IsStandard());
640 TEST(GURLTest, SchemeIsHTTPOrHTTPS) {
641 EXPECT_TRUE(GURL("http://bar/").SchemeIsHTTPOrHTTPS());
642 EXPECT_TRUE(GURL("HTTPS://BAR").SchemeIsHTTPOrHTTPS());
643 EXPECT_FALSE(GURL("ftp://bar/").SchemeIsHTTPOrHTTPS());
646 TEST(GURLTest, SchemeIsWSOrWSS) {
647 EXPECT_TRUE(GURL("WS://BAR/").SchemeIsWSOrWSS());
648 EXPECT_TRUE(GURL("wss://bar/").SchemeIsWSOrWSS());
649 EXPECT_FALSE(GURL("http://bar/").SchemeIsWSOrWSS());
652 TEST(GURLTest, SchemeIsBlob) {
653 EXPECT_TRUE(GURL("BLOB://BAR/").SchemeIsBlob());
654 EXPECT_TRUE(GURL("blob://bar/").SchemeIsBlob());
655 EXPECT_FALSE(GURL("http://bar/").SchemeIsBlob());
658 TEST(GURLTest, ContentAndPathForNonStandardURLs) {
659 struct TestCase {
660 const char* url;
661 const char* expected;
662 } cases[] = {
663 {"null", ""},
664 {"not-a-standard-scheme:this is arbitrary content",
665 "this is arbitrary content"},
666 {"view-source:http://example.com/path", "http://example.com/path"},
667 {"blob:http://example.com/GUID", "http://example.com/GUID"},
668 {"blob://http://example.com/GUID", "//http://example.com/GUID"},
669 {"blob:http://user:password@example.com/GUID",
670 "http://user:password@example.com/GUID"},
672 // TODO(mkwst): This seems like a bug. https://crbug.com/513600
673 {"filesystem:http://example.com/path", "/"},
676 for (const auto& test : cases) {
677 GURL url(test.url);
678 EXPECT_EQ(test.expected, url.path()) << test.url;
679 EXPECT_EQ(test.expected, url.GetContent()) << test.url;
683 } // namespace url