refactor(cli): encapsulate response body regex

This commit is contained in:
ldm0
2026-08-22 03:20:24 +08:00
committed by Donough Liu
parent cba58e3aba
commit e7a8a709ef
2 changed files with 20 additions and 3 deletions
+19 -2
View File
@@ -96,7 +96,11 @@ pub struct FetchArgs {
/// Wait for a response whose body matches this regex. An invalid regex
/// fails the fetch command immediately.
#[arg(long, value_parser = parse_response_body_regex)]
#[arg(
long,
value_name = "REGEX",
value_parser = parse_response_body_regex
)]
pub wait_response_body: Option<ResponseBodyRegexArg>,
#[arg(long, value_parser = parse_response_json_path_arg)]
@@ -171,10 +175,14 @@ fn parse_response_json_path_arg(raw: &str) -> Result<ResponseJsonPathArg, String
#[derive(Debug, Clone)]
pub struct ResponseBodyRegexArg {
pub regex: Regex,
regex: Regex,
}
impl ResponseBodyRegexArg {
pub(crate) fn regex(&self) -> &Regex {
&self.regex
}
pub fn pattern(&self) -> &str {
self.regex.as_str()
}
@@ -592,6 +600,15 @@ mod tests {
assert!(help.contains("response match, selector, and script waits share one absolute"));
}
#[test]
fn fetch_help_labels_response_body_pattern_as_regex() {
let help = Cli::try_parse_from(["moli", "fetch", "--help"])
.unwrap_err()
.to_string();
assert!(help.contains("--wait-response-body <REGEX>"));
}
#[test]
fn parses_port_equals_with_explicit_serve_command() {
let args = normalize_args_for_compat(["moli", "serve", "--port=0"]);
+1 -1
View File
@@ -302,7 +302,7 @@ pub fn response_wait_criteria_from_args(
body_regex: args
.wait_response_body
.as_ref()
.map(|arg| arg.regex.clone()),
.map(|arg| arg.regex().clone()),
json_path_equals: args
.wait_response_json
.as_ref()