@@ -7,7 +7,7 @@ use std::convert::TryInto;
7
7
///
8
8
/// # Specifications
9
9
///
10
- /// - [RFC 7231, section 3.1.4.2: Content-Length ](https://tools.ietf.org/html/rfc7231#section-3.1.4.2)
10
+ /// - [RFC 7231, section 3.1.4.2: Content-Location ](https://tools.ietf.org/html/rfc7231#section-3.1.4.2)
11
11
///
12
12
/// # Examples
13
13
///
@@ -17,7 +17,7 @@ use std::convert::TryInto;
17
17
/// use http_types::{Response,Url};
18
18
/// use http_types::content::{ContentLocation};
19
19
///
20
- /// let content_location = ContentLocation::new("https://example.net/".to_string() );
20
+ /// let content_location = ContentLocation::new(Url::parse( "https://example.net/")? );
21
21
///
22
22
/// let mut res = Response::new(200);
23
23
/// content_location.apply(&mut res);
@@ -34,16 +34,16 @@ pub struct ContentLocation {
34
34
35
35
impl ContentLocation {
36
36
/// Create a new instance of `Content-Location` header.
37
- pub fn new ( url : String ) -> Self {
38
- Self { url : Url :: parse ( & url ) . unwrap ( ) }
37
+ pub fn new ( url : Url ) -> Self {
38
+ Self { url }
39
39
}
40
40
41
41
/// Create a new instance from headers.
42
42
pub fn from_headers < U > ( base_url : U , headers : impl AsRef < Headers > ) -> crate :: Result < Option < Self > >
43
43
where
44
44
U : TryInto < Url > ,
45
45
U :: Error : std:: fmt:: Debug ,
46
- {
46
+ {
47
47
let headers = match headers. as_ref ( ) . get ( CONTENT_LOCATION ) {
48
48
Some ( headers) => headers,
49
49
None => return Ok ( None ) ,
@@ -52,7 +52,7 @@ impl ContentLocation {
52
52
// If we successfully parsed the header then there's always at least one
53
53
// entry. We want the last entry.
54
54
let value = headers. iter ( ) . last ( ) . unwrap ( ) ;
55
- let base = base_url. try_into ( ) ? ;
55
+ let base = base_url. try_into ( ) . unwrap ( ) ;
56
56
let url = base. join ( value. as_str ( ) . trim ( ) ) . status ( 400 ) ?;
57
57
Ok ( Some ( Self { url } ) )
58
58
}
@@ -81,8 +81,8 @@ impl ContentLocation {
81
81
}
82
82
83
83
/// Set the url.
84
- pub fn set_location ( & mut self , location : & str ) {
85
- self . url = Url :: parse ( location) . unwrap ( ) ;
84
+ pub fn set_location ( & mut self , location : Url ) {
85
+ self . url = location
86
86
}
87
87
}
88
88
@@ -93,12 +93,14 @@ mod test {
93
93
94
94
#[ test]
95
95
fn smoke ( ) -> crate :: Result < ( ) > {
96
- let content_location = ContentLocation :: new ( "https://example.net/test.json" . to_string ( ) ) ;
96
+ let content_location = ContentLocation :: new ( Url :: parse ( "https://example.net/test.json" ) ? ) ;
97
97
98
98
let mut headers = Headers :: new ( ) ;
99
99
content_location. apply ( & mut headers) ;
100
100
101
- let content_location = ContentLocation :: from_headers ( Url :: parse ( "https://example.net/" ) . unwrap ( ) , headers ) ?. unwrap ( ) ;
101
+ let content_location =
102
+ ContentLocation :: from_headers ( Url :: parse ( "https://example.net/" ) . unwrap ( ) , headers) ?
103
+ . unwrap ( ) ;
102
104
assert_eq ! ( content_location. location( ) , "https://example.net/test.json" ) ;
103
105
Ok ( ( ) )
104
106
}
@@ -107,7 +109,9 @@ mod test {
107
109
fn bad_request_on_parse_error ( ) -> crate :: Result < ( ) > {
108
110
let mut headers = Headers :: new ( ) ;
109
111
headers. insert ( CONTENT_LOCATION , "htt://<nori ate the tag. yum.>" ) ;
110
- let err = ContentLocation :: from_headers ( Url :: parse ( "https://example.net" ) . unwrap ( ) , headers) . unwrap_err ( ) ;
112
+ let err =
113
+ ContentLocation :: from_headers ( Url :: parse ( "https://example.net" ) . unwrap ( ) , headers)
114
+ . unwrap_err ( ) ;
111
115
assert_eq ! ( err. status( ) , 400 ) ;
112
116
Ok ( ( ) )
113
117
}
0 commit comments