| 1 | #![cfg(feature = "alloc")] |
| 2 | |
| 3 | #[cfg(feature = "net")] |
| 4 | use core::net::{Ipv4Addr, Ipv6Addr}; |
| 5 | |
| 6 | #[cfg(feature = "net")] |
| 7 | use fluent_uri::component::Host; |
| 8 | |
| 9 | use fluent_uri::{IriRef, UriRef}; |
| 10 | |
| 11 | #[test] |
| 12 | fn normalize() { |
| 13 | // Example from Section 6.2 of RFC 3986. |
| 14 | let r = UriRef::parse("eXAMPLE://a/./b/../b/%63/%7bfoo%7d").unwrap(); |
| 15 | assert_eq!(r.normalize(), "example://a/b/c/%7Bfoo%7D"); |
| 16 | |
| 17 | // Examples from Section 6.2.3 of RFC 3986. |
| 18 | |
| 19 | // An empty path is not normalized to "/" for now. |
| 20 | let r = UriRef::parse("http://example.com").unwrap(); |
| 21 | assert_eq!(r.normalize(), "http://example.com"); |
| 22 | |
| 23 | let r = UriRef::parse("http://example.com/").unwrap(); |
| 24 | assert_eq!(r.normalize(), "http://example.com/"); |
| 25 | |
| 26 | let r = UriRef::parse("http://example.com:/").unwrap(); |
| 27 | assert_eq!(r.normalize(), "http://example.com/"); |
| 28 | |
| 29 | let r = UriRef::parse("http://example.com:80/").unwrap(); |
| 30 | assert_eq!(r.normalize(), "http://example.com/"); |
| 31 | |
| 32 | // Lowercase percent-encoded octet. |
| 33 | let r = UriRef::parse("%3a").unwrap(); |
| 34 | assert_eq!(r.normalize(), "%3A"); |
| 35 | |
| 36 | // Uppercase letters in scheme and registered name. |
| 37 | let r = UriRef::parse("HTTP://www.EXAMPLE.com/").unwrap(); |
| 38 | assert_eq!(r.normalize(), "http://www.example.com/"); |
| 39 | |
| 40 | // Underflow in path resolution. |
| 41 | let r = UriRef::parse("http://a/../../../g").unwrap(); |
| 42 | assert_eq!(r.normalize(), "http://a/g"); |
| 43 | |
| 44 | // Percent-encoded dot segments. |
| 45 | let r = UriRef::parse("http://a/b/c/%2E/%2E./%2e%2E/d").unwrap(); |
| 46 | assert_eq!(r.normalize(), "http://a/d"); |
| 47 | |
| 48 | // Don't remove dot segments from relative reference or rootless path. |
| 49 | let r = UriRef::parse("foo/../bar").unwrap(); |
| 50 | assert_eq!(r.normalize(), "foo/../bar"); |
| 51 | |
| 52 | let r = UriRef::parse("/foo/../bar").unwrap(); |
| 53 | assert_eq!(r.normalize(), "/foo/../bar"); |
| 54 | |
| 55 | let r = UriRef::parse("foo:bar/../baz").unwrap(); |
| 56 | assert_eq!(r.normalize(), "foo:bar/../baz"); |
| 57 | |
| 58 | // Do remove dot segments for a URI with absolute path. |
| 59 | let r = UriRef::parse("foo:/bar/./../baz").unwrap(); |
| 60 | assert_eq!(r.normalize(), "foo:/baz"); |
| 61 | |
| 62 | // However, make sure that the output is a valid URI reference. |
| 63 | let r = UriRef::parse("foo:/.//@@").unwrap(); |
| 64 | assert_eq!(r.normalize(), "foo:/.//@@"); |
| 65 | |
| 66 | // Percent-encoded uppercase letters in registered name. |
| 67 | let r = UriRef::parse("HTTP://%45XAMPLE.%43Om").unwrap(); |
| 68 | assert_eq!(r.normalize(), "http://example.com"); |
| 69 | |
| 70 | // Percent-encoded unreserved characters. |
| 71 | let r = UriRef::parse("%41%42%43%44%45%46%47%48%49%4A%4B%4C%4D%4E%4F%50%51%52%53%54%55%56%57%58%59%5A%61%62%63%64%65%66%67%68%69%6A%6B%6C%6D%6E%6F%70%71%72%73%74%75%76%77%78%79%7A%30%31%32%33%34%35%36%37%38%39%2D%2E%5F%7E").unwrap(); |
| 72 | assert_eq!( |
| 73 | r.normalize(), |
| 74 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~" |
| 75 | ); |
| 76 | |
| 77 | // Percent-encoded reserved characters. |
| 78 | let r = UriRef::parse("%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D%25").unwrap(); |
| 79 | assert_eq!(r.normalize(), r); |
| 80 | |
| 81 | // Builder example. |
| 82 | let r = UriRef::parse("foo://[email protected]:8042/over/there?name=ferret#nose").unwrap(); |
| 83 | assert_eq!(r.normalize(), r); |
| 84 | |
| 85 | // Normalization in all components. |
| 86 | let r = UriRef::parse("FOO://%55se%72@EXamp%4ce%2ecom:8042/%4b%2f?%4c%2b#%24%4d").unwrap(); |
| 87 | assert_eq!(r.normalize(), "foo://[email protected]:8042/K%2F?L%2B#%24M"); |
| 88 | |
| 89 | // Normal IPv4 address. |
| 90 | let r = UriRef::parse("//127.0.0.1").unwrap(); |
| 91 | assert_eq!(r.normalize(), "//127.0.0.1"); |
| 92 | #[cfg(feature = "net")] |
| 93 | assert!(matches!( |
| 94 | r.normalize().authority().unwrap().host_parsed(), |
| 95 | Host::Ipv4(Ipv4Addr::LOCALHOST) |
| 96 | )); |
| 97 | |
| 98 | // Percent-encoded IPv4 address. |
| 99 | let r = UriRef::parse("//127.0.0.%31").unwrap(); |
| 100 | assert_eq!(r.normalize(), "//127.0.0.1"); |
| 101 | #[cfg(feature = "net")] |
| 102 | assert!(matches!( |
| 103 | r.normalize().authority().unwrap().host_parsed(), |
| 104 | Host::Ipv4(Ipv4Addr::LOCALHOST) |
| 105 | )); |
| 106 | |
| 107 | // Normal IPv6 address. |
| 108 | let r = UriRef::parse("//[::1]").unwrap(); |
| 109 | assert_eq!(r.normalize(), "//[::1]"); |
| 110 | #[cfg(feature = "net")] |
| 111 | assert!(matches!( |
| 112 | r.normalize().authority().unwrap().host_parsed(), |
| 113 | Host::Ipv6(Ipv6Addr::LOCALHOST) |
| 114 | )); |
| 115 | |
| 116 | // Verbose IPv6 address. |
| 117 | let r = UriRef::parse("//[0000:0000:0000::1]").unwrap(); |
| 118 | assert_eq!(r.normalize(), "//[::1]"); |
| 119 | #[cfg(feature = "net")] |
| 120 | assert!(matches!( |
| 121 | r.normalize().authority().unwrap().host_parsed(), |
| 122 | Host::Ipv6(Ipv6Addr::LOCALHOST) |
| 123 | )); |
| 124 | |
| 125 | // IPv4-mapped IPv6 address. |
| 126 | let r = UriRef::parse("//[0:0:0:0:0:ffff:192.0.2.1]").unwrap(); |
| 127 | assert_eq!(r.normalize(), "//[::ffff:192.0.2.1]"); |
| 128 | |
| 129 | // Deprecated IPv4-compatible IPv6 address. |
| 130 | let r = UriRef::parse("//[::192.0.2.1]").unwrap(); |
| 131 | assert_eq!(r.normalize(), "//[::c000:201]"); |
| 132 | |
| 133 | // IPvFuture address. |
| 134 | let r = UriRef::parse("//[v1FdE.AddR]").unwrap(); |
| 135 | assert_eq!(r.normalize(), "//[v1fde.addr]"); |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn normalize_iri() { |
| 140 | // Example from Section 5.3.2 of RFC 3987. |
| 141 | let r = IriRef::parse("eXAMPLE://a/./b/../b/%63/%7bfoo%7d/ros%C3%A9").unwrap(); |
| 142 | assert_eq!(r.normalize(), "example://a/b/c/%7Bfoo%7D/rosé"); |
| 143 | |
| 144 | // Encoded private character in query. |
| 145 | let r = IriRef::parse("?%EE%80%80").unwrap(); |
| 146 | assert_eq!(r.normalize(), "?\u{e000}"); |
| 147 | } |