Merge pull request #632 from manuel-woelker/issue-629

feat(uri): implement fmt::Display for RequestUri (resolves #629)
This commit is contained in:
Sean McArthur
2015-08-18 15:05:13 -07:00

View File

@@ -1,4 +1,5 @@
//! HTTP RequestUris
use std::fmt::{Display, self};
use std::str::FromStr;
use url::Url;
use url::ParseError as UrlError;
@@ -72,6 +73,17 @@ impl FromStr for RequestUri {
}
}
impl Display for RequestUri {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
RequestUri::AbsolutePath(ref path) => f.write_str(path),
RequestUri::AbsoluteUri(ref url) => write!(f, "{}", url),
RequestUri::Authority(ref path) => f.write_str(path),
RequestUri::Star => f.write_str("*")
}
}
}
#[test]
fn test_uri_fromstr() {
fn read(s: &str, result: RequestUri) {
@@ -83,3 +95,16 @@ fn test_uri_fromstr() {
read("hyper.rs", RequestUri::Authority("hyper.rs".to_owned()));
read("/", RequestUri::AbsolutePath("/".to_owned()));
}
#[test]
fn test_uri_display() {
fn assert_display(expected_string: &str, request_uri: RequestUri) {
assert_eq!(expected_string, format!("{}", request_uri));
}
assert_display("*", RequestUri::Star);
assert_display("http://hyper.rs/", RequestUri::AbsoluteUri(Url::parse("http://hyper.rs/").unwrap()));
assert_display("hyper.rs", RequestUri::Authority("hyper.rs".to_owned()));
assert_display("/", RequestUri::AbsolutePath("/".to_owned()));
}