Skip to content
Open
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
42 changes: 40 additions & 2 deletions cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,7 @@ fn parse_set(rest: &[&str], id: &str) -> Result<Value, ParseError> {

/// Parse network interception, request inspection, and HAR recording commands.
fn parse_network(rest: &[&str], id: &str) -> Result<Value, ParseError> {
const VALID: &[&str] = &["route", "unroute", "requests", "har"];
const VALID: &[&str] = &["route", "unroute", "requests", "response", "har"];

match rest.first().copied() {
Some("route") => {
Expand Down Expand Up @@ -2074,6 +2074,19 @@ fn parse_network(rest: &[&str], id: &str) -> Result<Value, ParseError> {
}
Ok(cmd)
}
Some("response") => {
let url = rest.get(1).ok_or_else(|| ParseError::MissingArguments {
context: "network response".to_string(),
usage: "network response <url-pattern> [--timeout <ms>]",
})?;
let mut cmd = json!({ "id": id, "action": "responsebody", "url": url });
if let Some(idx) = rest.iter().position(|&s| s == "--timeout") {
if let Some(ms) = rest.get(idx + 1).and_then(|s| s.parse::<u64>().ok()) {
cmd["timeout"] = json!(ms);
}
}
Ok(cmd)
}
Some("har") => {
const HAR_VALID: &[&str] = &["start", "stop"];
match rest.get(1).copied() {
Expand Down Expand Up @@ -2101,7 +2114,7 @@ fn parse_network(rest: &[&str], id: &str) -> Result<Value, ParseError> {
}),
None => Err(ParseError::MissingArguments {
context: "network".to_string(),
usage: "network <route|unroute|requests|har> [args...]",
usage: "network <route|unroute|requests|response|har> [args...]",
}),
}
}
Expand Down Expand Up @@ -2709,6 +2722,31 @@ mod tests {
assert!(matches!(result, Err(ParseError::MissingArguments { .. })));
}

#[test]
fn test_network_response() {
let cmd = parse_command(&args("network response /api/data"), &default_flags()).unwrap();
assert_eq!(cmd["action"], "responsebody");
assert_eq!(cmd["url"], "/api/data");
}

#[test]
fn test_network_response_with_timeout() {
let cmd = parse_command(
&args("network response /api/data --timeout 5000"),
&default_flags(),
)
.unwrap();
assert_eq!(cmd["action"], "responsebody");
assert_eq!(cmd["url"], "/api/data");
assert_eq!(cmd["timeout"], 5000);
}

#[test]
fn test_network_response_missing_url() {
let result = parse_command(&args("network response"), &default_flags());
assert!(matches!(result, Err(ParseError::MissingArguments { .. })));
}

// === Screenshot ===

#[test]
Expand Down
13 changes: 13 additions & 0 deletions cli/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ pub fn print_response_with_opts(resp: &Response, action: Option<&str>, opts: &Ou
return;
}
}
// Response body (network response)
if action == Some("responsebody") {
if let Some(body) = data.get("body").and_then(|v| v.as_str()) {
let origin = data.get("origin").and_then(|v| v.as_str());
print_with_boundaries(body, origin, opts);
return;
}
}
// Inspect response (check before generic URL handler since it also has a "url" field)
if action == Some("inspect") {
let opened = data
Expand Down Expand Up @@ -1727,6 +1735,8 @@ Subcommands:
requests [options] List captured requests
--clear Clear request log
--filter <pattern> Filter by URL pattern
response <url-pattern> Wait for and return matching response body
--timeout <ms> Timeout in milliseconds (default: 30000)
har <start|stop> [path] Record and export a HAR file

Global Options:
Expand All @@ -1740,6 +1750,8 @@ Examples:
agent-browser network requests
agent-browser network requests --filter "api"
agent-browser network requests --clear
agent-browser network response "/api/products"
agent-browser network response "/api/data" --timeout 10000
agent-browser network har start
agent-browser network har stop ./capture.har
"##
Expand Down Expand Up @@ -2525,6 +2537,7 @@ Network: agent-browser network <action>
route <url> [--abort|--body <json>]
unroute [url]
requests [--clear] [--filter <pattern>]
response <url-pattern> [--timeout <ms>]
har <start|stop> [path]

Storage:
Expand Down
Loading