Skip to content

Commit

Permalink
Merge pull request http-rs#173 from jreyes33/master
Browse files Browse the repository at this point in the history
Implement Debug for Headers and related structs
  • Loading branch information
yoshuawuyts authored Jun 5, 2020
2 parents fd8d729 + 0d53c6e commit 01d8181
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
14 changes: 13 additions & 1 deletion src/headers/header_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::str::FromStr;
use crate::Error;

/// A header name.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct HeaderName(Cow<'static, str>);

impl HeaderName {
Expand Down Expand Up @@ -49,6 +49,12 @@ impl HeaderName {
}
}

impl Debug for HeaderName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.0)
}
}

impl Display for HeaderName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
Expand Down Expand Up @@ -150,4 +156,10 @@ mod tests {
let mut res = crate::Response::new(200);
res.insert_header(&crate::headers::HOST, "127.0.0.1");
}

#[test]
fn test_debug() {
let header_name = HeaderName::from_str("hello").unwrap();
assert_eq!(format!("{:?}", header_name), "\"hello\"");
}
}
21 changes: 19 additions & 2 deletions src/headers/header_value.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::convert::TryFrom;
use std::fmt::{self, Display};
use std::fmt::{self, Debug, Display};
use std::str::FromStr;

use crate::headers::HeaderValues;
use crate::Error;
use crate::{Cookie, Mime};

/// A header value.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct HeaderValue {
inner: String,
}
Expand Down Expand Up @@ -92,6 +92,12 @@ impl<'a> TryFrom<&'a str> for HeaderValue {
}
}

impl Debug for HeaderValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.inner)
}
}

impl Display for HeaderValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.inner)
Expand Down Expand Up @@ -131,3 +137,14 @@ impl From<HeaderValues> for HeaderValue {
.expect("HeaderValues should contain at least one value")
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_debug() {
let header_value = HeaderValue::from_str("foo0").unwrap();
assert_eq!(format!("{:?}", header_value), "\"foo0\"");
}
}
34 changes: 32 additions & 2 deletions src/headers/header_values.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::headers::{HeaderValue, Values};

use std::fmt::{self, Display};
use std::fmt::{self, Debug, Display};
use std::iter::FromIterator;
use std::ops::{Deref, DerefMut, Index};
use std::slice::SliceIndex;

/// A list of `HeaderValue`s.
///
/// This always contains at least one header value.
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct HeaderValues {
pub(crate) inner: Vec<HeaderValue>,
}
Expand Down Expand Up @@ -73,6 +73,16 @@ impl FromIterator<HeaderValue> for HeaderValues {
}
}

impl Debug for HeaderValues {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.inner.len() == 1 {
write!(f, "{:?}", self.inner[0])
} else {
f.debug_list().entries(self.inner.iter()).finish()
}
}
}

impl Display for HeaderValues {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut list = f.debug_list();
Expand Down Expand Up @@ -153,3 +163,23 @@ impl<'a> IntoIterator for &'a HeaderValues {
self.iter()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_debug_single() {
let header_values = HeaderValues {
inner: vec!["foo0".parse().unwrap()],
};
assert_eq!(format!("{:?}", header_values), "\"foo0\"");
}
#[test]
fn test_debug_multiple() {
let header_values = HeaderValues {
inner: vec!["foo0".parse().unwrap(), "foo1".parse().unwrap()],
};
assert_eq!(format!("{:?}", header_values), r#"["foo0", "foo1"]"#);
}
}
24 changes: 23 additions & 1 deletion src/headers/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::collections::HashMap;
use std::convert::Into;
use std::fmt::{self, Debug};
use std::iter::IntoIterator;
use std::ops::Index;
use std::str::FromStr;
Expand All @@ -26,7 +27,7 @@ use crate::headers::{
/// res.insert_header("hello", "foo0");
/// assert_eq!(res["hello"], "foo0");
/// ```
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct Headers {
pub(crate) headers: HashMap<HeaderName, HeaderValues>,
}
Expand Down Expand Up @@ -176,6 +177,12 @@ impl<'a> IntoIterator for &'a mut Headers {
}
}

impl Debug for Headers {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.headers.iter()).finish()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -207,4 +214,19 @@ mod tests {
assert_eq!(headers["hello"], "foo0");
assert_eq!(headers.get("hello").unwrap(), "foo0");
}

#[test]
fn test_debug_single() {
let mut headers = Headers::new();
headers.insert("single", "foo0");
assert_eq!(format!("{:?}", headers), r#"{"single": "foo0"}"#);
}

#[test]
fn test_debug_multiple() {
let mut headers = Headers::new();
headers.append("multi", "foo0");
headers.append("multi", "foo1");
assert_eq!(format!("{:?}", headers), r#"{"multi": ["foo0", "foo1"]}"#);
}
}

0 comments on commit 01d8181

Please sign in to comment.