1#![feature(matches_macro)]
42
43mod enc;
44pub use enc::encode;
45pub use enc::encode_binary;
46pub use enc::Encoded;
47
48mod dec;
49pub use dec::decode;
50pub use dec::decode_binary;
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55 use crate::dec::from_hex_digit;
56
57 #[test]
58 fn it_encodes_successfully() {
59 let expected = "this%20that";
60 assert_eq!(expected, encode("this that"));
61 }
62
63 #[test]
64 fn it_encodes_successfully_emoji() {
65 let emoji_string = "👾 Exterminate!";
66 let expected = "%F0%9F%91%BE%20Exterminate%21";
67 assert_eq!(expected, encode(emoji_string));
68 }
69
70 #[test]
71 fn it_decodes_successfully() {
72 let expected = String::from("this that");
73 let encoded = "this%20that";
74 assert_eq!(expected, decode(encoded).unwrap());
75 }
76
77 #[test]
78 fn it_decodes_successfully_emoji() {
79 let expected = String::from("👾 Exterminate!");
80 let encoded = "%F0%9F%91%BE%20Exterminate%21";
81 assert_eq!(expected, decode(encoded).unwrap());
82 }
83
84 #[test]
85 fn it_decodes_unsuccessfully_emoji() {
86 let bad_encoded_string = "👾 Exterminate!";
87
88 assert_eq!(bad_encoded_string, decode(bad_encoded_string).unwrap());
89 }
90
91
92 #[test]
93 fn misc() {
94 assert_eq!(3, from_hex_digit(b'3').unwrap());
95 assert_eq!(10, from_hex_digit(b'a').unwrap());
96 assert_eq!(15, from_hex_digit(b'F').unwrap());
97 assert_eq!(None, from_hex_digit(b'G'));
98 assert_eq!(None, from_hex_digit(9));
99
100 assert_eq!("pureascii", encode("pureascii"));
101 assert_eq!("pureascii", decode("pureascii").unwrap());
102 assert_eq!("", encode(""));
103 assert_eq!("", decode("").unwrap());
104 assert_eq!("%26a%25b%21c.d%3Fe", encode("&a%b!c.d?e"));
105 assert_eq!("%00", encode("\0"));
106 assert_eq!("%00x", encode("\0x"));
107 assert_eq!("x%00", encode("x\0"));
108 assert_eq!("x%00x", encode("x\0x"));
109 assert_eq!("aa%00%00bb", encode("aa\0\0bb"));
110 assert_eq!("\0", decode("\0").unwrap());
111 assert!(decode("%F0%0F%91%BE%20Hello%21").is_err());
112 assert_eq!("this that", decode("this%20that").unwrap());
113 assert_eq!("this that%", decode("this%20that%").unwrap());
114 assert_eq!("this that%2", decode("this%20that%2").unwrap());
115 assert_eq!("this that%%", decode("this%20that%%").unwrap());
116 assert_eq!("this that%2%", decode("this%20that%2%").unwrap());
117 assert_eq!("this%2that", decode("this%2that").unwrap());
118 assert_eq!("this%%2that", decode("this%%2that").unwrap());
119 assert_eq!("this%2x&that", decode("this%2x%26that").unwrap());
120 }
122
123 #[test]
124 fn lazy_writer() {
125 let mut s = "he".to_string();
126 Encoded("llo").append_to(&mut s);
127 assert_eq!("hello", s);
128
129 assert_eq!("hello", Encoded("hello").to_string());
130 assert_eq!("hello", format!("{}", Encoded("hello")));
131 assert_eq!("hello", Encoded("hello").to_str());
132 assert!(matches!(Encoded("hello").to_str(), std::borrow::Cow::Borrowed(_)));
133 }
134
135 #[test]
136 fn whatwg_examples() {
137 assert_eq!(*decode_binary(b"%25%s%1G"), b"%%s%1G"[..]);
138 assert_eq!(*decode_binary("‽%25%2E".as_bytes()), b"\xE2\x80\xBD\x25\x2E"[..]);
139 assert_eq!(encode("≡"), "%E2%89%A1");
140 assert_eq!(encode("‽"), "%E2%80%BD");
141 assert_eq!(encode("Say what‽"), "Say%20what%E2%80%BD");
142 }
143
144}