]> Lady’s Gitweb - Pisces/blob - iri.test.js
51749cdbd840d2791d775b70938065d439ff80b7
[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 iri2uri,
13 isAbsoluteIRI,
14 isAbsoluteURI,
15 isIRI,
16 isIRIReference,
17 isURI,
18 isURIReference,
19 parseReference,
20 removeDotSegments,
21 resolveReference,
22 } from "./iri.js";
23 import {
24 assert,
25 assertEquals,
26 assertStrictEquals,
27 } from "./dev-deps.js";
28
29 const exampleURIs = {
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 };
75
76 const exampleURIReferences = {
77 ...exampleURIs,
78 "./this:that": {
79 path: "./this:that",
80 },
81 };
82
83 const exampleIRIs = {
84 ...exampleURIs,
85 "http://ヒキワリ.ナットウ.ニホン": {
86 scheme: "http",
87 authority: "ヒキワリ.ナットウ.ニホン",
88 path: "",
89 },
90 "http://JP納豆.例.jp/dir1/引き割り.html": {
91 scheme: "http",
92 authority: "JP納豆.例.jp",
93 path: "/dir1/引き割り.html",
94 },
95 };
96
97 const exampleIRIReferences = {
98 ...exampleURIReferences,
99 ...exampleIRIs,
100 };
101
102 Deno.test({
103 name: "Identifies U·R·Is.",
104 fn: () => {
105 for (const uri of Object.keys(exampleURIs)) {
106 assert(isURI(uri));
107 }
108 },
109 });
110
111 Deno.test({
112 name: "Identifies absolute U·R·Is.",
113 fn: () => {
114 for (const [uri, { fragment }] of Object.entries(exampleURIs)) {
115 assertStrictEquals(isAbsoluteURI(uri), fragment == null);
116 }
117 },
118 });
119
120 Deno.test({
121 name: "Identifies U·R·I references.",
122 fn: () => {
123 for (const uri of Object.keys(exampleURIReferences)) {
124 assert(isURIReference(uri));
125 }
126 },
127 });
128
129 Deno.test({
130 name: "Identifies I·R·Is.",
131 fn: () => {
132 for (const iri of Object.keys(exampleIRIs)) {
133 assert(isIRI(iri));
134 }
135 },
136 });
137
138 Deno.test({
139 name: "Identifies absolute I·R·Is.",
140 fn: () => {
141 for (const [iri, { fragment }] of Object.entries(exampleIRIs)) {
142 assertStrictEquals(isAbsoluteIRI(iri), fragment == null);
143 }
144 },
145 });
146
147 Deno.test({
148 name: "Identifies I·R·I references.",
149 fn: () => {
150 for (const iri of Object.keys(exampleIRIReferences)) {
151 assert(isIRIReference(iri));
152 }
153 },
154 });
155
156 Deno.test({
157 name: "Correctly parses references.",
158 fn: () => {
159 for (const [iri, value] of Object.entries(exampleIRIReferences)) {
160 assertEquals(parseReference(iri), {
161 scheme: undefined,
162 authority: undefined,
163 path: undefined,
164 query: undefined,
165 fragment: undefined,
166 ...value,
167 });
168 }
169 },
170 });
171
172 Deno.test({
173 name: "Correctly composes references.",
174 fn: () => {
175 for (const [iri, value] of Object.entries(exampleIRIReferences)) {
176 assertStrictEquals(composeReference(value), iri);
177 }
178 },
179 });
180
181 Deno.test({
182 name: "Converts IRIs to URIs.",
183 fn: () => {
184 assertStrictEquals(
185 iri2uri("/dir1/引き割り.html"),
186 "/dir1/%E5%BC%95%E3%81%8D%E5%89%B2%E3%82%8A.html",
187 );
188 },
189 });
190
191 Deno.test({
192 name: "Correctly removes dot segments.",
193 fn: () => {
194 assertStrictEquals(removeDotSegments("/a/b/c/./../../g"), "/a/g");
195 assertStrictEquals(
196 removeDotSegments("mid/content=5/../6"),
197 "mid/6",
198 );
199 },
200 });
201
202 Deno.test({
203 name: "Correctly resolves references.",
204 fn: () => {
205 const base = "http://a/b/c/d;p?q";
206 assertStrictEquals(resolveReference("g:h", base), "g:h");
207 assertStrictEquals(resolveReference("g", base), "http://a/b/c/g");
208 assertStrictEquals(
209 resolveReference("./g", base),
210 "http://a/b/c/g",
211 );
212 assertStrictEquals(
213 resolveReference("g/", base),
214 "http://a/b/c/g/",
215 );
216 assertStrictEquals(resolveReference("/g", base), "http://a/g");
217 assertStrictEquals(resolveReference("//g", base), "http://g");
218 assertStrictEquals(
219 resolveReference("?y", base),
220 "http://a/b/c/d;p?y",
221 );
222 assertStrictEquals(
223 resolveReference("g?y", base),
224 "http://a/b/c/g?y",
225 );
226 assertStrictEquals(
227 resolveReference("#s", base),
228 "http://a/b/c/d;p?q#s",
229 );
230 assertStrictEquals(
231 resolveReference("g#s", base),
232 "http://a/b/c/g#s",
233 );
234 assertStrictEquals(
235 resolveReference("g?y#s", base),
236 "http://a/b/c/g?y#s",
237 );
238 assertStrictEquals(
239 resolveReference(";x", base),
240 "http://a/b/c/;x",
241 );
242 assertStrictEquals(
243 resolveReference("g;x", base),
244 "http://a/b/c/g;x",
245 );
246 assertStrictEquals(
247 resolveReference("g;x?y#s", base),
248 "http://a/b/c/g;x?y#s",
249 );
250 assertStrictEquals(
251 resolveReference("", base),
252 "http://a/b/c/d;p?q",
253 );
254 assertStrictEquals(resolveReference(".", base), "http://a/b/c/");
255 assertStrictEquals(resolveReference("./", base), "http://a/b/c/");
256 assertStrictEquals(resolveReference("..", base), "http://a/b/");
257 assertStrictEquals(resolveReference("../", base), "http://a/b/");
258 assertStrictEquals(resolveReference("../g", base), "http://a/b/g");
259 assertStrictEquals(resolveReference("../..", base), "http://a/");
260 assertStrictEquals(resolveReference("../../", base), "http://a/");
261 assertStrictEquals(
262 resolveReference("../../g", base),
263 "http://a/g",
264 );
265 assertStrictEquals(
266 resolveReference("../../../g", base),
267 "http://a/g",
268 );
269 assertStrictEquals(
270 resolveReference("../../../../g", base),
271 "http://a/g",
272 );
273 assertStrictEquals(resolveReference("/./g", base), "http://a/g");
274 assertStrictEquals(resolveReference("/../g", base), "http://a/g");
275 assertStrictEquals(
276 resolveReference("g.", base),
277 "http://a/b/c/g.",
278 );
279 assertStrictEquals(
280 resolveReference(".g", base),
281 "http://a/b/c/.g",
282 );
283 assertStrictEquals(
284 resolveReference("g..", base),
285 "http://a/b/c/g..",
286 );
287 assertStrictEquals(
288 resolveReference("..g", base),
289 "http://a/b/c/..g",
290 );
291 assertStrictEquals(
292 resolveReference("./../g", base),
293 "http://a/b/g",
294 );
295 assertStrictEquals(
296 resolveReference("./g/.", base),
297 "http://a/b/c/g/",
298 );
299 assertStrictEquals(
300 resolveReference("g/./h", base),
301 "http://a/b/c/g/h",
302 );
303 assertStrictEquals(
304 resolveReference("g/../h", base),
305 "http://a/b/c/h",
306 );
307 assertStrictEquals(
308 resolveReference("g;x=1/./y", base),
309 "http://a/b/c/g;x=1/y",
310 );
311 assertStrictEquals(
312 resolveReference("g;x=1/../y", base),
313 "http://a/b/c/y",
314 );
315 assertStrictEquals(
316 resolveReference("g?y/./x", base),
317 "http://a/b/c/g?y/./x",
318 );
319 assertStrictEquals(
320 resolveReference("g?y/../x", base),
321 "http://a/b/c/g?y/../x",
322 );
323 assertStrictEquals(
324 resolveReference("g#s/./x", base),
325 "http://a/b/c/g#s/./x",
326 );
327 assertStrictEquals(
328 resolveReference("g#s/../x", base),
329 "http://a/b/c/g#s/../x",
330 );
331 assertStrictEquals(resolveReference("http:g", base), "http:g");
332 },
333 });
This page took 0.127841 seconds and 3 git commands to generate.