better debug formatting of requests

This commit is contained in:
Martin Algesten
2018-06-22 16:01:51 +02:00
parent 919eab2f12
commit c5c878caca
3 changed files with 41 additions and 3 deletions

View File

@@ -2,13 +2,19 @@ use ascii::AsciiString;
use error::Error; use error::Error;
use std::str::FromStr; use std::str::FromStr;
#[derive(Debug, Clone)] #[derive(Clone)]
/// Wrapper type for a header line. /// Wrapper type for a header line.
pub struct Header { pub struct Header {
line: AsciiString, line: AsciiString,
index: usize, index: usize,
} }
impl ::std::fmt::Debug for Header {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
write!(f, "{}", self.line)
}
}
impl Header { impl Header {
/// The header name. /// The header name.
/// ///

View File

@@ -40,10 +40,13 @@ pub struct Request {
impl ::std::fmt::Debug for Request { impl ::std::fmt::Debug for Request {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
let url = self.to_url().unwrap();
let query = combine_query(&url, &self.query);
write!( write!(
f, f,
"Request[{} {}, {}, {:?}]", "Request({} {}{}, {:?})",
self.method, self.path, self.query, self.headers self.method, url.path(), query,
self.headers
) )
} }
} }

View File

@@ -96,3 +96,32 @@ fn escape_path() {
let s = String::from_utf8_lossy(&vec); let s = String::from_utf8_lossy(&vec);
assert!(s.contains("GET /escape_path%20here HTTP/1.1")) assert!(s.contains("GET /escape_path%20here HTTP/1.1"))
} }
#[test]
fn request_debug() {
let req = get("/my/page")
.set("Authorization", "abcdef")
.set("Content-Length", "1234")
.set("Content-Type", "application/json")
.build();
let s = format!("{:?}", req);
assert_eq!(
s,
"Request(GET /my/page, [Authorization: abcdef, \
Content-Length: 1234, Content-Type: application/json])"
);
let req = get("/my/page?q=z")
.query("foo", "bar baz")
.set("Authorization", "abcdef")
.build();
let s = format!("{:?}", req);
assert_eq!(
s,
"Request(GET /my/page?q=z&foo=bar%20baz, [Authorization: abcdef])"
);
}