Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add impl From<Uri> for Location + accessor #104

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions src/common/location.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::convert::TryFrom;

use http::Uri;
use HeaderValue;

/// `Location` header, defined in
Expand Down Expand Up @@ -28,6 +31,23 @@ derive_header! {
name: LOCATION
}

impl Location {
/// Accesses the header's value
pub fn value(&self) -> &HeaderValue {
&self.0
}
}

impl From<Uri> for Location {
fn from(uri: Uri) -> Self {
Self(
HeaderValue::try_from(uri.to_string())
// cf. https://www.rfc-editor.org/rfc/rfc3986#section-2
.expect("All URI characters should be valid HTTP header value characters"),
)
}
}

#[cfg(test)]
mod tests {
use super::super::test_decode;
Expand All @@ -48,4 +68,21 @@ mod tests {

assert_eq!(loc, Location(HeaderValue::from_static(s)));
}

#[test]
fn uri_constructor() {
let s = "https://www.rust-lang.org/tools";
let uri: Uri = s.parse().unwrap();
let loc = Location::from(uri);

assert_eq!(loc, Location(HeaderValue::from_static(s)));
assert_eq!(loc.value().to_str().unwrap(), s);
}

#[test]
fn uri_constructor_invalid_chars() {
let s = "https://www.rust-lang.org/hélas";
let uri: Result<Uri, _> = s.parse();
assert!(uri.is_err());
}
}