feat: allow to filter tests by regex on url

This commit is contained in:
əlemi 2023-06-15 17:20:59 +02:00
parent 7eb5a0dfe1
commit 420ea7a746
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 18 additions and 2 deletions

View file

@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
clap = { version = "4.3.0", features = ["derive"] }
postman_collection = "0.2.0"
regex = "1.8.4"
reqwest = { version = "0.11.18", features = ["json"] }
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"

View file

@ -4,6 +4,8 @@ use std::sync::Arc;
use clap::{Parser, Subcommand};
use regex::Regex;
use crate::model::PostWomanCollection;
/// API tester and debugger from your CLI
@ -48,6 +50,9 @@ pub enum PostWomanActions {
// },
/// run all saved requests
Test {
/// filter requests to fire by url (regex)
filter: Option<String>,
/// isolate each request client from others
#[arg(long, default_value_t = false)]
isolated: bool,
@ -139,20 +144,30 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// if args.verbose { println!(" │╵") }
// },
PostWomanActions::Test { isolated, pretty } => {
PostWomanActions::Test { filter, isolated, pretty } => {
let reqs = collection.requests();
let matcher = match filter {
Some(rex) => Some(Regex::new(&rex)?),
None => None,
};
let mut tasks = Vec::new();
let client = Arc::new(reqwest::Client::default());
for req in reqs {
let url = req.url().as_str().to_string();
if let Some(m) = &matcher {
if !m.is_match(&url) {
continue
}
}
let _c = client.clone();
let t = tokio::spawn(async move {
let c = if isolated {
Arc::new(reqwest::Client::default())
} else { _c };
let url = req.url().as_str().to_string();
let method = req.method().as_str().to_string();
let response = c.execute(req).await;
match response {