segments.js revision c57e584da242d96ba18578a71f4634adc9d1fff6
1description("Test URL segmentation");
2
3cases = [
4  // [URL, [SCHEME, HOST, PORT, PATH, QUERY, REF]]
5  ["http://user:pass@foo:21/bar;par?b#c",    ["http:","foo","21","/bar;par","?b","#c"]],
6  ["http:foo.com",                           ["http:","example.org","","/foo/foo.com","",""]],
7  ["\\t   :foo.com   \\n",                   ["http:","example.org","","/foo/:foo.com","",""]],
8  [" foo.com  ",                             ["http:","example.org","","/foo/foo.com","",""]],
9  ["a:\\t foo.com",                          ["a:","",""," foo.com","",""]],
10  ["http://f:21/ b ? d # e ",                ["http:","f","21","/%20b%20","?%20d%20","# e"]],
11  ["http://f:/c",                            ["http:","f","","/c","",""]],
12  ["http://f:0/c",                           ["http:","f","0","/c","",""]],
13  ["http://f:00000000000000/c",              ["http:","f","0","/c","",""]],
14  ["http://f:00000000000000000000080/c",     ["http:","f","0","/c","",""]],
15  ["http://f:b/c",                           [":","","","","",""]],
16  ["http://f: /c",                           [":","","","","",""]],
17  ["http://f:\\n/c",                         [":","","","","",""]],
18  ["http://f:fifty-two/c",                   [":","","","","",""]],
19  ["http://f:999999/c",                      [":","","0","","",""]],
20  ["http://f: 21 / b ? d # e ",              [":","","","","",""]],
21  ["",                                       ["http:","example.org","","/foo/bar","",""]],
22  ["  \\t",                                  ["http:","example.org","","/foo/bar","",""]],
23  [":foo.com/",                              ["http:","example.org","","/foo/:foo.com/","",""]],
24  [":foo.com\\\\",                           ["http:","example.org","","/foo/:foo.com/","",""]],
25  [":",                                      ["http:","example.org","","/foo/:","",""]],
26  [":a",                                     ["http:","example.org","","/foo/:a","",""]],
27  [":/",                                     ["http:","example.org","","/foo/:/","",""]],
28  [":\\\\",                                  ["http:","example.org","","/foo/:/","",""]],
29  [":#",                                     ["http:","example.org","","/foo/:","",""]],
30  ["#",                                      ["http:","example.org","","/foo/bar","",""]],
31  ["#/",                                     ["http:","example.org","","/foo/bar","","#/"]],
32  ["#\\\\",                                  ["http:","example.org","","/foo/bar","","#\\\\"]],
33  ["#;?",                                    ["http:","example.org","","/foo/bar","","#;?"]],
34  ["?",                                      ["http:","example.org","","/foo/bar","",""]],
35  ["/",                                      ["http:","example.org","","/","",""]],
36  [":23",                                    ["http:","example.org","","/foo/:23","",""]],
37  ["/:23",                                   ["http:","example.org","","/:23","",""]],
38  ["//",                                     [":","","","","",""]],
39  ["::",                                     ["http:","example.org","","/foo/::","",""]],
40  ["::23",                                   ["http:","example.org","","/foo/::23","",""]],
41  ["foo://",                                 ["foo:","","","//","",""]],
42  ["http://a:b@c:29/d",                      ["http:","c","29","/d","",""]],
43  ["http::@c:29",                            ["http:","example.org","","/foo/:@c:29","",""]],
44  ["http://&a:foo(b]c@d:2/",                 ["http:","d","2","/","",""]],
45  ["http://::@c@d:2",                        ["http:","d","2","/","",""]],
46  ["http://foo.com:b@d/",                    ["http:","d","","/","",""]],
47  ["http://foo.com/\\\\@",                   ["http:","foo.com","","//@","",""]],
48  ["http:\\\\\\\\foo.com\\\\",               ["http:","foo.com","","/","",""]],
49  ["http:\\\\\\\\a\\\\b:c\\\\d@foo.com\\\\", ["http:","a","","/b:c/d@foo.com/","",""]],
50  ["foo:/",                                  ["foo:","","","/","",""]],
51  ["foo:/bar.com/",                          ["foo:","","","/bar.com/","",""]],
52  ["foo://///////",                          ["foo:","","","/////////","",""]],
53  ["foo://///////bar.com/",                  ["foo:","","","/////////bar.com/","",""]],
54  ["foo:////://///",                         ["foo:","","","////://///","",""]],
55  ["c:/foo",                                 ["c:","","","/foo","",""]],
56  ["//foo/bar",                              ["http:","foo","","/bar","",""]],
57  ["http://foo/path;a??e#f#g",               ["http:","foo","","/path;a","??e","#f#g"]],
58  ["http://foo/abcd?efgh?ijkl",              ["http:","foo","","/abcd","?efgh?ijkl",""]],
59  ["http://foo/abcd#foo?bar",                ["http:","foo","","/abcd","","#foo?bar"]],
60  ["[61:24:74]:98",                          ["http:","example.org","","/foo/[61:24:74]:98","",""]],
61  ["http://[61:27]:98",                      [":","","0","","",""]],
62  ["http:[61:27]/:foo",                      ["http:","example.org","","/foo/[61:27]/:foo","",""]],
63  ["http://[1::2]:3:4",                      [":","","","","",""]],
64  ["http://2001::1",                         [":","","","","",""]],
65  ["http://[2001::1",                        [":","","","","",""]],
66  ["http://2001::1]",                        [":","","","","",""]],
67  ["http://2001::1]:80",                     [":","","","","",""]],
68  ["http://[2001::1]",                       ["http:","[2001::1]","","/","",""]],
69  ["http://[2001::1]:80",                    ["http:","[2001::1]","","/","",""]],
70  ["http://[[::]]",                          [":","","","","",""]],
71  ["http:/example.com/",                     ["http:","example.org","","/example.com/","",""]],
72  ["ftp:/example.com/",                      ["ftp:","example.com","","/","",""]],
73  ["https:/example.com/",                    ["https:","example.com","","/","",""]],
74  ["madeupscheme:/example.com/",             ["madeupscheme:","","","/example.com/","",""]],
75  ["file:/example.com/",                     ["file:","","","/example.com/","",""]],
76  ["ftps:/example.com/",                     ["ftps:","","","/example.com/","",""]],
77  ["gopher:/example.com/",                   ["gopher:","example.com","","/","",""]],
78  ["ws:/example.com/",                       ["ws:","example.com","","/","",""]],
79  ["wss:/example.com/",                      ["wss:","example.com","","/","",""]],
80  ["data:/example.com/",                     ["data:","","","/example.com/","",""]],
81  ["javascript:/example.com/",               ["javascript:","","","/example.com/","",""]],
82  ["mailto:/example.com/",                   ["mailto:","","","/example.com/","",""]],
83  ["http:example.com/",                      ["http:","example.org","","/foo/example.com/","",""]],
84  ["ftp:example.com/",                       ["ftp:","example.com","","/","",""]],
85  ["https:example.com/",                     ["https:","example.com","","/","",""]],
86  ["madeupscheme:example.com/",              ["madeupscheme:","","","example.com/","",""]],
87  ["file:example.com/",                      ["file:","","","/example.com/","",""]],
88  ["ftps:example.com/",                      ["ftps:","","","example.com/","",""]],
89  ["gopher:example.com/",                    ["gopher:","example.com","","/","",""]],
90  ["ws:example.com/",                        ["ws:","example.com","","/","",""]],
91  ["wss:example.com/",                       ["wss:","example.com","","/","",""]],
92  ["data:example.com/",                      ["data:","","","example.com/","",""]],
93  ["javascript:example.com/",                ["javascript:","","","example.com/","",""]],
94  ["mailto:example.com/",                    ["mailto:","","","example.com/","",""]],
95];
96
97var originalBaseURL = canonicalize(".");
98setBaseURL("http://example.org/foo/bar");
99
100for (var i = 0; i < cases.length; ++i) {
101  shouldBe("segments('" + cases[i][0] + "')",
102           "'" + JSON.stringify(cases[i][1]) + "'");
103}
104
105setBaseURL(originalBaseURL);
106
107var successfullyParsed = true;
108