Skip to content

Commit

Permalink
fix: MetadataKey::from_bytes returns an error (#1246)
Browse files Browse the repository at this point in the history
BREAKING: `Metadata::from_bytes` returns an error now
  • Loading branch information
andrewhickman authored Feb 8, 2023
1 parent b3567a3 commit 930c805
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion tonic/src/metadata/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<VE: ValueEncoding> MetadataKey<VE> {
match HeaderName::from_bytes(src) {
Ok(name) => {
if !VE::is_valid_key(name.as_str()) {
panic!("invalid metadata key")
return Err(InvalidMetadataKey::new());
}

Ok(MetadataKey {
Expand Down Expand Up @@ -282,3 +282,30 @@ impl Default for InvalidMetadataKey {
}

impl Error for InvalidMetadataKey {}

#[cfg(test)]
mod tests {
use super::{AsciiMetadataKey, BinaryMetadataKey};

#[test]
fn test_from_bytes_binary() {
assert!(BinaryMetadataKey::from_bytes(b"").is_err());
assert!(BinaryMetadataKey::from_bytes(b"\xFF").is_err());
assert!(BinaryMetadataKey::from_bytes(b"abc").is_err());
assert_eq!(
BinaryMetadataKey::from_bytes(b"abc-bin").unwrap().as_str(),
"abc-bin"
);
}

#[test]
fn test_from_bytes_ascii() {
assert!(AsciiMetadataKey::from_bytes(b"").is_err());
assert!(AsciiMetadataKey::from_bytes(b"\xFF").is_err());
assert_eq!(
AsciiMetadataKey::from_bytes(b"abc").unwrap().as_str(),
"abc"
);
assert!(AsciiMetadataKey::from_bytes(b"abc-bin").is_err());
}
}

0 comments on commit 930c805

Please sign in to comment.