Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn url_origin(url: &Url) -> Origin {
/// the URL does not have the same origin as any other URL.
///
/// For more information see https://url.spec.whatwg.org/#origin
#[derive(PartialEq, Eq, Clone, Debug)]
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub enum Origin {
/// A globally unique identifier
Opaque(OpaqueOrigin),
Expand Down Expand Up @@ -123,7 +123,7 @@ impl Origin {
}

/// Opaque identifier for URLs that have file or other schemes
#[derive(Eq, PartialEq, Clone, Debug)]
#[derive(Eq, PartialEq, Hash, Clone, Debug)]
pub struct OpaqueOrigin(usize);

#[cfg(feature = "heapsize")]
Expand Down
42 changes: 42 additions & 0 deletions tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,45 @@ fn define_encode_set_scopes() {

m::test();
}

#[test]
/// https://github.com/servo/rust-url/issues/302
fn test_origin_hash() {
use std::hash::{Hash,Hasher};
use std::collections::hash_map::DefaultHasher;

fn hash<T: Hash>(value: &T) -> u64 {
let mut hasher = DefaultHasher::new();
value.hash(&mut hasher);
hasher.finish()
}

let origin = &Url::parse("http://example.net/").unwrap().origin();

let origins_to_compare = [
Url::parse("http://example.net:80/").unwrap().origin(),
Url::parse("http://example.net:81/").unwrap().origin(),
Url::parse("http://example.net").unwrap().origin(),
Url::parse("http://example.net/hello").unwrap().origin(),
Url::parse("https://example.net").unwrap().origin(),
Url::parse("ftp://example.net").unwrap().origin(),
Url::parse("file://example.net").unwrap().origin(),
Url::parse("http://[email protected]/").unwrap().origin(),
Url::parse("http://user:[email protected]/").unwrap().origin(),
];

for origin_to_compare in &origins_to_compare {
if origin == origin_to_compare {
assert_eq!(hash(origin), hash(origin_to_compare));
} else {
assert_ne!(hash(origin), hash(origin_to_compare));
}
}

let opaque_origin = Url::parse("file://example.net").unwrap().origin();
let same_opaque_origin = Url::parse("file://example.net").unwrap().origin();
let other_opaque_origin = Url::parse("file://other").unwrap().origin();

assert_ne!(hash(&opaque_origin), hash(&same_opaque_origin));
assert_ne!(hash(&opaque_origin), hash(&other_opaque_origin));
}