Skip to content

Commit 4222e1e

Browse files
committed
feat: add SecWebsocketKey random constructor
1 parent 2ffcd7a commit 4222e1e

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
toolchain: ${{ matrix.rust }}
2828
components: rustfmt
2929
- run: cargo test --workspace
30+
- run: cargo test --workspace --features full
3031
- if: matrix.rust == 'nightly'
3132
run: cargo test --benches
3233

@@ -38,12 +39,12 @@ jobs:
3839
- uses: dtolnay/rust-toolchain@stable
3940
- uses: taiki-e/install-action@cargo-hack
4041
- uses: taiki-e/install-action@cargo-minimal-versions
41-
- run: cargo minimal-versions check --workspace
42+
- run: cargo minimal-versions check --workspace --all-features
4243

4344
MSRV:
4445
runs-on: ubuntu-latest
4546

4647
steps:
4748
- uses: actions/checkout@v4
4849
- uses: taiki-e/install-action@cargo-hack
49-
- run: cargo hack --rust-version --no-dev-deps check --workspace
50+
- run: cargo hack --rust-version --no-dev-deps check --workspace --all-features

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ bytes = "1"
2323
mime = "0.3.14"
2424
sha1 = "0.10"
2525
httpdate = "1"
26+
rand = { version = "0.8", optional = true }
2627

2728
[features]
29+
full = ["rand"]
2830
nightly = []

src/common/sec_websocket_key.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
1+
use http::HeaderValue;
2+
13
/// The `Sec-Websocket-Key` header.
24
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
3-
pub struct SecWebsocketKey(pub(super) http::HeaderValue);
5+
pub struct SecWebsocketKey(pub(super) HeaderValue);
46

57
derive_header! {
68
SecWebsocketKey(_),
79
name: SEC_WEBSOCKET_KEY
810
}
11+
12+
impl SecWebsocketKey {
13+
/// Create a random `SecWebsocketKey`.
14+
#[cfg(feature = "rand")]
15+
#[cfg_attr(docsrs, cfg(feature = "rand"))]
16+
pub fn random() -> Self {
17+
use base64::{engine::general_purpose::STANDARD, Engine};
18+
19+
let bytes: [u8; 16] = rand::random();
20+
let mut value = HeaderValue::from_str(&STANDARD.encode(bytes)).unwrap();
21+
22+
value.set_sensitive(true);
23+
24+
Self(value)
25+
}
26+
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
#[cfg(feature = "rand")]
31+
use super::SecWebsocketKey;
32+
33+
#[cfg(feature = "rand")]
34+
#[test]
35+
fn random() {
36+
let _ = SecWebsocketKey::random();
37+
}
38+
}

0 commit comments

Comments
 (0)