]> Lady’s Gitweb - Pisces/blob - iri.test.js
a586629ff1fbbf0bb45cabc1db6f33cf8d7cda2b
[Pisces] / iri.test.js
1 // ♓🌟 Piscēs ∷ iri.test.js
2 // ====================================================================
3 //
4 // Copyright © 2020–2022 Lady [@ Lady’s Computer].
5 //
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
9
10 import {
11 composeReference,
12 escapeForIRI,
13 escapeForURI,
14 isAbsoluteIRI,
15 isAbsoluteLEIRI,
16 isAbsoluteURI,
17 isIRI,
18 isIRIReference,
19 isLEIRI,
20 isLEIRIReference,
21 isURI,
22 isURIReference,
23 parseReference,
24 removeDotSegments,
25 resolveReference,
26 } from "./iri.js";
27 import { assertEquals, assertStrictEquals } from "./dev-deps.js";
28
29 const exampleURIReferences = {
30 "ftp://ftp.is.co.za/rfc/rfc1808.txt": {
31 scheme: "ftp",
32 authority: "ftp.is.co.za",
33 path: "/rfc/rfc1808.txt",
34 },
35 "http://www.ietf.org/rfc/rfc2396.txt": {
36 scheme: "http",
37 authority: "www.ietf.org",
38 path: "/rfc/rfc2396.txt",
39 },
40 "ldap://[2001:db8::7]/c=GB?objectClass?one": {
41 scheme: "ldap",
42 authority: "[2001:db8::7]",
43 path: "/c=GB",
44 query: "objectClass?one",
45 },
46 "mailto:John.Doe@example.com": {
47 scheme: "mailto",
48 path: "John.Doe@example.com",
49 },
50 "news:comp.infosystems.www.servers.unix": {
51 scheme: "news",
52 path: "comp.infosystems.www.servers.unix",
53 },
54 "tel:+1-816-555-1212": {
55 scheme: "tel",
56 path: "+1-816-555-1212",
57 },
58 "telnet://192.0.2.16:80/": {
59 scheme: "telnet",
60 authority: "192.0.2.16:80",
61 path: "/",
62 },
63 "urn:oasis:names:specification:docbook:dtd:xml:4.1.2": {
64 scheme: "urn",
65 path: "oasis:names:specification:docbook:dtd:xml:4.1.2",
66 },
67 "foo://example.com:8042/over/there?name=ferret#nose": {
68 scheme: "foo",
69 authority: "example.com:8042",
70 path: "/over/there",
71 query: "name=ferret",
72 fragment: "nose",
73 },
74 "./this:that": {
75 path: "./this:that",
76 },
77 };
78
79 const exampleIRIReferences = {
80 ...exampleURIReferences,
81 "http://ヒキワリ.ナットウ.ニホン": {
82 scheme: "http",
83 authority: "ヒキワリ.ナットウ.ニホン",
84 path: "",
85 },
86 "http://JP納豆.例.jp/dir1/引き割り.html": {
87 scheme: "http",
88 authority: "JP納豆.例.jp",
89 path: "/dir1/引き割り.html",
90 },
91 "/dir1/引き割り.html": {
92 path: "/dir1/引き割り.html",
93 },
94 };
95
96 const exampleLEIRIReferences = {
97 ...exampleIRIReferences,
98 "http://example.com/ foo /": {
99 scheme: "http",
100 authority: "example.com",
101 path: "/ foo /",
102 },
103 "\0": {
104 path: "\0",
105 },
106 };
107
108 const exampleReferences = {
109 ...exampleLEIRIReferences,
110 "\uD800": {},
111 "\uFFFE": {},
112 "\uFFFF": {},
113 };
114
115 Deno.test({
116 name: "Identifies U·R·Is.",
117 fn: () => {
118 for (
119 const [uri, { scheme }] of Object.entries(exampleReferences)
120 ) {
121 assertStrictEquals(
122 isURI(uri),
123 uri in exampleURIReferences && scheme != null,
124 );
125 }
126 },
127 });
128
129 Deno.test({
130 name: "Identifies absolute U·R·Is.",
131 fn: () => {
132 for (
133 const [uri, { scheme, fragment }] of Object.entries(
134 exampleReferences,
135 )
136 ) {
137 assertStrictEquals(
138 isAbsoluteURI(uri),
139 uri in exampleURIReferences && scheme != null &&
140 fragment == null,
141 );
142 }
143 },
144 });
145
146 Deno.test({
147 name: "Identifies U·R·I references.",
148 fn: () => {
149 for (const uri of Object.keys(exampleReferences)) {
150 assertStrictEquals(
151 isURIReference(uri),
152 uri in exampleURIReferences,
153 );
154 }
155 },
156 });
157
158 Deno.test({
159 name: "Identifies I·R·Is.",
160 fn: () => {
161 for (
162 const [iri, { scheme }] of Object.entries(exampleReferences)
163 ) {
164 assertStrictEquals(
165 isIRI(iri),
166 iri in exampleIRIReferences && scheme != null,
167 );
168 }
169 },
170 });
171
172 Deno.test({
173 name: "Identifies absolute I·R·Is.",
174 fn: () => {
175 for (
176 const [iri, { scheme, fragment }] of Object.entries(
177 exampleReferences,
178 )
179 ) {
180 assertStrictEquals(
181 isAbsoluteIRI(iri),
182 iri in exampleIRIReferences && scheme != null &&
183 fragment == null,
184 );
185 }
186 },
187 });
188
189 Deno.test({
190 name: "Identifies I·R·I references.",
191 fn: () => {
192 for (const iri of Object.keys(exampleReferences)) {
193 assertStrictEquals(
194 isIRIReference(iri),
195 iri in exampleIRIReferences,
196 );
197 }
198 },
199 });
200
201 Deno.test({
202 name: "Identifies L·E·I·R·Is.",
203 fn: () => {
204 for (
205 const [leiri, { scheme }] of Object.entries(exampleReferences)
206 ) {
207 assertStrictEquals(
208 isLEIRI(leiri),
209 leiri in exampleLEIRIReferences && scheme != null,
210 );
211 }
212 },
213 });
214
215 Deno.test({
216 name: "Identifies absolute L·E·I·R·Is.",
217 fn: () => {
218 for (
219 const [leiri, { scheme, fragment }] of Object.entries(
220 exampleReferences,
221 )
222 ) {
223 assertStrictEquals(
224 isAbsoluteLEIRI(leiri),
225 leiri in exampleLEIRIReferences && scheme != null &&
226 fragment == null,
227 );
228 }
229 },
230 });
231
232 Deno.test({
233 name: "Identifies L·E·I·R·I references.",
234 fn: () => {
235 for (const leiri of Object.keys(exampleReferences)) {
236 assertStrictEquals(
237 isLEIRIReference(leiri),
238 leiri in exampleLEIRIReferences,
239 );
240 }
241 },
242 });
243
244 Deno.test({
245 name: "Correctly parses references.",
246 fn: () => {
247 for (const [iri, value] of Object.entries(exampleReferences)) {
248 assertEquals(parseReference(iri), {
249 scheme: undefined,
250 authority: undefined,
251 path: undefined,
252 query: undefined,
253 fragment: undefined,
254 ...value,
255 });
256 }
257 },
258 });
259
260 Deno.test({
261 name: "Correctly composes references.",
262 fn: () => {
263 for (
264 const [iri, value] of Object.entries(exampleLEIRIReferences)
265 ) {
266 assertStrictEquals(composeReference(value), iri);
267 }
268 },
269 });
270
271 Deno.test({
272 name: "Converts (L·E·)I·R·Is to U·R·Is.",
273 fn: () => {
274 assertStrictEquals(
275 escapeForURI("/dir1/引き割り.html"),
276 "/dir1/%E5%BC%95%E3%81%8D%E5%89%B2%E3%82%8A.html",
277 );
278 assertStrictEquals(
279 escapeForURI(" æ\0"),
280 "%20%C3%A6%00",
281 );
282 assertStrictEquals(
283 escapeForURI("\u{F0000}?\u{F0000}#\u{F0000}"),
284 "%F3%B0%80%80?%F3%B0%80%80#%F3%B0%80%80",
285 );
286 },
287 });
288
289 Deno.test({
290 name: "Converts L·E·I·R·Is to I·R·Is.",
291 fn: () => {
292 assertStrictEquals(
293 escapeForIRI(" æ\0"),
294 "%20æ%00",
295 );
296 assertStrictEquals(
297 escapeForIRI("\u{F0000}?\u{F0000}#\u{F0000}"),
298 "%F3%B0%80%80?\u{F0000}#%F3%B0%80%80",
299 );
300 },
301 });
302
303 Deno.test({
304 name: "Correctly removes dot segments.",
305 fn: () => {
306 assertStrictEquals(removeDotSegments("/a/b/c/./../../g"), "/a/g");
307 assertStrictEquals(
308 removeDotSegments("mid/content=5/../6"),
309 "mid/6",
310 );
311 },
312 });
313
314 Deno.test({
315 name: "Correctly resolves references.",
316 fn: () => {
317 const base = "http://a/b/c/d;p?q";
318 assertStrictEquals(resolveReference("g:h", base), "g:h");
319 assertStrictEquals(resolveReference("g", base), "http://a/b/c/g");
320 assertStrictEquals(
321 resolveReference("./g", base),
322 "http://a/b/c/g",
323 );
324 assertStrictEquals(
325 resolveReference("g/", base),
326 "http://a/b/c/g/",
327 );
328 assertStrictEquals(resolveReference("/g", base), "http://a/g");
329 assertStrictEquals(resolveReference("//g", base), "http://g");
330 assertStrictEquals(
331 resolveReference("?y", base),
332 "http://a/b/c/d;p?y",
333 );
334 assertStrictEquals(
335 resolveReference("g?y", base),
336 "http://a/b/c/g?y",
337 );
338 assertStrictEquals(
339 resolveReference("#s", base),
340 "http://a/b/c/d;p?q#s",
341 );
342 assertStrictEquals(
343 resolveReference("g#s", base),
344 "http://a/b/c/g#s",
345 );
346 assertStrictEquals(
347 resolveReference("g?y#s", base),
348 "http://a/b/c/g?y#s",
349 );
350 assertStrictEquals(
351 resolveReference(";x", base),
352 "http://a/b/c/;x",
353 );
354 assertStrictEquals(
355 resolveReference("g;x", base),
356 "http://a/b/c/g;x",
357 );
358 assertStrictEquals(
359 resolveReference("g;x?y#s", base),
360 "http://a/b/c/g;x?y#s",
361 );
362 assertStrictEquals(
363 resolveReference("", base),
364 "http://a/b/c/d;p?q",
365 );
366 assertStrictEquals(resolveReference(".", base), "http://a/b/c/");
367 assertStrictEquals(resolveReference("./", base), "http://a/b/c/");
368 assertStrictEquals(resolveReference("..", base), "http://a/b/");
369 assertStrictEquals(resolveReference("../", base), "http://a/b/");
370 assertStrictEquals(resolveReference("../g", base), "http://a/b/g");
371 assertStrictEquals(resolveReference("../..", base), "http://a/");
372 assertStrictEquals(resolveReference("../../", base), "http://a/");
373 assertStrictEquals(
374 resolveReference("../../g", base),
375 "http://a/g",
376 );
377 assertStrictEquals(
378 resolveReference("../../../g", base),
379 "http://a/g",
380 );
381 assertStrictEquals(
382 resolveReference("../../../../g", base),
383 "http://a/g",
384 );
385 assertStrictEquals(resolveReference("/./g", base), "http://a/g");
386 assertStrictEquals(resolveReference("/../g", base), "http://a/g");
387 assertStrictEquals(
388 resolveReference("g.", base),
389 "http://a/b/c/g.",
390 );
391 assertStrictEquals(
392 resolveReference(".g", base),
393 "http://a/b/c/.g",
394 );
395 assertStrictEquals(
396 resolveReference("g..", base),
397 "http://a/b/c/g..",
398 );
399 assertStrictEquals(
400 resolveReference("..g", base),
401 "http://a/b/c/..g",
402 );
403 assertStrictEquals(
404 resolveReference("./../g", base),
405 "http://a/b/g",
406 );
407 assertStrictEquals(
408 resolveReference("./g/.", base),
409 "http://a/b/c/g/",
410 );
411 assertStrictEquals(
412 resolveReference("g/./h", base),
413 "http://a/b/c/g/h",
414 );
415 assertStrictEquals(
416 resolveReference("g/../h", base),
417 "http://a/b/c/h",
418 );
419 assertStrictEquals(
420 resolveReference("g;x=1/./y", base),
421 "http://a/b/c/g;x=1/y",
422 );
423 assertStrictEquals(
424 resolveReference("g;x=1/../y", base),
425 "http://a/b/c/y",
426 );
427 assertStrictEquals(
428 resolveReference("g?y/./x", base),
429 "http://a/b/c/g?y/./x",
430 );
431 assertStrictEquals(
432 resolveReference("g?y/../x", base),
433 "http://a/b/c/g?y/../x",
434 );
435 assertStrictEquals(
436 resolveReference("g#s/./x", base),
437 "http://a/b/c/g#s/./x",
438 );
439 assertStrictEquals(
440 resolveReference("g#s/../x", base),
441 "http://a/b/c/g#s/../x",
442 );
443 assertStrictEquals(resolveReference("http:g", base), "http:g");
444 },
445 });
This page took 0.080716 seconds and 3 git commands to generate.