Improve header ouputs with --include.

This commit is contained in:
jcamiel 2022-10-20 14:34:43 +02:00
parent debbeb250e
commit 8f96cd990c
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
3 changed files with 96 additions and 80 deletions

View File

@ -224,7 +224,7 @@ impl Client {
body: Vec::new(),
};
for header in &debug_request.headers {
logger.header_out(&header.name, &header.value);
logger.debug_header_out(&header.name, &header.value);
}
logger.info(">");
@ -245,7 +245,7 @@ impl Client {
body: Vec::from(data),
};
for header in &debug_request.headers {
logger.header_out(&header.name, &header.value);
logger.debug_header_out(&header.name, &header.value);
}
logger.info(">");
@ -342,10 +342,10 @@ impl Client {
status_lines
.iter()
.filter(|s| s.starts_with("HTTP/"))
.for_each(|s| logger.status_version_in(s.trim()));
.for_each(|s| logger.debug_status_version_in(s.trim()));
for header in &response.headers {
logger.header_in(&header.name, &header.value);
logger.debug_header_in(&header.name, &header.value);
}
logger.info("<");
if very_verbose {

View File

@ -27,7 +27,7 @@ use colored::*;
use hurl::cli;
use hurl::cli::{CliError, CliOptions, Logger, OutputType};
use hurl::http;
use hurl::http::ContextDir;
use hurl::http::{ContextDir, Response};
use hurl::report;
use hurl::report::canonicalize_filename;
use hurl::runner;
@ -343,14 +343,8 @@ fn main() {
// If include options is set, we output the HTTP response headers
// with status and version (to mimic curl outputs)
if cli_options.include {
let status_line =
format!("HTTP/{} {}\n", response.version, response.status);
output.append(&mut status_line.into_bytes());
for header in response.headers.clone() {
let header_line = format!("{}: {}\n", header.name, header.value);
output.append(&mut header_line.into_bytes());
}
output.append(&mut "\n".to_string().into_bytes());
let text = get_status_line_headers(response, color);
output.append(&mut text.into_bytes());
}
let mut body = if entry_result.compressed {
match response.uncompress_body() {
@ -586,3 +580,25 @@ fn get_summary(duration: u128, hurl_results: &[HurlResult]) -> String {
s.push_str(format!("Duration: {} ms\n", duration).as_str());
s
}
/// Returns status, version and HTTP headers from an HTTP `response`.
fn get_status_line_headers(response: &Response, color: bool) -> String {
let mut str = String::new();
let status_line = format!("HTTP/{} {}\n", response.version, response.status);
let status_line = if color {
format!("{}", status_line.green().bold())
} else {
status_line
};
str.push_str(&status_line);
for header in response.headers.iter() {
let header_line = if color {
format!("{}: {}\n", header.name.cyan().bold(), header.value)
} else {
format!("{}: {}\n", header.name, header.value)
};
str.push_str(&header_line);
}
str.push('\n');
str
}

View File

@ -85,14 +85,14 @@ pub struct Logger<'a> {
pub debug: fn(&str),
pub debug_curl: fn(&str),
pub debug_error: fn(&str, &str, &dyn Error),
pub debug_header_in: fn(&str, &str),
pub debug_header_out: fn(&str, &str),
pub debug_important: fn(&str),
pub debug_method_version_out: fn(&str),
pub debug_status_version_in: fn(&str),
pub warning: fn(&str),
pub error: fn(&str),
pub error_rich: fn(&str, &str, &dyn Error),
pub method_version_out: fn(&str),
pub status_version_in: fn(&str),
pub header_in: fn(&str, &str),
pub header_out: fn(&str, &str),
pub capture: fn(&str, &Value),
pub test_running: fn(&str, usize, usize),
pub test_completed: fn(result: &HurlResult),
@ -111,15 +111,15 @@ impl<'a> Logger<'a> {
debug: log_debug,
debug_curl: log_debug_curl,
debug_error: log_debug_error,
debug_header_in: log_debug_header_in,
debug_header_out: log_debug_header_out,
debug_important: log_debug_important,
debug_method_version_out: log_debug_method_version_out,
debug_status_version_in: log_debug_status_version_in,
warning: log_warning,
error: log_error,
error_rich: log_error_rich,
method_version_out: log_method_version_out,
status_version_in: log_status_version_in,
capture: log_capture,
header_in: log_header_in,
header_out: log_header_out,
test_running: log_test_running,
test_completed: log_test_completed,
color,
@ -132,15 +132,15 @@ impl<'a> Logger<'a> {
debug: log_debug_no_color,
debug_curl: log_debug_curl_no_color,
debug_error: log_debug_error_no_color,
debug_header_in: log_debug_header_in_no_color,
debug_header_out: log_debug_header_out_no_color,
debug_important: log_debug_no_color,
debug_method_version_out: log_debug_method_version_out_no_color,
debug_status_version_in: log_debug_status_version_in_no_color,
warning: log_warning_no_color,
error: log_error_no_color,
error_rich: log_error_rich_no_color,
method_version_out: log_method_version_out_no_color,
status_version_in: log_status_version_in_no_color,
capture: log_capture_no_color,
header_in: log_header_in_no_color,
header_out: log_header_out_no_color,
test_running: log_test_running_no_color,
test_completed: log_test_completed_no_color,
color,
@ -153,15 +153,15 @@ impl<'a> Logger<'a> {
debug: |_| {},
debug_curl: |_| {},
debug_error: |_, _, _| {},
debug_header_in: |_, _| {},
debug_header_out: |_, _| {},
debug_important: |_| {},
debug_method_version_out: |_| {},
debug_status_version_in: |_| {},
warning: log_warning,
error: log_error,
error_rich: log_error_rich,
method_version_out: |_| {},
status_version_in: |_| {},
capture: |_, _| {},
header_in: |_, _| {},
header_out: |_, _| {},
test_running: log_test_running,
test_completed: log_test_completed,
color,
@ -174,15 +174,15 @@ impl<'a> Logger<'a> {
debug: |_| {},
debug_curl: |_| {},
debug_error: |_, _, _| {},
debug_header_in: |_, _| {},
debug_header_out: |_, _| {},
debug_important: |_| {},
debug_method_version_out: |_| {},
debug_status_version_in: |_| {},
warning: log_warning_no_color,
error: log_error_no_color,
error_rich: log_error_rich_no_color,
method_version_out: |_| {},
status_version_in: |_| {},
capture: |_, _| {},
header_in: |_, _| {},
header_out: |_, _| {},
test_running: log_test_running_no_color,
test_completed: log_test_completed_no_color,
color,
@ -205,12 +205,24 @@ impl<'a> Logger<'a> {
(self.debug_curl)(message)
}
pub fn debug_error(&self, error: &dyn Error) {
(self.debug_error)(self.filename, self.content, error)
}
pub fn debug_header_in(&self, name: &str, value: &str) {
(self.debug_header_in)(name, value)
}
pub fn debug_header_out(&self, name: &str, value: &str) {
(self.debug_header_out)(name, value)
}
pub fn debug_important(&self, message: &str) {
(self.debug_important)(message)
}
pub fn debug_error(&self, error: &dyn Error) {
(self.debug_error)(self.filename, self.content, error)
pub fn debug_status_version_in(&self, line: &str) {
(self.debug_status_version_in)(line)
}
pub fn warning(&self, message: &str) {
@ -226,25 +238,13 @@ impl<'a> Logger<'a> {
}
pub fn method_version_out(&self, line: &str) {
(self.method_version_out)(line)
}
pub fn status_version_in(&self, line: &str) {
(self.status_version_in)(line)
(self.debug_method_version_out)(line)
}
pub fn capture(&self, name: &str, value: &Value) {
(self.capture)(name, value)
}
pub fn header_in(&self, name: &str, value: &str) {
(self.header_in)(name, value)
}
pub fn header_out(&self, name: &str, value: &str) {
(self.header_out)(name, value)
}
pub fn test_running(&self, current: usize, total: usize) {
(self.test_running)(self.filename, current, total)
}
@ -310,6 +310,38 @@ fn log_debug_error_no_color(filename: &str, content: &str, error: &dyn Error) {
.for_each(|l| log_debug_no_color(l));
}
fn log_debug_header_in(name: &str, value: &str) {
eprintln!("< {}: {}", name.cyan().bold(), value)
}
fn log_debug_header_in_no_color(name: &str, value: &str) {
eprintln!("< {}: {}", name, value)
}
fn log_debug_header_out(name: &str, value: &str) {
eprintln!("> {}: {}", name.cyan().bold(), value)
}
fn log_debug_header_out_no_color(name: &str, value: &str) {
eprintln!("> {}: {}", name, value)
}
fn log_debug_method_version_out(line: &str) {
eprintln!("> {}", line.purple().bold())
}
fn log_debug_method_version_out_no_color(line: &str) {
eprintln!("> {}", line)
}
fn log_debug_status_version_in(line: &str) {
eprintln!("< {}", line.green().bold())
}
fn log_debug_status_version_in_no_color(line: &str) {
eprintln!("< {}", line)
}
fn log_warning(message: &str) {
eprintln!("{}: {}", "warning".yellow().bold(), message.bold());
}
@ -336,22 +368,6 @@ fn log_error_rich_no_color(filename: &str, content: &str, error: &dyn Error) {
eprintln!("error: {}\n", &message)
}
fn log_method_version_out(line: &str) {
eprintln!("> {}", line.purple().bold())
}
fn log_method_version_out_no_color(line: &str) {
eprintln!("> {}", line)
}
fn log_status_version_in(line: &str) {
eprintln!("< {}", line.green().bold())
}
fn log_status_version_in_no_color(line: &str) {
eprintln!("< {}", line)
}
fn log_capture(name: &str, value: &Value) {
eprintln!("{} {}: {}", "*".blue().bold(), name.yellow().bold(), value)
}
@ -360,22 +376,6 @@ fn log_capture_no_color(name: &str, value: &Value) {
eprintln!("* {}: {}", name, value)
}
fn log_header_in(name: &str, value: &str) {
eprintln!("< {}: {}", name.cyan().bold(), value)
}
fn log_header_in_no_color(name: &str, value: &str) {
eprintln!("< {}: {}", name, value)
}
fn log_header_out(name: &str, value: &str) {
eprintln!("> {}: {}", name.cyan().bold(), value)
}
fn log_header_out_no_color(name: &str, value: &str) {
eprintln!("> {}: {}", name, value)
}
fn log_test_running(filename: &str, current: usize, total: usize) {
eprintln!(
"{}: {} [{}/{}]",