Skip to content

Commit

Permalink
(to squash) add unit tests for ReasonPhrase validation
Browse files Browse the repository at this point in the history
  • Loading branch information
acfoltzer committed Mar 23, 2022
1 parent d621c74 commit 4ca1eee
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/ext/h1_reason_phrase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,61 @@ const fn find_invalid_byte(bytes: &[u8]) -> Option<u8> {
while i < bytes.len() {
let b = bytes[i];
if !is_valid_byte(b) {
return Some(b)
return Some(b);
}
i += 1;
}
None
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn basic_valid() {
const PHRASE: &'static [u8] = b"OK";
assert_eq!(ReasonPhrase::from_static(PHRASE).as_bytes(), PHRASE);
assert_eq!(ReasonPhrase::try_from(PHRASE).unwrap().as_bytes(), PHRASE);
}

#[test]
fn empty_valid() {
const PHRASE: &'static [u8] = b"";
assert_eq!(ReasonPhrase::from_static(PHRASE).as_bytes(), PHRASE);
assert_eq!(ReasonPhrase::try_from(PHRASE).unwrap().as_bytes(), PHRASE);
}

#[test]
fn obs_text_valid() {
const PHRASE: &'static [u8] = b"hyp\xe9r";
assert_eq!(ReasonPhrase::from_static(PHRASE).as_bytes(), PHRASE);
assert_eq!(ReasonPhrase::try_from(PHRASE).unwrap().as_bytes(), PHRASE);
}

const NEWLINE_PHRASE: &'static [u8] = b"hyp\ner";

#[test]
#[should_panic]
fn newline_invalid_panic() {
ReasonPhrase::from_static(NEWLINE_PHRASE);
}

#[test]
fn newline_invalid_err() {
assert!(ReasonPhrase::try_from(NEWLINE_PHRASE).is_err());
}

const CR_PHRASE: &'static [u8] = b"hyp\rer";

#[test]
#[should_panic]
fn cr_invalid_panic() {
ReasonPhrase::from_static(CR_PHRASE);
}

#[test]
fn cr_invalid_err() {
assert!(ReasonPhrase::try_from(CR_PHRASE).is_err());
}
}

0 comments on commit 4ca1eee

Please sign in to comment.