Add new option error-format to enable HTTP request/response output when there are errors.

This commit is contained in:
jcamiel 2023-05-22 17:23:18 +02:00
parent b624003679
commit 0d152e31b1
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
4 changed files with 50 additions and 3 deletions

View File

@ -94,6 +94,16 @@ pub fn cookies_output_file() -> clap::Arg {
.help("Write cookies to FILE after running the session (only for one session)")
.num_args(1)
}
pub fn error_format() -> clap::Arg {
clap::Arg::new("error_format")
.long("error-format")
.value_name("FORMAT")
.value_parser(["short", "long"])
.default_value("short")
.help("Control the format of error messages")
}
pub fn fail_at_en() -> clap::Arg {
clap::Arg::new("fail_at_end")
.long("fail-at-end")
@ -105,7 +115,7 @@ pub fn file_root() -> clap::Arg {
clap::Arg::new("file_root")
.long("file-root")
.value_name("DIR")
.help("Set root filesystem to import files (default is current directory)")
.help("Set root filesystem to import files [default: current directory]")
.num_args(1)
}
@ -156,6 +166,7 @@ pub fn insecure() -> clap::Arg {
.help("Allow insecure SSL connections")
.action(ArgAction::SetTrue)
}
pub fn interactive() -> clap::Arg {
clap::Arg::new("interactive")
.long("interactive")

View File

@ -17,6 +17,7 @@
*/
use super::variables::{parse as parse_variable, parse_value};
use super::OptionsError;
use crate::cli::options::ErrorFormat;
use crate::cli::OutputType;
use atty::Stream;
use clap::ArgMatches;
@ -110,6 +111,15 @@ pub fn cookie_output_file(arg_matches: &ArgMatches) -> Option<String> {
get::<String>(arg_matches, "cookies_output_file")
}
pub fn error_format(arg_matches: &ArgMatches) -> ErrorFormat {
let error_format = get::<String>(arg_matches, "error_format");
match error_format.as_deref() {
Some("long") => ErrorFormat::Long,
Some("short") => ErrorFormat::Short,
_ => ErrorFormat::Short,
}
}
pub fn fail_fast(arg_matches: &ArgMatches) -> bool {
!has_flag(arg_matches, "fail_at_end")
}

View File

@ -44,6 +44,7 @@ pub struct Options {
pub connects_to: Vec<String>,
pub cookie_input_file: Option<String>,
pub cookie_output_file: Option<String>,
pub error_format: ErrorFormat,
pub fail_fast: bool,
pub file_root: Option<String>,
pub follow_location: bool,
@ -89,6 +90,13 @@ impl From<clap::Error> for OptionsError {
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ErrorFormat {
Short,
Long,
}
fn get_version() -> String {
let libcurl_version = libcurl_version_info();
format!(
@ -113,6 +121,7 @@ pub fn parse() -> Result<Options, OptionsError> {
.arg(commands::connect_to())
.arg(commands::cookies_input_file())
.arg(commands::cookies_output_file())
.arg(commands::error_format())
.arg(commands::fail_at_en())
.arg(commands::file_root())
.arg(commands::follow_location())
@ -174,6 +183,7 @@ fn parse_matches(arg_matches: &ArgMatches) -> Result<Options, OptionsError> {
let connects_to = matches::connects_to(arg_matches);
let cookie_input_file = matches::cookie_input_file(arg_matches);
let cookie_output_file = matches::cookie_output_file(arg_matches);
let error_format = matches::error_format(arg_matches);
let fail_fast = matches::fail_fast(arg_matches);
let file_root = matches::file_root(arg_matches);
let follow_location = matches::follow_location(arg_matches);
@ -212,6 +222,7 @@ fn parse_matches(arg_matches: &ArgMatches) -> Result<Options, OptionsError> {
connects_to,
cookie_input_file,
cookie_output_file,
error_format,
fail_fast,
file_root,
follow_location,
@ -303,8 +314,8 @@ impl Options {
let ignore_asserts = self.ignore_asserts;
let ssl_no_revoke = self.ssl_no_revoke;
let mut bd = RunnerOptionsBuilder::new();
bd.cacert_file(cacert_file)
RunnerOptionsBuilder::new()
.cacert_file(cacert_file)
.client_cert_file(client_cert_file)
.client_key_file(client_key_file)
.compressed(compressed)

View File

@ -31,6 +31,7 @@ pub struct RunnerOptionsBuilder {
connects_to: Vec<String>,
context_dir: ContextDir,
cookie_input_file: Option<String>,
error_format: ErrorFormat,
fail_fast: bool,
follow_location: bool,
ignore_asserts: bool,
@ -52,6 +53,11 @@ pub struct RunnerOptionsBuilder {
verbosity: Option<Verbosity>,
}
pub enum ErrorFormat {
Short,
Long,
}
impl Default for RunnerOptionsBuilder {
fn default() -> Self {
RunnerOptionsBuilder {
@ -63,6 +69,7 @@ impl Default for RunnerOptionsBuilder {
connects_to: vec![],
context_dir: ContextDir::default(),
cookie_input_file: None,
error_format: ErrorFormat::Short,
fail_fast: true,
follow_location: false,
ignore_asserts: false,
@ -149,6 +156,14 @@ impl RunnerOptionsBuilder {
self
}
/// Control the format of error messages.
/// If `error_format` is [`ErrorFormat::Long`], the HTTP request and response that has
/// errors is displayed (headers, body, etc..)
pub fn error_format(&mut self, error_format: ErrorFormat) -> &mut Self {
self.error_format = error_format;
self
}
/// Sets stopping or continuing executing requests to the end of the Hurl file even when an assert error occurs.
///
/// By default, Hurl exits after an assert error in the HTTP response. Note that this option does