Fix URI display for root path.

This commit is contained in:
Sergio Benitez 2016-10-31 17:32:43 +01:00
parent d91e3e0454
commit 785d0d2a6a
1 changed files with 18 additions and 0 deletions

View File

@ -193,6 +193,11 @@ impl<'a> URI<'a> {
impl<'a> fmt::Display for URI<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// If this is the root path, then there are "zero" segments.
if self.as_str().starts_with("/") && self.segment_count() == 0 {
return write!(f, "/");
}
for segment in self.segments() {
write!(f, "/{}", segment)?;
}
@ -577,4 +582,17 @@ mod tests {
test_fragment("/a/b/c?123", None);
test_fragment("/a", None);
}
#[test]
fn to_string() {
let uri_to_string = |string| URI::new(string).to_string();
assert_eq!(uri_to_string("/"), "/".to_string());
assert_eq!(uri_to_string("//"), "/".to_string());
assert_eq!(uri_to_string("//////a/"), "/a".to_string());
assert_eq!(uri_to_string("//ab"), "/ab".to_string());
assert_eq!(uri_to_string("//a"), "/a".to_string());
assert_eq!(uri_to_string("/a/b///c"), "/a/b/c".to_string());
assert_eq!(uri_to_string("/a///b/c/d///"), "/a/b/c/d".to_string());
}
}