From 097c19344320da005297572384bd262f19a759d8 Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Thu, 26 Sep 2024 13:45:30 -0700 Subject: [PATCH] Add tests for percent_encoding --- percent_encoding/src/lib.rs | 118 +++++++++++++++++++++++++++++++++++- 1 file changed, 117 insertions(+), 1 deletion(-) diff --git a/percent_encoding/src/lib.rs b/percent_encoding/src/lib.rs index ee3bc56d..6ea05237 100644 --- a/percent_encoding/src/lib.rs +++ b/percent_encoding/src/lib.rs @@ -138,7 +138,7 @@ pub fn utf8_percent_encode<'a>(input: &'a str, ascii_set: &'static AsciiSet) -> } /// The return type of [`percent_encode`] and [`utf8_percent_encode`]. -#[derive(Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct PercentEncode<'a> { bytes: &'a [u8], ascii_set: &'static AsciiSet, @@ -363,3 +363,119 @@ fn decode_utf8_lossy(input: Cow<'_, [u8]>) -> Cow<'_, str> { } } } + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn percent_encode_byte() { + for i in 0..=0xFF { + let encoded = super::percent_encode_byte(i); + assert_eq!(encoded, alloc::format!("%{i:02X}")); + } + } + + #[test] + fn percent_encode_accepts_ascii_set_ref() { + let encoded = percent_encode(b"foo bar?", &AsciiSet::EMPTY); + assert_eq!(encoded.collect::(), "foo bar?"); + } + + #[test] + fn percent_encode_collect() { + let encoded = percent_encode(b"foo bar?", NON_ALPHANUMERIC); + assert_eq!(encoded.collect::(), String::from("foo%20bar%3F")); + + let encoded = percent_encode(b"\x00\x01\x02\x03", CONTROLS); + assert_eq!(encoded.collect::(), String::from("%00%01%02%03")); + } + + #[test] + fn percent_encode_display() { + let encoded = percent_encode(b"foo bar?", NON_ALPHANUMERIC); + assert_eq!(alloc::format!("{}", encoded), "foo%20bar%3F"); + } + + #[test] + fn percent_encode_cow() { + let encoded = percent_encode(b"foo bar?", NON_ALPHANUMERIC); + assert_eq!(Cow::from(encoded), "foo%20bar%3F"); + } + + #[test] + fn utf8_percent_encode_accepts_ascii_set_ref() { + let encoded = super::utf8_percent_encode("foo bar?", &AsciiSet::EMPTY); + assert_eq!(encoded.collect::(), "foo bar?"); + } + + #[test] + fn utf8_percent_encode() { + assert_eq!( + super::utf8_percent_encode("foo bar?", NON_ALPHANUMERIC), + percent_encode(b"foo bar?", NON_ALPHANUMERIC) + ); + } + + #[test] + fn percent_decode() { + assert_eq!( + super::percent_decode(b"foo%20bar%3f") + .decode_utf8() + .unwrap(), + "foo bar?" + ); + } + + #[test] + fn percent_decode_str() { + assert_eq!( + super::percent_decode_str("foo%20bar%3f") + .decode_utf8() + .unwrap(), + "foo bar?" + ); + } + + #[test] + fn percent_decode_collect() { + let decoded = super::percent_decode(b"foo%20bar%3f"); + assert_eq!(decoded.collect::>(), b"foo bar?"); + } + + #[test] + fn percent_decode_cow() { + let decoded = super::percent_decode(b"foo%20bar%3f"); + assert_eq!(Cow::from(decoded), Cow::Owned::<[u8]>(b"foo bar?".to_vec())); + + let decoded = super::percent_decode(b"foo bar?"); + assert_eq!(Cow::from(decoded), Cow::Borrowed(b"foo bar?")); + } + + #[test] + fn percent_decode_invalid_utf8() { + // Invalid UTF-8 sequence + let decoded = super::percent_decode(b"%00%9F%92%96") + .decode_utf8() + .unwrap_err(); + assert_eq!(decoded.valid_up_to(), 1); + assert_eq!(decoded.error_len(), Some(1)); + } + + #[test] + fn percent_decode_utf8_lossy() { + assert_eq!( + super::percent_decode(b"%F0%9F%92%96").decode_utf8_lossy(), + "💖" + ); + } + + #[test] + fn percent_decode_utf8_lossy_invalid_utf8() { + assert_eq!( + super::percent_decode(b"%00%9F%92%96").decode_utf8_lossy(), + "\u{0}���" + ); + } +}