Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add very verbose option #623

Merged
merged 4 commits into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 44 additions & 37 deletions packages/hurl/src/cli/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct CliOptions {
pub user_agent: Option<String>,
pub variables: HashMap<String, Value>,
pub verbose: bool,
pub very_verbose: bool,
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -297,10 +298,15 @@ pub fn app(version: &str) -> Command {
.long("verbose")
.help("Turn on verbose output"),
)
.arg(
clap::Arg::new("very_verbose")
.long("very-verbose")
.help("Turn on verbose output, uncluding HTTP response"),
)
}

pub fn parse_options(matches: ArgMatches) -> Result<CliOptions, CliError> {
let cacert_file = match get_string(&matches, "cacert_file") {
pub fn parse_options(matches: &ArgMatches) -> Result<CliOptions, CliError> {
let cacert_file = match get_string(matches, "cacert_file") {
None => None,
Some(filename) => {
if !Path::new(&filename).is_file() {
Expand All @@ -311,9 +317,9 @@ pub fn parse_options(matches: ArgMatches) -> Result<CliOptions, CliError> {
}
}
};
let color = output_color(matches.clone());
let compressed = has_flag(&matches, "compressed");
let connect_timeout = match get_string(&matches, "connect_timeout") {
let color = output_color(matches);
let compressed = has_flag(matches, "compressed");
let connect_timeout = match get_string(matches, "connect_timeout") {
None => ClientOptions::default().connect_timeout,
Some(s) => match s.parse::<u64>() {
Ok(n) => Duration::from_secs(n),
Expand All @@ -324,14 +330,13 @@ pub fn parse_options(matches: ArgMatches) -> Result<CliOptions, CliError> {
}
},
};
let cookie_input_file = get_string(&matches, "cookies_input_file");
let cookie_output_file = get_string(&matches, "cookies_output_file");

let fail_fast = !has_flag(&matches, "fail_at_end");
let file_root = get_string(&matches, "file_root");
let follow_location = has_flag(&matches, "follow_location");
let glob_files = match_glob_files(&matches)?;
let report_html = get_string(&matches, "report_html");
let cookie_input_file = get_string(matches, "cookies_input_file");
let cookie_output_file = get_string(matches, "cookies_output_file");
let fail_fast = !has_flag(matches, "fail_at_end");
let file_root = get_string(matches, "file_root");
let follow_location = has_flag(matches, "follow_location");
let glob_files = match_glob_files(matches)?;
let report_html = get_string(matches, "report_html");
let html_dir = if let Some(dir) = report_html {
let path = Path::new(&dir);
if !path.exists() {
Expand All @@ -353,12 +358,12 @@ pub fn parse_options(matches: ArgMatches) -> Result<CliOptions, CliError> {
} else {
None
};
let ignore_asserts = has_flag(&matches, "ignore_asserts");
let include = has_flag(&matches, "include");
let insecure = has_flag(&matches, "insecure");
let interactive = has_flag(&matches, "interactive");
let junit_file = get_string(&matches, "junit");
let max_redirect = match get_string(&matches, "max_redirects").as_deref() {
let ignore_asserts = has_flag(matches, "ignore_asserts");
let include = has_flag(matches, "include");
let insecure = has_flag(matches, "insecure");
let interactive = has_flag(matches, "interactive");
let junit_file = get_string(matches, "junit");
let max_redirect = match get_string(matches, "max_redirects").as_deref() {
None => Some(50),
Some("-1") => None,
Some(s) => match s.parse::<usize>() {
Expand All @@ -370,20 +375,20 @@ pub fn parse_options(matches: ArgMatches) -> Result<CliOptions, CliError> {
}
},
};
let no_proxy = get_string(&matches, "proxy");
let output = get_string(&matches, "output");
let test = has_flag(&matches, "test");
let output_type = if has_flag(&matches, "json") {
let no_proxy = get_string(matches, "proxy");
let output = get_string(matches, "output");
let test = has_flag(matches, "test");
let output_type = if has_flag(matches, "json") {
OutputType::Json
} else if has_flag(&matches, "no_output") || test {
} else if has_flag(matches, "no_output") || test {
OutputType::NoOutput
} else {
OutputType::ResponseBody
};
let progress = has_flag(&matches, "progress") || test;
let proxy = get_string(&matches, "proxy");
let summary = has_flag(&matches, "summary") || test;
let timeout = match get_string(&matches, "max_time") {
let progress = has_flag(matches, "progress") || test;
let proxy = get_string(matches, "proxy");
let summary = has_flag(matches, "summary") || test;
let timeout = match get_string(matches, "max_time") {
None => ClientOptions::default().timeout,
Some(s) => match s.parse::<u64>() {
Ok(n) => Duration::from_secs(n),
Expand All @@ -394,11 +399,12 @@ pub fn parse_options(matches: ArgMatches) -> Result<CliOptions, CliError> {
}
},
};
let to_entry = to_entry(matches.clone())?;
let user = get_string(&matches, "user");
let user_agent = get_string(&matches, "user_agent");
let to_entry = to_entry(matches)?;
let user = get_string(matches, "user");
let user_agent = get_string(matches, "user_agent");
let variables = variables(matches.clone())?;
let verbose = has_flag(&matches, "verbose") || has_flag(&matches, "interactive");
let very_verbose = has_flag(matches, "very_verbose");
let verbose = has_flag(matches, "verbose") || has_flag(matches, "interactive") || very_verbose;

Ok(CliOptions {
cacert_file,
Expand Down Expand Up @@ -430,21 +436,22 @@ pub fn parse_options(matches: ArgMatches) -> Result<CliOptions, CliError> {
user_agent,
variables,
verbose,
very_verbose,
})
}

pub fn output_color(matches: ArgMatches) -> bool {
if has_flag(&matches, "color") {
pub fn output_color(matches: &ArgMatches) -> bool {
if has_flag(matches, "color") {
true
} else if has_flag(&matches, "no_color") {
} else if has_flag(matches, "no_color") {
false
} else {
atty::is(Stream::Stdout)
}
}

fn to_entry(matches: ArgMatches) -> Result<Option<usize>, CliError> {
match get_string(&matches, "to_entry") {
fn to_entry(matches: &ArgMatches) -> Result<Option<usize>, CliError> {
match get_string(matches, "to_entry") {
Some(value) => match value.parse() {
Ok(v) => Ok(Some(v)),
Err(_) => Err(CliError {
Expand Down
59 changes: 54 additions & 5 deletions packages/hurl/src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use super::options::ClientOptions;
use super::request::*;
use super::request_spec::*;
use super::response::*;
use crate::http::HttpError;
use crate::http::{HttpError, Verbosity};
use std::str::FromStr;
use url::Url;

Expand Down Expand Up @@ -70,8 +70,11 @@ impl Client {
}
}

/// Execute an HTTP request and returns a list
///
/// Execute an http request
/// # Arguments
///
/// * request - A request specification
///
pub fn execute_with_redirect(
&mut self,
Expand Down Expand Up @@ -153,7 +156,7 @@ impl Client {

self.set_headers(request);

let verbose = self.options.verbose;
let verbose = self.options.verbosity != None;
let mut request_headers: Vec<Header> = vec![];

let start = Instant::now();
Expand Down Expand Up @@ -251,6 +254,11 @@ impl Client {
body,
duration,
};

if self.options.verbosity == Some(Verbosity::VeryVerbose) {
response.log_body();
}

Ok((request, response))
}

Expand Down Expand Up @@ -493,7 +501,7 @@ impl Client {
/// Add cookie to Cookiejar
///
pub fn add_cookie(&mut self, cookie: Cookie) {
if self.options.verbose {
if self.options.verbosity != None {
eprintln!("* add to cookie store: {}", cookie);
}
self.handle
Expand All @@ -505,7 +513,7 @@ impl Client {
/// Clear cookie storage
///
pub fn clear_cookie_storage(&mut self) {
if self.options.verbose {
if self.options.verbosity != None {
eprintln!("* clear cookie storage");
}
self.handle.cookie_list("ALL").unwrap();
Expand Down Expand Up @@ -632,6 +640,47 @@ pub fn decode_header(data: &[u8]) -> Option<String> {
}
}

impl Response {
///
/// Log a response body as text if possible, or a slice of body bytes.
///
/// # Arguments
///
/// * `response` - The HTTP response
fn log_body(&self) {
eprintln!("* Response:");

// We try to decode the HTTP body as text if the response has a text kind content type.
// If it ok, we print each line of the body in debug format. Otherwise, we
// print the body first 64 bytes.
if let Some(content_type) = self.content_type() {
if !content_type.contains("text") {
self.log_bytes(64);
return;
}
}
match self.text() {
Ok(text) => text.split('\n').for_each(|l| eprintln!("* {}", l)),
Err(_) => self.log_bytes(64),
}
}

///
/// Log a response bytes with a maximum size.
///
/// # Arguments
///
/// * `max` - The maximum number if bytes to log
fn log_bytes(&self, max: usize) {
let bytes = if self.body.len() > max {
&self.body[..max]
} else {
&self.body
};
eprintln!("* Bytes <{}...>", hex::encode(bytes))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion packages/hurl/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use self::client::Client;
pub use self::cookie::{CookieAttribute, ResponseCookie};
pub use self::core::{Cookie, Header, Param, RequestCookie};
pub use self::error::HttpError;
pub use self::options::ClientOptions;
pub use self::options::{ClientOptions, Verbosity};
pub use self::request::Request;
#[cfg(test)]
pub use self::request_spec::tests::*;
Expand Down
12 changes: 9 additions & 3 deletions packages/hurl/src/http/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct ClientOptions {
pub cookie_input_file: Option<String>,
pub proxy: Option<String>,
pub no_proxy: Option<String>,
pub verbose: bool,
pub verbosity: Option<Verbosity>,
pub insecure: bool,
pub timeout: Duration,
pub connect_timeout: Duration,
Expand All @@ -36,6 +36,12 @@ pub struct ClientOptions {
pub context_dir: PathBuf,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Verbosity {
Verbose,
VeryVerbose,
}

impl Default for ClientOptions {
fn default() -> Self {
ClientOptions {
Expand All @@ -45,7 +51,7 @@ impl Default for ClientOptions {
cookie_input_file: None,
proxy: None,
no_proxy: None,
verbose: false,
verbosity: None,
insecure: false,
timeout: Duration::from_secs(300),
connect_timeout: Duration::from_secs(300),
Expand Down Expand Up @@ -130,7 +136,7 @@ mod tests {
cookie_input_file: Some("cookie_file".to_string()),
proxy: Some("localhost:3128".to_string()),
no_proxy: None,
verbose: true,
verbosity: None,
insecure: true,
timeout: Duration::from_secs(10),
connect_timeout: Duration::from_secs(20),
Expand Down
Loading