@@ -5,6 +5,8 @@ use std::convert::TryInto;
55
66/// Indicates an alternate location for the returned data.
77///
8+ /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Location)
9+ ///
810/// # Specifications
911///
1012/// - [RFC 7231, section 3.1.4.2: Content-Location](https://tools.ietf.org/html/rfc7231#section-3.1.4.2)
@@ -14,8 +16,8 @@ use std::convert::TryInto;
1416/// ```
1517/// # fn main() -> http_types::Result<()> {
1618/// #
17- /// use http_types::{Response,Url};
18- /// use http_types::content::{ ContentLocation} ;
19+ /// use http_types::{Response, Url};
20+ /// use http_types::content::ContentLocation;
1921///
2022/// let content_location = ContentLocation::new(Url::parse("https://example.net/")?);
2123///
@@ -24,7 +26,7 @@ use std::convert::TryInto;
2426///
2527/// let url = Url::parse("https://example.net/")?;
2628/// let content_location = ContentLocation::from_headers(url, res)?.unwrap();
27- /// assert_eq!(content_location.location(), "https://example.net/");
29+ /// assert_eq!(content_location.location(), &Url::parse( "https://example.net/")? );
2830/// #
2931/// # Ok(()) }
3032/// ```
@@ -81,13 +83,19 @@ impl ContentLocation {
8183 }
8284
8385 /// Get the url.
84- pub fn location ( & self ) -> String {
85- self . url . to_string ( )
86+ pub fn location ( & self ) -> & Url {
87+ & self . url
8688 }
8789
8890 /// Set the url.
89- pub fn set_location ( & mut self , location : Url ) {
91+ pub fn set_location < U > ( & mut self , location : U )
92+ where
93+ U : TryInto < Url > ,
94+ U :: Error : std:: fmt:: Debug ,
95+ {
9096 self . url = location
97+ . try_into ( )
98+ . expect ( "Could not convert into valid URL" )
9199 }
92100}
93101
@@ -106,7 +114,10 @@ mod test {
106114 let content_location =
107115 ContentLocation :: from_headers ( Url :: parse ( "https://example.net/" ) . unwrap ( ) , headers) ?
108116 . unwrap ( ) ;
109- assert_eq ! ( content_location. location( ) , "https://example.net/test.json" ) ;
117+ assert_eq ! (
118+ content_location. location( ) ,
119+ & Url :: parse( "https://example.net/test.json" ) ?
120+ ) ;
110121 Ok ( ( ) )
111122 }
112123
0 commit comments