1description("Test resolution of relative URLs.");
2
3cases = [
4  // Format: [baseURL, relativeURL, expectedURL],
5  // Basic absolute input.
6  ["http://host/a", "http://another/", "http://another/"],
7  ["http://host/a", "http:////another/", "http://another/"],
8  // Empty relative URLs should only remove the ref part of the URL,
9  // leaving the rest unchanged.
10  ["http://foo/bar", "", "http://foo/bar"],
11  ["http://foo/bar#ref", "", "http://foo/bar"],
12  ["http://foo/bar#", "", "http://foo/bar"],
13  // Spaces at the ends of the relative path should be ignored.
14  ["http://foo/bar", "  another  ", "http://foo/another"],
15  ["http://foo/bar", "  .  ", "http://foo/"],
16  ["http://foo/bar", " \t ", "http://foo/bar"],
17  // Matching schemes without two slashes are treated as relative.
18  ["http://host/a", "http:path", "http://host/path"],
19  ["http://host/a/", "http:path", "http://host/a/path"],
20  ["http://host/a", "http:/path", "http://host/path"],
21  ["http://host/a", "HTTP:/path", "http://host/path"],
22  // Nonmatching schemes are absolute.
23  ["http://host/a", "https:host2", "https://host2/"],
24  ["http://host/a", "htto:/host2", "htto:/host2"],
25  // Absolute path input
26  ["http://host/a", "/b/c/d", "http://host/b/c/d"],
27  ["http://host/a", "\\\\b\\\\c\\\\d", "http://host/b/c/d"],
28  ["http://host/a", "/b/../c", "http://host/c"],
29  ["http://host/a?b#c", "/b/../c", "http://host/c"],
30  ["http://host/a", "\\\\b/../c?x#y", "http://host/c?x#y"],
31  ["http://host/a?b#c", "/b/../c?x#y", "http://host/c?x#y"],
32  // Relative path input
33  ["http://host/a", "b", "http://host/b"],
34  ["http://host/a", "bc/de", "http://host/bc/de"],
35  ["http://host/a/", "bc/de?query#ref", "http://host/a/bc/de?query#ref"],
36  ["http://host/a/", ".", "http://host/a/"],
37  ["http://host/a/", "..", "http://host/"],
38  ["http://host/a/", "./..", "http://host/"],
39  ["http://host/a/", "../.", "http://host/"],
40  ["http://host/a/", "././.", "http://host/a/"],
41  ["http://host/a?query#ref", "../../../foo", "http://host/foo"],
42  // Query input
43  ["http://host/a", "?foo=bar", "http://host/a?foo=bar"],
44  ["http://host/a?x=y#z", "?", "http://host/a?"],
45  ["http://host/a?x=y#z", "?foo=bar#com", "http://host/a?foo=bar#com"],
46  // Ref input
47  ["http://host/a", "#ref", "http://host/a#ref"],
48  ["http://host/a#b", "#", "http://host/a#"],
49  ["http://host/a?foo=bar#hello", "#bye", "http://host/a?foo=bar#bye"],
50  // Non-hierarchical base: no relative handling. Relative input should
51  // error, and if a scheme is present, it should be treated as absolute.
52  ["data:foobar", "baz.html", ""],
53  ["data:foobar", "data:baz", "data:baz"],
54  ["data:foobar", "data:/base", "data:/base"],
55  // Non-hierarchical base: absolute input should succeed.
56  ["data:foobar", "http://host/", "http://host/"],
57  ["data:foobar", "http:host", "http://host/"],
58  // Invalid schemes should be treated as relative.
59  ["http://foo/bar", "./asd:fgh", "http://foo/asd:fgh"],
60  ["http://foo/bar", ":foo", "http://foo/:foo"],
61  ["http://foo/bar", " hello world", "http://foo/hello%20world"],
62  ["data:asdf", ":foo", ""],
63  // We should treat semicolons like any other character in URL resolving
64  ["http://host/a", ";foo", "http://host/;foo"],
65  ["http://host/a;", ";foo", "http://host/;foo"],
66  ["http://host/a", ";/../bar", "http://host/bar"],
67  // Relative URLs can also be written as "//foo/bar" which is relative to
68  // the scheme. In this case, it would take the old scheme, so for http
69  // the example would resolve to "http://foo/bar".
70  ["http://host/a", "//another", "http://another/"],
71  ["http://host/a", "//another/path?query#ref", "http://another/path?query#ref"],
72  ["http://host/a", "///another/path", "http://another/path"],
73  ["http://host/a", "//Another\\\\path", "http://another/path"],
74  ["http://host/a", "//", "http:"],
75  // IE will also allow one or the other to be a backslash to get the same
76  // behavior.
77  ["http://host/a", "\\\\/another/path", "http://another/path"],
78  ["http://host/a", "/\\\\Another\\\\path", "http://another/path"],
79];
80
81var originalBaseURL = canonicalize(".");
82
83for (var i = 0; i < cases.length; ++i) {
84  baseURL = cases[i][0];
85  relativeURL = cases[i][1];
86  expectedURL = cases[i][2];
87  setBaseURL(baseURL);
88  shouldBe("canonicalize('" + relativeURL + "')",
89           "'" + expectedURL + "'");
90}
91
92setBaseURL(originalBaseURL);
93
94var successfullyParsed = true;
95