Skip to content

Commit b9cecb5

Browse files
Merge pull request #16 from monoscope-tech/feature/dump-curl
feat: add curl dump
2 parents 691f7d2 + e131448 commit b9cecb5

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

src/base_request.rs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pub struct RequestConfig {
122122
pub request_body: Option<HashMap<String, String>>,
123123
pub pre_request_hook: Option<String>,
124124
pub post_response_hook: Option<String>,
125+
pub curl: Option<String>,
125126
}
126127

127128
#[derive(Debug, Serialize, Deserialize)]
@@ -423,6 +424,73 @@ pub async fn base_request(
423424
headers.insert("X-Testkit-Collection-ID".into(), col.clone());
424425
request_config.headers = Some(headers);
425426
}
427+
428+
let method_str = test_item.request.http_method.get_method().to_string();
429+
let params_vec = if let Some(v) = test_item.request.params.clone() {
430+
Some(
431+
v.iter()
432+
.map(|(k, v)| {
433+
(replace_vars(k, &exports_map), replace_vars(v, &exports_map))
434+
})
435+
.collect(),
436+
)
437+
} else {
438+
None
439+
};
440+
441+
let mut processed_headers = HashMap::new();
442+
if let Some(headers) = &test_item.request.headers {
443+
for (name, mut value) in headers.clone() {
444+
for env_var in get_env_variable_paths(&value) {
445+
if let Ok(val) = get_env_variable(&env_var) {
446+
value = value.replace(&env_var, &val);
447+
}
448+
}
449+
for export_var in get_vars(&value) {
450+
let key_str = export_var.replace("{{", "").replace("}}", "");
451+
if let Some(val) = exports_map.lock().unwrap().get(&key_str) {
452+
value = value.replace(&export_var, &val.to_string());
453+
}
454+
}
455+
processed_headers.insert(name, value);
456+
}
457+
}
458+
459+
let json_body_str = if let Some(json) = &test_item.request.json {
460+
let js_string = match json {
461+
Value::String(s) => s.clone(),
462+
_ => json.to_string(),
463+
};
464+
let j_string =
465+
prepare_json_body(js_string.clone(), &exports_map, &mut step_result, false);
466+
processed_headers
467+
.insert("Content-Type".to_string(), "application/json".to_string());
468+
Some(j_string)
469+
} else {
470+
None
471+
};
472+
473+
let request_body_str = if let Some(b) = &test_item.request.request_body {
474+
let mut body = b.clone();
475+
for (key, val) in body.clone().iter() {
476+
body.insert(key.clone(), replace_vars(val, &exports_map));
477+
}
478+
serde_json::to_string(&body).ok()
479+
} else {
480+
None
481+
};
482+
483+
let curl_cmd = generate_curl_command(
484+
&method_str,
485+
&url,
486+
&Some(processed_headers),
487+
&json_body_str,
488+
&request_body_str,
489+
&params_vec,
490+
);
491+
492+
request_config.curl = Some(curl_cmd);
493+
426494
let response = request_builder.send().await;
427495
match response {
428496
Err(e) => {
@@ -960,6 +1028,53 @@ async fn check_assertions(
9601028
assert_results
9611029
}
9621030

1031+
fn generate_curl_command(
1032+
method: &str,
1033+
url: &str,
1034+
headers: &Option<HashMap<String, String>>,
1035+
json_body: &Option<String>,
1036+
request_body: &Option<String>,
1037+
params: &Option<Vec<(String, String)>>,
1038+
) -> String {
1039+
let mut curl_cmd = String::from("curl");
1040+
1041+
// Build URL with query params first
1042+
let mut final_url = url.to_string();
1043+
if let Some(params_vec) = params {
1044+
if !params_vec.is_empty() {
1045+
let query_string: Vec<String> =
1046+
params_vec.iter().map(|(k, v)| format!("{}={}", k, v)).collect();
1047+
final_url.push_str(&format!("?{}", query_string.join("&")));
1048+
}
1049+
}
1050+
1051+
// Add method if not GET
1052+
if method != "GET" {
1053+
curl_cmd.push_str(&format!(" -X {} '{}'", method, final_url));
1054+
} else {
1055+
curl_cmd.push_str(&format!(" '{}'", final_url));
1056+
}
1057+
1058+
if let Some(headers_map) = headers {
1059+
for (key, value) in headers_map {
1060+
let escaped_value = value.replace("'", "'\\''");
1061+
curl_cmd.push_str(&format!(" -H '{}: {}'", key, escaped_value));
1062+
}
1063+
}
1064+
1065+
if let Some(json) = json_body {
1066+
let escaped_json = json.replace("'", "'\\''");
1067+
curl_cmd.push_str(&format!(" -d '{}'", escaped_json));
1068+
}
1069+
1070+
if let Some(body) = request_body {
1071+
let escaped_body = body.replace("'", "'\\''");
1072+
curl_cmd.push_str(&format!(" -d '{}'", escaped_body));
1073+
}
1074+
1075+
curl_cmd
1076+
}
1077+
9631078
#[cfg(test)]
9641079
mod tests {
9651080
// Unit tests for this module can be added here.

0 commit comments

Comments
 (0)