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