| 1 | #![cfg(feature = "alloc")] |
| 2 | |
| 3 | use fluent_uri::{ |
| 4 | resolve::{ResolveError, Resolver}, |
| 5 | Uri, UriRef, |
| 6 | }; |
| 7 | |
| 8 | trait Test { |
| 9 | fn pass(&self, r: &str, res: &str); |
| 10 | fn fail(&self, r: &str, err: ResolveError); |
| 11 | } |
| 12 | |
| 13 | impl Test for Uri<&str> { |
| 14 | #[track_caller] |
| 15 | fn pass(&self, r: &str, expected: &str) { |
| 16 | let r = UriRef::parse(r).unwrap(); |
| 17 | for b in [true, false] { |
| 18 | let resolver = Resolver::with_base(*self).allow_path_underflow(b); |
| 19 | assert_eq!(resolver.resolve(&r).unwrap(), expected); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | #[track_caller] |
| 24 | fn fail(&self, r: &str, expected: ResolveError) { |
| 25 | let r = UriRef::parse(r).unwrap(); |
| 26 | for b in [true, false] { |
| 27 | let resolver = Resolver::with_base(*self).allow_path_underflow(b); |
| 28 | assert_eq!(resolver.resolve(&r).unwrap_err(), expected); |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | #[test] |
| 34 | fn resolve() { |
| 35 | // Examples from Section 5.4 of RFC 3986. |
| 36 | let base = Uri::parse("http://a/b/c/d;p?q").unwrap(); |
| 37 | |
| 38 | base.pass("g:h", "g:h"); |
| 39 | base.pass("g", "http://a/b/c/g"); |
| 40 | base.pass("./g", "http://a/b/c/g"); |
| 41 | base.pass("g/", "http://a/b/c/g/"); |
| 42 | base.pass("/g", "http://a/g"); |
| 43 | base.pass("//g", "http://g"); |
| 44 | base.pass("?y", "http://a/b/c/d;p?y"); |
| 45 | base.pass("g?y", "http://a/b/c/g?y"); |
| 46 | base.pass("#s", "http://a/b/c/d;p?q#s"); |
| 47 | base.pass("g#s", "http://a/b/c/g#s"); |
| 48 | base.pass("g?y#s", "http://a/b/c/g?y#s"); |
| 49 | base.pass(";x", "http://a/b/c/;x"); |
| 50 | base.pass("g;x", "http://a/b/c/g;x"); |
| 51 | base.pass("g;x?y#s", "http://a/b/c/g;x?y#s"); |
| 52 | base.pass("", "http://a/b/c/d;p?q"); |
| 53 | base.pass(".", "http://a/b/c/"); |
| 54 | base.pass("./", "http://a/b/c/"); |
| 55 | base.pass("..", "http://a/b/"); |
| 56 | base.pass("../", "http://a/b/"); |
| 57 | base.pass("../g", "http://a/b/g"); |
| 58 | base.pass("../..", "http://a/"); |
| 59 | base.pass("../../", "http://a/"); |
| 60 | base.pass("../../g", "http://a/g"); |
| 61 | |
| 62 | base.pass("/./g", "http://a/g"); |
| 63 | base.pass("g.", "http://a/b/c/g."); |
| 64 | base.pass(".g", "http://a/b/c/.g"); |
| 65 | base.pass("g..", "http://a/b/c/g.."); |
| 66 | base.pass("..g", "http://a/b/c/..g"); |
| 67 | |
| 68 | base.pass("./../g", "http://a/b/g"); |
| 69 | base.pass("./g/.", "http://a/b/c/g/"); |
| 70 | base.pass("g/./h", "http://a/b/c/g/h"); |
| 71 | base.pass("g/../h", "http://a/b/c/h"); |
| 72 | base.pass("g;x=1/./y", "http://a/b/c/g;x=1/y"); |
| 73 | base.pass("g;x=1/../y", "http://a/b/c/y"); |
| 74 | |
| 75 | base.pass("g?y/./x", "http://a/b/c/g?y/./x"); |
| 76 | base.pass("g?y/../x", "http://a/b/c/g?y/../x"); |
| 77 | base.pass("g#s/./x", "http://a/b/c/g#s/./x"); |
| 78 | base.pass("g#s/../x", "http://a/b/c/g#s/../x"); |
| 79 | |
| 80 | base.pass("http:g", "http:g"); |
| 81 | |
| 82 | // Non-hierarchical base URI. |
| 83 | let base = Uri::parse("foo:bar").unwrap(); |
| 84 | |
| 85 | base.pass("", "foo:bar"); |
| 86 | base.pass("#baz", "foo:bar#baz"); |
| 87 | base.pass("http://example.com/", "http://example.com/"); |
| 88 | base.pass("foo:baz", "foo:baz"); |
| 89 | base.pass("bar:baz", "bar:baz"); |
| 90 | |
| 91 | let base = Uri::parse("foo:/").unwrap(); |
| 92 | // The result would be "foo://@@" using the original algorithm. |
| 93 | base.pass(".//@@", "foo:/.//@@"); |
| 94 | |
| 95 | let base = Uri::parse("foo:/bar/baz/.%2E/").unwrap(); |
| 96 | // The result would be "foo:/bar/baz" using the original algorithm. |
| 97 | base.pass("..", "foo:/"); |
| 98 | |
| 99 | let base = Uri::parse("foo:/bar/..").unwrap(); |
| 100 | // The result would be "foo:/bar/" using the original algorithm. |
| 101 | base.pass(".", "foo:/"); |
| 102 | |
| 103 | let base = base.normalize(); |
| 104 | base.borrow().pass(".", "foo:/"); |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn resolve_error() { |
| 109 | let base = Uri::parse("http://example.com/#title1").unwrap(); |
| 110 | base.fail("foo", ResolveError::BaseWithFragment); |
| 111 | |
| 112 | let base = Uri::parse("foo:bar").unwrap(); |
| 113 | base.fail("baz", ResolveError::InvalidReferenceAgainstOpaqueBase); |
| 114 | base.fail("?baz", ResolveError::InvalidReferenceAgainstOpaqueBase); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn resolve_underflow() { |
| 119 | for (base, rs) in [ |
| 120 | ( |
| 121 | "http://a/b/c/d;p?q", |
| 122 | ["../../../g", "../../../../g", "/../g"], |
| 123 | ), |
| 124 | ("foo:/..", ["", "?a", "#a"]), |
| 125 | ] { |
| 126 | let base = Uri::parse(base).unwrap(); |
| 127 | |
| 128 | for r in rs { |
| 129 | let resolver = Resolver::with_base(base).allow_path_underflow(true); |
| 130 | assert!(resolver.resolve(&UriRef::parse(r).unwrap()).is_ok()); |
| 131 | |
| 132 | let resolver = Resolver::with_base(base).allow_path_underflow(false); |
| 133 | assert_eq!( |
| 134 | resolver.resolve(&UriRef::parse(r).unwrap()).unwrap_err(), |
| 135 | ResolveError::PathUnderflow |
| 136 | ); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | let base = Uri::parse("foo:bar/..").unwrap(); |
| 141 | base.pass("", "foo:bar/.."); |
| 142 | base.fail("?a", ResolveError::InvalidReferenceAgainstOpaqueBase); |
| 143 | base.pass("#a", "foo:bar/..#a"); |
| 144 | } |