| 1 | use fluent_uri::{ConvertError, Iri, IriRef, Uri, UriRef}; |
| 2 | |
| 3 | #[cfg(feature = "alloc")] |
| 4 | #[test] |
| 5 | fn iri_to_uri() { |
| 6 | let iri = Iri::parse("http://résumé.example.org").unwrap(); |
| 7 | assert_eq!(iri.to_uri(), "http://r%C3%A9sum%C3%A9.example.org"); |
| 8 | |
| 9 | let iri = Iri::parse("http://www.example.org/red%09rosé#red").unwrap(); |
| 10 | assert_eq!(iri.to_uri(), "http://www.example.org/red%09ros%C3%A9#red"); |
| 11 | |
| 12 | let iri = Iri::parse("foo://[email protected]:8042/over/there?name=ferret#nose").unwrap(); |
| 13 | assert_eq!(iri.to_uri(), iri.as_str()); |
| 14 | |
| 15 | let iri = Iri::parse("http://example.com/\u{10300}\u{10301}\u{10302}").unwrap(); |
| 16 | assert_eq!( |
| 17 | iri.to_uri(), |
| 18 | "http://example.com/%F0%90%8C%80%F0%90%8C%81%F0%90%8C%82" |
| 19 | ); |
| 20 | } |
| 21 | |
| 22 | #[cfg(feature = "alloc")] |
| 23 | #[test] |
| 24 | fn uri_to_iri() { |
| 25 | let uri = Uri::parse("http://www.example.org/r%E9sum%E9.html").unwrap(); |
| 26 | assert_eq!( |
| 27 | Iri::from(uri).normalize(), |
| 28 | "http://www.example.org/r%E9sum%E9.html" |
| 29 | ); |
| 30 | |
| 31 | let uri = Uri::parse("http://www.example.org/D%C3%BCrst").unwrap(); |
| 32 | assert_eq!(Iri::from(uri).normalize(), "http://www.example.org/Dürst"); |
| 33 | |
| 34 | let uri = Uri::parse("http://www.example.org/D%FCrst").unwrap(); |
| 35 | assert_eq!(Iri::from(uri).normalize(), "http://www.example.org/D%FCrst"); |
| 36 | |
| 37 | let uri = Uri::parse("http://xn--99zt52a.example.org/%e2%80%ae").unwrap(); |
| 38 | // TODO: Determine if we should implement the MUST in Section 4.1 of RFC 3987. |
| 39 | // assert_eq!( |
| 40 | // uri.as_iri().normalize(), |
| 41 | // "http://xn--99zt52a.example.org/%E2%80%AE" |
| 42 | // ); |
| 43 | assert_eq!( |
| 44 | Iri::from(uri).normalize(), |
| 45 | "http://xn--99zt52a.example.org/\u{202e}" |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn convert_error() { |
| 51 | let uri_ref = UriRef::parse("rel/ref").unwrap(); |
| 52 | let e = Uri::try_from(uri_ref).unwrap_err(); |
| 53 | assert_eq!(e, ConvertError::NoScheme); |
| 54 | |
| 55 | let uri_ref = UriRef::parse("").unwrap(); |
| 56 | let e = Uri::try_from(uri_ref).unwrap_err(); |
| 57 | assert_eq!(e, ConvertError::NoScheme); |
| 58 | |
| 59 | let iri = Iri::parse("http://你好.example.com/").unwrap(); |
| 60 | let e = Uri::try_from(iri).unwrap_err(); |
| 61 | assert_eq!(e, ConvertError::NotAscii { index: 7 }); |
| 62 | |
| 63 | let iri_ref = IriRef::parse("réf/rel").unwrap(); |
| 64 | |
| 65 | let e = Uri::try_from(iri_ref).unwrap_err(); |
| 66 | assert_eq!(e, ConvertError::NoScheme); |
| 67 | |
| 68 | let e = UriRef::try_from(iri_ref).unwrap_err(); |
| 69 | assert_eq!(e, ConvertError::NotAscii { index: 1 }); |
| 70 | |
| 71 | let e = Iri::try_from(iri_ref).unwrap_err(); |
| 72 | assert_eq!(e, ConvertError::NoScheme); |
| 73 | } |