blob: bea1a0cd58a5af8b5c198af7a97b93ab2a4da25e [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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
James Robinson80d418c2014-10-16 16:00:02 -07005#include "base/macros.h"
James Robinson646469d2014-10-03 15:33:28 -07006#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"
10
James Robinson646469d2014-10-03 15:33:28 -070011namespace url {
12
13using test_utils::WStringToUTF16;
14using test_utils::ConvertUTF8ToUTF16;
15
16namespace {
17
18template<typename CHAR>
19void 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);
28 }
29}
30
31// Returns the canonicalized string for the given URL string for the
32// GURLTest.Types test.
33std::string TypesTestCase(const char* src) {
34 GURL gurl(src);
35 return gurl.possibly_invalid_spec();
36}
37
38} // namespace
39
40// Different types of URLs should be handled differently, and handed off to
41// different canonicalizers.
42TEST(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/"));
47
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"));
53
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
61}
62
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.
66TEST(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());
71
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());
74
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());
84
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());
92}
93
94TEST(GURLTest, Empty) {
95 GURL url;
96 EXPECT_FALSE(url.is_valid());
97 EXPECT_EQ("", url.spec());
98
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());
108}
109
110TEST(GURLTest, Copy) {
111 GURL url(WStringToUTF16(L"http://user:pass@google.com:99/foo;bar?q=a#ref"));
112
113 GURL url2(url);
114 EXPECT_TRUE(url2.is_valid());
115
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());
126
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());
141}
142
143TEST(GURLTest, Assign) {
144 GURL url(WStringToUTF16(L"http://user:pass@google.com:99/foo;bar?q=a#ref"));
145
146 GURL url2;
147 url2 = url;
148 EXPECT_TRUE(url2.is_valid());
149
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());
160
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());
176}
177
178// This is a regression test for http://crbug.com/309975 .
179TEST(GURLTest, SelfAssign) {
180 GURL a("filesystem:http://example.com/temporary/");
181 // This should not crash.
182 a = a;
183}
184
185TEST(GURLTest, CopyFileSystem) {
186 GURL url(WStringToUTF16(L"filesystem:https://user:pass@google.com:99/t/foo;bar?q=a#ref"));
187
188 GURL url2(url);
189 EXPECT_TRUE(url2.is_valid());
190
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());
201
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());
213}
214
215TEST(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",
228 };
James Robinson80d418c2014-10-16 16:00:02 -0700229 for (size_t i = 0; i < arraysize(valid_cases); i++) {
James Robinson646469d2014-10-03 15:33:28 -0700230 EXPECT_TRUE(GURL(valid_cases[i]).is_valid())
231 << "Case: " << valid_cases[i];
232 }
233
234 const char* invalid_cases[] = {
235 "http://?k=v",
236 "http:://google.com",
237 "http//google.com",
238 "http://google.com:12three45",
239 "path",
240 };
James Robinson80d418c2014-10-16 16:00:02 -0700241 for (size_t i = 0; i < arraysize(invalid_cases); i++) {
James Robinson646469d2014-10-03 15:33:28 -0700242 EXPECT_FALSE(GURL(invalid_cases[i]).is_valid())
243 << "Case: " << invalid_cases[i];
244 }
245}
246
247TEST(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());
254}
255
256// Given an invalid URL, we should still get most of the components.
257TEST(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());
261
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());
271}
272
273TEST(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"},
Elliot Glayshereae49292015-01-28 10:47:32 -0800284 {"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"},
James Robinson646469d2014-10-03 15:33:28 -0700287 {"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"},
297 };
298
James Robinson80d418c2014-10-16 16:00:02 -0700299 for (size_t i = 0; i < arraysize(resolve_cases); i++) {
James Robinson646469d2014-10-03 15:33:28 -0700300 // 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);
306
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);
314 }
315}
316
317TEST(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/"},
330 };
James Robinson80d418c2014-10-16 16:00:02 -0700331 for (size_t i = 0; i < arraysize(cases); i++) {
James Robinson646469d2014-10-03 15:33:28 -0700332 GURL url(cases[i].input);
333 GURL origin = url.GetOrigin();
334 EXPECT_EQ(cases[i].expected, origin.spec());
335 }
336}
337
338TEST(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"},
James Robinson6a64b812014-12-03 13:38:42 -0800349 {"not a url", ""},
350 {"unknown-scheme://foo.html", ""},
351 {"file:///tmp/test.html", ""},
352 {"https://www.google.com", "https://www.google.com/"},
James Robinson646469d2014-10-03 15:33:28 -0700353 };
James Robinson80d418c2014-10-16 16:00:02 -0700354 for (size_t i = 0; i < arraysize(cases); i++) {
James Robinson646469d2014-10-03 15:33:28 -0700355 GURL url(cases[i].input);
356 GURL origin = url.GetAsReferrer();
357 EXPECT_EQ(cases[i].expected, origin.spec());
358 }
359}
360
361TEST(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/"},
371 };
372
James Robinson80d418c2014-10-16 16:00:02 -0700373 for (size_t i = 0; i < arraysize(cases); i++) {
James Robinson646469d2014-10-03 15:33:28 -0700374 GURL url(cases[i].input);
375 GURL empty_path = url.GetWithEmptyPath();
376 EXPECT_EQ(cases[i].expected, empty_path.spec());
377 }
378}
379
380TEST(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/"},
403 };
404
James Robinson80d418c2014-10-16 16:00:02 -0700405 for (size_t i = 0; i < arraysize(replace_cases); i++) {
James Robinson646469d2014-10-03 15:33:28 -0700406 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);
418
419 EXPECT_EQ(replace_cases[i].expected, output.spec());
420 EXPECT_EQ(output.SchemeIsFileSystem(), output.inner_url() != NULL);
421 }
422}
423
424TEST(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 ");
429
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);
435
436 EXPECT_EQ("data: one ? two ", url_no_ref.spec());
437
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 ");
445}
446
447TEST(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"},
460 };
461
James Robinson80d418c2014-10-16 16:00:02 -0700462 for (size_t i = 0; i < arraysize(cases); i++) {
James Robinson646469d2014-10-03 15:33:28 -0700463 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());
469 }
470}
471
472TEST(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},
481
482 // https
483 {"https://www.google.com/", 443},
484 {"https://www.google.com:443/", 443},
485 {"https://www.google.com:80/", 80},
486
487 // ftp
488 {"ftp://www.google.com/", 21},
489 {"ftp://www.google.com:21/", 21},
490 {"ftp://www.google.com:80/", 80},
491
492 // gopher
493 {"gopher://www.google.com/", 70},
494 {"gopher://www.google.com:70/", 70},
495 {"gopher://www.google.com:80/", 80},
496
497 // file - no port
498 {"file://www.google.com/", PORT_UNSPECIFIED},
499 {"file://www.google.com:443/", PORT_UNSPECIFIED},
500
501 // data - no port
502 {"data:www.google.com:90", PORT_UNSPECIFIED},
503 {"data:www.google.com", PORT_UNSPECIFIED},
504
505 // filesystem - no port
506 {"filesystem:http://www.google.com:90/t/foo", PORT_UNSPECIFIED},
507 {"filesystem:file:///t/foo", PORT_UNSPECIFIED},
508 };
509
James Robinson80d418c2014-10-16 16:00:02 -0700510 for (size_t i = 0; i < arraysize(port_tests); i++) {
James Robinson646469d2014-10-03 15:33:28 -0700511 GURL url(port_tests[i].spec);
512 EXPECT_EQ(port_tests[i].expected_int_port, url.EffectiveIntPort());
513 }
514}
515
516TEST(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},
529 };
530
James Robinson80d418c2014-10-16 16:00:02 -0700531 for (size_t i = 0; i < arraysize(ip_tests); i++) {
James Robinson646469d2014-10-03 15:33:28 -0700532 GURL url(ip_tests[i].spec);
533 EXPECT_EQ(ip_tests[i].expected_ip, url.HostIsIPAddress());
534 }
535}
536
537TEST(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://[::]/", "[::]", "::"},
546
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 {"", "", ""},
555 };
James Robinson80d418c2014-10-16 16:00:02 -0700556 for (size_t i = 0; i < arraysize(cases); i++) {
James Robinson646469d2014-10-03 15:33:28 -0700557 GURL url(cases[i].input);
558 EXPECT_EQ(cases[i].expected_host, url.host());
559 EXPECT_EQ(cases[i].expected_plainhost, url.HostNoBrackets());
560 }
561}
562
563TEST(GURLTest, DomainIs) {
564 const char google_domain[] = "google.com";
565
566 GURL url_1("http://www.google.com:99/foo");
567 EXPECT_TRUE(url_1.DomainIs(google_domain));
568
569 GURL url_2("http://google.com:99/foo");
570 EXPECT_TRUE(url_2.DomainIs(google_domain));
571
572 GURL url_3("http://google.com./foo");
573 EXPECT_TRUE(url_3.DomainIs(google_domain));
574
575 GURL url_4("http://google.com/foo");
576 EXPECT_FALSE(url_4.DomainIs("google.com."));
577
578 GURL url_5("http://google.com./foo");
579 EXPECT_TRUE(url_5.DomainIs("google.com."));
580
581 GURL url_6("http://www.google.com./foo");
582 EXPECT_TRUE(url_6.DomainIs(".com."));
583
584 GURL url_7("http://www.balabala.com/foo");
585 EXPECT_FALSE(url_7.DomainIs(google_domain));
586
587 GURL url_8("http://www.google.com.cn/foo");
588 EXPECT_FALSE(url_8.DomainIs(google_domain));
589
590 GURL url_9("http://www.iamnotgoogle.com/foo");
591 EXPECT_FALSE(url_9.DomainIs(google_domain));
592
593 GURL url_10("http://www.iamnotgoogle.com../foo");
594 EXPECT_FALSE(url_10.DomainIs(".com"));
595
596 GURL url_11("filesystem:http://www.google.com:99/foo/");
597 EXPECT_TRUE(url_11.DomainIs(google_domain));
598
599 GURL url_12("filesystem:http://www.iamnotgoogle.com/foo/");
600 EXPECT_FALSE(url_12.DomainIs(google_domain));
601}
602
603// Newlines should be stripped from inputs.
604TEST(GURLTest, Newlines) {
605 // Constructor.
606 GURL url_1(" \t ht\ntp://\twww.goo\rgle.com/as\ndf \n ");
607 EXPECT_EQ("http://www.google.com/asdf", url_1.spec());
608
609 // Relative path resolver.
610 GURL url_2 = url_1.Resolve(" \n /fo\to\r ");
611 EXPECT_EQ("http://www.google.com/foo", url_2.spec());
612
613 // Note that newlines are NOT stripped from ReplaceComponents.
614}
615
616TEST(GURLTest, IsStandard) {
617 GURL a("http:foo/bar");
618 EXPECT_TRUE(a.IsStandard());
619
620 GURL b("foo:bar/baz");
621 EXPECT_FALSE(b.IsStandard());
622
623 GURL c("foo://bar/baz");
624 EXPECT_FALSE(c.IsStandard());
625}
626
627TEST(GURLTest, SchemeIsHTTPOrHTTPS) {
628 EXPECT_TRUE(GURL("http://bar/").SchemeIsHTTPOrHTTPS());
629 EXPECT_TRUE(GURL("HTTPS://BAR").SchemeIsHTTPOrHTTPS());
630 EXPECT_FALSE(GURL("ftp://bar/").SchemeIsHTTPOrHTTPS());
631}
632
633TEST(GURLTest, SchemeIsWSOrWSS) {
634 EXPECT_TRUE(GURL("WS://BAR/").SchemeIsWSOrWSS());
635 EXPECT_TRUE(GURL("wss://bar/").SchemeIsWSOrWSS());
636 EXPECT_FALSE(GURL("http://bar/").SchemeIsWSOrWSS());
637}
638
639TEST(GURLTest, SchemeIsBlob) {
640 EXPECT_TRUE(GURL("BLOB://BAR/").SchemeIsBlob());
641 EXPECT_TRUE(GURL("blob://bar/").SchemeIsBlob());
642 EXPECT_FALSE(GURL("http://bar/").SchemeIsBlob());
643}
644
645} // namespace url