headers/common/
sec_websocket_key.rs

1use base64::{engine::general_purpose::STANDARD, Engine};
2use http::HeaderValue;
3
4/// The `Sec-Websocket-Key` header.
5#[derive(Clone, Debug, PartialEq, Eq, Hash)]
6pub struct SecWebsocketKey(pub(super) HeaderValue);
7
8derive_header! {
9    SecWebsocketKey(_),
10    name: SEC_WEBSOCKET_KEY
11}
12
13impl 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)]
22mod 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}