tests.js 34 KB

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