Debian Codegraph @

tests/lib.rs

raw symbols SCIP
1 #![allow(clippy::eq_op)]
2 use oxiri::{Iri, IriRef};
3 #[cfg(feature = "serde")]
4 use serde_test::{assert_de_tokens, assert_de_tokens_error, assert_tokens, Token};
5 use std::collections::hash_map::DefaultHasher;
6 use std::hash::{Hash, Hasher};
7
8 #[test]
9 fn test_parsing() {
10 let examples = [
11 "file://foo",
12 "ftp://ftp.is.co.za/rfc/rfc1808.txt",
13 "http://www.ietf.org/rfc/rfc2396.txt",
14 "ldap://[2001:db8::7]/c=GB?objectClass?one",
15 "mailto:[email protected]",
16 "news:comp.infosystems.www.servers.unix",
17 "tel:+1-816-555-1212",
18 "telnet://192.0.2.16:80/",
19 "urn:oasis:names:specification:docbook:dtd:xml:4.1.2",
20 "http://example.com",
21 "http://example.com/",
22 "http://example.com/foo",
23 "http://example.com/foo/bar",
24 "http://example.com/foo/bar/",
25 "http://example.com/foo/bar?q=1&r=2",
26 "http://example.com/foo/bar/?q=1&r=2",
27 "http://example.com#toto",
28 "http://example.com/#toto",
29 "http://example.com/foo#toto",
30 "http://example.com/foo/bar#toto",
31 "http://example.com/foo/bar/#toto",
32 "http://example.com/foo/bar?q=1&r=2#toto",
33 "http://example.com/foo/bar/?q=1&r=2#toto",
34 "http://example.com/foo/bar/.././baz",
35 "http://a.example/AZaz\u{00C0}\u{00D6}\u{00D8}\u{00F6}\u{00F8}\u{02FF}\u{0370}\u{037D}\u{037F}\u{1FFF}\u{200C}\u{200D}\u{2070}\u{218F}\u{2C00}\u{2FEF}\u{3001}\u{D7FF}\u{FA0E}\u{FDCF}\u{FDF0}\u{FFEF}\u{10000}\u{EFFFD}",
36 "http://a.example/?AZaz\u{E000}\u{F8FF}\u{F0000}\u{FFFFD}\u{100000}\u{10FFFD}\u{00C0}\u{00D6}\u{00D8}\u{00F6}\u{00F8}\u{02FF}\u{0370}\u{037D}\u{037F}\u{1FFF}\u{200C}\u{200D}\u{2070}\u{218F}\u{2C00}\u{2FEF}\u{3001}\u{D7FF}\u{FA0E}\u{FDCF}\u{FDF0}\u{FFEF}\u{10000}\u{EFFFD}",
37 "http://[va.12z]",
38 "http://[vff.B]",
39 "http://[V0.a]"
40 ];
41
42 for e in examples {
43 let unchecked = Iri::parse_unchecked(e);
44 assert_eq!(unchecked, e);
45 match Iri::parse(e) {
46 Ok(iri) => {
47 assert_eq!(unchecked, iri);
48 assert_eq!(unchecked.scheme(), iri.scheme());
49 assert_eq!(unchecked.authority(), iri.authority());
50 assert_eq!(unchecked.path(), iri.path());
51 assert_eq!(unchecked.query(), iri.query());
52 assert_eq!(unchecked.fragment(), iri.fragment());
53 }
54 Err(error) => panic!("{} on IRI {}", error, e),
55 }
56 }
57 }
58
59 #[test]
60 fn test_relative_parsing() {
61 // From https://sourceforge.net/projects/foursuite/ under Apache License
62
63 let examples = [
64 "file:///foo/bar",
65 "mailto:user@host?subject=blah",
66 "dav:", // empty opaque part / rel-path allowed by RFC 2396bis
67 "about:", // empty opaque part / rel-path allowed by RFC 2396bis
68 //
69 // the following test cases are from a Perl script by David A. Wheeler
70 // at http://www.dwheeler.com/secure-programs/url.pl
71 "http://www.yahoo.com",
72 "http://www.yahoo.com/",
73 "http://1.2.3.4/",
74 "http://www.yahoo.com/stuff",
75 "http://www.yahoo.com/stuff/",
76 "http://www.yahoo.com/hello%20world/",
77 "http://www.yahoo.com?name=obi",
78 "http://www.yahoo.com?name=obi+wan&status=jedi",
79 "http://www.yahoo.com?onery",
80 "http://www.yahoo.com#bottom",
81 "http://www.yahoo.com/yelp.html#bottom",
82 "https://www.yahoo.com/",
83 "ftp://www.yahoo.com/",
84 "ftp://www.yahoo.com/hello",
85 "demo.txt",
86 "demo/hello.txt",
87 "demo/hello.txt?query=hello#fragment",
88 "/cgi-bin/query?query=hello#fragment",
89 "/demo.txt",
90 "/hello/demo.txt",
91 "hello/demo.txt",
92 "/",
93 "",
94 "#",
95 "#here",
96 // Wheeler"s script says these are invalid, but they aren"t
97 "http://www.yahoo.com?name=%00%01",
98 "http://www.yaho%6f.com",
99 "http://www.yahoo.com/hello%00world/",
100 "http://www.yahoo.com/hello+world/",
101 "http://www.yahoo.com?name=obi&",
102 "http://www.yahoo.com?name=obi&type=",
103 "http://www.yahoo.com/yelp.html#",
104 "//",
105 // the following test cases are from a Haskell program by Graham Klyne
106 // at http://www.ninebynine.org/Software/HaskellUtils/Network/URITest.hs
107 "http://example.org/aaa/bbb#ccc",
108 "mailto:[email protected]",
109 "mailto:[email protected]#frag",
110 "HTTP://EXAMPLE.ORG/AAA/BBB#CCC",
111 "//example.org/aaa/bbb#ccc",
112 "/aaa/bbb#ccc",
113 "bbb#ccc",
114 "#ccc",
115 "#",
116 "A'C",
117 //-- escapes
118 "http://example.org/aaa%2fbbb#ccc",
119 "http://example.org/aaa%2Fbbb#ccc",
120 "%2F",
121 "?%2F",
122 "#?%2F",
123 "aaa%2Fbbb",
124 //-- ports
125 "http://example.org:80/aaa/bbb#ccc",
126 "http://example.org:/aaa/bbb#ccc",
127 "http://example.org./aaa/bbb#ccc",
128 "http://example.123./aaa/bbb#ccc",
129 //-- bare authority
130 "http://example.org",
131 //-- IPv6 literals (from RFC2732):
132 "http://[FEDC:AA98:7654:3210:FEDC:AA98:7654:3210]:80/index.html",
133 "http://[1080:0:0:0:8:800:200C:417A]/index.html",
134 "http://[3ffe:2a00:100:7031::1]",
135 "http://[1080::8:800:200C:417A]/foo",
136 "http://[::192.9.5.5]/ipng",
137 "http://[::FFFF:129.144.52.38]:80/index.html",
138 "http://[2010:836B:4179::836B:4179]",
139 "//[2010:836B:4179::836B:4179]",
140 //-- Random other things that crop up
141 "http://example/Andrȷ",
142 "file:///C:/DEV/Haskell/lib/HXmlToolbox-3.01/examples/",
143 // iprivate characters are allowed in query
144 "http://a/?\u{E000}",
145 "?\u{E000}",
146 ];
147
148 let base = Iri::parse("http://a/b/c/d;p?q").unwrap();
149 for e in examples {
150 let unchecked = IriRef::parse_unchecked(e);
151 assert_eq!(unchecked, e);
152 match IriRef::parse(e) {
153 Ok(iri) => {
154 assert_eq!(unchecked, iri);
155 assert_eq!(unchecked.scheme(), iri.scheme());
156 assert_eq!(unchecked.authority(), iri.authority());
157 assert_eq!(unchecked.path(), iri.path());
158 assert_eq!(unchecked.query(), iri.query());
159 assert_eq!(unchecked.fragment(), iri.fragment());
160 }
161 Err(error) => panic!("{} on relative IRI {}", error, e),
162 }
163 match base.resolve(e) {
164 Ok(iri) => assert_eq!(base.resolve_unchecked(e), iri),
165 Err(error) => panic!("{} on relative IRI {}", error, e),
166 }
167 }
168 }
169
170 #[test]
171 fn test_wrong_relative_parsing() {
172 // From https://sourceforge.net/projects/foursuite/ under Apache License
173
174 let examples = [
175 "beepbeep\x07\x07",
176 "\n",
177 // "::", // not OK, per Roy Fielding on the W3C uri list on 2004-04-01
178 //
179 // the following test cases are from a Perl script by David A. Wheeler
180 // at http://www.dwheeler.com/secure-programs/url.pl
181 "http://www yahoo.com",
182 "http://www.yahoo.com/hello world/",
183 "http://www.yahoo.com/yelp.html#\"",
184 //
185 // the following test cases are from a Haskell program by Graham Klyne
186 // at http://www.ninebynine.org/Software/HaskellUtils/Network/URITest.hs
187 "[2010:836B:4179::836B:4179]",
188 " ",
189 "%",
190 "A%Z",
191 "%ZZ",
192 "%AZ",
193 "A C",
194 // "A'C",
195 "A`C",
196 "A<C",
197 "A>C",
198 "A^C",
199 "A\\C",
200 "A{C",
201 "A|C",
202 "A}C",
203 "A[C",
204 "A]C",
205 "A[**]C",
206 "http://[xyz]/",
207 "http://]/",
208 "http://example.org/[2010:836B:4179::836B:4179]",
209 "http://example.org/abc#[2010:836B:4179::836B:4179]",
210 "http://example.org/xxx/[qwerty]#a[b]",
211 // from a post to the W3C uri list on 2004-02-17
212 "http://w3c.org:80path1/path2",
213 // relative IRIs do not accept colon in the first path segment
214 ":a/b",
215 // iprivate characters are not allowed in path not in fragment
216 "http://example.com/\u{E000}",
217 "\u{E000}",
218 "http://example.com/#\u{E000}",
219 "#\u{E000}",
220 // bad characters
221 "//\u{FFFF}",
222 "?\u{FFFF}",
223 "/\u{0000}",
224 "?\u{0000}",
225 "#\u{0000}",
226 "/\u{E000}",
227 "/\u{F8FF}",
228 "/\u{F0000}",
229 "/\u{FFFFD}",
230 "/\u{100000}",
231 "/\u{10FFFD}",
232 "?\u{FDEF}",
233 "?\u{FFFF}",
234 "/\u{FDEF}",
235 "/\u{FFFF}",
236 "/\u{1FFFF}",
237 "/\u{2FFFF}",
238 "/\u{3FFFF}",
239 "/\u{4FFFF}",
240 "/\u{5FFFF}",
241 "/\u{6FFFF}",
242 "/\u{7FFFF}",
243 "/\u{8FFFF}",
244 "/\u{9FFFF}",
245 "/\u{AFFFF}",
246 "/\u{BFFFF}",
247 "/\u{CFFFF}",
248 "/\u{DFFFF}",
249 "/\u{EFFFF}",
250 "/\u{FFFFF}",
251 // bad host
252 "http://[/",
253 "http://[::1]a/",
254 // fuzzing bugs
255 "//͏@[]",
256 "//@@",
257 "$:",
258 "-:",
259 ":",
260 // IPvFuture
261 "http://[]",
262 "http://[a]",
263 "http://[vz]",
264 "http://[v11]",
265 "http://[v1.]",
266 "http://[v.a]",
267 "http://[v1.@]",
268 "http://[v1.%01]",
269 "//[v1.ւ]",
270 ];
271
272 let base = Iri::parse("http://a/b/c/d;p?q").unwrap();
273 for e in examples {
274 let result = base.resolve(e);
275 assert!(result.is_err(), "{} is wrongly considered valid", e);
276 }
277 }
278
279 #[test]
280 fn test_wrong_relative_parsing_on_scheme() {
281 let examples = [".///C:::"];
282
283 let base = Iri::parse("x:").unwrap();
284 for e in examples {
285 let result = base.resolve(e);
286 assert!(result.is_err(), "{} is wrongly considered valid", e);
287 }
288 }
289
290 #[test]
291 fn test_resolve_relative_iri() {
292 // From https://sourceforge.net/projects/foursuite/ under Apache License
293
294 let examples = [
295 // http://lists.w3.org/Archives/Public/uri/2004Feb/0114.html
296 ("/.", "http://a/b/c/d;p?q", "http://a/"),
297 ("/.foo", "http://a/b/c/d;p?q", "http://a/.foo"),
298 (".foo", "http://a/b/c/d;p?q", "http://a/b/c/.foo"),
299 // http://gbiv.com/protocols/uri/test/rel_examples1.html
300 // examples from RFC 2396
301 ("g:h", "http://a/b/c/d;p?q", "g:h"),
302 ("g", "http://a/b/c/d;p?q", "http://a/b/c/g"),
303 ("./g", "http://a/b/c/d;p?q", "http://a/b/c/g"),
304 ("g/", "http://a/b/c/d;p?q", "http://a/b/c/g/"),
305 ("/g", "http://a/b/c/d;p?q", "http://a/g"),
306 ("//g", "http://a/b/c/d;p?q", "http://g"),
307 // changed with RFC 2396bis
308 //("?y" , "http://a/b/c/d;p?q", "http://a/b/c/d;p?y"),
309 ("?y", "http://a/b/c/d;p?q", "http://a/b/c/d;p?y"),
310 ("g?y", "http://a/b/c/d;p?q", "http://a/b/c/g?y"),
311 // changed with RFC 2396bis
312 //("#s" , "http://a/b/c/d;p?q", CURRENT_DOC_URI + "#s"),
313 ("#s", "http://a/b/c/d;p?q", "http://a/b/c/d;p?q#s"),
314 ("g#s", "http://a/b/c/d;p?q", "http://a/b/c/g#s"),
315 ("g?y#s", "http://a/b/c/d;p?q", "http://a/b/c/g?y#s"),
316 (";x", "http://a/b/c/d;p?q", "http://a/b/c/;x"),
317 ("g;x", "http://a/b/c/d;p?q", "http://a/b/c/g;x"),
318 ("g;x?y#s", "http://a/b/c/d;p?q", "http://a/b/c/g;x?y#s"),
319 // changed with RFC 2396bis
320 //("" , "http://a/b/c/d;p?q", CURRENT_DOC_URI),
321 ("", "http://a/b/c/d;p?q", "http://a/b/c/d;p?q"),
322 (".", "http://a/b/c/d;p?q", "http://a/b/c/"),
323 ("./", "http://a/b/c/d;p?q", "http://a/b/c/"),
324 ("..", "http://a/b/c/d;p?q", "http://a/b/"),
325 ("../", "http://a/b/c/d;p?q", "http://a/b/"),
326 ("../g", "http://a/b/c/d;p?q", "http://a/b/g"),
327 ("../..", "http://a/b/c/d;p?q", "http://a/"),
328 ("../../", "http://a/b/c/d;p?q", "http://a/"),
329 ("../../g", "http://a/b/c/d;p?q", "http://a/g"),
330 ("../../../g", "http://a/b/c/d;p?q", "http://a/g"),
331 ("../../../../g", "http://a/b/c/d;p?q", "http://a/g"),
332 // changed with RFC 2396bis
333 ("/./g", "http://a/b/c/d;p?q", "http://a/g"),
334 // changed with RFC 2396bis
335 ("/../g", "http://a/b/c/d;p?q", "http://a/g"),
336 ("g.", "http://a/b/c/d;p?q", "http://a/b/c/g."),
337 (".g", "http://a/b/c/d;p?q", "http://a/b/c/.g"),
338 ("g..", "http://a/b/c/d;p?q", "http://a/b/c/g.."),
339 ("..g", "http://a/b/c/d;p?q", "http://a/b/c/..g"),
340 ("./../g", "http://a/b/c/d;p?q", "http://a/b/g"),
341 ("./g/.", "http://a/b/c/d;p?q", "http://a/b/c/g/"),
342 ("g/./h", "http://a/b/c/d;p?q", "http://a/b/c/g/h"),
343 ("g/../h", "http://a/b/c/d;p?q", "http://a/b/c/h"),
344 ("g;x=1/./y", "http://a/b/c/d;p?q", "http://a/b/c/g;x=1/y"),
345 ("g;x=1/../y", "http://a/b/c/d;p?q", "http://a/b/c/y"),
346 ("g?y/./x", "http://a/b/c/d;p?q", "http://a/b/c/g?y/./x"),
347 ("g?y/../x", "http://a/b/c/d;p?q", "http://a/b/c/g?y/../x"),
348 ("g#s/./x", "http://a/b/c/d;p?q", "http://a/b/c/g#s/./x"),
349 ("g#s/../x", "http://a/b/c/d;p?q", "http://a/b/c/g#s/../x"),
350 ("http:g", "http://a/b/c/d;p?q", "http:g"),
351 ("http:", "http://a/b/c/d;p?q", "http:"),
352 // not sure where this one originated
353 ("/a/b/c/./../../g", "http://a/b/c/d;p?q", "http://a/a/g"),
354 // http://gbiv.com/protocols/uri/test/rel_examples2.html
355 // slashes in base URI"s query args
356 ("g", "http://a/b/c/d;p?q=1/2", "http://a/b/c/g"),
357 ("./g", "http://a/b/c/d;p?q=1/2", "http://a/b/c/g"),
358 ("g/", "http://a/b/c/d;p?q=1/2", "http://a/b/c/g/"),
359 ("/g", "http://a/b/c/d;p?q=1/2", "http://a/g"),
360 ("//g", "http://a/b/c/d;p?q=1/2", "http://g"),
361 // changed in RFC 2396bis
362 ("?y", "http://a/b/c/d;p?q=1/2", "http://a/b/c/d;p?y"),
363 ("g?y", "http://a/b/c/d;p?q=1/2", "http://a/b/c/g?y"),
364 ("g?y/./x", "http://a/b/c/d;p?q=1/2", "http://a/b/c/g?y/./x"),
365 (
366 "g?y/../x",
367 "http://a/b/c/d;p?q=1/2",
368 "http://a/b/c/g?y/../x",
369 ),
370 ("g#s", "http://a/b/c/d;p?q=1/2", "http://a/b/c/g#s"),
371 ("g#s/./x", "http://a/b/c/d;p?q=1/2", "http://a/b/c/g#s/./x"),
372 (
373 "g#s/../x",
374 "http://a/b/c/d;p?q=1/2",
375 "http://a/b/c/g#s/../x",
376 ),
377 ("./", "http://a/b/c/d;p?q=1/2", "http://a/b/c/"),
378 ("../", "http://a/b/c/d;p?q=1/2", "http://a/b/"),
379 ("../g", "http://a/b/c/d;p?q=1/2", "http://a/b/g"),
380 ("../../", "http://a/b/c/d;p?q=1/2", "http://a/"),
381 ("../../g", "http://a/b/c/d;p?q=1/2", "http://a/g"),
382 // http://gbiv.com/protocols/uri/test/rel_examples3.html
383 // slashes in path params
384 // all of these changed in RFC 2396bis
385 ("g", "http://a/b/c/d;p=1/2?q", "http://a/b/c/d;p=1/g"),
386 ("./g", "http://a/b/c/d;p=1/2?q", "http://a/b/c/d;p=1/g"),
387 ("g/", "http://a/b/c/d;p=1/2?q", "http://a/b/c/d;p=1/g/"),
388 ("g?y", "http://a/b/c/d;p=1/2?q", "http://a/b/c/d;p=1/g?y"),
389 (";x", "http://a/b/c/d;p=1/2?q", "http://a/b/c/d;p=1/;x"),
390 ("g;x", "http://a/b/c/d;p=1/2?q", "http://a/b/c/d;p=1/g;x"),
391 (
392 "g;x=1/./y",
393 "http://a/b/c/d;p=1/2?q",
394 "http://a/b/c/d;p=1/g;x=1/y",
395 ),
396 (
397 "g;x=1/../y",
398 "http://a/b/c/d;p=1/2?q",
399 "http://a/b/c/d;p=1/y",
400 ),
401 ("./", "http://a/b/c/d;p=1/2?q", "http://a/b/c/d;p=1/"),
402 ("../", "http://a/b/c/d;p=1/2?q", "http://a/b/c/"),
403 ("../g", "http://a/b/c/d;p=1/2?q", "http://a/b/c/g"),
404 ("../../", "http://a/b/c/d;p=1/2?q", "http://a/b/"),
405 ("../../g", "http://a/b/c/d;p=1/2?q", "http://a/b/g"),
406 // http://gbiv.com/protocols/uri/test/rel_examples4.html
407 // double and triple slash, unknown scheme
408 ("g:h", "fred:///s//a/b/c", "g:h"),
409 ("g", "fred:///s//a/b/c", "fred:///s//a/b/g"),
410 ("./g", "fred:///s//a/b/c", "fred:///s//a/b/g"),
411 ("g/", "fred:///s//a/b/c", "fred:///s//a/b/g/"),
412 ("/g", "fred:///s//a/b/c", "fred:///g"), // may change to fred:///s//a/g
413 ("//g", "fred:///s//a/b/c", "fred://g"), // may change to fred:///s//g
414 ("//g/x", "fred:///s//a/b/c", "fred://g/x"), // may change to fred:///s//g/x
415 ("///g", "fred:///s//a/b/c", "fred:///g"),
416 ("./", "fred:///s//a/b/c", "fred:///s//a/b/"),
417 ("../", "fred:///s//a/b/c", "fred:///s//a/"),
418 ("../g", "fred:///s//a/b/c", "fred:///s//a/g"),
419 ("../../", "fred:///s//a/b/c", "fred:///s//"), // may change to fred:///s//a/../
420 ("../../g", "fred:///s//a/b/c", "fred:///s//g"), // may change to fred:///s//a/../g
421 ("../../../g", "fred:///s//a/b/c", "fred:///s/g"), // may change to fred:///s//a/../../g
422 ("../../../../g", "fred:///s//a/b/c", "fred:///g"), // may change to fred:///s//a/../../../g
423 // http://gbiv.com/protocols/uri/test/rel_examples5.html
424 // double and triple slash, well-known scheme
425 ("g:h", "http:///s//a/b/c", "g:h"),
426 ("g", "http:///s//a/b/c", "http:///s//a/b/g"),
427 ("./g", "http:///s//a/b/c", "http:///s//a/b/g"),
428 ("g/", "http:///s//a/b/c", "http:///s//a/b/g/"),
429 ("/g", "http:///s//a/b/c", "http:///g"), // may change to http:///s//a/g
430 ("//g", "http:///s//a/b/c", "http://g"), // may change to http:///s//g
431 ("//g/x", "http:///s//a/b/c", "http://g/x"), // may change to http:///s//g/x
432 ("///g", "http:///s//a/b/c", "http:///g"),
433 ("./", "http:///s//a/b/c", "http:///s//a/b/"),
434 ("../", "http:///s//a/b/c", "http:///s//a/"),
435 ("../g", "http:///s//a/b/c", "http:///s//a/g"),
436 ("../../", "http:///s//a/b/c", "http:///s//"), // may change to http:///s//a/../
437 ("../../g", "http:///s//a/b/c", "http:///s//g"), // may change to http:///s//a/../g
438 ("../../../g", "http:///s//a/b/c", "http:///s/g"), // may change to http:///s//a/../../g
439 ("../../../../g", "http:///s//a/b/c", "http:///g"), // may change to http:///s//a/../../../g
440 // from Dan Connelly"s tests in http://www.w3.org/2000/10/swap/uripath.py
441 ("bar:abc", "foo:xyz", "bar:abc"),
442 ("../abc", "http://example/x/y/z", "http://example/x/abc"),
443 (
444 "http://example/x/abc",
445 "http://example2/x/y/z",
446 "http://example/x/abc",
447 ),
448 ("../r", "http://ex/x/y/z", "http://ex/x/r"),
449 ("q/r", "http://ex/x/y", "http://ex/x/q/r"),
450 ("q/r#s", "http://ex/x/y", "http://ex/x/q/r#s"),
451 ("q/r#s/t", "http://ex/x/y", "http://ex/x/q/r#s/t"),
452 ("ftp://ex/x/q/r", "http://ex/x/y", "ftp://ex/x/q/r"),
453 ("", "http://ex/x/y", "http://ex/x/y"),
454 ("", "http://ex/x/y/", "http://ex/x/y/"),
455 ("", "http://ex/x/y/pdq", "http://ex/x/y/pdq"),
456 ("z/", "http://ex/x/y/", "http://ex/x/y/z/"),
457 (
458 "#Animal",
459 "file:/swap/test/animal.rdf",
460 "file:/swap/test/animal.rdf#Animal",
461 ),
462 ("../abc", "file:/e/x/y/z", "file:/e/x/abc"),
463 (
464 "/example/x/abc",
465 "file:/example2/x/y/z",
466 "file:/example/x/abc",
467 ),
468 ("../r", "file:/ex/x/y/z", "file:/ex/x/r"),
469 ("/r", "file:/ex/x/y/z", "file:/r"),
470 ("q/r", "file:/ex/x/y", "file:/ex/x/q/r"),
471 ("q/r#s", "file:/ex/x/y", "file:/ex/x/q/r#s"),
472 ("q/r#", "file:/ex/x/y", "file:/ex/x/q/r#"),
473 ("q/r#s/t", "file:/ex/x/y", "file:/ex/x/q/r#s/t"),
474 ("ftp://ex/x/q/r", "file:/ex/x/y", "ftp://ex/x/q/r"),
475 ("", "file:/ex/x/y", "file:/ex/x/y"),
476 ("", "file:/ex/x/y/", "file:/ex/x/y/"),
477 ("", "file:/ex/x/y/pdq", "file:/ex/x/y/pdq"),
478 ("z/", "file:/ex/x/y/", "file:/ex/x/y/z/"),
479 (
480 "file://meetings.example.com/cal#m1",
481 "file:/devel/WWW/2000/10/swap/test/reluri-1.n3",
482 "file://meetings.example.com/cal#m1",
483 ),
484 (
485 "file://meetings.example.com/cal#m1",
486 "file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/reluri-1.n3",
487 "file://meetings.example.com/cal#m1",
488 ),
489 ("./#blort", "file:/some/dir/foo", "file:/some/dir/#blort"),
490 ("./#", "file:/some/dir/foo", "file:/some/dir/#"),
491 // Ryan Lee
492 ("./", "http://example/x/abc.efg", "http://example/x/"),
493 // Graham Klyne"s tests
494 // http://www.ninebynine.org/Software/HaskellUtils/Network/UriTest.xls
495 // 01-31 are from Connelly"s cases
496
497 // 32-49
498 ("./q:r", "http://ex/x/y", "http://ex/x/q:r"),
499 ("./p=q:r", "http://ex/x/y", "http://ex/x/p=q:r"),
500 ("?pp/rr", "http://ex/x/y?pp/qq", "http://ex/x/y?pp/rr"),
501 ("y/z", "http://ex/x/y?pp/qq", "http://ex/x/y/z"),
502 ("y?q", "http://ex/x/y?q", "http://ex/x/y?q"),
503 ("/x/y?q", "http://ex?p", "http://ex/x/y?q"),
504 ("c/d", "foo:a/b", "foo:a/c/d"),
505 ("/c/d", "foo:a/b", "foo:/c/d"),
506 ("", "foo:a/b?c#d", "foo:a/b?c"),
507 ("b/c", "foo:a", "foo:b/c"),
508 ("../b/c", "foo:/a/y/z", "foo:/a/b/c"),
509 ("./b/c", "foo:a", "foo:b/c"),
510 ("/./b/c", "foo:a", "foo:/b/c"),
511 ("../../d", "foo://a//b/c", "foo://a/d"),
512 (".", "foo:a", "foo:"),
513 ("..", "foo:a", "foo:"),
514 // 50-57 (cf. TimBL comments --
515 // http://lists.w3.org/Archives/Public/uri/2003Feb/0028.html,
516 // http://lists.w3.org/Archives/Public/uri/2003Jan/0008.html)
517 ("abc", "http://example/x/y%2Fz", "http://example/x/abc"),
518 (
519 "../../x%2Fabc",
520 "http://example/a/x/y/z",
521 "http://example/a/x%2Fabc",
522 ),
523 (
524 "../x%2Fabc",
525 "http://example/a/x/y%2Fz",
526 "http://example/a/x%2Fabc",
527 ),
528 ("abc", "http://example/x%2Fy/z", "http://example/x%2Fy/abc"),
529 ("q%3Ar", "http://ex/x/y", "http://ex/x/q%3Ar"),
530 (
531 "/x%2Fabc",
532 "http://example/x/y%2Fz",
533 "http://example/x%2Fabc",
534 ),
535 ("/x%2Fabc", "http://example/x/y/z", "http://example/x%2Fabc"),
536 (
537 "/x%2Fabc",
538 "http://example/x/y%2Fz",
539 "http://example/x%2Fabc",
540 ),
541 // 70-77
542 (
543 "http://example/a/b?c/../d",
544 "foo:bar",
545 "http://example/a/b?c/../d",
546 ),
547 (
548 "http://example/a/b#c/../d",
549 "foo:bar",
550 "http://example/a/b#c/../d",
551 ),
552 // 82-88
553 ("http:this", "http://example.org/base/uri", "http:this"),
554 ("http:this", "http:base", "http:this"),
555 (
556 "mini1.xml",
557 "file:///C:/DEV/Haskell/lib/HXmlToolbox-3.01/examples/",
558 "file:///C:/DEV/Haskell/lib/HXmlToolbox-3.01/examples/mini1.xml",
559 ),
560 // More bad test by Rio
561 ("?bar", "file:foo", "file:foo?bar"),
562 ("#bar", "file:foo", "file:foo#bar"),
563 ("/lv2.h", "file:foo", "file:/lv2.h"),
564 ("/lv2.h", "file:foo", "file:/lv2.h"),
565 ("///lv2.h", "file:foo", "file:///lv2.h"),
566 ("lv2.h", "file:foo", "file:lv2.h"),
567 ("s", "http://example.com", "http://example.com/s"),
568 (".", "file:", "file:"),
569 ("..", "file:", "file:"),
570 ("./", "file:", "file:"),
571 ("../", "file:", "file:"),
572 ("./.", "file:", "file:"),
573 ("../..", "file:", "file:"),
574 (
575 "http:./examplxm+ns/Seq/exhttpwsa//DtaAccnss/tencile#frag",
576 "http://foo",
577 "http:./examplxm+ns/Seq/exhttpwsa//DtaAccnss/tencile#frag",
578 ),
579 ];
580
581 for (relative, base, output) in examples {
582 let base = Iri::parse(base).unwrap();
583 let output = Iri::parse(output).unwrap();
584 match base.resolve(relative) {
585 Ok(result) => {
586 assert_eq!(
587 result,
588 output,
589 "Resolving of {relative} against {base} is wrong. Found {result} and expecting {output}"
590 );
591 assert_eq!(result.scheme(), output.scheme());
592 assert_eq!(result.authority(), output.authority());
593 assert_eq!(result.path(), output.path());
594 assert_eq!(result.query(), output.query());
595 assert_eq!(result.fragment(), output.fragment());
596 }
597 Err(error) => panic!(
598 "Resolving of {} against {} failed with error: {}",
599 relative, base, error
600 ),
601 }
602 let result = base.resolve_unchecked(relative);
603 assert_eq!(
604 result.as_str(),
605 output,
606 "Lenient resolving of {relative} against {base} is wrong. Found {result} and expecting {output}"
607 );
608 }
609 }
610
611 #[test]
612 fn test_resolve_relative_iri_unchecked() {
613 // Tests from https://github.com/apache/jena/blob/main/jena-iri/src/test/resources/org/apache/jena/iri/test.xml
614 // License: https://github.com/apache/jena/blob/main/LICENSE
615 // Notice: https://github.com/apache/jena/blob/main/NOTICE
616
617 let examples = [
618 ("../foo", "http://host/", "http://host/foo"),
619 ("../foo", "http://host/xyz", "http://host/foo"),
620 ("d/z?x=a", "http://www.example.org/a/b/c/d", "http://www.example.org/a/b/c/d/z?x=a"),
621 ("http://example.com/A", "http://www.example.org/a/b/c/d", "http://example.com/A"),
622 ("", "http://www.example.org/a/b/c/d/", "http://www.example.org/a/b/c/d/"),
623 (".", "http://www.example.org/a/b/c/d/", "http://www.example.org/a/b/c/d/"),
624 ("../../C/D", "http://www.example.org/a/b/c/d/", "http://www.example.org/a/b/C/D"),
625 ("../../c/d/", "http://www.example.org/a/b/c/d/", "http://www.example.org/a/b/c/d/"),
626 ("../../c/d/X#bar", "http://www.example.org/a/b/c/d/", "http://www.example.org/a/b/c/d/X#bar"),
627 ("../../c/d/e/f/g/", "http://www.example.org/a/b/c/d/", "http://www.example.org/a/b/c/d/e/f/g/"),
628 ("../../c/d/z?x=a", "http://www.example.org/a/b/c/d/", "http://www.example.org/a/b/c/d/z?x=a"),
629 // we are not removing dots inside of path ("http://ex.org/../../c/d/z?x=a", "http://www.example.org./a/b/c/d/", "http://ex.org/c/d/z?x=a"),
630 // we are not removing dots inside of path ("http://ex.org/c/./d/z?x=a", "http://www.example.org/a/b/c/d/", "http://ex.org/c/d/z?x=a"),
631 ("http://example.org/#André", "http://www.w3.org/2000/10/rdf-tests/rdfcore/rdf-charmod-uris/test001.rdf", "http://example.org/#André"),
632 ("http://example.org/#Andr%C3%A9", "http://www.w3.org/2000/10/rdf-tests/rdfcore/rdf-charmod-uris/test002.rdf", "http://example.org/#Andr%C3%A9"),
633 ("#Dürst", "http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfms-difference-between-ID-and-about/test2.rdf", "http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfms-difference-between-ID-and-about/test2.rdf#Dürst"),
634 ("#", "base:x", "base:x#"),
635 ("", "file:///C:/Documents and Settings/jjchplb/Local Settings/Temp/test-load-with-41.rdf", "file:///C:/Documents and Settings/jjchplb/Local Settings/Temp/test-load-with-41.rdf"),
636 ("eh:/a", "file:///C:/Documents and Settings/jjchplb/Local Settings/Temp/test-load-with-41.rdf", "eh:/a"),
637 ("#", "file:///C:/eclipse/workspace/jena2/", "file:///C:/eclipse/workspace/jena2/#"),
638 ("", "file:///C:/eclipse/workspace/jena2/", "file:///C:/eclipse/workspace/jena2/"),
639 ("base", "file:///C:/eclipse/workspace/jena2/", "file:///C:/eclipse/workspace/jena2/base"),
640 ("eh://R", "file:///C:/eclipse/workspace/jena2/", "eh://R"),
641 ("eh:/O", "file:///C:/eclipse/workspace/jena2/", "eh:/O"),
642 // we are not aware of file: scheme ("file:foo.n3", "file:///C:/eclipse/workspace/jena2/", "file:///C:/eclipse/workspace/jena2/foo.n3"),
643 // we are not aware of file: scheme("file:model8.n3", "file:///C:/eclipse/workspace/jena2/", "file:///C:/eclipse/workspace/jena2/model8.n3"),
644 ("rdf://test.com#", "file:///C:/eclipse/workspace/jena2/", "rdf://test.com#"),
645 ("z", "file:///C:/eclipse/workspace/jena2/foo.n3", "file:///C:/eclipse/workspace/jena2/z"),
646 ("", "file:///C:/eclipse/workspace/jena2/testing/ARQ/Ask/manifest.ttl", "file:///C:/eclipse/workspace/jena2/testing/ARQ/Ask/manifest.ttl"),
647 ("r-base-prefix-3.ttl", "file:///C:/eclipse/workspace/jena2/testing/ARQ/Basic/manifest.ttl", "file:///C:/eclipse/workspace/jena2/testing/ARQ/Basic/r-base-prefix-3.ttl"),
648 ("r-base-prefix-4.ttl", "file:///C:/eclipse/workspace/jena2/testing/ARQ/Basic/manifest.ttl", "file:///C:/eclipse/workspace/jena2/testing/ARQ/Basic/r-base-prefix-4.ttl"),
649 ("mailto:[email protected]", "file:///C:/eclipse/workspace/jena2/testing/ARQ/Optional/result-opt-1.ttl", "mailto:[email protected]"),
650 ("Bound/manifest.n3", "file:///C:/eclipse/workspace/jena2/testing/ARQ/manifest-arq.ttl", "file:///C:/eclipse/workspace/jena2/testing/ARQ/Bound/manifest.n3"),
651 ("Construct/manifest.ttl", "file:///C:/eclipse/workspace/jena2/testing/ARQ/manifest-arq.ttl", "file:///C:/eclipse/workspace/jena2/testing/ARQ/Construct/manifest.ttl"),
652 ("Dataset/manifest.n3", "file:///C:/eclipse/workspace/jena2/testing/ARQ/manifest-arq.ttl", "file:///C:/eclipse/workspace/jena2/testing/ARQ/Dataset/manifest.n3"),
653 ("mailto:[email protected]", "file:///C:/eclipse/workspace/jena2/testing/DAWG-Approved/examples/ex2-4a.n3", "mailto:[email protected]"),
654 ("ex11.2.3.2_0.rq", "file:///C:/eclipse/workspace/jena2/testing/DAWG/examples/manifest.n3", "file:///C:/eclipse/workspace/jena2/testing/DAWG/examples/ex11.2.3.2_0.rq"),
655 ("urn:/*not_a_comment*/", "file:///C:/eclipse/workspace/jena2/testing/RDQL-ARQ/result-0-01.n3", "urn:/*not_a_comment*/"),
656 ("#y1", "file:///C:/eclipse/workspace/jena2/testing/ontology/bugs/test_hk_06/b.owl", "file:///C:/eclipse/workspace/jena2/testing/ontology/bugs/test_hk_06/b.owl#y1"),
657 ("", "file:///C:/eclipse/workspace/jena2/testing/ontology/bugs/test_hk_06/b.owl", "file:///C:/eclipse/workspace/jena2/testing/ontology/bugs/test_hk_06/b.owl"),
658 ("foo#ClassAC", "file:///C:/eclipse/workspace/jena2/testing/ontology/bugs/test_hk_07A.owl", "file:///C:/eclipse/workspace/jena2/testing/ontology/bugs/foo#ClassAC"),
659 // we are not aware of file: scheme ("file:testing/ontology/bugs/test_hk_07A.owl", "file:///C:/eclipse/workspace/jena2/testing/ontology/bugs/test_hk_07A.owl", "file:///C:/eclipse/workspace/jena2/testing/ontology/bugs/testing/ontology/bugs/test_hk_07A.owl"),
660 ("jason6", "file:///C:/eclipse/workspace/jena2/testing/reasoners/bugs/sbug.rdf", "file:///C:/eclipse/workspace/jena2/testing/reasoners/bugs/jason6"),
661 ("urn:x-propNum100", "file:///C:/eclipse/workspace/jena2/testing/reasoners/bugs/subpropertyModel.n3", "urn:x-propNum100"),
662 ("eh:/V", "file:///C:/eclipse/workspace/jena2/testing/reasoners/bugs/unbroken.n3", "eh:/V"),
663 ("eh:/a", "file:///C:/eclipse/workspace/jena2/testing/reasoners/bugs/unbroken.n3", "eh:/a"),
664 ("", "file:C:\\DOCUME~1\\jjchplb\\LOCALS~1\\Temp\\hedgehog6739.rdf", "file:C:\\DOCUME~1\\jjchplb\\LOCALS~1\\Temp\\hedgehog6739.rdf"),
665 ("http://spoo.net/O", "file:C:\\DOCUME~1\\jjchplb\\LOCALS~1\\Temp\\hedgehog6739.rdf", "http://spoo.net/O"),
666 ("http://spoo.net/S", "file:C:\\DOCUME~1\\jjchplb\\LOCALS~1\\Temp\\hedgehog6739.rdf", "http://spoo.net/S"),
667 ("urn:x-hp:eg/", "file:doc/inference/data/owlDemoSchema.xml", "urn:x-hp:eg/"),
668 ("", "file:testing/abbreviated/relative-uris.rdf", "file:testing/abbreviated/relative-uris.rdf"),
669 (".", "file:testing/abbreviated/relative-uris.rdf", "file:testing/abbreviated/"),
670 ("../../C/D", "file:testing/abbreviated/relative-uris.rdf", "file:C/D"),
671 ("//example.com/A", "file:testing/abbreviated/relative-uris.rdf", "file://example.com/A"),
672 ("/A/B#foo/", "file:testing/abbreviated/relative-uris.rdf", "file:/A/B#foo/"),
673 ("X#bar", "file:testing/abbreviated/relative-uris.rdf", "file:testing/abbreviated/X#bar"),
674 ("e/f/g/", "file:testing/abbreviated/relative-uris.rdf", "file:testing/abbreviated/e/f/g/"),
675 ("http://www.example.org/a/b/c/d/", "file:testing/abbreviated/relative-uris.rdf", "http://www.example.org/a/b/c/d/"),
676 ("z?x=a", "file:testing/abbreviated/relative-uris.rdf", "file:testing/abbreviated/z?x=a"),
677 ("", "file:testing/arp/error-msgs/test06.rdf", "file:testing/arp/error-msgs/test06.rdf"),
678 ("#one", "file:testing/arp/qname-in-ID/bug74_0.rdf", "file:testing/arp/qname-in-ID/bug74_0.rdf#one"),
679 ("#sw:test", "file:testing/arp/qname-in-ID/bug74_0.rdf", "file:testing/arp/qname-in-ID/bug74_0.rdf#sw:test"),
680 ("http://localhost:8080/Repository/QueryAgent/UserOntology/qgen-example-1#", "file:testing/ontology/bugs/test_dk_01.xml", "http://localhost:8080/Repository/QueryAgent/UserOntology/qgen-example-1#"),
681 ("owl#Thing", "file:testing/ontology/bugs/test_dk_01.xml", "file:testing/ontology/bugs/owl#Thing"),
682 ("#__rest3", "file:testing/ontology/bugs/test_oh_01.owl", "file:testing/ontology/bugs/test_oh_01.owl#__rest3"),
683 // Seems wrong ("file:testing/ontology/daml/test-instance-load-classes.daml", "file:testing/ontology/daml/test-instance-load.daml", "file:testing/ontology/daml/testing/ontology/daml/test-instance-load-classes.daml"),
684 ("#Union2", "file:testing/ontology/owl/list-syntax/test-ldp.rdf", "file:testing/ontology/owl/list-syntax/test-ldp.rdf#Union2"),
685 ("urn:foo", "file:testing/reasoners/bugs/cardFPTest.owl", "urn:foo"),
686 ("http://decsai.ugr.es/~ontoserver/bacarex2.owl", "file:testing/reasoners/bugs/deleteBug.owl", "http://decsai.ugr.es/~ontoserver/bacarex2.owl"),
687 ("#A", "file:testing/reasoners/bugs/equivalentClassTest.owl", "file:testing/reasoners/bugs/equivalentClassTest.owl#A"),
688 ("NC:ispinfo", "http://bar.com/irrelevant", "NC:ispinfo"),
689 ("NC:trickMe", "http://bar.com/irrelevant", "NC:trickMe"),
690 ("chrome://messenger/content/mailPrefsOverlay.xul", "http://bar.com/irrelevant", "chrome://messenger/content/mailPrefsOverlay.xul"),
691 ("domain:aol.com", "http://bar.com/irrelevant", "domain:aol.com"),
692 ("http://foo.com/ ", "http://bar.com/irrelevant", "http://foo.com/ "),
693 ("http://foo.com/ ", "http://bar.com/irrelevant", "http://foo.com/ "),
694 ("http://foo.com/ ", "http://bar.com/irrelevant", "http://foo.com/ "),
695 ("http://foo.com/ ", "http://bar.com/irrelevant", "http://foo.com/ "),
696 ("http://foo.com/\t", "http://bar.com/irrelevant", "http://foo.com/\t"),
697 ("http://foo.com/\n\n", "http://bar.com/irrelevant", "http://foo.com/\n\n"),
698 ("http://foo.com/\r", "http://bar.com/irrelevant", "http://foo.com/\r"),
699 ("http://foo.com/'", "http://bar.com/irrelevant", "http://foo.com/'"),
700 ("http://foo.com/<b>boo", "http://bar.com/irrelevant", "http://foo.com/<b>boo"),
701 ("http://foo.com/\"", "http://bar.com/irrelevant", "http://foo.com/\""),
702 ("http://foo.com/", "http://bar.com/irrelevant", "http://foo.com/"),
703 // we are not aware of file: ("file:foo.n3", "file:///C:/eclipse/workspace/jena2/", "file:///C:/eclipse/workspace/jena2/foo.n3"),
704 // we are not aware of file: ("file:model8.n3", "file:///C:/eclipse/workspace/jena2/", "file:///C:/eclipse/workspace/jena2/model8.n3"),
705 // we are not aware of file: ("rdf://test.com#", "file:///C:/eclipse/workspace/jena2/", "rdf://test.com#"),
706 // we are not aware of file: ("file:testing/ontology/bugs/test_hk_07A.owl", "file:///C:/eclipse/workspace/jena2/testing/ontology/bugs/test_hk_07A.owl", "file:///C:/eclipse/workspace/jena2/testing/ontology/bugs/testing/ontology/bugs/test_hk_07A.owl"),
707 // we are not aware of file: ("file:testing/ontology/daml/test-instance-load-classes.daml", "file:testing/ontology/daml/test-instance-load.daml", "file:testing/ontology/daml/testing/ontology/daml/test-instance-load-classes.daml"),
708 ("http://foo.com/", "http://bar.com/irrelevant", "http://foo.com/")
709 ];
710
711 for (relative, base, output) in examples {
712 let base = Iri::parse_unchecked(base);
713 let result = base.resolve_unchecked(relative);
714 assert_eq!(
715 result.as_str(),
716 output,
717 "Resolving of {relative} against {base} is wrong. Found {result} and expecting {output}"
718 )
719 }
720 }
721
722 #[test]
723 fn test_relativize_iri() {
724 let examples = [
725 ("http:", "http:", ""),
726 ("http://example.com", "http://example.com", ""),
727 ("http://example.com/foo", "http://example.com/foo", ""),
728 (
729 "http://example.com/foo/bar",
730 "http://example.com/foo/bar",
731 "",
732 ),
733 (
734 "http://example.com/foo/bar?bat",
735 "http://example.com/foo/bar?bat",
736 "",
737 ),
738 (
739 "http://example.com/foo/bar?bat#baz",
740 "http://example.com/foo/bar?bat#baz",
741 "#baz",
742 ),
743 ("http:", "https:", "http:"),
744 ("http://example.com", "http://example.org", "//example.com"),
745 ("http://example.com/foo", "http://example.com/bar", "foo"),
746 (
747 "http://example.com/foo?bat",
748 "http://example.com/foo?foo",
749 "?bat",
750 ),
751 (
752 "http://example.com/foo?bat#baz",
753 "http://example.com/foo?bat#foo",
754 "#baz",
755 ),
756 ("http://example.com", "http:", "//example.com"),
757 ("http://example.com", "http://", "//example.com"),
758 ("http://example.com", "http://example.org", "//example.com"),
759 ("http://example.com/foo", "http://example.com/", "foo"),
760 ("http://example.com/foo", "http://example.com/bar", "foo"),
761 (
762 "http://example.com/foo",
763 "http://example.com/bar/baz",
764 "/foo",
765 ),
766 (
767 "http://example.com/foo/bar",
768 "http://example.com/foo/baz",
769 "bar",
770 ),
771 (
772 "http://example.com/foo/bar",
773 "http://example.com/foo",
774 "foo/bar",
775 ),
776 (
777 "http://example.com/foo?bar",
778 "http://example.com/foo?baz",
779 "?bar",
780 ),
781 (
782 "http://example.com?bar",
783 "http://example.com/a",
784 "//example.com?bar",
785 ),
786 ("http://example.com?bar", "http://example.com", "?bar"),
787 (
788 "http://example.com?bar",
789 "http://example.com/",
790 "//example.com?bar",
791 ),
792 (
793 "http://example.com/foo#bar",
794 "http://example.com/foo#baz",
795 "#bar",
796 ),
797 ("http://example.com/foo/", "http://example.com/foo/bar", "."),
798 ("http://example.com/:", "http://example.com/foo", "/:"),
799 ("http:", "http://example.com", "http:"),
800 ("http:?foo", "http://example.com", "http:?foo"),
801 (
802 "http://example.com",
803 "http://example.com/foo",
804 "//example.com",
805 ),
806 (
807 "http://example.com",
808 "http://example.com?query",
809 "//example.com",
810 ),
811 (
812 "http://example.com/foo",
813 "http://example.com/foo?query",
814 "foo",
815 ),
816 ("http:?query", "http://example.com?query", "http:?query"),
817 ("http:/path", "http://example.com/foo", "http:/path"),
818 (
819 "http://example.com//a",
820 "http://example.com/",
821 "//example.com//a",
822 ),
823 ("urn:ab", "urn:", "ab"),
824 ("urn:isbn:foo", "urn:", "urn:isbn:foo"),
825 ("urn:is/bn:foo", "urn:", "is/bn:foo"),
826 ("t:e/e/p", "t:e/s", "e/p"),
827 ("htt:/foo/gp", "htt:/foo/", "gp"),
828 ("htt:/gp", "htt:/", "gp"),
829 ("x:", "x://foo", "x:"),
830 ("x:", "x:02", "x:"),
831 ("x:", "x:?foo", "x:"),
832 ("x:", "x:?foo", "x:"),
833 ("http://example.com", "http://example.com#foo", ""),
834 ("http://example.com/a/", "http://example.com/a/b", "."),
835 ("http://example.com/a/?c", "http://example.com/a/b", ".?c"),
836 ("t:o//", "t:o/", "t:o//"),
837 ];
838
839 for (original, base, output) in examples {
840 let original = Iri::parse(original).unwrap();
841 let base = Iri::parse(base).unwrap();
842 let output = IriRef::parse(output).unwrap();
843 let actual = base.relativize(&original).unwrap();
844 assert_eq!(
845 actual, output,
846 "Relativizing {original} against {base} gives {actual} and not {output}"
847 );
848 assert_eq!(actual.scheme(), output.scheme());
849 assert_eq!(actual.authority(), output.authority());
850 assert_eq!(actual.path(), output.path());
851 assert_eq!(actual.query(), output.query());
852 assert_eq!(actual.fragment(), output.fragment());
853 let resolved = base.resolve(actual.as_str()).unwrap();
854 assert_eq!(
855 resolved, original,
856 "Resolving {actual} against {base} gives {resolved} and not {original}"
857 );
858 }
859 }
860
861 #[test]
862 fn test_relativize_iri_fails() {
863 let examples = [
864 ("http://example.com/a/../b", "http://example.com/s"),
865 ("http://example.com/a/..", "http://example.com/s"),
866 ("http://example.com/./b", "http://example.com/s"),
867 ("http://example.com/.", "http://example.com/s"),
868 ("urn:.", "urn:"),
869 ];
870
871 for (iri, base) in examples {
872 let iri = Iri::parse(iri).unwrap();
873 let base = Iri::parse(base).unwrap();
874 assert!(
875 base.relativize(&iri).is_err(),
876 "Relativize {iri} against {base} is not properly failing"
877 );
878 }
879 }
880
881 #[test]
882 fn test_eq() {
883 let iri = Iri::parse("http://example.com").unwrap();
884 assert_eq!(iri, iri);
885 assert_eq!(iri, "http://example.com");
886 assert_eq!("http://example.com", iri);
887 assert_eq!(hash(iri), hash("http://example.com"));
888 }
889
890 fn hash(value: impl Hash) -> u64 {
891 let mut hasher = DefaultHasher::new();
892 value.hash(&mut hasher);
893 hasher.finish()
894 }
895
896 #[test]
897 fn test_str() {
898 let iri = Iri::parse("http://example.com").unwrap();
899 assert!(iri.starts_with("http://"));
900 }
901
902 #[cfg(feature = "serde")]
903 #[test]
904 fn test_iriref_serde_impl() {
905 assert_tokens(
906 &IriRef::parse("//example.com").unwrap(),
907 &[Token::BorrowedStr("//example.com")],
908 );
909 assert_tokens(
910 &IriRef::parse("//example.com".to_string()).unwrap(),
911 &[Token::String("//example.com")],
912 );
913 assert_de_tokens(
914 &IriRef::parse("//example.com".to_string()).unwrap(),
915 &[Token::BorrowedStr("//example.com")],
916 );
917 assert_de_tokens_error::<IriRef<String>>(
918 &[Token::String(":")],
919 "No scheme found in an absolute IRI",
920 );
921 }
922
923 #[cfg(feature = "serde")]
924 #[test]
925 fn test_iri_serde_impl() {
926 assert_tokens(
927 &Iri::parse("http://example.com").unwrap(),
928 &[Token::BorrowedStr("http://example.com")],
929 );
930 assert_tokens(
931 &Iri::parse("http://example.com".to_string()).unwrap(),
932 &[Token::String("http://example.com")],
933 );
934 assert_de_tokens(
935 &Iri::parse("http://example.com".to_string()).unwrap(),
936 &[Token::BorrowedStr("http://example.com")],
937 );
938 assert_de_tokens_error::<Iri<String>>(
939 &[Token::String(":")],
940 "No scheme found in an absolute IRI",
941 );
942 assert_de_tokens_error::<Iri<String>>(
943 &[Token::String("//example.com")],
944 "No scheme found in an absolute IRI",
945 );
946 }