blob: c77cf71a988e55aa5de5aa55fb319f20f6f20732 [file] [log] [blame]
[email protected]51bcc5d2013-04-24 01:41:371// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]07abf6ab2013-04-10 21:41:042// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]e7bba5f82013-04-10 20:10:524
avic0c60312015-12-21 21:03:505#include <stddef.h>
6
viettrungluu4b6915862014-10-16 03:42:497#include "base/macros.h"
brettw1b8582f2016-11-03 20:37:178#include "base/strings/utf_string_conversions.h"
[email protected]e7bba5f82013-04-10 20:10:529#include "testing/gtest/include/gtest/gtest.h"
[email protected]318076b2013-04-18 21:19:4510#include "url/gurl.h"
11#include "url/url_canon.h"
12#include "url/url_test_utils.h"
[email protected]e7bba5f82013-04-10 20:10:5213
[email protected]0318f922014-04-22 00:09:2314namespace url {
15
[email protected]e7bba5f82013-04-10 20:10:5216namespace {
17
18template<typename CHAR>
[email protected]0318f922014-04-22 00:09:2319void SetupReplacement(
20 void (Replacements<CHAR>::*func)(const CHAR*, const Component&),
21 Replacements<CHAR>* replacements,
22 const CHAR* str) {
[email protected]e7bba5f82013-04-10 20:10:5223 if (str) {
[email protected]0318f922014-04-22 00:09:2324 Component comp;
[email protected]e7bba5f82013-04-10 20:10:5225 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
[email protected]47e365de2014-05-02 00:02:2540// Different types of URLs should be handled differently, and handed off to
41// different canonicalizers.
[email protected]e7bba5f82013-04-10 20:10:5242TEST(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
qyearsley2bc727d2015-08-14 20:17:1548 // Conversely, URLs with known schemes should always trigger standard URL
49 // handling.
[email protected]e7bba5f82013-04-10 20:10:5250 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"));
54
55#ifdef WIN32
qyearsley2bc727d2015-08-14 20:17:1556 // URLs that look like Windows absolute path specs.
[email protected]e7bba5f82013-04-10 20:10:5257 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
62}
63
qyearsley2bc727d2015-08-14 20:17:1564// Test the basic creation and querying of components in a GURL. We assume that
[email protected]e7bba5f82013-04-10 20:10:5265// the parser is already tested and works, so we are mostly interested if the
66// object does the right thing with the results.
67TEST(GURLTest, Components) {
brettw1b8582f2016-11-03 20:37:1768 GURL empty_url(base::UTF8ToUTF16(""));
mblsha93895b12016-01-18 14:07:5269 EXPECT_TRUE(empty_url.is_empty());
70 EXPECT_FALSE(empty_url.is_valid());
71
brettw1b8582f2016-11-03 20:37:1772 GURL url(base::UTF8ToUTF16("http://user:[email protected]:99/foo;bar?q=a#ref"));
mblsha93895b12016-01-18 14:07:5273 EXPECT_FALSE(url.is_empty());
[email protected]e7bba5f82013-04-10 20:10:5274 EXPECT_TRUE(url.is_valid());
75 EXPECT_TRUE(url.SchemeIs("http"));
76 EXPECT_FALSE(url.SchemeIsFile());
77
78 // This is the narrow version of the URL, which should match the wide input.
79 EXPECT_EQ("http://user:[email protected]:99/foo;bar?q=a#ref", url.spec());
80
81 EXPECT_EQ("http", url.scheme());
82 EXPECT_EQ("user", url.username());
83 EXPECT_EQ("pass", url.password());
84 EXPECT_EQ("google.com", url.host());
85 EXPECT_EQ("99", url.port());
86 EXPECT_EQ(99, url.IntPort());
87 EXPECT_EQ("/foo;bar", url.path());
88 EXPECT_EQ("q=a", url.query());
89 EXPECT_EQ("ref", url.ref());
[email protected]e9185c82014-04-18 10:06:5090
91 // Test parsing userinfo with special characters.
92 GURL url_special_pass("http://user:%40!$&'()*+,;=:@google.com:12345");
93 EXPECT_TRUE(url_special_pass.is_valid());
94 // GURL canonicalizes some delimiters.
95 EXPECT_EQ("%40!$&%27()*+,%3B%3D%3A", url_special_pass.password());
96 EXPECT_EQ("google.com", url_special_pass.host());
97 EXPECT_EQ("12345", url_special_pass.port());
[email protected]e7bba5f82013-04-10 20:10:5298}
99
100TEST(GURLTest, Empty) {
101 GURL url;
102 EXPECT_FALSE(url.is_valid());
103 EXPECT_EQ("", url.spec());
104
105 EXPECT_EQ("", url.scheme());
106 EXPECT_EQ("", url.username());
107 EXPECT_EQ("", url.password());
108 EXPECT_EQ("", url.host());
109 EXPECT_EQ("", url.port());
[email protected]0318f922014-04-22 00:09:23110 EXPECT_EQ(PORT_UNSPECIFIED, url.IntPort());
[email protected]e7bba5f82013-04-10 20:10:52111 EXPECT_EQ("", url.path());
112 EXPECT_EQ("", url.query());
113 EXPECT_EQ("", url.ref());
114}
115
116TEST(GURLTest, Copy) {
brettw1b8582f2016-11-03 20:37:17117 GURL url(base::UTF8ToUTF16(
118 "http://user:[email protected]:99/foo;bar?q=a#ref"));
[email protected]e7bba5f82013-04-10 20:10:52119
120 GURL url2(url);
121 EXPECT_TRUE(url2.is_valid());
122
123 EXPECT_EQ("http://user:[email protected]:99/foo;bar?q=a#ref", url2.spec());
124 EXPECT_EQ("http", url2.scheme());
125 EXPECT_EQ("user", url2.username());
126 EXPECT_EQ("pass", url2.password());
127 EXPECT_EQ("google.com", url2.host());
128 EXPECT_EQ("99", url2.port());
129 EXPECT_EQ(99, url2.IntPort());
130 EXPECT_EQ("/foo;bar", url2.path());
131 EXPECT_EQ("q=a", url2.query());
132 EXPECT_EQ("ref", url2.ref());
133
134 // Copying of invalid URL should be invalid
135 GURL invalid;
136 GURL invalid2(invalid);
137 EXPECT_FALSE(invalid2.is_valid());
138 EXPECT_EQ("", invalid2.spec());
139 EXPECT_EQ("", invalid2.scheme());
140 EXPECT_EQ("", invalid2.username());
141 EXPECT_EQ("", invalid2.password());
142 EXPECT_EQ("", invalid2.host());
143 EXPECT_EQ("", invalid2.port());
[email protected]0318f922014-04-22 00:09:23144 EXPECT_EQ(PORT_UNSPECIFIED, invalid2.IntPort());
[email protected]e7bba5f82013-04-10 20:10:52145 EXPECT_EQ("", invalid2.path());
146 EXPECT_EQ("", invalid2.query());
147 EXPECT_EQ("", invalid2.ref());
148}
149
[email protected]8093a31b2013-10-24 21:56:33150TEST(GURLTest, Assign) {
brettw1b8582f2016-11-03 20:37:17151 GURL url(base::UTF8ToUTF16(
152 "http://user:[email protected]:99/foo;bar?q=a#ref"));
[email protected]8093a31b2013-10-24 21:56:33153
154 GURL url2;
155 url2 = url;
156 EXPECT_TRUE(url2.is_valid());
157
158 EXPECT_EQ("http://user:[email protected]:99/foo;bar?q=a#ref", url2.spec());
159 EXPECT_EQ("http", url2.scheme());
160 EXPECT_EQ("user", url2.username());
161 EXPECT_EQ("pass", url2.password());
162 EXPECT_EQ("google.com", url2.host());
163 EXPECT_EQ("99", url2.port());
164 EXPECT_EQ(99, url2.IntPort());
165 EXPECT_EQ("/foo;bar", url2.path());
166 EXPECT_EQ("q=a", url2.query());
167 EXPECT_EQ("ref", url2.ref());
168
169 // Assignment of invalid URL should be invalid
170 GURL invalid;
171 GURL invalid2;
172 invalid2 = invalid;
173 EXPECT_FALSE(invalid2.is_valid());
174 EXPECT_EQ("", invalid2.spec());
175 EXPECT_EQ("", invalid2.scheme());
176 EXPECT_EQ("", invalid2.username());
177 EXPECT_EQ("", invalid2.password());
178 EXPECT_EQ("", invalid2.host());
179 EXPECT_EQ("", invalid2.port());
[email protected]0318f922014-04-22 00:09:23180 EXPECT_EQ(PORT_UNSPECIFIED, invalid2.IntPort());
[email protected]8093a31b2013-10-24 21:56:33181 EXPECT_EQ("", invalid2.path());
182 EXPECT_EQ("", invalid2.query());
183 EXPECT_EQ("", invalid2.ref());
184}
185
qyearsley2bc727d2015-08-14 20:17:15186// This is a regression test for http://crbug.com/309975.
[email protected]8093a31b2013-10-24 21:56:33187TEST(GURLTest, SelfAssign) {
188 GURL a("filesystem:http://example.com/temporary/");
189 // This should not crash.
Hans Wennborg792903f2018-04-09 11:20:06190 a = *&a; // The *& defeats Clang's -Wself-assign warning.
[email protected]8093a31b2013-10-24 21:56:33191}
192
[email protected]e7bba5f82013-04-10 20:10:52193TEST(GURLTest, CopyFileSystem) {
brettw1b8582f2016-11-03 20:37:17194 GURL url(base::UTF8ToUTF16(
195 "filesystem:https://user:[email protected]:99/t/foo;bar?q=a#ref"));
[email protected]e7bba5f82013-04-10 20:10:52196
197 GURL url2(url);
198 EXPECT_TRUE(url2.is_valid());
199
Nick Carterff69a102018-04-04 00:15:17200 EXPECT_EQ("filesystem:https://google.com:99/t/foo;bar?q=a#ref", url2.spec());
[email protected]e7bba5f82013-04-10 20:10:52201 EXPECT_EQ("filesystem", url2.scheme());
202 EXPECT_EQ("", url2.username());
203 EXPECT_EQ("", url2.password());
204 EXPECT_EQ("", url2.host());
205 EXPECT_EQ("", url2.port());
[email protected]0318f922014-04-22 00:09:23206 EXPECT_EQ(PORT_UNSPECIFIED, url2.IntPort());
[email protected]e7bba5f82013-04-10 20:10:52207 EXPECT_EQ("/foo;bar", url2.path());
208 EXPECT_EQ("q=a", url2.query());
209 EXPECT_EQ("ref", url2.ref());
210
211 const GURL* inner = url2.inner_url();
212 ASSERT_TRUE(inner);
213 EXPECT_EQ("https", inner->scheme());
Nick Carterff69a102018-04-04 00:15:17214 EXPECT_EQ("", inner->username());
215 EXPECT_EQ("", inner->password());
[email protected]e7bba5f82013-04-10 20:10:52216 EXPECT_EQ("google.com", inner->host());
217 EXPECT_EQ("99", inner->port());
218 EXPECT_EQ(99, inner->IntPort());
219 EXPECT_EQ("/t", inner->path());
220 EXPECT_EQ("", inner->query());
221 EXPECT_EQ("", inner->ref());
222}
223
[email protected]e9185c82014-04-18 10:06:50224TEST(GURLTest, IsValid) {
225 const char* valid_cases[] = {
226 "http://google.com",
227 "unknown://google.com",
228 "http://user:[email protected]",
229 "http://google.com:12345",
230 "http://google.com/path",
231 "http://google.com//path",
232 "http://google.com?k=v#fragment",
233 "http://user:[email protected]:12345/path?k=v#fragment",
234 "http:/path",
235 "http:path",
[email protected]e9185c82014-04-18 10:06:50236 };
viettrungluu4b6915862014-10-16 03:42:49237 for (size_t i = 0; i < arraysize(valid_cases); i++) {
[email protected]e9185c82014-04-18 10:06:50238 EXPECT_TRUE(GURL(valid_cases[i]).is_valid())
239 << "Case: " << valid_cases[i];
240 }
241
242 const char* invalid_cases[] = {
243 "http://?k=v",
244 "http:://google.com",
245 "http//google.com",
246 "http://google.com:12three45",
brettw46f9b832016-10-05 19:22:48247 "://google.com",
[email protected]e9185c82014-04-18 10:06:50248 "path",
249 };
viettrungluu4b6915862014-10-16 03:42:49250 for (size_t i = 0; i < arraysize(invalid_cases); i++) {
[email protected]e9185c82014-04-18 10:06:50251 EXPECT_FALSE(GURL(invalid_cases[i]).is_valid())
252 << "Case: " << invalid_cases[i];
253 }
254}
255
256TEST(GURLTest, ExtraSlashesBeforeAuthority) {
qyearsley2bc727d2015-08-14 20:17:15257 // According to RFC3986, the hierarchical part for URI with an authority
258 // must use only two slashes; GURL intentionally just ignores extra slashes
259 // if there are more than 2, and parses the following part as an authority.
[email protected]e9185c82014-04-18 10:06:50260 GURL url("http:///host");
261 EXPECT_EQ("host", url.host());
262 EXPECT_EQ("/", url.path());
263}
264
[email protected]e7bba5f82013-04-10 20:10:52265// Given an invalid URL, we should still get most of the components.
[email protected]e9185c82014-04-18 10:06:50266TEST(GURLTest, ComponentGettersWorkEvenForInvalidURL) {
[email protected]e7bba5f82013-04-10 20:10:52267 GURL url("http:google.com:foo");
268 EXPECT_FALSE(url.is_valid());
269 EXPECT_EQ("http://google.com:foo/", url.possibly_invalid_spec());
270
271 EXPECT_EQ("http", url.scheme());
272 EXPECT_EQ("", url.username());
273 EXPECT_EQ("", url.password());
274 EXPECT_EQ("google.com", url.host());
275 EXPECT_EQ("foo", url.port());
[email protected]0318f922014-04-22 00:09:23276 EXPECT_EQ(PORT_INVALID, url.IntPort());
[email protected]e7bba5f82013-04-10 20:10:52277 EXPECT_EQ("/", url.path());
278 EXPECT_EQ("", url.query());
279 EXPECT_EQ("", url.ref());
280}
281
282TEST(GURLTest, Resolve) {
283 // The tricky cases for relative URL resolving are tested in the
284 // canonicalizer unit test. Here, we just test that the GURL integration
285 // works properly.
286 struct ResolveCase {
287 const char* base;
288 const char* relative;
289 bool expected_valid;
290 const char* expected;
291 } resolve_cases[] = {
292 {"http://www.google.com/", "foo.html", true, "http://www.google.com/foo.html"},
maxbogueac0afb82015-01-27 03:10:49293 {"http://www.google.com/foo/", "bar", true, "http://www.google.com/foo/bar"},
294 {"http://www.google.com/foo/", "/bar", true, "http://www.google.com/bar"},
295 {"http://www.google.com/foo", "bar", true, "http://www.google.com/bar"},
[email protected]e7bba5f82013-04-10 20:10:52296 {"http://www.google.com/", "http://images.google.com/foo.html", true, "http://images.google.com/foo.html"},
csharrisonc64537202016-12-01 14:15:14297 {"http://www.google.com/", "http://images.\tgoogle.\ncom/\rfoo.html", true, "http://images.google.com/foo.html"},
[email protected]e7bba5f82013-04-10 20:10:52298 {"http://www.google.com/blah/bloo?c#d", "../../../hello/./world.html?a#b", true, "http://www.google.com/hello/world.html?a#b"},
299 {"http://www.google.com/foo#bar", "#com", true, "http://www.google.com/foo#com"},
300 {"http://www.google.com/", "Https:images.google.com", true, "https://images.google.com/"},
301 // A non-standard base can be replaced with a standard absolute URL.
302 {"data:blahblah", "http://google.com/", true, "http://google.com/"},
303 {"data:blahblah", "http:google.com", true, "http://google.com/"},
304 // Filesystem URLs have different paths to test.
305 {"filesystem:http://www.google.com/type/", "foo.html", true, "filesystem:http://www.google.com/type/foo.html"},
306 {"filesystem:http://www.google.com/type/", "../foo.html", true, "filesystem:http://www.google.com/type/foo.html"},
307 };
308
viettrungluu4b6915862014-10-16 03:42:49309 for (size_t i = 0; i < arraysize(resolve_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52310 // 8-bit code path.
311 GURL input(resolve_cases[i].base);
312 GURL output = input.Resolve(resolve_cases[i].relative);
313 EXPECT_EQ(resolve_cases[i].expected_valid, output.is_valid()) << i;
314 EXPECT_EQ(resolve_cases[i].expected, output.spec()) << i;
315 EXPECT_EQ(output.SchemeIsFileSystem(), output.inner_url() != NULL);
316
317 // Wide code path.
brettw1b8582f2016-11-03 20:37:17318 GURL inputw(base::UTF8ToUTF16(resolve_cases[i].base));
[email protected]e7bba5f82013-04-10 20:10:52319 GURL outputw =
brettw1b8582f2016-11-03 20:37:17320 input.Resolve(base::UTF8ToUTF16(resolve_cases[i].relative));
[email protected]e7bba5f82013-04-10 20:10:52321 EXPECT_EQ(resolve_cases[i].expected_valid, outputw.is_valid()) << i;
322 EXPECT_EQ(resolve_cases[i].expected, outputw.spec()) << i;
323 EXPECT_EQ(outputw.SchemeIsFileSystem(), outputw.inner_url() != NULL);
324 }
325}
326
327TEST(GURLTest, GetOrigin) {
328 struct TestCase {
329 const char* input;
330 const char* expected;
331 } cases[] = {
Nick Carter8aa718ed2018-09-07 21:39:51332 {"http://www.google.com", "http://www.google.com/"},
333 {"javascript:window.alert(\"hello,world\");", ""},
334 {"http://user:[email protected]:21/blah#baz",
335 "http://www.google.com:21/"},
336 {"http://[email protected]", "http://www.google.com/"},
337 {"http://:[email protected]", "http://www.google.com/"},
338 {"http://:@www.google.com", "http://www.google.com/"},
339 {"filesystem:http://www.google.com/temp/foo?q#b",
340 "http://www.google.com/"},
341 {"filesystem:http://user:[email protected]:21/blah#baz",
342 "http://google.com:21/"},
343 {"blob:null/guid-goes-here", ""},
344 {"blob:http://origin/guid-goes-here", "" /* should be http://origin/ */},
[email protected]e7bba5f82013-04-10 20:10:52345 };
viettrungluu4b6915862014-10-16 03:42:49346 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52347 GURL url(cases[i].input);
348 GURL origin = url.GetOrigin();
349 EXPECT_EQ(cases[i].expected, origin.spec());
350 }
351}
352
[email protected]6b775ee2014-03-20 20:27:25353TEST(GURLTest, GetAsReferrer) {
354 struct TestCase {
355 const char* input;
356 const char* expected;
357 } cases[] = {
358 {"http://www.google.com", "http://www.google.com/"},
359 {"http://user:[email protected]:21/blah#baz", "http://www.google.com:21/blah"},
360 {"http://[email protected]", "http://www.google.com/"},
361 {"http://:[email protected]", "http://www.google.com/"},
362 {"http://:@www.google.com", "http://www.google.com/"},
363 {"http://www.google.com/temp/foo?q#b", "http://www.google.com/temp/foo?q"},
jochen42450392014-11-24 19:47:22364 {"not a url", ""},
365 {"unknown-scheme://foo.html", ""},
366 {"file:///tmp/test.html", ""},
367 {"https://www.google.com", "https://www.google.com/"},
[email protected]6b775ee2014-03-20 20:27:25368 };
viettrungluu4b6915862014-10-16 03:42:49369 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]6b775ee2014-03-20 20:27:25370 GURL url(cases[i].input);
371 GURL origin = url.GetAsReferrer();
372 EXPECT_EQ(cases[i].expected, origin.spec());
373 }
374}
375
[email protected]e7bba5f82013-04-10 20:10:52376TEST(GURLTest, GetWithEmptyPath) {
377 struct TestCase {
378 const char* input;
379 const char* expected;
380 } cases[] = {
381 {"http://www.google.com", "http://www.google.com/"},
382 {"javascript:window.alert(\"hello, world\");", ""},
383 {"http://www.google.com/foo/bar.html?baz=22", "http://www.google.com/"},
384 {"filesystem:http://www.google.com/temporary/bar.html?baz=22", "filesystem:http://www.google.com/temporary/"},
385 {"filesystem:file:///temporary/bar.html?baz=22", "filesystem:file:///temporary/"},
386 };
387
viettrungluu4b6915862014-10-16 03:42:49388 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52389 GURL url(cases[i].input);
390 GURL empty_path = url.GetWithEmptyPath();
391 EXPECT_EQ(cases[i].expected, empty_path.spec());
392 }
393}
394
Giovanni Ortuño Urquidi61b24eda2017-08-09 08:13:10395TEST(GURLTest, GetWithoutFilename) {
396 struct TestCase {
397 const char* input;
398 const char* expected;
399 } cases[] = {
400 // Common Standard URLs.
401 {"https://www.google.com", "https://www.google.com/"},
402 {"https://www.google.com/", "https://www.google.com/"},
403 {"https://www.google.com/maps.htm", "https://www.google.com/"},
404 {"https://www.google.com/maps/", "https://www.google.com/maps/"},
405 {"https://www.google.com/index.html", "https://www.google.com/"},
406 {"https://www.google.com/index.html?q=maps", "https://www.google.com/"},
407 {"https://www.google.com/index.html#maps/", "https://www.google.com/"},
408 {"https://foo:[email protected]/maps.htm", "https://foo:[email protected]/"},
409 {"https://www.google.com/maps/au/index.html", "https://www.google.com/maps/au/"},
410 {"https://www.google.com/maps/au/north", "https://www.google.com/maps/au/"},
411 {"https://www.google.com/maps/au/north/", "https://www.google.com/maps/au/north/"},
412 {"https://www.google.com/maps/au/index.html?q=maps#fragment/", "https://www.google.com/maps/au/"},
413 {"http://www.google.com:8000/maps/au/index.html?q=maps#fragment/", "http://www.google.com:8000/maps/au/"},
414 {"https://www.google.com/maps/au/north/?q=maps#fragment", "https://www.google.com/maps/au/north/"},
415 {"https://www.google.com/maps/au/north?q=maps#fragment", "https://www.google.com/maps/au/"},
416 // Less common standard URLs.
417 {"filesystem:http://www.google.com/temporary/bar.html?baz=22", "filesystem:http://www.google.com/temporary/"},
418 {"file:///temporary/bar.html?baz=22","file:///temporary/"},
419 {"ftp://foo/test/index.html", "ftp://foo/test/"},
420 {"gopher://foo/test/index.html", "gopher://foo/test/"},
421 {"ws://foo/test/index.html", "ws://foo/test/"},
422 // Non-standard, hierarchical URLs.
423 {"chrome://foo/bar.html", "chrome://foo/"},
424 {"httpa://foo/test/index.html", "httpa://foo/test/"},
425 // Non-standard, non-hierarchical URLs.
426 {"blob:https://foo.bar/test/index.html", ""},
427 {"about:blank", ""},
428 {"data:foobar", ""},
429 {"scheme:opaque_data", ""},
430 // Invalid URLs.
431 {"foobar", ""},
432 };
433
434 for (size_t i = 0; i < arraysize(cases); i++) {
435 GURL url(cases[i].input);
436 GURL without_filename = url.GetWithoutFilename();
437 EXPECT_EQ(cases[i].expected, without_filename.spec()) << i;
438 }
439}
440
[email protected]e7bba5f82013-04-10 20:10:52441TEST(GURLTest, Replacements) {
qyearsley2bc727d2015-08-14 20:17:15442 // The URL canonicalizer replacement test will handle most of these case.
[email protected]e7bba5f82013-04-10 20:10:52443 // The most important thing to do here is to check that the proper
444 // canonicalizer gets called based on the scheme of the input.
445 struct ReplaceCase {
446 const char* base;
447 const char* scheme;
448 const char* username;
449 const char* password;
450 const char* host;
451 const char* port;
452 const char* path;
453 const char* query;
454 const char* ref;
455 const char* expected;
456 } replace_cases[] = {
mmenke73cea7e4a2016-06-13 19:04:57457 {"http://www.google.com/foo/bar.html?foo#bar", NULL, NULL, NULL, NULL,
458 NULL, "/", "", "", "http://www.google.com/"},
459 {"http://www.google.com/foo/bar.html?foo#bar", "javascript", "", "", "",
460 "", "window.open('foo');", "", "", "javascript:window.open('foo');"},
461 {"file:///C:/foo/bar.txt", "http", NULL, NULL, "www.google.com", "99",
462 "/foo", "search", "ref", "http://www.google.com:99/foo?search#ref"},
[email protected]e7bba5f82013-04-10 20:10:52463#ifdef WIN32
mmenke73cea7e4a2016-06-13 19:04:57464 {"http://www.google.com/foo/bar.html?foo#bar", "file", "", "", "", "",
465 "c:\\", "", "", "file:///C:/"},
[email protected]e7bba5f82013-04-10 20:10:52466#endif
mmenke73cea7e4a2016-06-13 19:04:57467 {"filesystem:http://www.google.com/foo/bar.html?foo#bar", NULL, NULL,
468 NULL, NULL, NULL, "/", "", "", "filesystem:http://www.google.com/foo/"},
469 // Lengthen the URL instead of shortening it, to test creation of
470 // inner_url.
471 {"filesystem:http://www.google.com/foo/", NULL, NULL, NULL, NULL, NULL,
472 "bar.html", "foo", "bar",
473 "filesystem:http://www.google.com/foo/bar.html?foo#bar"},
[email protected]e7bba5f82013-04-10 20:10:52474 };
475
viettrungluu4b6915862014-10-16 03:42:49476 for (size_t i = 0; i < arraysize(replace_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52477 const ReplaceCase& cur = replace_cases[i];
478 GURL url(cur.base);
479 GURL::Replacements repl;
480 SetupReplacement(&GURL::Replacements::SetScheme, &repl, cur.scheme);
481 SetupReplacement(&GURL::Replacements::SetUsername, &repl, cur.username);
482 SetupReplacement(&GURL::Replacements::SetPassword, &repl, cur.password);
483 SetupReplacement(&GURL::Replacements::SetHost, &repl, cur.host);
484 SetupReplacement(&GURL::Replacements::SetPort, &repl, cur.port);
485 SetupReplacement(&GURL::Replacements::SetPath, &repl, cur.path);
486 SetupReplacement(&GURL::Replacements::SetQuery, &repl, cur.query);
487 SetupReplacement(&GURL::Replacements::SetRef, &repl, cur.ref);
488 GURL output = url.ReplaceComponents(repl);
489
490 EXPECT_EQ(replace_cases[i].expected, output.spec());
mmenke73cea7e4a2016-06-13 19:04:57491
[email protected]e7bba5f82013-04-10 20:10:52492 EXPECT_EQ(output.SchemeIsFileSystem(), output.inner_url() != NULL);
mmenke73cea7e4a2016-06-13 19:04:57493 if (output.SchemeIsFileSystem()) {
494 // TODO(mmenke): inner_url()->spec() is currently the same as the spec()
495 // for the GURL itself. This should be fixed.
496 // See https://crbug.com/619596
497 EXPECT_EQ(replace_cases[i].expected, output.inner_url()->spec());
498 }
[email protected]e7bba5f82013-04-10 20:10:52499 }
500}
501
[email protected]369e84f72013-11-23 01:53:52502TEST(GURLTest, ClearFragmentOnDataUrl) {
503 // http://crbug.com/291747 - a data URL may legitimately have trailing
504 // whitespace in the spec after the ref is cleared. Test this does not trigger
[email protected]0318f922014-04-22 00:09:23505 // the Parsed importing validation DCHECK in GURL.
[email protected]369e84f72013-11-23 01:53:52506 GURL url(" data: one ? two # three ");
507
508 // By default the trailing whitespace will have been stripped.
509 EXPECT_EQ("data: one ? two # three", url.spec());
510 GURL::Replacements repl;
511 repl.ClearRef();
512 GURL url_no_ref = url.ReplaceComponents(repl);
513
514 EXPECT_EQ("data: one ? two ", url_no_ref.spec());
515
qyearsley2bc727d2015-08-14 20:17:15516 // Importing a parsed URL via this constructor overload will retain trailing
[email protected]369e84f72013-11-23 01:53:52517 // whitespace.
518 GURL import_url(url_no_ref.spec(),
519 url_no_ref.parsed_for_possibly_invalid_spec(),
520 url_no_ref.is_valid());
521 EXPECT_EQ(url_no_ref, import_url);
522 EXPECT_EQ(import_url.query(), " two ");
523}
524
[email protected]e7bba5f82013-04-10 20:10:52525TEST(GURLTest, PathForRequest) {
526 struct TestCase {
527 const char* input;
528 const char* expected;
529 const char* inner_expected;
530 } cases[] = {
531 {"http://www.google.com", "/", NULL},
532 {"http://www.google.com/", "/", NULL},
533 {"http://www.google.com/foo/bar.html?baz=22", "/foo/bar.html?baz=22", NULL},
534 {"http://www.google.com/foo/bar.html#ref", "/foo/bar.html", NULL},
535 {"http://www.google.com/foo/bar.html?query#ref", "/foo/bar.html?query", NULL},
536 {"filesystem:http://www.google.com/temporary/foo/bar.html?query#ref", "/foo/bar.html?query", "/temporary"},
537 {"filesystem:http://www.google.com/temporary/foo/bar.html?query", "/foo/bar.html?query", "/temporary"},
538 };
539
viettrungluu4b6915862014-10-16 03:42:49540 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52541 GURL url(cases[i].input);
542 std::string path_request = url.PathForRequest();
543 EXPECT_EQ(cases[i].expected, path_request);
544 EXPECT_EQ(cases[i].inner_expected == NULL, url.inner_url() == NULL);
545 if (url.inner_url() && cases[i].inner_expected)
546 EXPECT_EQ(cases[i].inner_expected, url.inner_url()->PathForRequest());
547 }
548}
549
550TEST(GURLTest, EffectiveIntPort) {
551 struct PortTest {
552 const char* spec;
553 int expected_int_port;
554 } port_tests[] = {
555 // http
556 {"http://www.google.com/", 80},
557 {"http://www.google.com:80/", 80},
558 {"http://www.google.com:443/", 443},
559
560 // https
561 {"https://www.google.com/", 443},
562 {"https://www.google.com:443/", 443},
563 {"https://www.google.com:80/", 80},
564
565 // ftp
566 {"ftp://www.google.com/", 21},
567 {"ftp://www.google.com:21/", 21},
568 {"ftp://www.google.com:80/", 80},
569
570 // gopher
571 {"gopher://www.google.com/", 70},
572 {"gopher://www.google.com:70/", 70},
573 {"gopher://www.google.com:80/", 80},
574
575 // file - no port
[email protected]0318f922014-04-22 00:09:23576 {"file://www.google.com/", PORT_UNSPECIFIED},
577 {"file://www.google.com:443/", PORT_UNSPECIFIED},
[email protected]e7bba5f82013-04-10 20:10:52578
579 // data - no port
[email protected]0318f922014-04-22 00:09:23580 {"data:www.google.com:90", PORT_UNSPECIFIED},
581 {"data:www.google.com", PORT_UNSPECIFIED},
[email protected]e7bba5f82013-04-10 20:10:52582
583 // filesystem - no port
[email protected]0318f922014-04-22 00:09:23584 {"filesystem:http://www.google.com:90/t/foo", PORT_UNSPECIFIED},
585 {"filesystem:file:///t/foo", PORT_UNSPECIFIED},
[email protected]e7bba5f82013-04-10 20:10:52586 };
587
viettrungluu4b6915862014-10-16 03:42:49588 for (size_t i = 0; i < arraysize(port_tests); i++) {
[email protected]e7bba5f82013-04-10 20:10:52589 GURL url(port_tests[i].spec);
590 EXPECT_EQ(port_tests[i].expected_int_port, url.EffectiveIntPort());
591 }
592}
593
594TEST(GURLTest, IPAddress) {
595 struct IPTest {
596 const char* spec;
597 bool expected_ip;
598 } ip_tests[] = {
599 {"http://www.google.com/", false},
600 {"http://192.168.9.1/", true},
601 {"http://192.168.9.1.2/", false},
602 {"http://192.168.m.1/", false},
603 {"http://2001:db8::1/", false},
604 {"http://[2001:db8::1]/", true},
605 {"", false},
606 {"some random input!", false},
607 };
608
viettrungluu4b6915862014-10-16 03:42:49609 for (size_t i = 0; i < arraysize(ip_tests); i++) {
[email protected]e7bba5f82013-04-10 20:10:52610 GURL url(ip_tests[i].spec);
611 EXPECT_EQ(ip_tests[i].expected_ip, url.HostIsIPAddress());
612 }
613}
614
615TEST(GURLTest, HostNoBrackets) {
616 struct TestCase {
617 const char* input;
618 const char* expected_host;
619 const char* expected_plainhost;
620 } cases[] = {
621 {"http://www.google.com", "www.google.com", "www.google.com"},
622 {"http://[2001:db8::1]/", "[2001:db8::1]", "2001:db8::1"},
623 {"http://[::]/", "[::]", "::"},
624
625 // Don't require a valid URL, but don't crash either.
626 {"http://[]/", "[]", ""},
627 {"http://[x]/", "[x]", "x"},
628 {"http://[x/", "[x", "[x"},
629 {"http://x]/", "x]", "x]"},
630 {"http://[/", "[", "["},
631 {"http://]/", "]", "]"},
632 {"", "", ""},
633 };
viettrungluu4b6915862014-10-16 03:42:49634 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52635 GURL url(cases[i].input);
636 EXPECT_EQ(cases[i].expected_host, url.host());
637 EXPECT_EQ(cases[i].expected_plainhost, url.HostNoBrackets());
ricea1c0de2f2017-07-03 08:21:43638 EXPECT_EQ(cases[i].expected_plainhost, url.HostNoBracketsPiece());
[email protected]e7bba5f82013-04-10 20:10:52639 }
640}
641
642TEST(GURLTest, DomainIs) {
qyearsley7ffaa682015-08-03 07:03:49643 GURL url_1("http://google.com/foo");
644 EXPECT_TRUE(url_1.DomainIs("google.com"));
[email protected]e7bba5f82013-04-10 20:10:52645
qyearsley7ffaa682015-08-03 07:03:49646 // Subdomain and port are ignored.
647 GURL url_2("http://www.google.com:99/foo");
648 EXPECT_TRUE(url_2.DomainIs("google.com"));
[email protected]e7bba5f82013-04-10 20:10:52649
qyearsley7ffaa682015-08-03 07:03:49650 // Different top-level domain.
651 GURL url_3("http://www.google.com.cn/foo");
652 EXPECT_FALSE(url_3.DomainIs("google.com"));
[email protected]e7bba5f82013-04-10 20:10:52653
qyearsley7ffaa682015-08-03 07:03:49654 // Different host name.
655 GURL url_4("http://www.iamnotgoogle.com/foo");
656 EXPECT_FALSE(url_4.DomainIs("google.com"));
[email protected]e7bba5f82013-04-10 20:10:52657
qyearsley7ffaa682015-08-03 07:03:49658 // The input must be lower-cased otherwise DomainIs returns false.
659 GURL url_5("http://www.google.com/foo");
660 EXPECT_FALSE(url_5.DomainIs("Google.com"));
[email protected]e7bba5f82013-04-10 20:10:52661
qyearsley7ffaa682015-08-03 07:03:49662 // If the URL is invalid, DomainIs returns false.
663 GURL invalid_url("google.com");
664 EXPECT_FALSE(invalid_url.is_valid());
665 EXPECT_FALSE(invalid_url.DomainIs("google.com"));
Charles Harrison81dc2fb2017-08-30 23:41:12666
667 GURL url_with_escape_chars("https://www.,.test");
668 EXPECT_TRUE(url_with_escape_chars.is_valid());
669 EXPECT_EQ(url_with_escape_chars.host(), "www.%2C.test");
670 EXPECT_TRUE(url_with_escape_chars.DomainIs("%2C.test"));
qyearsley7ffaa682015-08-03 07:03:49671}
[email protected]e7bba5f82013-04-10 20:10:52672
qyearsley7ffaa682015-08-03 07:03:49673TEST(GURLTest, DomainIsTerminatingDotBehavior) {
674 // If the host part ends with a dot, it matches input domains
675 // with or without a dot.
676 GURL url_with_dot("http://www.google.com./foo");
677 EXPECT_TRUE(url_with_dot.DomainIs("google.com"));
678 EXPECT_TRUE(url_with_dot.DomainIs("google.com."));
679 EXPECT_TRUE(url_with_dot.DomainIs(".com"));
680 EXPECT_TRUE(url_with_dot.DomainIs(".com."));
[email protected]e7bba5f82013-04-10 20:10:52681
qyearsley7ffaa682015-08-03 07:03:49682 // But, if the host name doesn't end with a dot and the input
683 // domain does, then it's considered to not match.
684 GURL url_without_dot("http://google.com/foo");
685 EXPECT_FALSE(url_without_dot.DomainIs("google.com."));
[email protected]e7bba5f82013-04-10 20:10:52686
qyearsley7ffaa682015-08-03 07:03:49687 // If the URL ends with two dots, it doesn't match.
688 GURL url_with_two_dots("http://www.google.com../foo");
689 EXPECT_FALSE(url_with_two_dots.DomainIs("google.com"));
690}
[email protected]e7bba5f82013-04-10 20:10:52691
qyearsley7ffaa682015-08-03 07:03:49692TEST(GURLTest, DomainIsWithFilesystemScheme) {
693 GURL url_1("filesystem:http://www.google.com:99/foo/");
694 EXPECT_TRUE(url_1.DomainIs("google.com"));
[email protected]e7bba5f82013-04-10 20:10:52695
qyearsley7ffaa682015-08-03 07:03:49696 GURL url_2("filesystem:http://www.iamnotgoogle.com/foo/");
697 EXPECT_FALSE(url_2.DomainIs("google.com"));
[email protected]e7bba5f82013-04-10 20:10:52698}
699
700// Newlines should be stripped from inputs.
701TEST(GURLTest, Newlines) {
702 // Constructor.
703 GURL url_1(" \t ht\ntp://\twww.goo\rgle.com/as\ndf \n ");
704 EXPECT_EQ("http://www.google.com/asdf", url_1.spec());
Mike West9e5ae902017-05-24 15:17:50705 EXPECT_FALSE(
706 url_1.parsed_for_possibly_invalid_spec().potentially_dangling_markup);
[email protected]e7bba5f82013-04-10 20:10:52707
708 // Relative path resolver.
709 GURL url_2 = url_1.Resolve(" \n /fo\to\r ");
710 EXPECT_EQ("http://www.google.com/foo", url_2.spec());
Mike West9e5ae902017-05-24 15:17:50711 EXPECT_FALSE(
712 url_2.parsed_for_possibly_invalid_spec().potentially_dangling_markup);
713
714 // Constructor.
715 GURL url_3(" \t ht\ntp://\twww.goo\rgle.com/as\ndf< \n ");
716 EXPECT_EQ("http://www.google.com/asdf%3C", url_3.spec());
717 EXPECT_TRUE(
718 url_3.parsed_for_possibly_invalid_spec().potentially_dangling_markup);
719
720 // Relative path resolver.
721 GURL url_4 = url_1.Resolve(" \n /fo\to<\r ");
722 EXPECT_EQ("http://www.google.com/foo%3C", url_4.spec());
723 EXPECT_TRUE(
724 url_4.parsed_for_possibly_invalid_spec().potentially_dangling_markup);
[email protected]e7bba5f82013-04-10 20:10:52725
726 // Note that newlines are NOT stripped from ReplaceComponents.
727}
728
729TEST(GURLTest, IsStandard) {
730 GURL a("http:foo/bar");
731 EXPECT_TRUE(a.IsStandard());
732
733 GURL b("foo:bar/baz");
734 EXPECT_FALSE(b.IsStandard());
735
736 GURL c("foo://bar/baz");
737 EXPECT_FALSE(c.IsStandard());
blundell5ef36cb42016-06-27 15:37:14738
739 GURL d("cid:bar@baz");
740 EXPECT_FALSE(d.IsStandard());
[email protected]e7bba5f82013-04-10 20:10:52741}
[email protected]9690b992013-11-22 07:40:46742
743TEST(GURLTest, SchemeIsHTTPOrHTTPS) {
744 EXPECT_TRUE(GURL("http://bar/").SchemeIsHTTPOrHTTPS());
745 EXPECT_TRUE(GURL("HTTPS://BAR").SchemeIsHTTPOrHTTPS());
746 EXPECT_FALSE(GURL("ftp://bar/").SchemeIsHTTPOrHTTPS());
747}
748
749TEST(GURLTest, SchemeIsWSOrWSS) {
750 EXPECT_TRUE(GURL("WS://BAR/").SchemeIsWSOrWSS());
751 EXPECT_TRUE(GURL("wss://bar/").SchemeIsWSOrWSS());
752 EXPECT_FALSE(GURL("http://bar/").SchemeIsWSOrWSS());
753}
[email protected]0318f922014-04-22 00:09:23754
jww04480402016-10-25 02:50:33755TEST(GURLTest, SchemeIsCryptographic) {
756 EXPECT_TRUE(GURL("https://foo.bar.com/").SchemeIsCryptographic());
757 EXPECT_TRUE(GURL("HTTPS://foo.bar.com/").SchemeIsCryptographic());
758 EXPECT_TRUE(GURL("HtTpS://foo.bar.com/").SchemeIsCryptographic());
759
760 EXPECT_TRUE(GURL("wss://foo.bar.com/").SchemeIsCryptographic());
761 EXPECT_TRUE(GURL("WSS://foo.bar.com/").SchemeIsCryptographic());
762 EXPECT_TRUE(GURL("WsS://foo.bar.com/").SchemeIsCryptographic());
763
jww04480402016-10-25 02:50:33764 EXPECT_FALSE(GURL("http://foo.bar.com/").SchemeIsCryptographic());
765 EXPECT_FALSE(GURL("ws://foo.bar.com/").SchemeIsCryptographic());
jww04480402016-10-25 02:50:33766}
767
amogh.bihaniee85a112014-09-01 06:06:51768TEST(GURLTest, SchemeIsBlob) {
769 EXPECT_TRUE(GURL("BLOB://BAR/").SchemeIsBlob());
770 EXPECT_TRUE(GURL("blob://bar/").SchemeIsBlob());
771 EXPECT_FALSE(GURL("http://bar/").SchemeIsBlob());
772}
773
mkwst414920e2015-07-28 05:30:07774TEST(GURLTest, ContentAndPathForNonStandardURLs) {
775 struct TestCase {
776 const char* url;
777 const char* expected;
778 } cases[] = {
779 {"null", ""},
780 {"not-a-standard-scheme:this is arbitrary content",
781 "this is arbitrary content"},
782 {"view-source:http://example.com/path", "http://example.com/path"},
783 {"blob:http://example.com/GUID", "http://example.com/GUID"},
784 {"blob://http://example.com/GUID", "//http://example.com/GUID"},
785 {"blob:http://user:[email protected]/GUID",
786 "http://user:[email protected]/GUID"},
787
Stephen McGruerb52ebdc2018-10-31 22:06:04788 {"http://www.example.com/GUID#ref", "www.example.com/GUID"},
789 {"http://me:[email protected]/GUID/#ref", "me:[email protected]/GUID/"},
790 {"data:text/html,Question?<div style=\"color: #bad\">idea</div>",
791 "text/html,Question?<div style=\"color: "},
792
793 // TODO(mkwst): This seems like a bug. https://crbug.com/513600
794 {"filesystem:http://example.com/path", "/"},
795
796 // Javascript URLs include '#' symbols in their content.
797 {"javascript:#", "#"},
798 {"javascript:alert('#');", "alert('#');"},
799 };
800
801 for (const auto& test : cases) {
802 GURL url(test.url);
803 EXPECT_EQ(test.expected, url.GetContent()) << test.url;
804 }
805}
806
807TEST(GURLTest, PathForNonStandardURLs) {
808 struct TestCase {
809 const char* url;
810 const char* expected;
811 } cases[] = {
812 {"null", ""},
813 {"not-a-standard-scheme:this is arbitrary content",
814 "this is arbitrary content"},
815 {"view-source:http://example.com/path", "http://example.com/path"},
816 {"blob:http://example.com/GUID", "http://example.com/GUID"},
817 {"blob://http://example.com/GUID", "//http://example.com/GUID"},
818 {"blob:http://user:[email protected]/GUID",
819 "http://user:[email protected]/GUID"},
820
821 {"http://www.example.com/GUID#ref", "/GUID"},
822 {"http://me:[email protected]/GUID/#ref", "/GUID/"},
823 {"data:text/html,Question?<div style=\"color: #bad\">idea</div>",
824 "text/html,Question"},
825
mkwst414920e2015-07-28 05:30:07826 // TODO(mkwst): This seems like a bug. https://crbug.com/513600
827 {"filesystem:http://example.com/path", "/"},
828 };
829
830 for (const auto& test : cases) {
831 GURL url(test.url);
832 EXPECT_EQ(test.expected, url.path()) << test.url;
mkwst414920e2015-07-28 05:30:07833 }
834}
835
clamy12bca18b2017-02-10 15:33:07836TEST(GURLTest, IsAboutBlank) {
837 const std::string kAboutBlankUrls[] = {"about:blank", "about:blank?foo",
838 "about:blank/#foo",
839 "about:blank?foo#foo"};
840 for (const auto& url : kAboutBlankUrls)
841 EXPECT_TRUE(GURL(url).IsAboutBlank()) << url;
842
843 const std::string kNotAboutBlankUrls[] = {
844 "http:blank", "about:blan", "about://blank",
845 "about:blank/foo", "about://:8000/blank", "about://foo:foo@/blank",
846 "foo@about:blank", "foo:bar@about:blank", "about:blank:8000"};
847 for (const auto& url : kNotAboutBlankUrls)
848 EXPECT_FALSE(GURL(url).IsAboutBlank()) << url;
849}
850
851TEST(GURLTest, EqualsIgnoringRef) {
852 const struct {
853 const char* url_a;
854 const char* url_b;
855 bool are_equals;
856 } kTestCases[] = {
857 // No ref.
858 {"http://a.com", "http://a.com", true},
859 {"http://a.com", "http://b.com", false},
860
861 // Same Ref.
862 {"http://a.com#foo", "http://a.com#foo", true},
863 {"http://a.com#foo", "http://b.com#foo", false},
864
865 // Different Refs.
866 {"http://a.com#foo", "http://a.com#bar", true},
867 {"http://a.com#foo", "http://b.com#bar", false},
868
869 // One has a ref, the other doesn't.
870 {"http://a.com#foo", "http://a.com", true},
871 {"http://a.com#foo", "http://b.com", false},
872
873 // Empty refs.
874 {"http://a.com#", "http://a.com#", true},
875 {"http://a.com#", "http://a.com", true},
876
877 // URLs that differ only by their last character.
878 {"http://aaa", "http://aab", false},
879 {"http://aaa#foo", "http://aab#foo", false},
880
881 // Different size of the part before the ref.
882 {"http://123#a", "http://123456#a", false},
883
884 // Blob URLs
885 {"blob:http://a.com#foo", "blob:http://a.com#foo", true},
886 {"blob:http://a.com#foo", "blob:http://a.com#bar", true},
887 {"blob:http://a.com#foo", "blob:http://b.com#bar", false},
888
889 // Filesystem URLs
890 {"filesystem:http://a.com#foo", "filesystem:http://a.com#foo", true},
891 {"filesystem:http://a.com#foo", "filesystem:http://a.com#bar", true},
892 {"filesystem:http://a.com#foo", "filesystem:http://b.com#bar", false},
arthursonzogni313d530f2017-12-13 13:05:29893
894 // Data URLs
895 {"data:text/html,a#foo", "data:text/html,a#bar", true},
896 {"data:text/html,a#foo", "data:text/html,a#foo", true},
897 {"data:text/html,a#foo", "data:text/html,b#foo", false},
clamy12bca18b2017-02-10 15:33:07898 };
899
900 for (const auto& test_case : kTestCases) {
901 SCOPED_TRACE(testing::Message()
902 << std::endl
903 << "url_a = " << test_case.url_a << std::endl
904 << "url_b = " << test_case.url_b << std::endl);
905 // A versus B.
906 EXPECT_EQ(test_case.are_equals,
907 GURL(test_case.url_a).EqualsIgnoringRef(GURL(test_case.url_b)));
908 // B versus A.
909 EXPECT_EQ(test_case.are_equals,
910 GURL(test_case.url_b).EqualsIgnoringRef(GURL(test_case.url_a)));
911 }
912}
913
Lukasz Anforowicz68c21772018-01-13 03:42:44914TEST(GURLTest, DebugAlias) {
915 GURL url("https://foo.com/bar");
916 DEBUG_ALIAS_FOR_GURL(url_debug_alias, url);
917 EXPECT_STREQ("https://foo.com/bar", url_debug_alias);
918}
919
[email protected]0318f922014-04-22 00:09:23920} // namespace url