twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

tests.js (35268B)


      1 //
      2 //
      3 // Tests
      4 //
      5 //
      6 
      7 if (typeof URI === "undefined") {
      8 	var URI = require("../dist/es5/uri.all");
      9 }
     10 
     11 test("Acquire URI", function () {
     12 	//URI = require("./uri").URI;
     13 	ok(URI);
     14 });
     15 
     16 test("URI Parsing", function () {
     17 	var components;
     18 
     19 	//scheme
     20 	components = URI.parse("uri:");
     21 	strictEqual(components.error, undefined, "scheme errors");
     22 	strictEqual(components.scheme, "uri", "scheme");
     23 	//strictEqual(components.authority, undefined, "authority");
     24 	strictEqual(components.userinfo, undefined, "userinfo");
     25 	strictEqual(components.host, undefined, "host");
     26 	strictEqual(components.port, undefined, "port");
     27 	strictEqual(components.path, "", "path");
     28 	strictEqual(components.query, undefined, "query");
     29 	strictEqual(components.fragment, undefined, "fragment");
     30 
     31 	//userinfo
     32 	components = URI.parse("//@");
     33 	strictEqual(components.error, undefined, "userinfo errors");
     34 	strictEqual(components.scheme, undefined, "scheme");
     35 	//strictEqual(components.authority, "@", "authority");
     36 	strictEqual(components.userinfo, "", "userinfo");
     37 	strictEqual(components.host, "", "host");
     38 	strictEqual(components.port, undefined, "port");
     39 	strictEqual(components.path, "", "path");
     40 	strictEqual(components.query, undefined, "query");
     41 	strictEqual(components.fragment, undefined, "fragment");
     42 
     43 	//host
     44 	components = URI.parse("//");
     45 	strictEqual(components.error, undefined, "host errors");
     46 	strictEqual(components.scheme, undefined, "scheme");
     47 	//strictEqual(components.authority, "", "authority");
     48 	strictEqual(components.userinfo, undefined, "userinfo");
     49 	strictEqual(components.host, "", "host");
     50 	strictEqual(components.port, undefined, "port");
     51 	strictEqual(components.path, "", "path");
     52 	strictEqual(components.query, undefined, "query");
     53 	strictEqual(components.fragment, undefined, "fragment");
     54 
     55 	//port
     56 	components = URI.parse("//:");
     57 	strictEqual(components.error, undefined, "port errors");
     58 	strictEqual(components.scheme, undefined, "scheme");
     59 	//strictEqual(components.authority, ":", "authority");
     60 	strictEqual(components.userinfo, undefined, "userinfo");
     61 	strictEqual(components.host, "", "host");
     62 	strictEqual(components.port, "", "port");
     63 	strictEqual(components.path, "", "path");
     64 	strictEqual(components.query, undefined, "query");
     65 	strictEqual(components.fragment, undefined, "fragment");
     66 
     67 	//path
     68 	components = URI.parse("");
     69 	strictEqual(components.error, undefined, "path errors");
     70 	strictEqual(components.scheme, undefined, "scheme");
     71 	//strictEqual(components.authority, undefined, "authority");
     72 	strictEqual(components.userinfo, undefined, "userinfo");
     73 	strictEqual(components.host, undefined, "host");
     74 	strictEqual(components.port, undefined, "port");
     75 	strictEqual(components.path, "", "path");
     76 	strictEqual(components.query, undefined, "query");
     77 	strictEqual(components.fragment, undefined, "fragment");
     78 
     79 	//query
     80 	components = URI.parse("?");
     81 	strictEqual(components.error, undefined, "query errors");
     82 	strictEqual(components.scheme, undefined, "scheme");
     83 	//strictEqual(components.authority, undefined, "authority");
     84 	strictEqual(components.userinfo, undefined, "userinfo");
     85 	strictEqual(components.host, undefined, "host");
     86 	strictEqual(components.port, undefined, "port");
     87 	strictEqual(components.path, "", "path");
     88 	strictEqual(components.query, "", "query");
     89 	strictEqual(components.fragment, undefined, "fragment");
     90 
     91 	//fragment
     92 	components = URI.parse("#");
     93 	strictEqual(components.error, undefined, "fragment errors");
     94 	strictEqual(components.scheme, undefined, "scheme");
     95 	//strictEqual(components.authority, undefined, "authority");
     96 	strictEqual(components.userinfo, undefined, "userinfo");
     97 	strictEqual(components.host, undefined, "host");
     98 	strictEqual(components.port, undefined, "port");
     99 	strictEqual(components.path, "", "path");
    100 	strictEqual(components.query, undefined, "query");
    101 	strictEqual(components.fragment, "", "fragment");
    102 
    103 	//fragment with character tabulation
    104 	components = URI.parse("#\t");
    105 	strictEqual(components.error, undefined, "path errors");
    106 	strictEqual(components.scheme, undefined, "scheme");
    107 	//strictEqual(components.authority, undefined, "authority");
    108 	strictEqual(components.userinfo, undefined, "userinfo");
    109 	strictEqual(components.host, undefined, "host");
    110 	strictEqual(components.port, undefined, "port");
    111 	strictEqual(components.path, "", "path");
    112 	strictEqual(components.query, undefined, "query");
    113 	strictEqual(components.fragment, "%09", "fragment");
    114 
    115 	//fragment with line feed
    116 	components = URI.parse("#\n");
    117 	strictEqual(components.error, undefined, "path errors");
    118 	strictEqual(components.scheme, undefined, "scheme");
    119 	//strictEqual(components.authority, undefined, "authority");
    120 	strictEqual(components.userinfo, undefined, "userinfo");
    121 	strictEqual(components.host, undefined, "host");
    122 	strictEqual(components.port, undefined, "port");
    123 	strictEqual(components.path, "", "path");
    124 	strictEqual(components.query, undefined, "query");
    125 	strictEqual(components.fragment, "%0A", "fragment");
    126 
    127 	//fragment with line tabulation
    128 	components = URI.parse("#\v");
    129 	strictEqual(components.error, undefined, "path errors");
    130 	strictEqual(components.scheme, undefined, "scheme");
    131 	//strictEqual(components.authority, undefined, "authority");
    132 	strictEqual(components.userinfo, undefined, "userinfo");
    133 	strictEqual(components.host, undefined, "host");
    134 	strictEqual(components.port, undefined, "port");
    135 	strictEqual(components.path, "", "path");
    136 	strictEqual(components.query, undefined, "query");
    137 	strictEqual(components.fragment, "%0B", "fragment");
    138 
    139 	//fragment with form feed
    140 	components = URI.parse("#\f");
    141 	strictEqual(components.error, undefined, "path errors");
    142 	strictEqual(components.scheme, undefined, "scheme");
    143 	//strictEqual(components.authority, undefined, "authority");
    144 	strictEqual(components.userinfo, undefined, "userinfo");
    145 	strictEqual(components.host, undefined, "host");
    146 	strictEqual(components.port, undefined, "port");
    147 	strictEqual(components.path, "", "path");
    148 	strictEqual(components.query, undefined, "query");
    149 	strictEqual(components.fragment, "%0C", "fragment");
    150 
    151 	//fragment with carriage return
    152 	components = URI.parse("#\r");
    153 	strictEqual(components.error, undefined, "path errors");
    154 	strictEqual(components.scheme, undefined, "scheme");
    155 	//strictEqual(components.authority, undefined, "authority");
    156 	strictEqual(components.userinfo, undefined, "userinfo");
    157 	strictEqual(components.host, undefined, "host");
    158 	strictEqual(components.port, undefined, "port");
    159 	strictEqual(components.path, "", "path");
    160 	strictEqual(components.query, undefined, "query");
    161 	strictEqual(components.fragment, "%0D", "fragment");
    162 
    163 	//all
    164 	components = URI.parse("uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body");
    165 	strictEqual(components.error, undefined, "all errors");
    166 	strictEqual(components.scheme, "uri", "scheme");
    167 	//strictEqual(components.authority, "user:pass@example.com:123", "authority");
    168 	strictEqual(components.userinfo, "user:pass", "userinfo");
    169 	strictEqual(components.host, "example.com", "host");
    170 	strictEqual(components.port, 123, "port");
    171 	strictEqual(components.path, "/one/two.three", "path");
    172 	strictEqual(components.query, "q1=a1&q2=a2", "query");
    173 	strictEqual(components.fragment, "body", "fragment");
    174 
    175 	//IPv4address
    176 	components = URI.parse("//10.10.10.10");
    177 	strictEqual(components.error, undefined, "IPv4address errors");
    178 	strictEqual(components.scheme, undefined, "scheme");
    179 	strictEqual(components.userinfo, undefined, "userinfo");
    180 	strictEqual(components.host, "10.10.10.10", "host");
    181 	strictEqual(components.port, undefined, "port");
    182 	strictEqual(components.path, "", "path");
    183 	strictEqual(components.query, undefined, "query");
    184 	strictEqual(components.fragment, undefined, "fragment");
    185 
    186 	//IPv6address
    187 	components = URI.parse("//[2001:db8::7]");
    188 	strictEqual(components.error, undefined, "IPv4address errors");
    189 	strictEqual(components.scheme, undefined, "scheme");
    190 	strictEqual(components.userinfo, undefined, "userinfo");
    191 	strictEqual(components.host, "2001:db8::7", "host");
    192 	strictEqual(components.port, undefined, "port");
    193 	strictEqual(components.path, "", "path");
    194 	strictEqual(components.query, undefined, "query");
    195 	strictEqual(components.fragment, undefined, "fragment");
    196 
    197 	//mixed IPv4address & IPv6address
    198 	components = URI.parse("//[::ffff:129.144.52.38]");
    199 	strictEqual(components.error, undefined, "IPv4address errors");
    200 	strictEqual(components.scheme, undefined, "scheme");
    201 	strictEqual(components.userinfo, undefined, "userinfo");
    202 	strictEqual(components.host, "::ffff:129.144.52.38", "host");
    203 	strictEqual(components.port, undefined, "port");
    204 	strictEqual(components.path, "", "path");
    205 	strictEqual(components.query, undefined, "query");
    206 	strictEqual(components.fragment, undefined, "fragment");
    207 
    208 	//mixed IPv4address & reg-name, example from terion-name (https://github.com/garycourt/uri-js/issues/4)
    209 	components = URI.parse("uri://10.10.10.10.example.com/en/process");
    210 	strictEqual(components.error, undefined, "mixed errors");
    211 	strictEqual(components.scheme, "uri", "scheme");
    212 	strictEqual(components.userinfo, undefined, "userinfo");
    213 	strictEqual(components.host, "10.10.10.10.example.com", "host");
    214 	strictEqual(components.port, undefined, "port");
    215 	strictEqual(components.path, "/en/process", "path");
    216 	strictEqual(components.query, undefined, "query");
    217 	strictEqual(components.fragment, undefined, "fragment");
    218 
    219 	//IPv6address, example from bkw (https://github.com/garycourt/uri-js/pull/16)
    220 	components = URI.parse("//[2606:2800:220:1:248:1893:25c8:1946]/test");
    221 	strictEqual(components.error, undefined, "IPv6address errors");
    222 	strictEqual(components.scheme, undefined, "scheme");
    223 	strictEqual(components.userinfo, undefined, "userinfo");
    224 	strictEqual(components.host, "2606:2800:220:1:248:1893:25c8:1946", "host");
    225 	strictEqual(components.port, undefined, "port");
    226 	strictEqual(components.path, "/test", "path");
    227 	strictEqual(components.query, undefined, "query");
    228 	strictEqual(components.fragment, undefined, "fragment");
    229 	
    230 	//IPv6address, example from RFC 5952
    231 	components = URI.parse("//[2001:db8::1]:80");
    232 	strictEqual(components.error, undefined, "IPv6address errors");
    233 	strictEqual(components.scheme, undefined, "scheme");
    234 	strictEqual(components.userinfo, undefined, "userinfo");
    235 	strictEqual(components.host, "2001:db8::1", "host");
    236 	strictEqual(components.port, 80, "port");
    237 	strictEqual(components.path, "", "path");
    238 	strictEqual(components.query, undefined, "query");
    239 	strictEqual(components.fragment, undefined, "fragment");
    240 
    241 	//IPv6address with zone identifier, RFC 6874
    242 	components = URI.parse("//[fe80::a%25en1]");
    243 	strictEqual(components.error, undefined, "IPv4address errors");
    244 	strictEqual(components.scheme, undefined, "scheme");
    245 	strictEqual(components.userinfo, undefined, "userinfo");
    246 	strictEqual(components.host, "fe80::a%en1", "host");
    247 	strictEqual(components.port, undefined, "port");
    248 	strictEqual(components.path, "", "path");
    249 	strictEqual(components.query, undefined, "query");
    250 	strictEqual(components.fragment, undefined, "fragment");
    251 
    252 	//IPv6address with an unescaped interface specifier, example from pekkanikander (https://github.com/garycourt/uri-js/pull/22)
    253 	components = URI.parse("//[2001:db8::7%en0]");
    254 	strictEqual(components.error, undefined, "IPv6address interface errors");
    255 	strictEqual(components.scheme, undefined, "scheme");
    256 	strictEqual(components.userinfo, undefined, "userinfo");
    257 	strictEqual(components.host, "2001:db8::7%en0", "host");
    258 	strictEqual(components.port, undefined, "port");
    259 	strictEqual(components.path, "", "path");
    260 	strictEqual(components.query, undefined, "query");
    261 	strictEqual(components.fragment, undefined, "fragment");
    262 });
    263 
    264 test("URI Serialization", function () {
    265 	var components = {
    266 		scheme : undefined,
    267 		userinfo : undefined,
    268 		host : undefined,
    269 		port : undefined,
    270 		path : undefined,
    271 		query : undefined,
    272 		fragment : undefined
    273 	};
    274 	strictEqual(URI.serialize(components), "", "Undefined Components");
    275 
    276 	components = {
    277 		scheme : "",
    278 		userinfo : "",
    279 		host : "",
    280 		port : 0,
    281 		path : "",
    282 		query : "",
    283 		fragment : ""
    284 	};
    285 	strictEqual(URI.serialize(components), "//@:0?#", "Empty Components");
    286 
    287 	components = {
    288 		scheme : "uri",
    289 		userinfo : "foo:bar",
    290 		host : "example.com",
    291 		port : 1,
    292 		path : "path",
    293 		query : "query",
    294 		fragment : "fragment"
    295 	};
    296 	strictEqual(URI.serialize(components), "uri://foo:bar@example.com:1/path?query#fragment", "All Components");
    297 
    298 	strictEqual(URI.serialize({path:"//path"}), "/%2Fpath", "Double slash path");
    299 	strictEqual(URI.serialize({path:"foo:bar"}), "foo%3Abar", "Colon path");
    300 	strictEqual(URI.serialize({path:"?query"}), "%3Fquery", "Query path");
    301 
    302 	//mixed IPv4address & reg-name, example from terion-name (https://github.com/garycourt/uri-js/issues/4)
    303 	strictEqual(URI.serialize({host:"10.10.10.10.example.com"}), "//10.10.10.10.example.com", "Mixed IPv4address & reg-name");
    304 
    305 	//IPv6address
    306 	strictEqual(URI.serialize({host:"2001:db8::7"}), "//[2001:db8::7]", "IPv6 Host");
    307 	strictEqual(URI.serialize({host:"::ffff:129.144.52.38"}), "//[::ffff:129.144.52.38]", "IPv6 Mixed Host");
    308 	strictEqual(URI.serialize({host:"2606:2800:220:1:248:1893:25c8:1946"}), "//[2606:2800:220:1:248:1893:25c8:1946]", "IPv6 Full Host");
    309 
    310 	//IPv6address with zone identifier, RFC 6874
    311 	strictEqual(URI.serialize({host:"fe80::a%en1"}), "//[fe80::a%25en1]", "IPv6 Zone Unescaped Host");
    312 	strictEqual(URI.serialize({host:"fe80::a%25en1"}), "//[fe80::a%25en1]", "IPv6 Zone Escaped Host");
    313 });
    314 
    315 test("URI Resolving", function () {
    316 	//normal examples from RFC 3986
    317 	var base = "uri://a/b/c/d;p?q";
    318 	strictEqual(URI.resolve(base, "g:h"), "g:h", "g:h");
    319 	strictEqual(URI.resolve(base, "g:h"), "g:h", "g:h");
    320 	strictEqual(URI.resolve(base, "g"), "uri://a/b/c/g", "g");
    321 	strictEqual(URI.resolve(base, "./g"), "uri://a/b/c/g", "./g");
    322 	strictEqual(URI.resolve(base, "g/"), "uri://a/b/c/g/", "g/");
    323 	strictEqual(URI.resolve(base, "/g"), "uri://a/g", "/g");
    324 	strictEqual(URI.resolve(base, "//g"), "uri://g", "//g");
    325 	strictEqual(URI.resolve(base, "?y"), "uri://a/b/c/d;p?y", "?y");
    326 	strictEqual(URI.resolve(base, "g?y"), "uri://a/b/c/g?y", "g?y");
    327 	strictEqual(URI.resolve(base, "#s"), "uri://a/b/c/d;p?q#s", "#s");
    328 	strictEqual(URI.resolve(base, "g#s"), "uri://a/b/c/g#s", "g#s");
    329 	strictEqual(URI.resolve(base, "g?y#s"), "uri://a/b/c/g?y#s", "g?y#s");
    330 	strictEqual(URI.resolve(base, ";x"), "uri://a/b/c/;x", ";x");
    331 	strictEqual(URI.resolve(base, "g;x"), "uri://a/b/c/g;x", "g;x");
    332 	strictEqual(URI.resolve(base, "g;x?y#s"), "uri://a/b/c/g;x?y#s", "g;x?y#s");
    333 	strictEqual(URI.resolve(base, ""), "uri://a/b/c/d;p?q", "");
    334 	strictEqual(URI.resolve(base, "."), "uri://a/b/c/", ".");
    335 	strictEqual(URI.resolve(base, "./"), "uri://a/b/c/", "./");
    336 	strictEqual(URI.resolve(base, ".."), "uri://a/b/", "..");
    337 	strictEqual(URI.resolve(base, "../"), "uri://a/b/", "../");
    338 	strictEqual(URI.resolve(base, "../g"), "uri://a/b/g", "../g");
    339 	strictEqual(URI.resolve(base, "../.."), "uri://a/", "../..");
    340 	strictEqual(URI.resolve(base, "../../"), "uri://a/", "../../");
    341 	strictEqual(URI.resolve(base, "../../g"), "uri://a/g", "../../g");
    342 
    343 	//abnormal examples from RFC 3986
    344 	strictEqual(URI.resolve(base, "../../../g"), "uri://a/g", "../../../g");
    345 	strictEqual(URI.resolve(base, "../../../../g"), "uri://a/g", "../../../../g");
    346 
    347 	strictEqual(URI.resolve(base, "/./g"), "uri://a/g", "/./g");
    348 	strictEqual(URI.resolve(base, "/../g"), "uri://a/g", "/../g");
    349 	strictEqual(URI.resolve(base, "g."), "uri://a/b/c/g.", "g.");
    350 	strictEqual(URI.resolve(base, ".g"), "uri://a/b/c/.g", ".g");
    351 	strictEqual(URI.resolve(base, "g.."), "uri://a/b/c/g..", "g..");
    352 	strictEqual(URI.resolve(base, "..g"), "uri://a/b/c/..g", "..g");
    353 
    354 	strictEqual(URI.resolve(base, "./../g"), "uri://a/b/g", "./../g");
    355 	strictEqual(URI.resolve(base, "./g/."), "uri://a/b/c/g/", "./g/.");
    356 	strictEqual(URI.resolve(base, "g/./h"), "uri://a/b/c/g/h", "g/./h");
    357 	strictEqual(URI.resolve(base, "g/../h"), "uri://a/b/c/h", "g/../h");
    358 	strictEqual(URI.resolve(base, "g;x=1/./y"), "uri://a/b/c/g;x=1/y", "g;x=1/./y");
    359 	strictEqual(URI.resolve(base, "g;x=1/../y"), "uri://a/b/c/y", "g;x=1/../y");
    360 
    361 	strictEqual(URI.resolve(base, "g?y/./x"), "uri://a/b/c/g?y/./x", "g?y/./x");
    362 	strictEqual(URI.resolve(base, "g?y/../x"), "uri://a/b/c/g?y/../x", "g?y/../x");
    363 	strictEqual(URI.resolve(base, "g#s/./x"), "uri://a/b/c/g#s/./x", "g#s/./x");
    364 	strictEqual(URI.resolve(base, "g#s/../x"), "uri://a/b/c/g#s/../x", "g#s/../x");
    365 
    366 	strictEqual(URI.resolve(base, "uri:g"), "uri:g", "uri:g");
    367 	strictEqual(URI.resolve(base, "uri:g", {tolerant:true}), "uri://a/b/c/g", "uri:g");
    368 
    369 	//examples by PAEz
    370 	strictEqual(URI.resolve("//www.g.com/","/adf\ngf"), "//www.g.com/adf%0Agf", "/adf\\ngf");
    371 	strictEqual(URI.resolve("//www.g.com/error\n/bleh/bleh",".."), "//www.g.com/error%0A/", "//www.g.com/error\\n/bleh/bleh");
    372 });
    373 
    374 test("URI Normalizing", function () {
    375 	//test from RFC 3987
    376 	strictEqual(URI.normalize("uri://www.example.org/red%09ros\xE9#red"), "uri://www.example.org/red%09ros%C3%A9#red");
    377 
    378 	//IPv4address
    379 	strictEqual(URI.normalize("//192.068.001.000"), "//192.68.1.0");
    380 
    381 	//IPv6address, example from RFC 3513
    382 	strictEqual(URI.normalize("http://[1080::8:800:200C:417A]/"), "http://[1080::8:800:200c:417a]/");
    383 
    384 	//IPv6address, examples from RFC 5952
    385 	strictEqual(URI.normalize("//[2001:0db8::0001]/"), "//[2001:db8::1]/");
    386 	strictEqual(URI.normalize("//[2001:db8::1:0000:1]/"), "//[2001:db8::1:0:1]/");
    387 	strictEqual(URI.normalize("//[2001:db8:0:0:0:0:2:1]/"), "//[2001:db8::2:1]/");
    388 	strictEqual(URI.normalize("//[2001:db8:0:1:1:1:1:1]/"), "//[2001:db8:0:1:1:1:1:1]/");
    389 	strictEqual(URI.normalize("//[2001:0:0:1:0:0:0:1]/"), "//[2001:0:0:1::1]/");
    390 	strictEqual(URI.normalize("//[2001:db8:0:0:1:0:0:1]/"), "//[2001:db8::1:0:0:1]/");
    391 	strictEqual(URI.normalize("//[2001:DB8::1]/"), "//[2001:db8::1]/");
    392 	strictEqual(URI.normalize("//[0:0:0:0:0:ffff:192.0.2.1]/"), "//[::ffff:192.0.2.1]/");
    393 
    394 	//Mixed IPv4 and IPv6 address
    395 	strictEqual(URI.normalize("//[1:2:3:4:5:6:192.0.2.1]/"), "//[1:2:3:4:5:6:192.0.2.1]/");
    396 	strictEqual(URI.normalize("//[1:2:3:4:5:6:192.068.001.000]/"), "//[1:2:3:4:5:6:192.68.1.0]/");
    397 });
    398 
    399 test("URI Equals", function () {
    400 	//test from RFC 3986
    401 	strictEqual(URI.equal("example://a/b/c/%7Bfoo%7D", "eXAMPLE://a/./b/../b/%63/%7bfoo%7d"), true);
    402 
    403 	//test from RFC 3987
    404 	strictEqual(URI.equal("http://example.org/~user", "http://example.org/%7euser"), true);
    405 });
    406 
    407 test("Escape Component", function () {
    408 	var chr;
    409 	for (var d = 0; d <= 129; ++d) {
    410 		chr = String.fromCharCode(d);
    411 		if (!chr.match(/[\$\&\+\,\;\=]/)) {
    412 			strictEqual(URI.escapeComponent(chr), encodeURIComponent(chr));
    413 		} else {
    414 			strictEqual(URI.escapeComponent(chr), chr);
    415 		}
    416 	}
    417 	strictEqual(URI.escapeComponent("\u00c0"), encodeURIComponent("\u00c0"));
    418 	strictEqual(URI.escapeComponent("\u07ff"), encodeURIComponent("\u07ff"));
    419 	strictEqual(URI.escapeComponent("\u0800"), encodeURIComponent("\u0800"));
    420 	strictEqual(URI.escapeComponent("\u30a2"), encodeURIComponent("\u30a2"));
    421 });
    422 
    423 test("Unescape Component", function () {
    424 	var chr;
    425 	for (var d = 0; d <= 129; ++d) {
    426 		chr = String.fromCharCode(d);
    427 		strictEqual(URI.unescapeComponent(encodeURIComponent(chr)), chr);
    428 	}
    429 	strictEqual(URI.unescapeComponent(encodeURIComponent("\u00c0")), "\u00c0");
    430 	strictEqual(URI.unescapeComponent(encodeURIComponent("\u07ff")), "\u07ff");
    431 	strictEqual(URI.unescapeComponent(encodeURIComponent("\u0800")), "\u0800");
    432 	strictEqual(URI.unescapeComponent(encodeURIComponent("\u30a2")), "\u30a2");
    433 });
    434 
    435 //
    436 // IRI
    437 //
    438 
    439 
    440 
    441 var IRI_OPTION = { iri : true, unicodeSupport : true };
    442 
    443 test("IRI Parsing", function () {
    444 	var components = URI.parse("uri://us\xA0er:pa\uD7FFss@example.com:123/o\uF900ne/t\uFDCFwo.t\uFDF0hree?q1=a1\uF8FF\uE000&q2=a2#bo\uFFEFdy", IRI_OPTION);
    445 	strictEqual(components.error, undefined, "all errors");
    446 	strictEqual(components.scheme, "uri", "scheme");
    447 	//strictEqual(components.authority, "us\xA0er:pa\uD7FFss@example.com:123", "authority");
    448 	strictEqual(components.userinfo, "us\xA0er:pa\uD7FFss", "userinfo");
    449 	strictEqual(components.host, "example.com", "host");
    450 	strictEqual(components.port, 123, "port");
    451 	strictEqual(components.path, "/o\uF900ne/t\uFDCFwo.t\uFDF0hree", "path");
    452 	strictEqual(components.query, "q1=a1\uF8FF\uE000&q2=a2", "query");
    453 	strictEqual(components.fragment, "bo\uFFEFdy", "fragment");
    454 });
    455 
    456 test("IRI Serialization", function () {
    457 	var components = {
    458 		scheme : "uri",
    459 		userinfo : "us\xA0er:pa\uD7FFss",
    460 		host : "example.com",
    461 		port : 123,
    462 		path : "/o\uF900ne/t\uFDCFwo.t\uFDF0hree",
    463 		query : "q1=a1\uF8FF\uE000&q2=a2",
    464 		fragment : "bo\uFFEFdy\uE001"
    465 	};
    466 	strictEqual(URI.serialize(components, IRI_OPTION), "uri://us\xA0er:pa\uD7FFss@example.com:123/o\uF900ne/t\uFDCFwo.t\uFDF0hree?q1=a1\uF8FF\uE000&q2=a2#bo\uFFEFdy%EE%80%81");
    467 });
    468 
    469 test("IRI Normalizing", function () {
    470 	strictEqual(URI.normalize("uri://www.example.org/red%09ros\xE9#red", IRI_OPTION), "uri://www.example.org/red%09ros\xE9#red");
    471 });
    472 
    473 test("IRI Equals", function () {
    474 	//example from RFC 3987
    475 	strictEqual(URI.equal("example://a/b/c/%7Bfoo%7D/ros\xE9", "eXAMPLE://a/./b/../b/%63/%7bfoo%7d/ros%C3%A9", IRI_OPTION), true);
    476 });
    477 
    478 test("Convert IRI to URI", function () {
    479 	//example from RFC 3987
    480 	strictEqual(URI.serialize(URI.parse("uri://www.example.org/red%09ros\xE9#red", IRI_OPTION)), "uri://www.example.org/red%09ros%C3%A9#red");
    481 
    482 	//Internationalized Domain Name conversion via punycode example from RFC 3987
    483 	strictEqual(URI.serialize(URI.parse("uri://r\xE9sum\xE9.example.org", {iri:true, domainHost:true}), {domainHost:true}), "uri://xn--rsum-bpad.example.org");
    484 });
    485 
    486 test("Convert URI to IRI", function () {
    487 	//examples from RFC 3987
    488 	strictEqual(URI.serialize(URI.parse("uri://www.example.org/D%C3%BCrst"), IRI_OPTION), "uri://www.example.org/D\xFCrst");
    489 	strictEqual(URI.serialize(URI.parse("uri://www.example.org/D%FCrst"), IRI_OPTION), "uri://www.example.org/D%FCrst");
    490 	strictEqual(URI.serialize(URI.parse("uri://xn--99zt52a.example.org/%e2%80%ae"), IRI_OPTION), "uri://xn--99zt52a.example.org/%E2%80%AE");  //or uri://\u7D0D\u8C46.example.org/%E2%80%AE
    491 
    492 	//Internationalized Domain Name conversion via punycode example from RFC 3987
    493 	strictEqual(URI.serialize(URI.parse("uri://xn--rsum-bpad.example.org", {domainHost:true}), {iri:true, domainHost:true}), "uri://r\xE9sum\xE9.example.org");
    494 });
    495 
    496 //
    497 // HTTP
    498 //
    499 
    500 if (URI.SCHEMES["http"]) {
    501 
    502 	//module("HTTP");
    503 
    504 	test("HTTP Equals", function () {
    505 		//test from RFC 2616
    506 		strictEqual(URI.equal("http://abc.com:80/~smith/home.html", "http://abc.com/~smith/home.html"), true);
    507 		strictEqual(URI.equal("http://ABC.com/%7Esmith/home.html", "http://abc.com/~smith/home.html"), true);
    508 		strictEqual(URI.equal("http://ABC.com:/%7esmith/home.html", "http://abc.com/~smith/home.html"), true);
    509 		strictEqual(URI.equal("HTTP://ABC.COM", "http://abc.com/"), true);
    510 		//test from RFC 3986
    511 		strictEqual(URI.equal("http://example.com:/", "http://example.com:80/"), true);
    512 	});
    513 
    514 }
    515 
    516 if (URI.SCHEMES["https"]) {
    517 
    518 	//module("HTTPS");
    519 
    520 	test("HTTPS Equals", function () {
    521 		strictEqual(URI.equal("https://example.com", "https://example.com:443/"), true);
    522 		strictEqual(URI.equal("https://example.com:/", "https://example.com:443/"), true);
    523 	});
    524 
    525 }
    526 
    527 //
    528 // URN
    529 //
    530 
    531 if (URI.SCHEMES["urn"]) {
    532 
    533 	//module("URN");
    534 
    535 	test("URN Parsing", function () {
    536 		//example from RFC 2141
    537 		var components = URI.parse("urn:foo:a123,456");
    538 		strictEqual(components.error, undefined, "errors");
    539 		strictEqual(components.scheme, "urn", "scheme");
    540 		//strictEqual(components.authority, undefined, "authority");
    541 		strictEqual(components.userinfo, undefined, "userinfo");
    542 		strictEqual(components.host, undefined, "host");
    543 		strictEqual(components.port, undefined, "port");
    544 		strictEqual(components.path, undefined, "path");
    545 		strictEqual(components.query, undefined, "query");
    546 		strictEqual(components.fragment, undefined, "fragment");
    547 		strictEqual(components.nid, "foo", "nid");
    548 		strictEqual(components.nss, "a123,456", "nss");
    549 	});
    550 
    551 	test("URN Serialization", function () {
    552 		//example from RFC 2141
    553 		var components = {
    554 			scheme : "urn",
    555 			nid : "foo",
    556 			nss : "a123,456"
    557 		};
    558 		strictEqual(URI.serialize(components), "urn:foo:a123,456");
    559 	});
    560 
    561 	test("URN Equals", function () {
    562 		//test from RFC 2141
    563 		strictEqual(URI.equal("urn:foo:a123,456", "urn:foo:a123,456"), true);
    564 		strictEqual(URI.equal("urn:foo:a123,456", "URN:foo:a123,456"), true);
    565 		strictEqual(URI.equal("urn:foo:a123,456", "urn:FOO:a123,456"), true);
    566 		strictEqual(URI.equal("urn:foo:a123,456", "urn:foo:A123,456"), false);
    567 		strictEqual(URI.equal("urn:foo:a123%2C456", "URN:FOO:a123%2c456"), true);
    568 	});
    569 
    570 	test("URN Resolving", function () {
    571 		//example from epoberezkin
    572 		strictEqual(URI.resolve('', 'urn:some:ip:prop'), 'urn:some:ip:prop');
    573 		strictEqual(URI.resolve('#', 'urn:some:ip:prop'), 'urn:some:ip:prop');
    574 		strictEqual(URI.resolve('urn:some:ip:prop', 'urn:some:ip:prop'), 'urn:some:ip:prop');
    575 		strictEqual(URI.resolve('urn:some:other:prop', 'urn:some:ip:prop'), 'urn:some:ip:prop');
    576 	});
    577 
    578 	//
    579 	// URN UUID
    580 	//
    581 
    582 	test("UUID Parsing", function () {
    583 		//example from RFC 4122
    584 		var components = URI.parse("urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6");
    585 		strictEqual(components.error, undefined, "errors");
    586 		strictEqual(components.scheme, "urn", "scheme");
    587 		//strictEqual(components.authority, undefined, "authority");
    588 		strictEqual(components.userinfo, undefined, "userinfo");
    589 		strictEqual(components.host, undefined, "host");
    590 		strictEqual(components.port, undefined, "port");
    591 		strictEqual(components.path, undefined, "path");
    592 		strictEqual(components.query, undefined, "query");
    593 		strictEqual(components.fragment, undefined, "fragment");
    594 		strictEqual(components.nid, "uuid", "nid");
    595 		strictEqual(components.nss, undefined, "nss");
    596 		strictEqual(components.uuid, "f81d4fae-7dec-11d0-a765-00a0c91e6bf6", "uuid");
    597 
    598 		components = URI.parse("urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6");
    599 		notStrictEqual(components.error, undefined, "errors");
    600 	});
    601 
    602 	test("UUID Serialization", function () {
    603 		//example from RFC 4122
    604 		var components = {
    605 			scheme : "urn",
    606 			nid : "uuid",
    607 			uuid : "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"
    608 		};
    609 		strictEqual(URI.serialize(components), "urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6");
    610 
    611 		components = {
    612 			scheme : "urn",
    613 			nid : "uuid",
    614 			uuid : "notauuid-7dec-11d0-a765-00a0c91e6bf6"
    615 		};
    616 		strictEqual(URI.serialize(components), "urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6");
    617 	});
    618 
    619 	test("UUID Equals", function () {
    620 		strictEqual(URI.equal("URN:UUID:F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6", "urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6"), true);
    621 	});
    622 
    623 	test("URN NID Override", function () {
    624 		var components = URI.parse("urn:foo:f81d4fae-7dec-11d0-a765-00a0c91e6bf6", {nid:"uuid"});
    625 		strictEqual(components.error, undefined, "errors");
    626 		strictEqual(components.scheme, "urn", "scheme");
    627 		strictEqual(components.path, undefined, "path");
    628 		strictEqual(components.nid, "foo", "nid");
    629 		strictEqual(components.nss, undefined, "nss");
    630 		strictEqual(components.uuid, "f81d4fae-7dec-11d0-a765-00a0c91e6bf6", "uuid");
    631 
    632 		var components = {
    633 			scheme : "urn",
    634 			nid : "foo",
    635 			uuid : "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"
    636 		};
    637 		strictEqual(URI.serialize(components, {nid:"uuid"}), "urn:foo:f81d4fae-7dec-11d0-a765-00a0c91e6bf6");
    638 	});
    639 }
    640 
    641 //
    642 // Mailto
    643 //
    644 
    645 if (URI.SCHEMES["mailto"]) {
    646 
    647 	//module("Mailto");
    648 
    649 	test("Mailto Parse", function () {
    650 		var components;
    651 
    652 		//tests from RFC 6068
    653 
    654 		components = URI.parse("mailto:chris@example.com");
    655 		strictEqual(components.error, undefined, "error");
    656 		strictEqual(components.scheme, "mailto", "scheme");
    657 		strictEqual(components.userinfo, undefined, "userinfo");
    658 		strictEqual(components.host, undefined, "host");
    659 		strictEqual(components.port, undefined, "port");
    660 		strictEqual(components.path, undefined, "path");
    661 		strictEqual(components.query, undefined, "query");
    662 		strictEqual(components.fragment, undefined, "fragment");
    663 		deepEqual(components.to, ["chris@example.com"], "to");
    664 		strictEqual(components.subject, undefined, "subject");
    665 		strictEqual(components.body, undefined, "body");
    666 		strictEqual(components.headers, undefined, "headers");
    667 
    668 		components = URI.parse("mailto:infobot@example.com?subject=current-issue");
    669 		deepEqual(components.to, ["infobot@example.com"], "to");
    670 		strictEqual(components.subject, "current-issue", "subject");
    671 
    672 		components = URI.parse("mailto:infobot@example.com?body=send%20current-issue");
    673 		deepEqual(components.to, ["infobot@example.com"], "to");
    674 		strictEqual(components.body, "send current-issue", "body");
    675 
    676 		components = URI.parse("mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index");
    677 		deepEqual(components.to, ["infobot@example.com"], "to");
    678 		strictEqual(components.body, "send current-issue\x0D\x0Asend index", "body");
    679 
    680 		components = URI.parse("mailto:list@example.org?In-Reply-To=%3C3469A91.D10AF4C@example.com%3E");
    681 		deepEqual(components.to, ["list@example.org"], "to");
    682 		deepEqual(components.headers, {"In-Reply-To":"<3469A91.D10AF4C@example.com>"}, "headers");
    683 
    684 		components = URI.parse("mailto:majordomo@example.com?body=subscribe%20bamboo-l");
    685 		deepEqual(components.to, ["majordomo@example.com"], "to");
    686 		strictEqual(components.body, "subscribe bamboo-l", "body");
    687 
    688 		components = URI.parse("mailto:joe@example.com?cc=bob@example.com&body=hello");
    689 		deepEqual(components.to, ["joe@example.com"], "to");
    690 		strictEqual(components.body, "hello", "body");
    691 		deepEqual(components.headers, {"cc":"bob@example.com"}, "headers");
    692 
    693 		components = URI.parse("mailto:joe@example.com?cc=bob@example.com?body=hello");
    694 		if (URI.VALIDATE_SUPPORT) ok(components.error, "invalid header fields");
    695 
    696 		components = URI.parse("mailto:gorby%25kremvax@example.com");
    697 		deepEqual(components.to, ["gorby%kremvax@example.com"], "to gorby%kremvax@example.com");
    698 
    699 		components = URI.parse("mailto:unlikely%3Faddress@example.com?blat=foop");
    700 		deepEqual(components.to, ["unlikely?address@example.com"], "to unlikely?address@example.com");
    701 		deepEqual(components.headers, {"blat":"foop"}, "headers");
    702 
    703 		components = URI.parse("mailto:Mike%26family@example.org");
    704 		deepEqual(components.to, ["Mike&family@example.org"], "to Mike&family@example.org");
    705 
    706 		components = URI.parse("mailto:%22not%40me%22@example.org");
    707 		deepEqual(components.to, ['"not@me"@example.org'], "to " + '"not@me"@example.org');
    708 
    709 		components = URI.parse("mailto:%22oh%5C%5Cno%22@example.org");
    710 		deepEqual(components.to, ['"oh\\\\no"@example.org'], "to " + '"oh\\\\no"@example.org');
    711 
    712 		components = URI.parse("mailto:%22%5C%5C%5C%22it's%5C%20ugly%5C%5C%5C%22%22@example.org");
    713 		deepEqual(components.to, ['"\\\\\\"it\'s\\ ugly\\\\\\""@example.org'], "to " + '"\\\\\\"it\'s\\ ugly\\\\\\""@example.org');
    714 
    715 		components = URI.parse("mailto:user@example.org?subject=caf%C3%A9");
    716 		deepEqual(components.to, ["user@example.org"], "to");
    717 		strictEqual(components.subject, "caf\xE9", "subject");
    718 
    719 		components = URI.parse("mailto:user@example.org?subject=%3D%3Futf-8%3FQ%3Fcaf%3DC3%3DA9%3F%3D");
    720 		deepEqual(components.to, ["user@example.org"], "to");
    721 		strictEqual(components.subject, "=?utf-8?Q?caf=C3=A9?=", "subject");  //TODO: Verify this
    722 
    723 		components = URI.parse("mailto:user@example.org?subject=%3D%3Fiso-8859-1%3FQ%3Fcaf%3DE9%3F%3D");
    724 		deepEqual(components.to, ["user@example.org"], "to");
    725 		strictEqual(components.subject, "=?iso-8859-1?Q?caf=E9?=", "subject");  //TODO: Verify this
    726 
    727 		components = URI.parse("mailto:user@example.org?subject=caf%C3%A9&body=caf%C3%A9");
    728 		deepEqual(components.to, ["user@example.org"], "to");
    729 		strictEqual(components.subject, "caf\xE9", "subject");
    730 		strictEqual(components.body, "caf\xE9", "body");
    731 
    732 		if (URI.IRI_SUPPORT) {
    733 			components = URI.parse("mailto:user@%E7%B4%8D%E8%B1%86.example.org?subject=Test&body=NATTO");
    734 			deepEqual(components.to, ["user@xn--99zt52a.example.org"], "to");
    735 			strictEqual(components.subject, "Test", "subject");
    736 			strictEqual(components.body, "NATTO", "body");
    737 		}
    738 
    739 	});
    740 
    741 	test("Mailto Serialize", function () {
    742 		var components;
    743 
    744 		//tests from RFC 6068
    745 		strictEqual(URI.serialize({scheme : "mailto", to : ["chris@example.com"]}), "mailto:chris@example.com");
    746 		strictEqual(URI.serialize({scheme : "mailto", to : ["infobot@example.com"], body : "current-issue"}), "mailto:infobot@example.com?body=current-issue");
    747 		strictEqual(URI.serialize({scheme : "mailto", to : ["infobot@example.com"], body : "send current-issue"}), "mailto:infobot@example.com?body=send%20current-issue");
    748 		strictEqual(URI.serialize({scheme : "mailto", to : ["infobot@example.com"], body : "send current-issue\x0D\x0Asend index"}), "mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index");
    749 		strictEqual(URI.serialize({scheme : "mailto", to : ["list@example.org"], headers : {"In-Reply-To" : "<3469A91.D10AF4C@example.com>"}}), "mailto:list@example.org?In-Reply-To=%3C3469A91.D10AF4C@example.com%3E");
    750 		strictEqual(URI.serialize({scheme : "mailto", to : ["majordomo@example.com"], body : "subscribe bamboo-l"}), "mailto:majordomo@example.com?body=subscribe%20bamboo-l");
    751 		strictEqual(URI.serialize({scheme : "mailto", to : ["joe@example.com"], headers : {"cc" : "bob@example.com", "body" : "hello"}}), "mailto:joe@example.com?cc=bob@example.com&body=hello");
    752 		strictEqual(URI.serialize({scheme : "mailto", to : ["gorby%25kremvax@example.com"]}), "mailto:gorby%25kremvax@example.com");
    753 		strictEqual(URI.serialize({scheme : "mailto", to : ["unlikely%3Faddress@example.com"], headers : {"blat" : "foop"}}), "mailto:unlikely%3Faddress@example.com?blat=foop");
    754 		strictEqual(URI.serialize({scheme : "mailto", to : ["Mike&family@example.org"]}), "mailto:Mike%26family@example.org");
    755 		strictEqual(URI.serialize({scheme : "mailto", to : ['"not@me"@example.org']}), "mailto:%22not%40me%22@example.org");
    756 		strictEqual(URI.serialize({scheme : "mailto", to : ['"oh\\\\no"@example.org']}), "mailto:%22oh%5C%5Cno%22@example.org");
    757 		strictEqual(URI.serialize({scheme : "mailto", to : ['"\\\\\\"it\'s\\ ugly\\\\\\""@example.org']}), "mailto:%22%5C%5C%5C%22it's%5C%20ugly%5C%5C%5C%22%22@example.org");
    758 		strictEqual(URI.serialize({scheme : "mailto", to : ["user@example.org"], subject : "caf\xE9"}), "mailto:user@example.org?subject=caf%C3%A9");
    759 		strictEqual(URI.serialize({scheme : "mailto", to : ["user@example.org"], subject : "=?utf-8?Q?caf=C3=A9?="}), "mailto:user@example.org?subject=%3D%3Futf-8%3FQ%3Fcaf%3DC3%3DA9%3F%3D");
    760 		strictEqual(URI.serialize({scheme : "mailto", to : ["user@example.org"], subject : "=?iso-8859-1?Q?caf=E9?="}), "mailto:user@example.org?subject=%3D%3Fiso-8859-1%3FQ%3Fcaf%3DE9%3F%3D");
    761 		strictEqual(URI.serialize({scheme : "mailto", to : ["user@example.org"], subject : "caf\xE9", body : "caf\xE9"}), "mailto:user@example.org?subject=caf%C3%A9&body=caf%C3%A9");
    762 		if (URI.IRI_SUPPORT) {
    763 			strictEqual(URI.serialize({scheme : "mailto", to : ["us\xE9r@\u7d0d\u8c46.example.org"], subject : "Test", body : "NATTO"}), "mailto:us%C3%A9r@xn--99zt52a.example.org?subject=Test&body=NATTO");
    764 		}
    765 
    766 	});
    767 
    768 	test("Mailto Equals", function () {
    769 		//tests from RFC 6068
    770 		strictEqual(URI.equal("mailto:addr1@an.example,addr2@an.example", "mailto:?to=addr1@an.example,addr2@an.example"), true);
    771 		strictEqual(URI.equal("mailto:?to=addr1@an.example,addr2@an.example", "mailto:addr1@an.example?to=addr2@an.example"), true);
    772 	});
    773 
    774 }