blob: 31767db6546b99e11c78735675215865f8370126 [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
Avi Drissmana92b3be2018-12-24 21:55:297#include "base/stl_util.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 };
Avi Drissmana92b3be2018-12-24 21:55:29237 for (size_t i = 0; i < base::size(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 };
Avi Drissmana92b3be2018-12-24 21:55:29250 for (size_t i = 0; i < base::size(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
Avi Drissmana92b3be2018-12-24 21:55:29309 for (size_t i = 0; i < base::size(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 };
Avi Drissmana92b3be2018-12-24 21:55:29346 for (size_t i = 0; i < base::size(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 };
Avi Drissmana92b3be2018-12-24 21:55:29369 for (size_t i = 0; i < base::size(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
Avi Drissmana92b3be2018-12-24 21:55:29388 for (size_t i = 0; i < base::size(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
Avi Drissmana92b3be2018-12-24 21:55:29434 for (size_t i = 0; i < base::size(cases); i++) {
Giovanni Ortuño Urquidi61b24eda2017-08-09 08:13:10435 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
Avi Drissmana92b3be2018-12-24 21:55:29476 for (size_t i = 0; i < base::size(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
Avi Drissmana92b3be2018-12-24 21:55:29540 for (size_t i = 0; i < base::size(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52541 GURL url(cases[i].input);
David Van Cleve5f374442019-11-06 15:01:17542 EXPECT_EQ(cases[i].expected, url.PathForRequest());
543 EXPECT_EQ(cases[i].expected, url.PathForRequestPiece());
[email protected]e7bba5f82013-04-10 20:10:52544 EXPECT_EQ(cases[i].inner_expected == NULL, url.inner_url() == NULL);
David Van Cleve5f374442019-11-06 15:01:17545 if (url.inner_url() && cases[i].inner_expected) {
[email protected]e7bba5f82013-04-10 20:10:52546 EXPECT_EQ(cases[i].inner_expected, url.inner_url()->PathForRequest());
David Van Cleve5f374442019-11-06 15:01:17547 EXPECT_EQ(cases[i].inner_expected,
548 url.inner_url()->PathForRequestPiece());
549 }
[email protected]e7bba5f82013-04-10 20:10:52550 }
551}
552
553TEST(GURLTest, EffectiveIntPort) {
554 struct PortTest {
555 const char* spec;
556 int expected_int_port;
557 } port_tests[] = {
558 // http
559 {"http://www.google.com/", 80},
560 {"http://www.google.com:80/", 80},
561 {"http://www.google.com:443/", 443},
562
563 // https
564 {"https://www.google.com/", 443},
565 {"https://www.google.com:443/", 443},
566 {"https://www.google.com:80/", 80},
567
568 // ftp
569 {"ftp://www.google.com/", 21},
570 {"ftp://www.google.com:21/", 21},
571 {"ftp://www.google.com:80/", 80},
572
[email protected]e7bba5f82013-04-10 20:10:52573 // file - no port
[email protected]0318f922014-04-22 00:09:23574 {"file://www.google.com/", PORT_UNSPECIFIED},
575 {"file://www.google.com:443/", PORT_UNSPECIFIED},
[email protected]e7bba5f82013-04-10 20:10:52576
577 // data - no port
[email protected]0318f922014-04-22 00:09:23578 {"data:www.google.com:90", PORT_UNSPECIFIED},
579 {"data:www.google.com", PORT_UNSPECIFIED},
[email protected]e7bba5f82013-04-10 20:10:52580
581 // filesystem - no port
[email protected]0318f922014-04-22 00:09:23582 {"filesystem:http://www.google.com:90/t/foo", PORT_UNSPECIFIED},
583 {"filesystem:file:///t/foo", PORT_UNSPECIFIED},
[email protected]e7bba5f82013-04-10 20:10:52584 };
585
Avi Drissmana92b3be2018-12-24 21:55:29586 for (size_t i = 0; i < base::size(port_tests); i++) {
[email protected]e7bba5f82013-04-10 20:10:52587 GURL url(port_tests[i].spec);
588 EXPECT_EQ(port_tests[i].expected_int_port, url.EffectiveIntPort());
589 }
590}
591
592TEST(GURLTest, IPAddress) {
593 struct IPTest {
594 const char* spec;
595 bool expected_ip;
596 } ip_tests[] = {
597 {"http://www.google.com/", false},
598 {"http://192.168.9.1/", true},
599 {"http://192.168.9.1.2/", false},
600 {"http://192.168.m.1/", false},
601 {"http://2001:db8::1/", false},
602 {"http://[2001:db8::1]/", true},
603 {"", false},
604 {"some random input!", false},
605 };
606
Avi Drissmana92b3be2018-12-24 21:55:29607 for (size_t i = 0; i < base::size(ip_tests); i++) {
[email protected]e7bba5f82013-04-10 20:10:52608 GURL url(ip_tests[i].spec);
609 EXPECT_EQ(ip_tests[i].expected_ip, url.HostIsIPAddress());
610 }
611}
612
613TEST(GURLTest, HostNoBrackets) {
614 struct TestCase {
615 const char* input;
616 const char* expected_host;
617 const char* expected_plainhost;
618 } cases[] = {
619 {"http://www.google.com", "www.google.com", "www.google.com"},
620 {"http://[2001:db8::1]/", "[2001:db8::1]", "2001:db8::1"},
621 {"http://[::]/", "[::]", "::"},
622
623 // Don't require a valid URL, but don't crash either.
624 {"http://[]/", "[]", ""},
625 {"http://[x]/", "[x]", "x"},
626 {"http://[x/", "[x", "[x"},
627 {"http://x]/", "x]", "x]"},
628 {"http://[/", "[", "["},
629 {"http://]/", "]", "]"},
630 {"", "", ""},
631 };
Avi Drissmana92b3be2018-12-24 21:55:29632 for (size_t i = 0; i < base::size(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52633 GURL url(cases[i].input);
634 EXPECT_EQ(cases[i].expected_host, url.host());
635 EXPECT_EQ(cases[i].expected_plainhost, url.HostNoBrackets());
ricea1c0de2f2017-07-03 08:21:43636 EXPECT_EQ(cases[i].expected_plainhost, url.HostNoBracketsPiece());
[email protected]e7bba5f82013-04-10 20:10:52637 }
638}
639
640TEST(GURLTest, DomainIs) {
qyearsley7ffaa682015-08-03 07:03:49641 GURL url_1("http://google.com/foo");
642 EXPECT_TRUE(url_1.DomainIs("google.com"));
[email protected]e7bba5f82013-04-10 20:10:52643
qyearsley7ffaa682015-08-03 07:03:49644 // Subdomain and port are ignored.
645 GURL url_2("http://www.google.com:99/foo");
646 EXPECT_TRUE(url_2.DomainIs("google.com"));
[email protected]e7bba5f82013-04-10 20:10:52647
qyearsley7ffaa682015-08-03 07:03:49648 // Different top-level domain.
649 GURL url_3("http://www.google.com.cn/foo");
650 EXPECT_FALSE(url_3.DomainIs("google.com"));
[email protected]e7bba5f82013-04-10 20:10:52651
qyearsley7ffaa682015-08-03 07:03:49652 // Different host name.
653 GURL url_4("http://www.iamnotgoogle.com/foo");
654 EXPECT_FALSE(url_4.DomainIs("google.com"));
[email protected]e7bba5f82013-04-10 20:10:52655
qyearsley7ffaa682015-08-03 07:03:49656 // The input must be lower-cased otherwise DomainIs returns false.
657 GURL url_5("http://www.google.com/foo");
658 EXPECT_FALSE(url_5.DomainIs("Google.com"));
[email protected]e7bba5f82013-04-10 20:10:52659
qyearsley7ffaa682015-08-03 07:03:49660 // If the URL is invalid, DomainIs returns false.
661 GURL invalid_url("google.com");
662 EXPECT_FALSE(invalid_url.is_valid());
663 EXPECT_FALSE(invalid_url.DomainIs("google.com"));
Charles Harrison81dc2fb2017-08-30 23:41:12664
665 GURL url_with_escape_chars("https://www.,.test");
666 EXPECT_TRUE(url_with_escape_chars.is_valid());
667 EXPECT_EQ(url_with_escape_chars.host(), "www.%2C.test");
668 EXPECT_TRUE(url_with_escape_chars.DomainIs("%2C.test"));
qyearsley7ffaa682015-08-03 07:03:49669}
[email protected]e7bba5f82013-04-10 20:10:52670
qyearsley7ffaa682015-08-03 07:03:49671TEST(GURLTest, DomainIsTerminatingDotBehavior) {
672 // If the host part ends with a dot, it matches input domains
673 // with or without a dot.
674 GURL url_with_dot("http://www.google.com./foo");
675 EXPECT_TRUE(url_with_dot.DomainIs("google.com"));
676 EXPECT_TRUE(url_with_dot.DomainIs("google.com."));
677 EXPECT_TRUE(url_with_dot.DomainIs(".com"));
678 EXPECT_TRUE(url_with_dot.DomainIs(".com."));
[email protected]e7bba5f82013-04-10 20:10:52679
qyearsley7ffaa682015-08-03 07:03:49680 // But, if the host name doesn't end with a dot and the input
681 // domain does, then it's considered to not match.
682 GURL url_without_dot("http://google.com/foo");
683 EXPECT_FALSE(url_without_dot.DomainIs("google.com."));
[email protected]e7bba5f82013-04-10 20:10:52684
qyearsley7ffaa682015-08-03 07:03:49685 // If the URL ends with two dots, it doesn't match.
686 GURL url_with_two_dots("http://www.google.com../foo");
687 EXPECT_FALSE(url_with_two_dots.DomainIs("google.com"));
688}
[email protected]e7bba5f82013-04-10 20:10:52689
qyearsley7ffaa682015-08-03 07:03:49690TEST(GURLTest, DomainIsWithFilesystemScheme) {
691 GURL url_1("filesystem:http://www.google.com:99/foo/");
692 EXPECT_TRUE(url_1.DomainIs("google.com"));
[email protected]e7bba5f82013-04-10 20:10:52693
qyearsley7ffaa682015-08-03 07:03:49694 GURL url_2("filesystem:http://www.iamnotgoogle.com/foo/");
695 EXPECT_FALSE(url_2.DomainIs("google.com"));
[email protected]e7bba5f82013-04-10 20:10:52696}
697
698// Newlines should be stripped from inputs.
699TEST(GURLTest, Newlines) {
700 // Constructor.
701 GURL url_1(" \t ht\ntp://\twww.goo\rgle.com/as\ndf \n ");
702 EXPECT_EQ("http://www.google.com/asdf", url_1.spec());
Mike West9e5ae902017-05-24 15:17:50703 EXPECT_FALSE(
704 url_1.parsed_for_possibly_invalid_spec().potentially_dangling_markup);
[email protected]e7bba5f82013-04-10 20:10:52705
706 // Relative path resolver.
707 GURL url_2 = url_1.Resolve(" \n /fo\to\r ");
708 EXPECT_EQ("http://www.google.com/foo", url_2.spec());
Mike West9e5ae902017-05-24 15:17:50709 EXPECT_FALSE(
710 url_2.parsed_for_possibly_invalid_spec().potentially_dangling_markup);
711
712 // Constructor.
713 GURL url_3(" \t ht\ntp://\twww.goo\rgle.com/as\ndf< \n ");
714 EXPECT_EQ("http://www.google.com/asdf%3C", url_3.spec());
715 EXPECT_TRUE(
716 url_3.parsed_for_possibly_invalid_spec().potentially_dangling_markup);
717
718 // Relative path resolver.
719 GURL url_4 = url_1.Resolve(" \n /fo\to<\r ");
720 EXPECT_EQ("http://www.google.com/foo%3C", url_4.spec());
721 EXPECT_TRUE(
722 url_4.parsed_for_possibly_invalid_spec().potentially_dangling_markup);
[email protected]e7bba5f82013-04-10 20:10:52723
724 // Note that newlines are NOT stripped from ReplaceComponents.
725}
726
727TEST(GURLTest, IsStandard) {
728 GURL a("http:foo/bar");
729 EXPECT_TRUE(a.IsStandard());
730
731 GURL b("foo:bar/baz");
732 EXPECT_FALSE(b.IsStandard());
733
734 GURL c("foo://bar/baz");
735 EXPECT_FALSE(c.IsStandard());
blundell5ef36cb42016-06-27 15:37:14736
737 GURL d("cid:bar@baz");
738 EXPECT_FALSE(d.IsStandard());
[email protected]e7bba5f82013-04-10 20:10:52739}
[email protected]9690b992013-11-22 07:40:46740
741TEST(GURLTest, SchemeIsHTTPOrHTTPS) {
742 EXPECT_TRUE(GURL("http://bar/").SchemeIsHTTPOrHTTPS());
743 EXPECT_TRUE(GURL("HTTPS://BAR").SchemeIsHTTPOrHTTPS());
744 EXPECT_FALSE(GURL("ftp://bar/").SchemeIsHTTPOrHTTPS());
745}
746
747TEST(GURLTest, SchemeIsWSOrWSS) {
748 EXPECT_TRUE(GURL("WS://BAR/").SchemeIsWSOrWSS());
749 EXPECT_TRUE(GURL("wss://bar/").SchemeIsWSOrWSS());
750 EXPECT_FALSE(GURL("http://bar/").SchemeIsWSOrWSS());
751}
[email protected]0318f922014-04-22 00:09:23752
jww04480402016-10-25 02:50:33753TEST(GURLTest, SchemeIsCryptographic) {
754 EXPECT_TRUE(GURL("https://foo.bar.com/").SchemeIsCryptographic());
755 EXPECT_TRUE(GURL("HTTPS://foo.bar.com/").SchemeIsCryptographic());
756 EXPECT_TRUE(GURL("HtTpS://foo.bar.com/").SchemeIsCryptographic());
757
758 EXPECT_TRUE(GURL("wss://foo.bar.com/").SchemeIsCryptographic());
759 EXPECT_TRUE(GURL("WSS://foo.bar.com/").SchemeIsCryptographic());
760 EXPECT_TRUE(GURL("WsS://foo.bar.com/").SchemeIsCryptographic());
761
jww04480402016-10-25 02:50:33762 EXPECT_FALSE(GURL("http://foo.bar.com/").SchemeIsCryptographic());
763 EXPECT_FALSE(GURL("ws://foo.bar.com/").SchemeIsCryptographic());
jww04480402016-10-25 02:50:33764}
765
Maks Orlovich44525ce2019-02-25 14:17:58766TEST(GURLTest, SchemeIsCryptographicStatic) {
767 EXPECT_TRUE(GURL::SchemeIsCryptographic("https"));
768 EXPECT_TRUE(GURL::SchemeIsCryptographic("wss"));
769 EXPECT_FALSE(GURL::SchemeIsCryptographic("http"));
770 EXPECT_FALSE(GURL::SchemeIsCryptographic("ws"));
771 EXPECT_FALSE(GURL::SchemeIsCryptographic("ftp"));
772}
773
amogh.bihaniee85a112014-09-01 06:06:51774TEST(GURLTest, SchemeIsBlob) {
775 EXPECT_TRUE(GURL("BLOB://BAR/").SchemeIsBlob());
776 EXPECT_TRUE(GURL("blob://bar/").SchemeIsBlob());
777 EXPECT_FALSE(GURL("http://bar/").SchemeIsBlob());
778}
779
Stephen McGruer35fad07f2018-11-21 15:09:19780// Tests that the 'content' of the URL is properly extracted. This can be
781// complex in cases such as multiple schemes (view-source:http:) or for
782// javascript URLs. See GURL::GetContent for more details.
783TEST(GURLTest, ContentForNonStandardURLs) {
mkwst414920e2015-07-28 05:30:07784 struct TestCase {
785 const char* url;
786 const char* expected;
787 } cases[] = {
788 {"null", ""},
789 {"not-a-standard-scheme:this is arbitrary content",
790 "this is arbitrary content"},
Stephen McGruer35fad07f2018-11-21 15:09:19791
792 // When there are multiple schemes, only the first is excluded from the
793 // content. Note also that for e.g. 'http://', the '//' is part of the
794 // content not the scheme.
mkwst414920e2015-07-28 05:30:07795 {"view-source:http://example.com/path", "http://example.com/path"},
796 {"blob:http://example.com/GUID", "http://example.com/GUID"},
797 {"blob://http://example.com/GUID", "//http://example.com/GUID"},
798 {"blob:http://user:[email protected]/GUID",
799 "http://user:[email protected]/GUID"},
800
Stephen McGruer35fad07f2018-11-21 15:09:19801 // The octothorpe character ('#') marks the end of the URL content, and
802 // the start of the fragment. It should not be included in the content.
Stephen McGruerb52ebdc2018-10-31 22:06:04803 {"http://www.example.com/GUID#ref", "www.example.com/GUID"},
804 {"http://me:[email protected]/GUID/#ref", "me:[email protected]/GUID/"},
805 {"data:text/html,Question?<div style=\"color: #bad\">idea</div>",
806 "text/html,Question?<div style=\"color: "},
807
808 // TODO(mkwst): This seems like a bug. https://crbug.com/513600
809 {"filesystem:http://example.com/path", "/"},
810
811 // Javascript URLs include '#' symbols in their content.
812 {"javascript:#", "#"},
813 {"javascript:alert('#');", "alert('#');"},
814 };
815
816 for (const auto& test : cases) {
817 GURL url(test.url);
818 EXPECT_EQ(test.expected, url.GetContent()) << test.url;
819 }
820}
821
Stephen McGruer35fad07f2018-11-21 15:09:19822// Tests that the URL path is properly extracted for unusual URLs. This can be
823// complex in cases such as multiple schemes (view-source:http:) or when
824// octothorpes ('#') are involved.
Stephen McGruerb52ebdc2018-10-31 22:06:04825TEST(GURLTest, PathForNonStandardURLs) {
826 struct TestCase {
827 const char* url;
828 const char* expected;
829 } cases[] = {
830 {"null", ""},
831 {"not-a-standard-scheme:this is arbitrary content",
832 "this is arbitrary content"},
833 {"view-source:http://example.com/path", "http://example.com/path"},
834 {"blob:http://example.com/GUID", "http://example.com/GUID"},
835 {"blob://http://example.com/GUID", "//http://example.com/GUID"},
836 {"blob:http://user:[email protected]/GUID",
837 "http://user:[email protected]/GUID"},
838
839 {"http://www.example.com/GUID#ref", "/GUID"},
840 {"http://me:[email protected]/GUID/#ref", "/GUID/"},
841 {"data:text/html,Question?<div style=\"color: #bad\">idea</div>",
842 "text/html,Question"},
843
mkwst414920e2015-07-28 05:30:07844 // TODO(mkwst): This seems like a bug. https://crbug.com/513600
845 {"filesystem:http://example.com/path", "/"},
846 };
847
848 for (const auto& test : cases) {
849 GURL url(test.url);
850 EXPECT_EQ(test.expected, url.path()) << test.url;
mkwst414920e2015-07-28 05:30:07851 }
852}
853
clamy12bca18b2017-02-10 15:33:07854TEST(GURLTest, IsAboutBlank) {
855 const std::string kAboutBlankUrls[] = {"about:blank", "about:blank?foo",
856 "about:blank/#foo",
857 "about:blank?foo#foo"};
858 for (const auto& url : kAboutBlankUrls)
859 EXPECT_TRUE(GURL(url).IsAboutBlank()) << url;
860
861 const std::string kNotAboutBlankUrls[] = {
862 "http:blank", "about:blan", "about://blank",
863 "about:blank/foo", "about://:8000/blank", "about://foo:foo@/blank",
Lukasz Anforowicz0bc073e2019-06-14 19:41:52864 "foo@about:blank", "foo:bar@about:blank", "about:blank:8000",
865 "about:blANk"};
clamy12bca18b2017-02-10 15:33:07866 for (const auto& url : kNotAboutBlankUrls)
867 EXPECT_FALSE(GURL(url).IsAboutBlank()) << url;
868}
869
Lukasz Anforowicz0bc073e2019-06-14 19:41:52870TEST(GURLTest, IsAboutSrcdoc) {
871 const std::string kAboutSrcdocUrls[] = {
872 "about:srcdoc", "about:srcdoc/", "about:srcdoc?foo", "about:srcdoc/#foo",
873 "about:srcdoc?foo#foo"};
874 for (const auto& url : kAboutSrcdocUrls)
875 EXPECT_TRUE(GURL(url).IsAboutSrcdoc()) << url;
876
877 const std::string kNotAboutSrcdocUrls[] = {"http:srcdoc",
878 "about:srcdo",
879 "about://srcdoc",
880 "about://srcdoc\\",
881 "about:srcdoc/foo",
882 "about://:8000/srcdoc",
883 "about://foo:foo@/srcdoc",
884 "foo@about:srcdoc",
885 "foo:bar@about:srcdoc",
886 "about:srcdoc:8000",
887 "about:srCDOc"};
888 for (const auto& url : kNotAboutSrcdocUrls)
889 EXPECT_FALSE(GURL(url).IsAboutSrcdoc()) << url;
890}
891
clamy12bca18b2017-02-10 15:33:07892TEST(GURLTest, EqualsIgnoringRef) {
893 const struct {
894 const char* url_a;
895 const char* url_b;
896 bool are_equals;
897 } kTestCases[] = {
898 // No ref.
899 {"http://a.com", "http://a.com", true},
900 {"http://a.com", "http://b.com", false},
901
902 // Same Ref.
903 {"http://a.com#foo", "http://a.com#foo", true},
904 {"http://a.com#foo", "http://b.com#foo", false},
905
906 // Different Refs.
907 {"http://a.com#foo", "http://a.com#bar", true},
908 {"http://a.com#foo", "http://b.com#bar", false},
909
910 // One has a ref, the other doesn't.
911 {"http://a.com#foo", "http://a.com", true},
912 {"http://a.com#foo", "http://b.com", false},
913
914 // Empty refs.
915 {"http://a.com#", "http://a.com#", true},
916 {"http://a.com#", "http://a.com", true},
917
918 // URLs that differ only by their last character.
919 {"http://aaa", "http://aab", false},
920 {"http://aaa#foo", "http://aab#foo", false},
921
922 // Different size of the part before the ref.
923 {"http://123#a", "http://123456#a", false},
924
925 // Blob URLs
926 {"blob:http://a.com#foo", "blob:http://a.com#foo", true},
927 {"blob:http://a.com#foo", "blob:http://a.com#bar", true},
928 {"blob:http://a.com#foo", "blob:http://b.com#bar", false},
929
930 // Filesystem URLs
931 {"filesystem:http://a.com#foo", "filesystem:http://a.com#foo", true},
932 {"filesystem:http://a.com#foo", "filesystem:http://a.com#bar", true},
933 {"filesystem:http://a.com#foo", "filesystem:http://b.com#bar", false},
arthursonzogni313d530f2017-12-13 13:05:29934
935 // Data URLs
936 {"data:text/html,a#foo", "data:text/html,a#bar", true},
937 {"data:text/html,a#foo", "data:text/html,a#foo", true},
938 {"data:text/html,a#foo", "data:text/html,b#foo", false},
clamy12bca18b2017-02-10 15:33:07939 };
940
941 for (const auto& test_case : kTestCases) {
942 SCOPED_TRACE(testing::Message()
943 << std::endl
944 << "url_a = " << test_case.url_a << std::endl
945 << "url_b = " << test_case.url_b << std::endl);
946 // A versus B.
947 EXPECT_EQ(test_case.are_equals,
948 GURL(test_case.url_a).EqualsIgnoringRef(GURL(test_case.url_b)));
949 // B versus A.
950 EXPECT_EQ(test_case.are_equals,
951 GURL(test_case.url_b).EqualsIgnoringRef(GURL(test_case.url_a)));
952 }
953}
954
Lukasz Anforowicz68c21772018-01-13 03:42:44955TEST(GURLTest, DebugAlias) {
956 GURL url("https://foo.com/bar");
957 DEBUG_ALIAS_FOR_GURL(url_debug_alias, url);
958 EXPECT_STREQ("https://foo.com/bar", url_debug_alias);
959}
960
[email protected]0318f922014-04-22 00:09:23961} // namespace url