1
// Copyright 2007, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
8
// * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// * Redistributions in binary form must reproduce the above
11
// copyright notice, this list of conditions and the following disclaimer
12
// in the documentation and/or other materials provided with the
14
// * Neither the name of Google Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from
16
// this software without specific prior written permission.
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
#include "base/basictypes.h"
31
#include "googleurl/src/url_parse.h"
32
#include "testing/gtest/include/gtest/gtest.h"
34
// Some implementations of base/basictypes.h may define ARRAYSIZE.
35
// If it's not defined, we define it to the ARRAYSIZE_UNSAFE macro
36
// which is in our version of basictypes.h.
38
#define ARRAYSIZE ARRAYSIZE_UNSAFE
41
// Interesting IE file:isms...
43
// file:/foo/bar file:///foo/bar
44
// The result here seems totally invalid!?!? This isn't UNC.
47
// file:// or any other number of slashes
48
// IE6 doesn't do anything at all if you click on this link. No error:
49
// nothing. IE6's history system seems to always color this link, so I'm
50
// guessing that it maps internally to the empty URL.
54
// /foo file:///C:/foo
55
// Interestingly, IE treats "/" as an alias for "c:\", which makes sense,
56
// but is weird to think about on Windows.
58
// file:foo/ file:foo/ (invalid?!?!?)
59
// file:/foo/ file:///foo/ (invalid?!?!?)
60
// file://foo/ file://foo/ (UNC to server "foo")
61
// file:///foo/ file:///foo/ (invalid)
62
// file:////foo/ file://foo/ (UNC to server "foo")
63
// Any more than four slashes is also treated as UNC.
65
// file:C:/ file://C:/
66
// file:/C:/ file://C:/
67
// The number of slashes after "file:" don't matter if the thing following
68
// it looks like an absolute drive path. Also, slashes and backslashes are
69
// equally valid here.
73
// Used for regular URL parse cases.
87
// Simpler version of URLParseCase for testing path URLs.
88
struct PathURLParseCase {
95
// Simpler version of URLParseCase for testing mailto URLs.
96
struct MailtoURLParseCase {
105
bool ComponentMatches(const char* input,
106
const char* reference,
107
const url_parse::Component& component) {
108
// If the component is nonexistant (length == -1), it should begin at 0.
109
EXPECT_TRUE(component.len >= 0 || component.len == -1);
111
// Begin should be valid.
112
EXPECT_LE(0, component.begin);
114
// A NULL reference means the component should be nonexistant.
116
return component.len == -1;
117
if (component.len < 0)
118
return false; // Reference is not NULL but we don't have anything
120
if (strlen(reference) != static_cast<size_t>(component.len))
121
return false; // Lengths don't match
123
// Now check the actual characters.
124
return strncmp(reference, &input[component.begin], component.len) == 0;
127
void ExpectInvalidComponent(const url_parse::Component& component) {
128
EXPECT_EQ(0, component.begin);
129
EXPECT_EQ(-1, component.len);
134
// Parsed ----------------------------------------------------------------------
136
TEST(URLParser, Length) {
137
const char* length_cases[] = {
138
// One with everything in it.
139
"http://user:pass@host:99/foo?bar#baz",
140
// One with nothing in it.
142
// Working backwards, let's start taking off stuff from the full one.
143
"http://user:pass@host:99/foo?bar#",
144
"http://user:pass@host:99/foo?bar",
145
"http://user:pass@host:99/foo?",
146
"http://user:pass@host:99/foo",
147
"http://user:pass@host:99/",
148
"http://user:pass@host:99",
149
"http://user:pass@host:",
150
"http://user:pass@host",
155
for (size_t i = 0; i < arraysize(length_cases); i++) {
156
int true_length = static_cast<int>(strlen(length_cases[i]));
158
url_parse::Parsed parsed;
159
url_parse::ParseStandardURL(length_cases[i], true_length, &parsed);
161
EXPECT_EQ(true_length, parsed.Length());
165
TEST(URLParser, CountCharactersBefore) {
166
using namespace url_parse;
169
Parsed::ComponentType component;
170
bool include_delimiter;
173
// Test each possibility in the case where all components are present.
175
// 0123456789012345678901
176
{"http://u:p@h:8/p?q#r", Parsed::SCHEME, true, 0},
177
{"http://u:p@h:8/p?q#r", Parsed::SCHEME, false, 0},
178
{"http://u:p@h:8/p?q#r", Parsed::USERNAME, true, 7},
179
{"http://u:p@h:8/p?q#r", Parsed::USERNAME, false, 7},
180
{"http://u:p@h:8/p?q#r", Parsed::PASSWORD, true, 9},
181
{"http://u:p@h:8/p?q#r", Parsed::PASSWORD, false, 9},
182
{"http://u:p@h:8/p?q#r", Parsed::HOST, true, 11},
183
{"http://u:p@h:8/p?q#r", Parsed::HOST, false, 11},
184
{"http://u:p@h:8/p?q#r", Parsed::PORT, true, 12},
185
{"http://u:p@h:8/p?q#r", Parsed::PORT, false, 13},
186
{"http://u:p@h:8/p?q#r", Parsed::PATH, false, 14},
187
{"http://u:p@h:8/p?q#r", Parsed::PATH, true, 14},
188
{"http://u:p@h:8/p?q#r", Parsed::QUERY, true, 16},
189
{"http://u:p@h:8/p?q#r", Parsed::QUERY, false, 17},
190
{"http://u:p@h:8/p?q#r", Parsed::REF, true, 18},
191
{"http://u:p@h:8/p?q#r", Parsed::REF, false, 19},
192
// Now test when the requested component is missing.
193
{"http://u:p@h:8/p?", Parsed::REF, true, 17},
194
{"http://u:p@h:8/p?q", Parsed::REF, true, 18},
195
{"http://u:p@h:8/p#r", Parsed::QUERY, true, 16},
196
{"http://u:p@h:8#r", Parsed::PATH, true, 14},
197
{"http://u:p@h/", Parsed::PORT, true, 12},
198
{"http://u:p@/", Parsed::HOST, true, 11},
199
// This case is a little weird. It will report that the password would
200
// start where the host begins. This is arguably correct, although you
201
// could also argue that it should start at the '@' sign. Doing it
202
// starting with the '@' sign is actually harder, so we don't bother.
203
{"http://u@h/", Parsed::PASSWORD, true, 9},
204
{"http://h/", Parsed::USERNAME, true, 7},
205
{"http:", Parsed::USERNAME, true, 5},
206
{"", Parsed::SCHEME, true, 0},
207
// Make sure a random component still works when there's nothing there.
208
{"", Parsed::REF, true, 0},
209
// File URLs are special with no host, so we test those.
210
{"file:///c:/foo", Parsed::USERNAME, true, 7},
211
{"file:///c:/foo", Parsed::PASSWORD, true, 7},
212
{"file:///c:/foo", Parsed::HOST, true, 7},
213
{"file:///c:/foo", Parsed::PATH, true, 7},
215
for (size_t i = 0; i < ARRAYSIZE(count_cases); i++) {
216
int length = static_cast<int>(strlen(count_cases[i].url));
218
// Simple test to distinguish file and standard URLs.
219
url_parse::Parsed parsed;
220
if (length > 0 && count_cases[i].url[0] == 'f')
221
url_parse::ParseFileURL(count_cases[i].url, length, &parsed);
223
url_parse::ParseStandardURL(count_cases[i].url, length, &parsed);
225
int chars_before = parsed.CountCharactersBefore(
226
count_cases[i].component, count_cases[i].include_delimiter);
227
EXPECT_EQ(count_cases[i].expected_count, chars_before);
231
// Standard --------------------------------------------------------------------
233
// Input Scheme Usrname Passwd Host Port Path Query Ref
234
// ------------------------------------ ------- ------- ---------- ------------ --- ---------- ------------ -----
235
static URLParseCase cases[] = {
236
// Regular URL with all the parts
237
{"http://user:pass@foo:21/bar;par?b#c", "http", "user", "pass", "foo", 21, "/bar;par","b", "c"},
239
// Known schemes should lean towards authority identification
240
{"http:foo.com", "http", NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
243
{"\t :foo.com \n", "", NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
244
{" foo.com ", NULL, NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
245
{"a:\t foo.com", "a", NULL, NULL, "\t foo.com", -1, NULL, NULL, NULL},
246
{"http://f:21/ b ? d # e ", "http", NULL, NULL, "f", 21, "/ b ", " d ", " e"},
248
// Invalid port numbers should be identified and turned into -2, empty port
249
// numbers should be -1. Spaces aren't allowed in port numbers
250
{"http://f:/c", "http", NULL, NULL, "f", -1, "/c", NULL, NULL},
251
{"http://f:0/c", "http", NULL, NULL, "f", 0, "/c", NULL, NULL},
252
{"http://f:00000000000000/c", "http", NULL, NULL, "f", 0, "/c", NULL, NULL},
253
{"http://f:00000000000000000000080/c", "http", NULL, NULL, "f", 80, "/c", NULL, NULL},
254
{"http://f:b/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
255
{"http://f: /c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
256
{"http://f:\n/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
257
{"http://f:fifty-two/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
258
{"http://f:999999/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
259
{"http://f: 21 / b ? d # e ", "http", NULL, NULL, "f", -2, "/ b ", " d ", " e"},
261
// Creative URLs missing key elements
262
{"", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
263
{" \t", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
264
{":foo.com/", "", NULL, NULL, "foo.com", -1, "/", NULL, NULL},
265
{":foo.com\\", "", NULL, NULL, "foo.com", -1, "\\", NULL, NULL},
266
{":", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
267
{":a", "", NULL, NULL, "a", -1, NULL, NULL, NULL},
268
{":/", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
269
{":\\", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
270
{":#", "", NULL, NULL, NULL, -1, NULL, NULL, ""},
271
{"#", NULL, NULL, NULL, NULL, -1, NULL, NULL, ""},
272
{"#/", NULL, NULL, NULL, NULL, -1, NULL, NULL, "/"},
273
{"#\\", NULL, NULL, NULL, NULL, -1, NULL, NULL, "\\"},
274
{"#;?", NULL, NULL, NULL, NULL, -1, NULL, NULL, ";?"},
275
{"?", NULL, NULL, NULL, NULL, -1, NULL, "", NULL},
276
{"/", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
277
{":23", "", NULL, NULL, "23", -1, NULL, NULL, NULL},
278
{"/:23", "/", NULL, NULL, "23", -1, NULL, NULL, NULL},
279
{"//", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
280
{"::", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
281
{"::23", "", NULL, NULL, NULL, 23, NULL, NULL, NULL},
282
{"foo://", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
284
// Username/passwords and things that look like them
285
{"http://a:b@c:29/d", "http", "a", "b", "c", 29, "/d", NULL, NULL},
286
{"http::@c:29", "http", "", "", "c", 29, NULL, NULL, NULL},
287
// ... "]" in the password field isn't allowed, but we tolerate it here...
288
{"http://&a:foo(b]c@d:2/", "http", "&a", "foo(b]c", "d", 2, "/", NULL, NULL},
289
{"http://::@c@d:2", "http", "", ":@c", "d", 2, NULL, NULL, NULL},
290
{"http://foo.com:b@d/", "http", "foo.com", "b", "d", -1, "/", NULL, NULL},
292
{"http://foo.com/\\@", "http", NULL, NULL, "foo.com", -1, "/\\@", NULL, NULL},
293
{"http:\\\\foo.com\\", "http", NULL, NULL, "foo.com", -1, "\\", NULL, NULL},
294
{"http:\\\\a\\b:c\\d@foo.com\\", "http", NULL, NULL, "a", -1, "\\b:c\\d@foo.com\\", NULL, NULL},
296
// Tolerate different numbers of slashes.
297
{"foo:/", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
298
{"foo:/bar.com/", "foo", NULL, NULL, "bar.com", -1, "/", NULL, NULL},
299
{"foo://///////", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
300
{"foo://///////bar.com/", "foo", NULL, NULL, "bar.com", -1, "/", NULL, NULL},
301
{"foo:////://///", "foo", NULL, NULL, NULL, -1, "/////", NULL, NULL},
303
// Raw file paths on Windows aren't handled by the parser.
304
{"c:/foo", "c", NULL, NULL, "foo", -1, NULL, NULL, NULL},
305
{"//foo/bar", NULL, NULL, NULL, "foo", -1, "/bar", NULL, NULL},
307
// Use the first question mark for the query and the ref.
308
{"http://foo/path;a??e#f#g", "http", NULL, NULL, "foo", -1, "/path;a", "?e", "f#g"},
309
{"http://foo/abcd?efgh?ijkl", "http", NULL, NULL, "foo", -1, "/abcd", "efgh?ijkl", NULL},
310
{"http://foo/abcd#foo?bar", "http", NULL, NULL, "foo", -1, "/abcd", NULL, "foo?bar"},
312
// IPV6, check also interesting uses of colons.
313
{"[61:24:74]:98", "[61", NULL, NULL, "24:74]", 98, NULL, NULL, NULL},
314
{"http://[61:27]:98", "http", NULL, NULL, "[61:27]", 98, NULL, NULL, NULL},
315
{"http:[61:27]/:foo", "http", NULL, NULL, "[61:27]", -1, "/:foo", NULL, NULL},
319
TEST(URLParser, Standard) {
320
// Declared outside for loop to try to catch cases in init() where we forget
321
// to reset something that is reset by the construtor.
322
url_parse::Parsed parsed;
323
for (size_t i = 0; i < arraysize(cases); i++) {
324
const char* url = cases[i].input;
325
url_parse::ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
326
int port = url_parse::ParsePort(url, parsed.port);
328
EXPECT_TRUE(ComponentMatches(url, cases[i].scheme, parsed.scheme));
329
EXPECT_TRUE(ComponentMatches(url, cases[i].username, parsed.username));
330
EXPECT_TRUE(ComponentMatches(url, cases[i].password, parsed.password));
331
EXPECT_TRUE(ComponentMatches(url, cases[i].host, parsed.host));
332
EXPECT_EQ(cases[i].port, port);
333
EXPECT_TRUE(ComponentMatches(url, cases[i].path, parsed.path));
334
EXPECT_TRUE(ComponentMatches(url, cases[i].query, parsed.query));
335
EXPECT_TRUE(ComponentMatches(url, cases[i].ref, parsed.ref));
339
// PathURL --------------------------------------------------------------------
341
// Various incarnations of path URLs.
342
static PathURLParseCase path_cases[] = {
347
{" This is \\interesting// \t", NULL, "This is \\interesting//"},
348
{"about:", "about", NULL},
349
{"about:blank", "about", "blank"},
350
{" about: blank ", "about", " blank"},
351
{"javascript :alert(\"He:/l\\l#o?foo\"); ", "javascript ", "alert(\"He:/l\\l#o?foo\");"},
354
TEST(URLParser, PathURL) {
355
// Declared outside for loop to try to catch cases in init() where we forget
356
// to reset something that is reset by the construtor.
357
url_parse::Parsed parsed;
358
for (size_t i = 0; i < arraysize(path_cases); i++) {
359
const char* url = path_cases[i].input;
360
url_parse::ParsePathURL(url, static_cast<int>(strlen(url)), &parsed);
362
EXPECT_TRUE(ComponentMatches(url, path_cases[i].scheme, parsed.scheme));
363
EXPECT_TRUE(ComponentMatches(url, path_cases[i].path, parsed.path));
365
// The remaining components are never used for path urls.
366
ExpectInvalidComponent(parsed.username);
367
ExpectInvalidComponent(parsed.password);
368
ExpectInvalidComponent(parsed.host);
369
ExpectInvalidComponent(parsed.port);
370
ExpectInvalidComponent(parsed.query);
371
ExpectInvalidComponent(parsed.ref);
377
// WindowsFile ----------------------------------------------------------------
379
// Various incarnations of file URLs. These are for Windows only.
380
static URLParseCase file_cases[] = {
381
{"file:server", "file", NULL, NULL, "server", -1, NULL, NULL, NULL},
382
{" file: server \t", "file", NULL, NULL, " server",-1, NULL, NULL, NULL},
383
{"FiLe:c|", "FiLe", NULL, NULL, NULL, -1, "c|", NULL, NULL},
384
{"FILE:/\\\\/server/file", "FILE", NULL, NULL, "server", -1, "/file", NULL, NULL},
385
{"file://server/", "file", NULL, NULL, "server", -1, "/", NULL, NULL},
386
{"file://localhost/c:/", "file", NULL, NULL, NULL, -1, "/c:/", NULL, NULL},
387
{"file://127.0.0.1/c|\\", "file", NULL, NULL, NULL, -1, "/c|\\", NULL, NULL},
388
{"file:/", "file", NULL, NULL, NULL, -1, NULL, NULL, NULL},
389
{"file:", "file", NULL, NULL, NULL, -1, NULL, NULL, NULL},
390
// If there is a Windows drive letter, treat any number of slashes as the
392
{"file:c:\\fo\\b", "file", NULL, NULL, NULL, -1, "c:\\fo\\b", NULL, NULL},
393
{"file:/c:\\foo/bar", "file", NULL, NULL, NULL, -1, "/c:\\foo/bar",NULL, NULL},
394
{"file://c:/f\\b", "file", NULL, NULL, NULL, -1, "/c:/f\\b", NULL, NULL},
395
{"file:///C:/foo", "file", NULL, NULL, NULL, -1, "/C:/foo", NULL, NULL},
396
{"file://///\\/\\/c:\\f\\b", "file", NULL, NULL, NULL, -1, "/c:\\f\\b", NULL, NULL},
397
// If there is not a drive letter, we should treat is as UNC EXCEPT for
398
// three slashes, which we treat as a Unix style path.
399
{"file:server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
400
{"file:/server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
401
{"file://server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
402
{"file:///server/file", "file", NULL, NULL, NULL, -1, "/server/file",NULL, NULL},
403
{"file://\\server/file", "file", NULL, NULL, NULL, -1, "\\server/file",NULL, NULL},
404
{"file:////server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
405
// Queries and refs are valid for file URLs as well.
406
{"file:///C:/foo.html?#", "file", NULL, NULL, NULL, -1, "/C:/foo.html", "", ""},
407
{"file:///C:/foo.html?query=yes#ref", "file", NULL, NULL, NULL, -1, "/C:/foo.html", "query=yes", "ref"},
410
TEST(URLParser, WindowsFile) {
411
// Declared outside for loop to try to catch cases in init() where we forget
412
// to reset something that is reset by the construtor.
413
url_parse::Parsed parsed;
414
for (int i = 0; i < arraysize(file_cases); i++) {
415
const char* url = file_cases[i].input;
416
url_parse::ParseFileURL(url, static_cast<int>(strlen(url)), &parsed);
417
int port = url_parse::ParsePort(url, parsed.port);
419
EXPECT_TRUE(ComponentMatches(url, file_cases[i].scheme, parsed.scheme));
420
EXPECT_TRUE(ComponentMatches(url, file_cases[i].username, parsed.username));
421
EXPECT_TRUE(ComponentMatches(url, file_cases[i].password, parsed.password));
422
EXPECT_TRUE(ComponentMatches(url, file_cases[i].host, parsed.host));
423
EXPECT_EQ(file_cases[i].port, port);
424
EXPECT_TRUE(ComponentMatches(url, file_cases[i].path, parsed.path));
425
EXPECT_TRUE(ComponentMatches(url, file_cases[i].query, parsed.query));
426
EXPECT_TRUE(ComponentMatches(url, file_cases[i].ref, parsed.ref));
432
TEST(URLParser, ExtractFileName) {
435
const char* expected;
437
{"http://www.google.com", NULL},
438
{"http://www.google.com/", ""},
439
{"http://www.google.com/search", "search"},
440
{"http://www.google.com/search/", ""},
441
{"http://www.google.com/foo/bar.html?baz=22", "bar.html"},
442
{"http://www.google.com/foo/bar.html#ref", "bar.html"},
443
{"http://www.google.com/search/;param", ""},
444
{"http://www.google.com/foo/bar.html;param#ref", "bar.html"},
445
{"http://www.google.com/foo/bar.html;foo;param#ref", "bar.html;foo"},
446
{"http://www.google.com/foo/bar.html?query#ref", "bar.html"},
449
for (size_t i = 0; i < ARRAYSIZE(file_cases); i++) {
450
const char* url = file_cases[i].input;
451
int len = static_cast<int>(strlen(url));
453
url_parse::Parsed parsed;
454
url_parse::ParseStandardURL(url, len, &parsed);
456
url_parse::Component file_name;
457
url_parse::ExtractFileName(url, parsed.path, &file_name);
459
EXPECT_TRUE(ComponentMatches(url, file_cases[i].expected, file_name));
463
// Returns true if the parameter with index |parameter| in the given URL's
464
// query string. The expected key can be NULL to indicate no such key index
465
// should exist. The parameter number is 1-based.
466
static bool NthParameterIs(const char* url,
468
const char* expected_key,
469
const char* expected_value) {
470
url_parse::Parsed parsed;
471
url_parse::ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
473
url_parse::Component query = parsed.query;
475
for (int i = 1; i <= parameter; i++) {
476
url_parse::Component key, value;
477
if (!url_parse::ExtractQueryKeyValue(url, &query, &key, &value)) {
478
if (parameter >= i && !expected_key)
479
return true; // Expected nonexistant key, got one.
480
return false; // Not enough keys.
483
if (i == parameter) {
487
if (strncmp(&url[key.begin], expected_key, key.len) != 0)
489
if (strncmp(&url[value.begin], expected_value, value.len) != 0)
494
return expected_key == NULL; // We didn't find that many parameters.
497
TEST(URLParser, ExtractQueryKeyValue) {
498
EXPECT_TRUE(NthParameterIs("http://www.google.com", 1, NULL, NULL));
501
char a[] = "http://www.google.com?arg1=1&arg2=2&bar";
502
EXPECT_TRUE(NthParameterIs(a, 1, "arg1", "1"));
503
EXPECT_TRUE(NthParameterIs(a, 2, "arg2", "2"));
504
EXPECT_TRUE(NthParameterIs(a, 3, "bar", ""));
505
EXPECT_TRUE(NthParameterIs(a, 4, NULL, NULL));
507
// Empty param at the end.
508
char b[] = "http://www.google.com?foo=bar&";
509
EXPECT_TRUE(NthParameterIs(b, 1, "foo", "bar"));
510
EXPECT_TRUE(NthParameterIs(b, 2, NULL, NULL));
512
// Empty param at the beginning.
513
char c[] = "http://www.google.com?&foo=bar";
514
EXPECT_TRUE(NthParameterIs(c, 1, "", ""));
515
EXPECT_TRUE(NthParameterIs(c, 2, "foo", "bar"));
516
EXPECT_TRUE(NthParameterIs(c, 3, NULL, NULL));
518
// Empty key with value.
519
char d[] = "http://www.google.com?=foo";
520
EXPECT_TRUE(NthParameterIs(d, 1, "", "foo"));
521
EXPECT_TRUE(NthParameterIs(d, 2, NULL, NULL));
523
// Empty value with key.
524
char e[] = "http://www.google.com?foo=";
525
EXPECT_TRUE(NthParameterIs(e, 1, "foo", ""));
526
EXPECT_TRUE(NthParameterIs(e, 2, NULL, NULL));
528
// Empty key and values.
529
char f[] = "http://www.google.com?&&==&=";
530
EXPECT_TRUE(NthParameterIs(f, 1, "", ""));
531
EXPECT_TRUE(NthParameterIs(f, 2, "", ""));
532
EXPECT_TRUE(NthParameterIs(f, 3, "", "="));
533
EXPECT_TRUE(NthParameterIs(f, 4, "", ""));
534
EXPECT_TRUE(NthParameterIs(f, 5, NULL, NULL));
537
// MailtoURL --------------------------------------------------------------------
539
static MailtoURLParseCase mailto_cases[] = {
540
//|input |scheme |path |query
541
{"mailto:foo@gmail.com", "mailto", "foo@gmail.com", NULL},
542
{" mailto: to \t", "mailto", " to", NULL},
543
{"mailto:addr1%2C%20addr2 ", "mailto", "addr1%2C%20addr2", NULL},
544
{"Mailto:addr1, addr2 ", "Mailto", "addr1, addr2", NULL},
545
{"mailto:addr1:addr2 ", "mailto", "addr1:addr2", NULL},
546
{"mailto:?to=addr1,addr2", "mailto", NULL, "to=addr1,addr2"},
547
{"mailto:?to=addr1%2C%20addr2", "mailto", NULL, "to=addr1%2C%20addr2"},
548
{"mailto:addr1?to=addr2", "mailto", "addr1", "to=addr2"},
549
{"mailto:?body=#foobar#", "mailto", NULL, "body=#foobar#",},
550
{"mailto:#?body=#foobar#", "mailto", "#", "body=#foobar#"},
553
TEST(URLParser, MailtoUrl) {
554
// Declared outside for loop to try to catch cases in init() where we forget
555
// to reset something that is reset by the construtor.
556
url_parse::Parsed parsed;
557
for (size_t i = 0; i < arraysize(mailto_cases); ++i) {
558
const char* url = mailto_cases[i].input;
559
url_parse::ParseMailtoURL(url, static_cast<int>(strlen(url)), &parsed);
560
int port = url_parse::ParsePort(url, parsed.port);
562
EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].scheme, parsed.scheme));
563
EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].path, parsed.path));
564
EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].query, parsed.query));
565
EXPECT_EQ(url_parse::PORT_UNSPECIFIED, port);
567
// The remaining components are never used for mailto urls.
568
ExpectInvalidComponent(parsed.username);
569
ExpectInvalidComponent(parsed.password);
570
ExpectInvalidComponent(parsed.port);
571
ExpectInvalidComponent(parsed.ref);