File tree 1 file changed +28
-1
lines changed
1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ impl<VE: ValueEncoding> MetadataKey<VE> {
42
42
match HeaderName :: from_bytes ( src) {
43
43
Ok ( name) => {
44
44
if !VE :: is_valid_key ( name. as_str ( ) ) {
45
- panic ! ( "invalid metadata key" )
45
+ return Err ( InvalidMetadataKey :: new ( ) ) ;
46
46
}
47
47
48
48
Ok ( MetadataKey {
@@ -282,3 +282,30 @@ impl Default for InvalidMetadataKey {
282
282
}
283
283
284
284
impl Error for InvalidMetadataKey { }
285
+
286
+ #[ cfg( test) ]
287
+ mod tests {
288
+ use super :: { AsciiMetadataKey , BinaryMetadataKey } ;
289
+
290
+ #[ test]
291
+ fn test_from_bytes_binary ( ) {
292
+ assert ! ( BinaryMetadataKey :: from_bytes( b"" ) . is_err( ) ) ;
293
+ assert ! ( BinaryMetadataKey :: from_bytes( b"\xFF " ) . is_err( ) ) ;
294
+ assert ! ( BinaryMetadataKey :: from_bytes( b"abc" ) . is_err( ) ) ;
295
+ assert_eq ! (
296
+ BinaryMetadataKey :: from_bytes( b"abc-bin" ) . unwrap( ) . as_str( ) ,
297
+ "abc-bin"
298
+ ) ;
299
+ }
300
+
301
+ #[ test]
302
+ fn test_from_bytes_ascii ( ) {
303
+ assert ! ( AsciiMetadataKey :: from_bytes( b"" ) . is_err( ) ) ;
304
+ assert ! ( AsciiMetadataKey :: from_bytes( b"\xFF " ) . is_err( ) ) ;
305
+ assert_eq ! (
306
+ AsciiMetadataKey :: from_bytes( b"abc" ) . unwrap( ) . as_str( ) ,
307
+ "abc"
308
+ ) ;
309
+ assert ! ( AsciiMetadataKey :: from_bytes( b"abc-bin" ) . is_err( ) ) ;
310
+ }
311
+ }
You can’t perform that action at this time.
0 commit comments