Skip to content

Commit

Permalink
Trim optional separating whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
bryfox authored and daniel-abramov committed Dec 9, 2024
1 parent 4af7316 commit b31e88f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/handshake/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ pub fn generate_request(mut request: Request) -> Result<(Vec<u8>, String)> {

fn extract_subprotocols_from_request(request: &Request) -> Result<Option<Vec<String>>> {
if let Some(subprotocols) = request.headers().get("Sec-WebSocket-Protocol") {
Ok(Some(subprotocols.to_str()?.split(',').map(ToString::to_string).collect()))
Ok(Some(subprotocols.to_str()?.split(',').map(|s| s.trim().to_string()).collect()))
} else {
Ok(None)
}
Expand Down
22 changes: 19 additions & 3 deletions tests/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn create_http_request(uri: &str, subprotocols: Option<Vec<String>>) -> http::Re
.header("Sec-WebSocket-Key", generate_key());

if let Some(subprotocols) = subprotocols {
builder = builder.header("Sec-WebSocket-Protocol", subprotocols.join(","));
builder = builder.header("Sec-WebSocket-Protocol", subprotocols.join(", "));
}

builder.uri(uri).body(()).unwrap()
Expand Down Expand Up @@ -130,12 +130,28 @@ fn test_request_multiple_subprotocols() {
}

#[test]
fn test_request_single_subprotocol() {
fn test_request_multiple_subprotocols_with_initial_unknown() {
server_thread(3016, Some(vec!["my-sub-protocol".to_string()]));
sleep(Duration::from_secs(1));

let (_, response) = connect(create_http_request(
"ws://127.0.0.1:3016",
Some(vec!["protocol-unknown-to-server".to_string(), "my-sub-protocol".to_string()]),
))
.unwrap();

assert_eq!(
response.headers().get("Sec-WebSocket-Protocol").unwrap(),
"my-sub-protocol".parse::<http::HeaderValue>().unwrap()
);
}

#[test]
fn test_request_single_subprotocol() {
server_thread(3017, Some(vec!["my-sub-protocol".to_string()]));
sleep(Duration::from_secs(1));

let (_, response) = connect(create_http_request(
"ws://127.0.0.1:3017",
Some(vec!["my-sub-protocol".to_string()]),
))
.unwrap();
Expand Down

0 comments on commit b31e88f

Please sign in to comment.