mirror of
https://github.com/Orange-OpenSource/hurl.git
synced 2024-11-23 09:44:22 +03:00
Make curl import retry more explicit
This commit is contained in:
parent
0cdc8610db
commit
7b20289446
@ -33,6 +33,9 @@ location: true
|
||||
GET http://localhost:8000/retry/until-200
|
||||
[Options]
|
||||
retry: 4
|
||||
HTTP *
|
||||
[Asserts]
|
||||
status < 500
|
||||
|
||||
GET https://localhost:8001/hello
|
||||
[Options]
|
||||
|
@ -15,6 +15,7 @@
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
use super::HurlOption;
|
||||
use clap::ArgMatches;
|
||||
|
||||
pub fn body(arg_matches: &ArgMatches) -> Option<String> {
|
||||
@ -68,22 +69,22 @@ pub fn headers(arg_matches: &ArgMatches) -> Vec<String> {
|
||||
headers
|
||||
}
|
||||
|
||||
pub fn options(arg_matches: &ArgMatches) -> Vec<String> {
|
||||
pub fn options(arg_matches: &ArgMatches) -> Vec<HurlOption> {
|
||||
let mut options = vec![];
|
||||
if has_flag(arg_matches, "compressed") {
|
||||
options.push("compressed: true".to_string());
|
||||
options.push(HurlOption::new("compressed", "true"));
|
||||
}
|
||||
if has_flag(arg_matches, "location") {
|
||||
options.push("location: true".to_string());
|
||||
options.push(HurlOption::new("location", "true"));
|
||||
}
|
||||
if has_flag(arg_matches, "insecure") {
|
||||
options.push("insecure: true".to_string());
|
||||
options.push(HurlOption::new("insecure", "true"));
|
||||
}
|
||||
if let Some(value) = get::<i32>(arg_matches, "max_redirects") {
|
||||
options.push(format!("max-redirs: {value}"));
|
||||
options.push(HurlOption::new("max-redirs", value.to_string().as_str()));
|
||||
}
|
||||
if let Some(value) = get::<i32>(arg_matches, "retry") {
|
||||
options.push(format!("retry: {value}"));
|
||||
options.push(HurlOption::new("retry", value.to_string().as_str()));
|
||||
}
|
||||
options
|
||||
}
|
||||
|
@ -16,10 +16,32 @@
|
||||
*
|
||||
*/
|
||||
|
||||
use std::fmt;
|
||||
|
||||
mod args;
|
||||
mod commands;
|
||||
mod matches;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct HurlOption {
|
||||
name: String,
|
||||
value: String,
|
||||
}
|
||||
|
||||
impl HurlOption {
|
||||
pub fn new(name: &str, value: &str) -> HurlOption {
|
||||
HurlOption {
|
||||
name: name.to_string(),
|
||||
value: value.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl fmt::Display for HurlOption {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}: {}", self.name, self.value)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(s: &str) -> Result<String, String> {
|
||||
let lines: Vec<&str> = regex::Regex::new(r"\n|\r\n")
|
||||
.unwrap()
|
||||
@ -59,7 +81,7 @@ fn parse_line(s: &str) -> Result<String, String> {
|
||||
let headers = matches::headers(&arg_matches);
|
||||
let options = matches::options(&arg_matches);
|
||||
let body = matches::body(&arg_matches);
|
||||
let s = format(&method, &url, headers, options, body);
|
||||
let s = format(&method, &url, headers, &options, body);
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
@ -67,7 +89,7 @@ fn format(
|
||||
method: &str,
|
||||
url: &str,
|
||||
headers: Vec<String>,
|
||||
options: Vec<String>,
|
||||
options: &[HurlOption],
|
||||
body: Option<String>,
|
||||
) -> String {
|
||||
let mut s = format!("{method} {url}");
|
||||
@ -84,10 +106,35 @@ fn format(
|
||||
s.push('\n');
|
||||
s.push_str(body.as_str());
|
||||
}
|
||||
let asserts = additional_asserts(options);
|
||||
if !asserts.is_empty() {
|
||||
s.push_str("\nHTTP *");
|
||||
s.push_str("\n[Asserts]");
|
||||
for assert in asserts {
|
||||
s.push_str(format!("\n{assert}").as_str());
|
||||
}
|
||||
}
|
||||
s.push('\n');
|
||||
s
|
||||
}
|
||||
|
||||
fn has_option(options: &[HurlOption], name: &str) -> bool {
|
||||
for option in options {
|
||||
if option.name == name {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn additional_asserts(options: &[HurlOption]) -> Vec<String> {
|
||||
let mut asserts = vec![];
|
||||
if has_option(options, "retry") {
|
||||
asserts.push("status < 500".to_string());
|
||||
}
|
||||
asserts
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::curl::*;
|
||||
|
Loading…
Reference in New Issue
Block a user