mirror of
https://github.com/Orange-OpenSource/hurl.git
synced 2024-12-25 12:05:32 +03:00
Log response body if very verbose option is used.
This commit is contained in:
parent
75b34f8d9c
commit
1c81471e45
@ -254,6 +254,11 @@ impl Client {
|
|||||||
body,
|
body,
|
||||||
duration,
|
duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if self.options.verbosity == Some(Verbosity::VeryVerbose) {
|
||||||
|
response.log_body();
|
||||||
|
}
|
||||||
|
|
||||||
Ok((request, response))
|
Ok((request, response))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -635,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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
Loading…
Reference in New Issue
Block a user