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