mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-03 13:30:39 +08:00
feat(test_utils): Add custom-headers and custom delay support to rustman (#2636)
This commit is contained in:
@ -1,22 +1,34 @@
|
||||
use std::{env, process::Command};
|
||||
use std::{
|
||||
env,
|
||||
fs::OpenOptions,
|
||||
io::{self, Write},
|
||||
path::Path,
|
||||
process::Command,
|
||||
};
|
||||
|
||||
use clap::{arg, command, Parser};
|
||||
use masking::PeekInterface;
|
||||
|
||||
use crate::connector_auth::{ConnectorAuthType, ConnectorAuthenticationMap};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about = "Postman collection runner using newman!", long_about = None)]
|
||||
struct Args {
|
||||
/// Admin API Key of the environment
|
||||
#[arg(short, long = "admin_api_key")]
|
||||
#[arg(short, long)]
|
||||
admin_api_key: String,
|
||||
/// Base URL of the Hyperswitch environment
|
||||
#[arg(short, long = "base_url")]
|
||||
#[arg(short, long)]
|
||||
base_url: String,
|
||||
/// Name of the connector
|
||||
#[arg(short, long = "connector_name")]
|
||||
#[arg(short, long)]
|
||||
connector_name: String,
|
||||
/// Custom headers
|
||||
#[arg(short = 'H', long = "header")]
|
||||
custom_headers: Option<Vec<String>>,
|
||||
/// Minimum delay in milliseconds to be added before sending a request
|
||||
/// By default, 7 milliseconds will be the delay
|
||||
#[arg(short, long, default_value_t = 7)]
|
||||
delay_request: u32,
|
||||
/// Folder name of specific tests
|
||||
#[arg(short, long = "folder")]
|
||||
folders: Option<String>,
|
||||
@ -25,6 +37,12 @@ struct Args {
|
||||
verbose: bool,
|
||||
}
|
||||
|
||||
pub struct ReturnArgs {
|
||||
pub newman_command: Command,
|
||||
pub file_modified_flag: bool,
|
||||
pub collection_path: String,
|
||||
}
|
||||
|
||||
// Just by the name of the connector, this function generates the name of the collection dir
|
||||
// Example: CONNECTOR_NAME="stripe" -> OUTPUT: postman/collection-dir/stripe
|
||||
#[inline]
|
||||
@ -32,7 +50,29 @@ fn get_path(name: impl AsRef<str>) -> String {
|
||||
format!("postman/collection-dir/{}", name.as_ref())
|
||||
}
|
||||
|
||||
pub fn command_generate() -> Command {
|
||||
// This function currently allows you to add only custom headers.
|
||||
// In future, as we scale, this can be modified based on the need
|
||||
fn insert_content<T, U>(dir: T, content_to_insert: U) -> io::Result<()>
|
||||
where
|
||||
T: AsRef<Path> + std::fmt::Debug,
|
||||
U: AsRef<str> + std::fmt::Debug,
|
||||
{
|
||||
let file_name = "event.prerequest.js";
|
||||
let file_path = dir.as_ref().join(file_name);
|
||||
|
||||
// Open the file in write mode or create it if it doesn't exist
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(file_path)?;
|
||||
|
||||
write!(file, "\n{:#?}", content_to_insert)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn generate_newman_command() -> ReturnArgs {
|
||||
let args = Args::parse();
|
||||
|
||||
let connector_name = args.connector_name;
|
||||
@ -129,7 +169,10 @@ pub fn command_generate() -> Command {
|
||||
]);
|
||||
}
|
||||
|
||||
newman_command.arg("--delay-request").arg("7"); // 7 milli seconds delay
|
||||
newman_command.args([
|
||||
"--delay-request",
|
||||
format!("{}", &args.delay_request).as_str(),
|
||||
]);
|
||||
|
||||
newman_command.arg("--color").arg("on");
|
||||
|
||||
@ -151,5 +194,24 @@ pub fn command_generate() -> Command {
|
||||
newman_command.arg("--verbose");
|
||||
}
|
||||
|
||||
newman_command
|
||||
let mut modified = false;
|
||||
if let Some(headers) = &args.custom_headers {
|
||||
for header in headers {
|
||||
if let Some((key, value)) = header.split_once(':') {
|
||||
let content_to_insert =
|
||||
format!(r#"pm.request.headers.add({{key: "{key}", value: "{value}"}});"#);
|
||||
if insert_content(&collection_path, &content_to_insert).is_ok() {
|
||||
modified = true;
|
||||
}
|
||||
} else {
|
||||
eprintln!("Invalid header format: {}", header);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ReturnArgs {
|
||||
newman_command,
|
||||
file_modified_flag: modified,
|
||||
collection_path,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user