Skip to content

Commit 0a6527a

Browse files
committed
Posibility to specify form parameter of the request
1 parent 3c98204 commit 0a6527a

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/main.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use std::sync::Arc;
23

34
use clap::{Parser, ValueEnum};
@@ -36,6 +37,9 @@ struct Args {
3637

3738
#[arg(short = 'H', long = "header")]
3839
headers: Vec<String>,
40+
41+
#[arg(short = 'F', long = "form")]
42+
form_params: Vec<String>,
3943
}
4044

4145
#[tokio::main]
@@ -54,6 +58,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
5458

5559
let metrics = Arc::new(Metrics::new());
5660

61+
let mut form_params = HashMap::new();
5762
let mut header_map = reqwest::header::HeaderMap::new();
5863

5964
for h in &args.headers {
@@ -73,15 +78,26 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
7378
}
7479
}
7580
}
81+
for h in &args.form_params {
82+
match h.split_once('=') {
83+
Some((key, value)) => {
84+
form_params.insert(key.trim().to_string(), value.trim().to_string());
85+
}
86+
None => {
87+
eprintln!("Invalid form parameter: {}", h);
88+
}
89+
}
90+
}
7691

7792
let scheduler = Scheduler::new(
7893
&metrics,
7994
args.method,
8095
args.url,
8196
args.body,
97+
form_params,
98+
header_map,
8299
args.concurrency,
83100
args.duration,
84-
header_map,
85101
);
86102

87103
scheduler.run().await;

src/scheduler/scheduler.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::Arc;
1+
use std::{collections::HashMap, sync::Arc};
22

33
use reqwest::{header::HeaderMap, Client, Method};
44

@@ -9,6 +9,7 @@ pub struct Scheduler<'a> {
99
method: Method,
1010
url: String,
1111
body: Option<String>,
12+
form_params: HashMap<String, String>,
1213
concurrency: u64,
1314
duration: u64,
1415
headers: HeaderMap,
@@ -20,15 +21,17 @@ impl<'a> Scheduler<'a> {
2021
method: Method,
2122
url: String,
2223
body: Option<String>,
24+
form_params: HashMap<String, String>,
25+
headers: HeaderMap,
2326
concurrency: u64,
2427
duration: u64,
25-
headers: HeaderMap,
2628
) -> Self {
2729
Scheduler {
2830
metrics,
2931
method,
3032
url,
3133
body,
34+
form_params,
3235
concurrency,
3336
duration,
3437
headers,
@@ -46,13 +49,23 @@ impl<'a> Scheduler<'a> {
4649
let method = self.method.clone();
4750
let url = url.clone();
4851
let body = self.body.clone();
52+
let form_params = self.form_params.clone();
4953
let headers = headers.clone();
5054
let duration = self.duration;
5155
let metrics = Arc::clone(self.metrics);
5256

5357
tasks.push(tokio::spawn(async move {
54-
Scheduler::run_client(&metrics, start_bench, method, url, body, duration, headers)
55-
.await;
58+
Scheduler::run_client(
59+
&metrics,
60+
start_bench,
61+
method,
62+
url,
63+
body,
64+
form_params,
65+
duration,
66+
headers,
67+
)
68+
.await;
5669
}));
5770
}
5871

@@ -93,13 +106,15 @@ impl<'a> Scheduler<'a> {
93106
method: Method,
94107
url: String,
95108
body: Option<String>,
109+
form_params: HashMap<String, String>,
96110
duration: u64,
97111
headers: HeaderMap,
98112
) {
99113
let client = Client::builder().default_headers(headers).build().unwrap();
100114

101115
loop {
102-
let result = Self::make_request(metrics, &client, &method, &url, &body).await;
116+
let result =
117+
Self::make_request(metrics, &client, &method, &url, &body, &form_params).await;
103118
Self::handle_request_result(metrics, result).await;
104119

105120
if std::time::Instant::now() >= start_bench + std::time::Duration::from_secs(duration) {
@@ -114,10 +129,16 @@ impl<'a> Scheduler<'a> {
114129
method: &Method,
115130
url: &str,
116131
body: &Option<String>,
132+
form_params: &HashMap<String, String>,
117133
) -> Result<String, reqwest::Error> {
118134
let start = std::time::Instant::now();
119135

120136
let req_builder = client.request(method.clone(), url);
137+
let req_builder = if form_params.len() > 0 {
138+
req_builder.form(form_params)
139+
} else {
140+
req_builder
141+
};
121142
let req_builder = match body {
122143
Some(b) => req_builder.body(b.clone()),
123144
None => req_builder,

0 commit comments

Comments
 (0)