Remove broken closed push logic in SpdyHttpStream::SendRequest()
[chromium-blink-merge.git] / url / url_parse_unittest.cc
blobf080fb585db14565927b3604c1a2914f9a3b3f68
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "url/url_parse.h"
7 #include "base/basictypes.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "url/url_parse.h"
11 // Some implementations of base/basictypes.h may define ARRAYSIZE.
12 // If it's not defined, we define it to the ARRAYSIZE_UNSAFE macro
13 // which is in our version of basictypes.h.
14 #ifndef ARRAYSIZE
15 #define ARRAYSIZE ARRAYSIZE_UNSAFE
16 #endif
18 // Interesting IE file:isms...
20 // file:/foo/bar file:///foo/bar
21 // The result here seems totally invalid!?!? This isn't UNC.
23 // file:/
24 // file:// or any other number of slashes
25 // IE6 doesn't do anything at all if you click on this link. No error:
26 // nothing. IE6's history system seems to always color this link, so I'm
27 // guessing that it maps internally to the empty URL.
29 // C:\ file:///C:/
30 // / file:///C:/
31 // /foo file:///C:/foo
32 // Interestingly, IE treats "/" as an alias for "c:\", which makes sense,
33 // but is weird to think about on Windows.
35 // file:foo/ file:foo/ (invalid?!?!?)
36 // file:/foo/ file:///foo/ (invalid?!?!?)
37 // file://foo/ file://foo/ (UNC to server "foo")
38 // file:///foo/ file:///foo/ (invalid)
39 // file:////foo/ file://foo/ (UNC to server "foo")
40 // Any more than four slashes is also treated as UNC.
42 // file:C:/ file://C:/
43 // file:/C:/ file://C:/
44 // The number of slashes after "file:" don't matter if the thing following
45 // it looks like an absolute drive path. Also, slashes and backslashes are
46 // equally valid here.
48 namespace url_parse {
49 namespace {
51 // Used for regular URL parse cases.
52 struct URLParseCase {
53 const char* input;
55 const char* scheme;
56 const char* username;
57 const char* password;
58 const char* host;
59 int port;
60 const char* path;
61 const char* query;
62 const char* ref;
65 // Simpler version of URLParseCase for testing path URLs.
66 struct PathURLParseCase {
67 const char* input;
69 const char* scheme;
70 const char* path;
73 // Simpler version of URLParseCase for testing mailto URLs.
74 struct MailtoURLParseCase {
75 const char* input;
77 const char* scheme;
78 const char* path;
79 const char* query;
82 // More complicated version of URLParseCase for testing filesystem URLs.
83 struct FileSystemURLParseCase {
84 const char* input;
86 const char* inner_scheme;
87 const char* inner_username;
88 const char* inner_password;
89 const char* inner_host;
90 int inner_port;
91 const char* inner_path;
92 const char* path;
93 const char* query;
94 const char* ref;
97 bool ComponentMatches(const char* input,
98 const char* reference,
99 const url_parse::Component& component) {
100 // If the component is nonexistant (length == -1), it should begin at 0.
101 EXPECT_TRUE(component.len >= 0 || component.len == -1);
103 // Begin should be valid.
104 EXPECT_LE(0, component.begin);
106 // A NULL reference means the component should be nonexistant.
107 if (!reference)
108 return component.len == -1;
109 if (component.len < 0)
110 return false; // Reference is not NULL but we don't have anything
112 if (strlen(reference) != static_cast<size_t>(component.len))
113 return false; // Lengths don't match
115 // Now check the actual characters.
116 return strncmp(reference, &input[component.begin], component.len) == 0;
119 void ExpectInvalidComponent(const url_parse::Component& component) {
120 EXPECT_EQ(0, component.begin);
121 EXPECT_EQ(-1, component.len);
124 // Parsed ----------------------------------------------------------------------
126 TEST(URLParser, Length) {
127 const char* length_cases[] = {
128 // One with everything in it.
129 "http://user:pass@host:99/foo?bar#baz",
130 // One with nothing in it.
132 // Working backwards, let's start taking off stuff from the full one.
133 "http://user:pass@host:99/foo?bar#",
134 "http://user:pass@host:99/foo?bar",
135 "http://user:pass@host:99/foo?",
136 "http://user:pass@host:99/foo",
137 "http://user:pass@host:99/",
138 "http://user:pass@host:99",
139 "http://user:pass@host:",
140 "http://user:pass@host",
141 "http://host",
142 "http://user@",
143 "http:",
145 for (size_t i = 0; i < arraysize(length_cases); i++) {
146 int true_length = static_cast<int>(strlen(length_cases[i]));
148 url_parse::Parsed parsed;
149 url_parse::ParseStandardURL(length_cases[i], true_length, &parsed);
151 EXPECT_EQ(true_length, parsed.Length());
155 TEST(URLParser, CountCharactersBefore) {
156 struct CountCase {
157 const char* url;
158 Parsed::ComponentType component;
159 bool include_delimiter;
160 int expected_count;
161 } count_cases[] = {
162 // Test each possibility in the case where all components are present.
163 // 0 1 2
164 // 0123456789012345678901
165 {"http://u:p@h:8/p?q#r", Parsed::SCHEME, true, 0},
166 {"http://u:p@h:8/p?q#r", Parsed::SCHEME, false, 0},
167 {"http://u:p@h:8/p?q#r", Parsed::USERNAME, true, 7},
168 {"http://u:p@h:8/p?q#r", Parsed::USERNAME, false, 7},
169 {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, true, 9},
170 {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, false, 9},
171 {"http://u:p@h:8/p?q#r", Parsed::HOST, true, 11},
172 {"http://u:p@h:8/p?q#r", Parsed::HOST, false, 11},
173 {"http://u:p@h:8/p?q#r", Parsed::PORT, true, 12},
174 {"http://u:p@h:8/p?q#r", Parsed::PORT, false, 13},
175 {"http://u:p@h:8/p?q#r", Parsed::PATH, false, 14},
176 {"http://u:p@h:8/p?q#r", Parsed::PATH, true, 14},
177 {"http://u:p@h:8/p?q#r", Parsed::QUERY, true, 16},
178 {"http://u:p@h:8/p?q#r", Parsed::QUERY, false, 17},
179 {"http://u:p@h:8/p?q#r", Parsed::REF, true, 18},
180 {"http://u:p@h:8/p?q#r", Parsed::REF, false, 19},
181 // Now test when the requested component is missing.
182 {"http://u:p@h:8/p?", Parsed::REF, true, 17},
183 {"http://u:p@h:8/p?q", Parsed::REF, true, 18},
184 {"http://u:p@h:8/p#r", Parsed::QUERY, true, 16},
185 {"http://u:p@h:8#r", Parsed::PATH, true, 14},
186 {"http://u:p@h/", Parsed::PORT, true, 12},
187 {"http://u:p@/", Parsed::HOST, true, 11},
188 // This case is a little weird. It will report that the password would
189 // start where the host begins. This is arguably correct, although you
190 // could also argue that it should start at the '@' sign. Doing it
191 // starting with the '@' sign is actually harder, so we don't bother.
192 {"http://u@h/", Parsed::PASSWORD, true, 9},
193 {"http://h/", Parsed::USERNAME, true, 7},
194 {"http:", Parsed::USERNAME, true, 5},
195 {"", Parsed::SCHEME, true, 0},
196 // Make sure a random component still works when there's nothing there.
197 {"", Parsed::REF, true, 0},
198 // File URLs are special with no host, so we test those.
199 {"file:///c:/foo", Parsed::USERNAME, true, 7},
200 {"file:///c:/foo", Parsed::PASSWORD, true, 7},
201 {"file:///c:/foo", Parsed::HOST, true, 7},
202 {"file:///c:/foo", Parsed::PATH, true, 7},
204 for (size_t i = 0; i < ARRAYSIZE(count_cases); i++) {
205 int length = static_cast<int>(strlen(count_cases[i].url));
207 // Simple test to distinguish file and standard URLs.
208 url_parse::Parsed parsed;
209 if (length > 0 && count_cases[i].url[0] == 'f')
210 url_parse::ParseFileURL(count_cases[i].url, length, &parsed);
211 else
212 url_parse::ParseStandardURL(count_cases[i].url, length, &parsed);
214 int chars_before = parsed.CountCharactersBefore(
215 count_cases[i].component, count_cases[i].include_delimiter);
216 EXPECT_EQ(count_cases[i].expected_count, chars_before);
220 // Standard --------------------------------------------------------------------
222 // Input Scheme Usrname Passwd Host Port Path Query Ref
223 // ------------------------------------ ------- ------- ---------- ------------ --- ---------- ------------ -----
224 static URLParseCase cases[] = {
225 // Regular URL with all the parts
226 {"http://user:pass@foo:21/bar;par?b#c", "http", "user", "pass", "foo", 21, "/bar;par","b", "c"},
228 // Known schemes should lean towards authority identification
229 {"http:foo.com", "http", NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
231 // Spaces!
232 {"\t :foo.com \n", "", NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
233 {" foo.com ", NULL, NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
234 {"a:\t foo.com", "a", NULL, NULL, "\t foo.com", -1, NULL, NULL, NULL},
235 {"http://f:21/ b ? d # e ", "http", NULL, NULL, "f", 21, "/ b ", " d ", " e"},
237 // Invalid port numbers should be identified and turned into -2, empty port
238 // numbers should be -1. Spaces aren't allowed in port numbers
239 {"http://f:/c", "http", NULL, NULL, "f", -1, "/c", NULL, NULL},
240 {"http://f:0/c", "http", NULL, NULL, "f", 0, "/c", NULL, NULL},
241 {"http://f:00000000000000/c", "http", NULL, NULL, "f", 0, "/c", NULL, NULL},
242 {"http://f:00000000000000000000080/c", "http", NULL, NULL, "f", 80, "/c", NULL, NULL},
243 {"http://f:b/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
244 {"http://f: /c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
245 {"http://f:\n/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
246 {"http://f:fifty-two/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
247 {"http://f:999999/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
248 {"http://f: 21 / b ? d # e ", "http", NULL, NULL, "f", -2, "/ b ", " d ", " e"},
250 // Creative URLs missing key elements
251 {"", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
252 {" \t", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
253 {":foo.com/", "", NULL, NULL, "foo.com", -1, "/", NULL, NULL},
254 {":foo.com\\", "", NULL, NULL, "foo.com", -1, "\\", NULL, NULL},
255 {":", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
256 {":a", "", NULL, NULL, "a", -1, NULL, NULL, NULL},
257 {":/", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
258 {":\\", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
259 {":#", "", NULL, NULL, NULL, -1, NULL, NULL, ""},
260 {"#", NULL, NULL, NULL, NULL, -1, NULL, NULL, ""},
261 {"#/", NULL, NULL, NULL, NULL, -1, NULL, NULL, "/"},
262 {"#\\", NULL, NULL, NULL, NULL, -1, NULL, NULL, "\\"},
263 {"#;?", NULL, NULL, NULL, NULL, -1, NULL, NULL, ";?"},
264 {"?", NULL, NULL, NULL, NULL, -1, NULL, "", NULL},
265 {"/", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
266 {":23", "", NULL, NULL, "23", -1, NULL, NULL, NULL},
267 {"/:23", "/", NULL, NULL, "23", -1, NULL, NULL, NULL},
268 {"//", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
269 {"::", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
270 {"::23", "", NULL, NULL, NULL, 23, NULL, NULL, NULL},
271 {"foo://", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
273 // Username/passwords and things that look like them
274 {"http://a:b@c:29/d", "http", "a", "b", "c", 29, "/d", NULL, NULL},
275 {"http::@c:29", "http", "", "", "c", 29, NULL, NULL, NULL},
276 // ... "]" in the password field isn't allowed, but we tolerate it here...
277 {"http://&a:foo(b]c@d:2/", "http", "&a", "foo(b]c", "d", 2, "/", NULL, NULL},
278 {"http://::@c@d:2", "http", "", ":@c", "d", 2, NULL, NULL, NULL},
279 {"http://foo.com:b@d/", "http", "foo.com", "b", "d", -1, "/", NULL, NULL},
281 {"http://foo.com/\\@", "http", NULL, NULL, "foo.com", -1, "/\\@", NULL, NULL},
282 {"http:\\\\foo.com\\", "http", NULL, NULL, "foo.com", -1, "\\", NULL, NULL},
283 {"http:\\\\a\\b:c\\d@foo.com\\", "http", NULL, NULL, "a", -1, "\\b:c\\d@foo.com\\", NULL, NULL},
285 // Tolerate different numbers of slashes.
286 {"foo:/", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
287 {"foo:/bar.com/", "foo", NULL, NULL, "bar.com", -1, "/", NULL, NULL},
288 {"foo://///////", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
289 {"foo://///////bar.com/", "foo", NULL, NULL, "bar.com", -1, "/", NULL, NULL},
290 {"foo:////://///", "foo", NULL, NULL, NULL, -1, "/////", NULL, NULL},
292 // Raw file paths on Windows aren't handled by the parser.
293 {"c:/foo", "c", NULL, NULL, "foo", -1, NULL, NULL, NULL},
294 {"//foo/bar", NULL, NULL, NULL, "foo", -1, "/bar", NULL, NULL},
296 // Use the first question mark for the query and the ref.
297 {"http://foo/path;a??e#f#g", "http", NULL, NULL, "foo", -1, "/path;a", "?e", "f#g"},
298 {"http://foo/abcd?efgh?ijkl", "http", NULL, NULL, "foo", -1, "/abcd", "efgh?ijkl", NULL},
299 {"http://foo/abcd#foo?bar", "http", NULL, NULL, "foo", -1, "/abcd", NULL, "foo?bar"},
301 // IPv6, check also interesting uses of colons.
302 {"[61:24:74]:98", "[61", NULL, NULL, "24:74]", 98, NULL, NULL, NULL},
303 {"http://[61:27]:98", "http", NULL, NULL, "[61:27]", 98, NULL, NULL, NULL},
304 {"http:[61:27]/:foo", "http", NULL, NULL, "[61:27]", -1, "/:foo", NULL, NULL},
305 {"http://[1::2]:3:4", "http", NULL, NULL, "[1::2]:3", 4, NULL, NULL, NULL},
307 // Partially-complete IPv6 literals, and related cases.
308 {"http://2001::1", "http", NULL, NULL, "2001:", 1, NULL, NULL, NULL},
309 {"http://[2001::1", "http", NULL, NULL, "[2001::1", -1, NULL, NULL, NULL},
310 {"http://2001::1]", "http", NULL, NULL, "2001::1]", -1, NULL, NULL, NULL},
311 {"http://2001::1]:80", "http", NULL, NULL, "2001::1]", 80, NULL, NULL, NULL},
312 {"http://[2001::1]", "http", NULL, NULL, "[2001::1]", -1, NULL, NULL, NULL},
313 {"http://[2001::1]:80", "http", NULL, NULL, "[2001::1]", 80, NULL, NULL, NULL},
314 {"http://[[::]]", "http", NULL, NULL, "[[::]]", -1, NULL, NULL, NULL},
318 TEST(URLParser, Standard) {
319 // Declared outside for loop to try to catch cases in init() where we forget
320 // to reset something that is reset by the constructor.
321 url_parse::Parsed parsed;
322 for (size_t i = 0; i < arraysize(cases); i++) {
323 const char* url = cases[i].input;
324 url_parse::ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
325 int port = url_parse::ParsePort(url, parsed.port);
327 EXPECT_TRUE(ComponentMatches(url, cases[i].scheme, parsed.scheme));
328 EXPECT_TRUE(ComponentMatches(url, cases[i].username, parsed.username));
329 EXPECT_TRUE(ComponentMatches(url, cases[i].password, parsed.password));
330 EXPECT_TRUE(ComponentMatches(url, cases[i].host, parsed.host));
331 EXPECT_EQ(cases[i].port, port);
332 EXPECT_TRUE(ComponentMatches(url, cases[i].path, parsed.path));
333 EXPECT_TRUE(ComponentMatches(url, cases[i].query, parsed.query));
334 EXPECT_TRUE(ComponentMatches(url, cases[i].ref, parsed.ref));
338 // PathURL --------------------------------------------------------------------
340 // Various incarnations of path URLs.
341 static PathURLParseCase path_cases[] = {
342 {"", NULL, NULL},
343 {":", "", NULL},
344 {":/", "", "/"},
345 {"/", NULL, "/"},
346 {" This is \\interesting// \t", NULL, "This is \\interesting// \t"},
347 {"about:", "about", NULL},
348 {"about:blank", "about", "blank"},
349 {" about: blank ", "about", " blank "},
350 {"javascript :alert(\"He:/l\\l#o?foo\"); ", "javascript ", "alert(\"He:/l\\l#o?foo\"); "},
353 TEST(URLParser, PathURL) {
354 // Declared outside for loop to try to catch cases in init() where we forget
355 // to reset something that is reset by the construtor.
356 url_parse::Parsed parsed;
357 for (size_t i = 0; i < arraysize(path_cases); i++) {
358 const char* url = path_cases[i].input;
359 url_parse::ParsePathURL(url, static_cast<int>(strlen(url)), false, &parsed);
361 EXPECT_TRUE(ComponentMatches(url, path_cases[i].scheme, parsed.scheme))
362 << i;
363 EXPECT_TRUE(ComponentMatches(url, path_cases[i].path, parsed.GetContent()))
364 << i;
366 // The remaining components are never used for path urls.
367 ExpectInvalidComponent(parsed.username);
368 ExpectInvalidComponent(parsed.password);
369 ExpectInvalidComponent(parsed.host);
370 ExpectInvalidComponent(parsed.port);
374 // Various incarnations of file URLs.
375 static URLParseCase file_cases[] = {
376 #ifdef WIN32
377 {"file:server", "file", NULL, NULL, "server", -1, NULL, NULL, NULL},
378 {" file: server \t", "file", NULL, NULL, " server",-1, NULL, NULL, NULL},
379 {"FiLe:c|", "FiLe", NULL, NULL, NULL, -1, "c|", NULL, NULL},
380 {"FILE:/\\\\/server/file", "FILE", NULL, NULL, "server", -1, "/file", NULL, NULL},
381 {"file://server/", "file", NULL, NULL, "server", -1, "/", NULL, NULL},
382 {"file://localhost/c:/", "file", NULL, NULL, NULL, -1, "/c:/", NULL, NULL},
383 {"file://127.0.0.1/c|\\", "file", NULL, NULL, NULL, -1, "/c|\\", NULL, NULL},
384 {"file:/", "file", NULL, NULL, NULL, -1, NULL, NULL, NULL},
385 {"file:", "file", NULL, NULL, NULL, -1, NULL, NULL, NULL},
386 // If there is a Windows drive letter, treat any number of slashes as the
387 // path part.
388 {"file:c:\\fo\\b", "file", NULL, NULL, NULL, -1, "c:\\fo\\b", NULL, NULL},
389 {"file:/c:\\foo/bar", "file", NULL, NULL, NULL, -1, "/c:\\foo/bar",NULL, NULL},
390 {"file://c:/f\\b", "file", NULL, NULL, NULL, -1, "/c:/f\\b", NULL, NULL},
391 {"file:///C:/foo", "file", NULL, NULL, NULL, -1, "/C:/foo", NULL, NULL},
392 {"file://///\\/\\/c:\\f\\b", "file", NULL, NULL, NULL, -1, "/c:\\f\\b", NULL, NULL},
393 // If there is not a drive letter, we should treat is as UNC EXCEPT for
394 // three slashes, which we treat as a Unix style path.
395 {"file:server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
396 {"file:/server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
397 {"file://server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
398 {"file:///server/file", "file", NULL, NULL, NULL, -1, "/server/file",NULL, NULL},
399 {"file://\\server/file", "file", NULL, NULL, NULL, -1, "\\server/file",NULL, NULL},
400 {"file:////server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
401 // Queries and refs are valid for file URLs as well.
402 {"file:///C:/foo.html?#", "file", NULL, NULL, NULL, -1, "/C:/foo.html", "", ""},
403 {"file:///C:/foo.html?query=yes#ref", "file", NULL, NULL, NULL, -1, "/C:/foo.html", "query=yes", "ref"},
404 #else // WIN32
405 // No slashes.
406 {"file:", "file", NULL, NULL, NULL, -1, NULL, NULL, NULL},
407 {"file:path", "file", NULL, NULL, NULL, -1, "path", NULL, NULL},
408 {"file:path/", "file", NULL, NULL, NULL, -1, "path/", NULL, NULL},
409 {"file:path/f.txt", "file", NULL, NULL, NULL, -1, "path/f.txt", NULL, NULL},
410 // One slash.
411 {"file:/", "file", NULL, NULL, NULL, -1, "/", NULL, NULL},
412 {"file:/path", "file", NULL, NULL, NULL, -1, "/path", NULL, NULL},
413 {"file:/path/", "file", NULL, NULL, NULL, -1, "/path/", NULL, NULL},
414 {"file:/path/f.txt", "file", NULL, NULL, NULL, -1, "/path/f.txt", NULL, NULL},
415 // Two slashes.
416 {"file://", "file", NULL, NULL, NULL, -1, NULL, NULL, NULL},
417 {"file://server", "file", NULL, NULL, "server", -1, NULL, NULL, NULL},
418 {"file://server/", "file", NULL, NULL, "server", -1, "/", NULL, NULL},
419 {"file://server/f.txt", "file", NULL, NULL, "server", -1, "/f.txt", NULL, NULL},
420 // Three slashes.
421 {"file:///", "file", NULL, NULL, NULL, -1, "/", NULL, NULL},
422 {"file:///path", "file", NULL, NULL, NULL, -1, "/path", NULL, NULL},
423 {"file:///path/", "file", NULL, NULL, NULL, -1, "/path/", NULL, NULL},
424 {"file:///path/f.txt", "file", NULL, NULL, NULL, -1, "/path/f.txt", NULL, NULL},
425 // More than three slashes.
426 {"file:////", "file", NULL, NULL, NULL, -1, "/", NULL, NULL},
427 {"file:////path", "file", NULL, NULL, NULL, -1, "/path", NULL, NULL},
428 {"file:////path/", "file", NULL, NULL, NULL, -1, "/path/", NULL, NULL},
429 {"file:////path/f.txt", "file", NULL, NULL, NULL, -1, "/path/f.txt", NULL, NULL},
430 // Schemeless URLs
431 {"path/f.txt", NULL, NULL, NULL, NULL, -1, "path/f.txt", NULL, NULL},
432 {"path:80/f.txt", "path", NULL, NULL, NULL, -1, "80/f.txt", NULL, NULL},
433 {"path/f.txt:80", "path/f.txt",NULL, NULL, NULL, -1, "80", NULL, NULL}, // Wrong.
434 {"/path/f.txt", NULL, NULL, NULL, NULL, -1, "/path/f.txt", NULL, NULL},
435 {"/path:80/f.txt", NULL, NULL, NULL, NULL, -1, "/path:80/f.txt",NULL, NULL},
436 {"/path/f.txt:80", NULL, NULL, NULL, NULL, -1, "/path/f.txt:80",NULL, NULL},
437 {"//server/f.txt", NULL, NULL, NULL, "server", -1, "/f.txt", NULL, NULL},
438 {"//server:80/f.txt", NULL, NULL, NULL, "server:80",-1, "/f.txt", NULL, NULL},
439 {"//server/f.txt:80", NULL, NULL, NULL, "server", -1, "/f.txt:80", NULL, NULL},
440 {"///path/f.txt", NULL, NULL, NULL, NULL, -1, "/path/f.txt", NULL, NULL},
441 {"///path:80/f.txt", NULL, NULL, NULL, NULL, -1, "/path:80/f.txt",NULL, NULL},
442 {"///path/f.txt:80", NULL, NULL, NULL, NULL, -1, "/path/f.txt:80",NULL, NULL},
443 {"////path/f.txt", NULL, NULL, NULL, NULL, -1, "/path/f.txt", NULL, NULL},
444 {"////path:80/f.txt", NULL, NULL, NULL, NULL, -1, "/path:80/f.txt",NULL, NULL},
445 {"////path/f.txt:80", NULL, NULL, NULL, NULL, -1, "/path/f.txt:80",NULL, NULL},
446 // Queries and refs are valid for file URLs as well.
447 {"file:///foo.html?#", "file", NULL, NULL, NULL, -1, "/foo.html", "", ""},
448 {"file:///foo.html?q=y#ref", "file", NULL, NULL, NULL, -1, "/foo.html", "q=y", "ref"},
449 #endif // WIN32
452 TEST(URLParser, ParseFileURL) {
453 // Declared outside for loop to try to catch cases in init() where we forget
454 // to reset something that is reset by the construtor.
455 url_parse::Parsed parsed;
456 for (size_t i = 0; i < arraysize(file_cases); i++) {
457 const char* url = file_cases[i].input;
458 url_parse::ParseFileURL(url, static_cast<int>(strlen(url)), &parsed);
459 int port = url_parse::ParsePort(url, parsed.port);
461 EXPECT_TRUE(ComponentMatches(url, file_cases[i].scheme, parsed.scheme))
462 << " for case #" << i << " [" << url << "] "
463 << parsed.scheme.begin << ", " << parsed.scheme.len;
465 EXPECT_TRUE(ComponentMatches(url, file_cases[i].username, parsed.username))
466 << " for case #" << i << " [" << url << "] "
467 << parsed.username.begin << ", " << parsed.username.len;
469 EXPECT_TRUE(ComponentMatches(url, file_cases[i].password, parsed.password))
470 << " for case #" << i << " [" << url << "] "
471 << parsed.password.begin << ", " << parsed.password.len;
473 EXPECT_TRUE(ComponentMatches(url, file_cases[i].host, parsed.host))
474 << " for case #" << i << " [" << url << "] "
475 << parsed.host.begin << ", " << parsed.host.len;
477 EXPECT_EQ(file_cases[i].port, port)
478 << " for case #" << i << " [ " << url << "] " << port;
480 EXPECT_TRUE(ComponentMatches(url, file_cases[i].path, parsed.path))
481 << " for case #" << i << " [" << url << "] "
482 << parsed.path.begin << ", " << parsed.path.len;
484 EXPECT_TRUE(ComponentMatches(url, file_cases[i].query, parsed.query))
485 << " for case #" << i << " [" << url << "] "
486 << parsed.query.begin << ", " << parsed.query.len;
488 EXPECT_TRUE(ComponentMatches(url, file_cases[i].ref, parsed.ref))
489 << " for case #" << i << " [ "<< url << "] "
490 << parsed.query.begin << ", " << parsed.scheme.len;
495 TEST(URLParser, ExtractFileName) {
496 struct FileCase {
497 const char* input;
498 const char* expected;
499 } file_cases[] = {
500 {"http://www.google.com", NULL},
501 {"http://www.google.com/", ""},
502 {"http://www.google.com/search", "search"},
503 {"http://www.google.com/search/", ""},
504 {"http://www.google.com/foo/bar.html?baz=22", "bar.html"},
505 {"http://www.google.com/foo/bar.html#ref", "bar.html"},
506 {"http://www.google.com/search/;param", ""},
507 {"http://www.google.com/foo/bar.html;param#ref", "bar.html"},
508 {"http://www.google.com/foo/bar.html;foo;param#ref", "bar.html;foo"},
509 {"http://www.google.com/foo/bar.html?query#ref", "bar.html"},
512 for (size_t i = 0; i < ARRAYSIZE(file_cases); i++) {
513 const char* url = file_cases[i].input;
514 int len = static_cast<int>(strlen(url));
516 url_parse::Parsed parsed;
517 url_parse::ParseStandardURL(url, len, &parsed);
519 url_parse::Component file_name;
520 url_parse::ExtractFileName(url, parsed.path, &file_name);
522 EXPECT_TRUE(ComponentMatches(url, file_cases[i].expected, file_name));
526 // Returns true if the parameter with index |parameter| in the given URL's
527 // query string. The expected key can be NULL to indicate no such key index
528 // should exist. The parameter number is 1-based.
529 static bool NthParameterIs(const char* url,
530 int parameter,
531 const char* expected_key,
532 const char* expected_value) {
533 url_parse::Parsed parsed;
534 url_parse::ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
536 url_parse::Component query = parsed.query;
538 for (int i = 1; i <= parameter; i++) {
539 url_parse::Component key, value;
540 if (!url_parse::ExtractQueryKeyValue(url, &query, &key, &value)) {
541 if (parameter >= i && !expected_key)
542 return true; // Expected nonexistant key, got one.
543 return false; // Not enough keys.
546 if (i == parameter) {
547 if (!expected_key)
548 return false;
550 if (strncmp(&url[key.begin], expected_key, key.len) != 0)
551 return false;
552 if (strncmp(&url[value.begin], expected_value, value.len) != 0)
553 return false;
554 return true;
557 return expected_key == NULL; // We didn't find that many parameters.
560 TEST(URLParser, ExtractQueryKeyValue) {
561 EXPECT_TRUE(NthParameterIs("http://www.google.com", 1, NULL, NULL));
563 // Basic case.
564 char a[] = "http://www.google.com?arg1=1&arg2=2&bar";
565 EXPECT_TRUE(NthParameterIs(a, 1, "arg1", "1"));
566 EXPECT_TRUE(NthParameterIs(a, 2, "arg2", "2"));
567 EXPECT_TRUE(NthParameterIs(a, 3, "bar", ""));
568 EXPECT_TRUE(NthParameterIs(a, 4, NULL, NULL));
570 // Empty param at the end.
571 char b[] = "http://www.google.com?foo=bar&";
572 EXPECT_TRUE(NthParameterIs(b, 1, "foo", "bar"));
573 EXPECT_TRUE(NthParameterIs(b, 2, NULL, NULL));
575 // Empty param at the beginning.
576 char c[] = "http://www.google.com?&foo=bar";
577 EXPECT_TRUE(NthParameterIs(c, 1, "", ""));
578 EXPECT_TRUE(NthParameterIs(c, 2, "foo", "bar"));
579 EXPECT_TRUE(NthParameterIs(c, 3, NULL, NULL));
581 // Empty key with value.
582 char d[] = "http://www.google.com?=foo";
583 EXPECT_TRUE(NthParameterIs(d, 1, "", "foo"));
584 EXPECT_TRUE(NthParameterIs(d, 2, NULL, NULL));
586 // Empty value with key.
587 char e[] = "http://www.google.com?foo=";
588 EXPECT_TRUE(NthParameterIs(e, 1, "foo", ""));
589 EXPECT_TRUE(NthParameterIs(e, 2, NULL, NULL));
591 // Empty key and values.
592 char f[] = "http://www.google.com?&&==&=";
593 EXPECT_TRUE(NthParameterIs(f, 1, "", ""));
594 EXPECT_TRUE(NthParameterIs(f, 2, "", ""));
595 EXPECT_TRUE(NthParameterIs(f, 3, "", "="));
596 EXPECT_TRUE(NthParameterIs(f, 4, "", ""));
597 EXPECT_TRUE(NthParameterIs(f, 5, NULL, NULL));
600 // MailtoURL --------------------------------------------------------------------
602 static MailtoURLParseCase mailto_cases[] = {
603 //|input |scheme |path |query
604 {"mailto:foo@gmail.com", "mailto", "foo@gmail.com", NULL},
605 {" mailto: to \t", "mailto", " to", NULL},
606 {"mailto:addr1%2C%20addr2 ", "mailto", "addr1%2C%20addr2", NULL},
607 {"Mailto:addr1, addr2 ", "Mailto", "addr1, addr2", NULL},
608 {"mailto:addr1:addr2 ", "mailto", "addr1:addr2", NULL},
609 {"mailto:?to=addr1,addr2", "mailto", NULL, "to=addr1,addr2"},
610 {"mailto:?to=addr1%2C%20addr2", "mailto", NULL, "to=addr1%2C%20addr2"},
611 {"mailto:addr1?to=addr2", "mailto", "addr1", "to=addr2"},
612 {"mailto:?body=#foobar#", "mailto", NULL, "body=#foobar#",},
613 {"mailto:#?body=#foobar#", "mailto", "#", "body=#foobar#"},
616 TEST(URLParser, MailtoUrl) {
617 // Declared outside for loop to try to catch cases in init() where we forget
618 // to reset something that is reset by the construtor.
619 url_parse::Parsed parsed;
620 for (size_t i = 0; i < arraysize(mailto_cases); ++i) {
621 const char* url = mailto_cases[i].input;
622 url_parse::ParseMailtoURL(url, static_cast<int>(strlen(url)), &parsed);
623 int port = url_parse::ParsePort(url, parsed.port);
625 EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].scheme, parsed.scheme));
626 EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].path, parsed.path));
627 EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].query, parsed.query));
628 EXPECT_EQ(url_parse::PORT_UNSPECIFIED, port);
630 // The remaining components are never used for mailto urls.
631 ExpectInvalidComponent(parsed.username);
632 ExpectInvalidComponent(parsed.password);
633 ExpectInvalidComponent(parsed.port);
634 ExpectInvalidComponent(parsed.ref);
638 // Various incarnations of filesystem URLs.
639 static FileSystemURLParseCase filesystem_cases[] = {
640 // Regular URL with all the parts
641 {"filesystem:http://user:pass@foo:21/temporary/bar;par?b#c", "http", "user", "pass", "foo", 21, "/temporary", "/bar;par", "b", "c"},
642 {"filesystem:https://foo/persistent/bar;par/", "https", NULL, NULL, "foo", -1, "/persistent", "/bar;par/", NULL, NULL},
643 {"filesystem:file:///persistent/bar;par/", "file", NULL, NULL, NULL, -1, "/persistent", "/bar;par/", NULL, NULL},
644 {"filesystem:file:///persistent/bar;par/?query#ref", "file", NULL, NULL, NULL, -1, "/persistent", "/bar;par/", "query", "ref"},
645 {"filesystem:file:///persistent", "file", NULL, NULL, NULL, -1, "/persistent", "", NULL, NULL},
648 TEST(URLParser, FileSystemURL) {
649 // Declared outside for loop to try to catch cases in init() where we forget
650 // to reset something that is reset by the construtor.
651 url_parse::Parsed parsed;
652 for (size_t i = 0; i < arraysize(filesystem_cases); i++) {
653 const FileSystemURLParseCase* parsecase = &filesystem_cases[i];
654 const char* url = parsecase->input;
655 url_parse::ParseFileSystemURL(url, static_cast<int>(strlen(url)), &parsed);
657 EXPECT_TRUE(ComponentMatches(url, "filesystem", parsed.scheme));
658 EXPECT_EQ(!parsecase->inner_scheme, !parsed.inner_parsed());
659 // Only check the inner_parsed if there is one.
660 if (parsed.inner_parsed()) {
661 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_scheme,
662 parsed.inner_parsed()->scheme));
663 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_username,
664 parsed.inner_parsed()->username));
665 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_password,
666 parsed.inner_parsed()->password));
667 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_host,
668 parsed.inner_parsed()->host));
669 int port = url_parse::ParsePort(url, parsed.inner_parsed()->port);
670 EXPECT_EQ(parsecase->inner_port, port);
672 // The remaining components are never used for filesystem urls.
673 ExpectInvalidComponent(parsed.inner_parsed()->query);
674 ExpectInvalidComponent(parsed.inner_parsed()->ref);
677 EXPECT_TRUE(ComponentMatches(url, parsecase->path, parsed.path));
678 EXPECT_TRUE(ComponentMatches(url, parsecase->query, parsed.query));
679 EXPECT_TRUE(ComponentMatches(url, parsecase->ref, parsed.ref));
681 // The remaining components are never used for filesystem urls.
682 ExpectInvalidComponent(parsed.username);
683 ExpectInvalidComponent(parsed.password);
684 ExpectInvalidComponent(parsed.host);
685 ExpectInvalidComponent(parsed.port);
689 } // namespace
690 } // namespace url_parse