Skip to content

Commit 1b4efe2

Browse files
tottotoseanmonstar
authored andcommitted
feat: implement From bytes for SecWebsocketKey
1 parent 74fcf53 commit 1b4efe2

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/common/sec_websocket_key.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
1+
use base64::{engine::general_purpose::STANDARD, Engine};
2+
use http::HeaderValue;
3+
14
/// The `Sec-Websocket-Key` header.
25
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
3-
pub struct SecWebsocketKey(pub(super) http::HeaderValue);
6+
pub struct SecWebsocketKey(pub(super) HeaderValue);
47

58
derive_header! {
69
SecWebsocketKey(_),
710
name: SEC_WEBSOCKET_KEY
811
}
12+
13+
impl From<[u8; 16]> for SecWebsocketKey {
14+
fn from(bytes: [u8; 16]) -> Self {
15+
let mut value = HeaderValue::from_str(&STANDARD.encode(bytes)).unwrap();
16+
value.set_sensitive(true);
17+
Self(value)
18+
}
19+
}
20+
21+
#[cfg(test)]
22+
mod tests {
23+
use super::*;
24+
25+
#[test]
26+
fn from_bytes() {
27+
let bytes: [u8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
28+
let _ = SecWebsocketKey::from(bytes);
29+
}
30+
}

0 commit comments

Comments
 (0)