Skip to content

Commit

Permalink
feat: implement From bytes for SecWebsocketKey
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto authored and seanmonstar committed Apr 2, 2024
1 parent 74fcf53 commit 1b4efe2
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/common/sec_websocket_key.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
use base64::{engine::general_purpose::STANDARD, Engine};
use http::HeaderValue;

/// The `Sec-Websocket-Key` header.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SecWebsocketKey(pub(super) http::HeaderValue);
pub struct SecWebsocketKey(pub(super) HeaderValue);

derive_header! {
SecWebsocketKey(_),
name: SEC_WEBSOCKET_KEY
}

impl From<[u8; 16]> for SecWebsocketKey {
fn from(bytes: [u8; 16]) -> Self {
let mut value = HeaderValue::from_str(&STANDARD.encode(bytes)).unwrap();
value.set_sensitive(true);
Self(value)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn from_bytes() {
let bytes: [u8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
let _ = SecWebsocketKey::from(bytes);
}
}

0 comments on commit 1b4efe2

Please sign in to comment.