Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/uri/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl fmt::Display for PathAndQuery {

impl hash::Hash for PathAndQuery {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.data.hash(state);
self.as_str().hash(state);
}
}

Expand All @@ -301,7 +301,7 @@ impl hash::Hash for PathAndQuery {
impl PartialEq for PathAndQuery {
#[inline]
fn eq(&self, other: &PathAndQuery) -> bool {
self.data == other.data
self.as_str() == other.as_str()
}
}

Expand Down Expand Up @@ -703,6 +703,33 @@ mod tests {
);
}

#[test]
fn empty_path_equals_slash() {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

let empty = PathAndQuery::empty();
let slash = PathAndQuery::slash();

assert_eq!(empty.as_str(), "/");
assert_eq!(slash.as_str(), "/");
assert_eq!(format!("{empty}"), "/");
assert_eq!(format!("{slash}"), "/");

// PartialOrd already compares via as_str(), so these are Equal.
assert!(empty <= slash && empty >= slash);

// PartialEq and Hash must agree with that, and with Uri equality.
assert_eq!(empty, slash);
assert_eq!(slash, empty);

let mut h1 = DefaultHasher::new();
let mut h2 = DefaultHasher::new();
empty.hash(&mut h1);
slash.hash(&mut h2);
assert_eq!(h1.finish(), h2.finish());
}

fn pq(s: &str) -> PathAndQuery {
s.parse().expect(&format!("parsing {}", s))
}
Expand Down
16 changes: 16 additions & 0 deletions src/uri/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,19 @@ fn test_partial_eq_path_with_terminating_questionmark() {

assert_eq!(uri, a);
}

#[test]
fn test_empty_path_and_query_equals_slash() {
let uri1 = Uri::from_static("http://example.com");
let uri2 = Uri::from_static("http://example.com/");

assert_eq!(uri1.to_string(), uri2.to_string());
assert_eq!(uri1, uri2);

let path1 = uri1.path_and_query().unwrap();
let path2 = uri2.path_and_query().unwrap();
assert_eq!(path1.to_string(), path2.to_string());
assert_eq!(format!("{path1:?}"), format!("{path2:?}"));
assert!(path1 <= path2 && path1 >= path2);
assert_eq!(path1, path2);
}