Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,11 @@ fn validate_websocket_exclusives(cli: &Cli) -> Result<(), FetchError> {
)
.into());
}
if let Some(method) = cli.method.as_deref()
&& !method.eq_ignore_ascii_case("GET")
{
return Err(format!("WebSocket requires GET; method {method} is not supported").into());
}
let conflicting_flag = if cli.clobber {
Some("clobber")
} else if cli.copy {
Expand Down Expand Up @@ -1517,6 +1522,17 @@ mod tests {
validate_websocket_exclusives(&cli).unwrap();
}

#[test]
fn websocket_rejects_non_get_methods() {
let cli = Cli::try_parse_from(["fetch", "ws://example.com", "--method", "POST"]).unwrap();
let err = validate_websocket_exclusives(&cli).unwrap_err().to_string();

assert_eq!(err, "WebSocket requires GET; method POST is not supported");

let cli = Cli::try_parse_from(["fetch", "ws://example.com", "--method", "GET"]).unwrap();
validate_websocket_exclusives(&cli).unwrap();
}

#[test]
fn websocket_allows_network_options() {
let cli =
Expand Down
6 changes: 0 additions & 6 deletions src/websocket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ pub async fn execute(cli: &Cli) -> Result<i32, FetchError> {

let method = effective_method(cli);
let mut warnings = Vec::new();
if !cli.method().eq_ignore_ascii_case("GET") {
warnings.push(format!(
"WebSocket requires GET; ignoring method {}",
cli.method()
));
}
if cli.timing {
warnings.push("--timing is not supported for WebSocket connections".to_string());
}
Expand Down
18 changes: 11 additions & 7 deletions tests/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,14 @@ fn websocket_noninteractive_go_cases() {
assert!(!res.stderr.contains("--help"), "stderr:\n{}", res.stderr);

let res = run_fetch(&[&ws_url, "--method", "POST", "--timing", "--dry-run"]);
assert_exit(&res, 0);
assert!(res.stderr.contains("GET / HTTP/1.1"));
assert_exit(&res, 1);
assert!(
res.stderr.contains(
"warning: WebSocket requires GET; ignoring method POST\nwarning: --timing is not supported for WebSocket connections\n\n> GET / HTTP/1.1"
),
res.stderr
.contains("WebSocket requires GET; method POST is not supported"),
"{:?}",
res.stderr
);
assert!(!res.stderr.contains("GET / HTTP/1.1"), "{:?}", res.stderr);

let res = run_fetch(&[
&ws_url,
Expand All @@ -344,8 +343,13 @@ fn websocket_noninteractive_go_cases() {
"--ws-interactive",
"off",
]);
assert_exit(&res, 0);
assert!(res.stderr.contains("GET") || res.stderr.contains("method"));
assert_exit(&res, 1);
assert!(
res.stderr
.contains("WebSocket requires GET; method POST is not supported"),
"{:?}",
res.stderr
);
}

#[test]
Expand Down
Loading