Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/uri/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ impl Authority {
///
/// # Panics
///
/// This function panics if the argument contains invalid characters or
/// is empty.
/// This function panics if the argument is not a valid authority.
///
/// # Examples
///
Expand All @@ -61,10 +60,10 @@ impl Authority {
#[inline]
pub const fn from_static(src: &'static str) -> Self {
match validate_authority_bytes(src.as_bytes()) {
Ok(_) => Authority {
Ok(end) if end == src.len() => Authority {
data: ByteStr::from_static(src),
},
Err(_) => panic!("static str is not valid authority"),
_ => panic!("static str is not valid authority"),
}
}

Expand Down Expand Up @@ -722,4 +721,32 @@ mod tests {
let err = Authority::parse_non_empty(b"]o[").unwrap_err();
assert_eq!(err.0, ErrorKind::InvalidAuthority);
}

#[test]
#[should_panic(expected = "static str is not valid authority")]
fn from_static_rejects_scheme_prefix() {
assert!("https://127.0.0.1".parse::<Authority>().is_err());
Authority::from_static("https://127.0.0.1");
}

#[test]
#[should_panic(expected = "static str is not valid authority")]
fn from_static_rejects_trailing_path() {
assert!("example.com/path".parse::<Authority>().is_err());
Authority::from_static("example.com/path");
}

#[test]
#[should_panic(expected = "static str is not valid authority")]
fn from_static_rejects_trailing_query() {
assert!("example.com?query=1".parse::<Authority>().is_err());
Authority::from_static("example.com?query=1");
}

#[test]
#[should_panic(expected = "static str is not valid authority")]
fn from_static_rejects_trailing_fragment() {
assert!("example.com#frag".parse::<Authority>().is_err());
Authority::from_static("example.com#frag");
}
}