|
4 | 4 |
|
5 | 5 | //! This crate includes a base HTTP client to interact with
|
6 | 6 | //! the AWS Lambda Runtime API.
|
7 |
| -use http::{uri::Scheme, Request, Response, Uri}; |
| 7 | +use http::{uri::PathAndQuery, uri::Scheme, Request, Response, Uri}; |
8 | 8 | use hyper::{
|
9 | 9 | client::{connect::Connection, HttpConnector},
|
10 | 10 | Body,
|
@@ -58,26 +58,24 @@ where
|
58 | 58 |
|
59 | 59 | fn set_origin<B>(&self, req: Request<B>) -> Result<Request<B>, Error> {
|
60 | 60 | let (mut parts, body) = req.into_parts();
|
61 |
| - let (scheme, authority) = { |
| 61 | + let (scheme, authority, base_path) = { |
62 | 62 | let scheme = self.base.scheme().unwrap_or(&Scheme::HTTP);
|
63 | 63 | let authority = self.base.authority().expect("Authority not found");
|
64 |
| - (scheme, authority) |
| 64 | + let base_path = self.base.path().trim_end_matches('/'); |
| 65 | + (scheme, authority, base_path) |
65 | 66 | };
|
66 | 67 | let path = parts.uri.path_and_query().expect("PathAndQuery not found");
|
| 68 | + let pq: PathAndQuery = format!("{}{}", base_path, path).parse().expect("PathAndQuery invalid"); |
67 | 69 |
|
68 | 70 | let uri = Uri::builder()
|
69 |
| - .scheme(scheme.clone()) |
70 |
| - .authority(authority.clone()) |
71 |
| - .path_and_query(path.clone()) |
72 |
| - .build(); |
73 |
| - |
74 |
| - match uri { |
75 |
| - Ok(u) => { |
76 |
| - parts.uri = u; |
77 |
| - Ok(Request::from_parts(parts, body)) |
78 |
| - } |
79 |
| - Err(e) => Err(Box::new(e)), |
80 |
| - } |
| 71 | + .scheme(scheme.as_ref()) |
| 72 | + .authority(authority.as_ref()) |
| 73 | + .path_and_query(pq) |
| 74 | + .build() |
| 75 | + .map_err(Box::new)?; |
| 76 | + |
| 77 | + parts.uri = uri; |
| 78 | + Ok(Request::from_parts(parts, body)) |
81 | 79 | }
|
82 | 80 | }
|
83 | 81 |
|
@@ -133,3 +131,50 @@ where
|
133 | 131 | pub fn build_request() -> http::request::Builder {
|
134 | 132 | http::Request::builder().header(USER_AGENT_HEADER, USER_AGENT)
|
135 | 133 | }
|
| 134 | + |
| 135 | +#[cfg(test)] |
| 136 | +mod tests { |
| 137 | + use super::*; |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_set_origin() { |
| 141 | + let base = "http://localhost:9001"; |
| 142 | + let client = Client::builder().with_endpoint(base.parse().unwrap()).build().unwrap(); |
| 143 | + let req = build_request() |
| 144 | + .uri("/2018-06-01/runtime/invocation/next") |
| 145 | + .body(()) |
| 146 | + .unwrap(); |
| 147 | + let req = client.set_origin(req).unwrap(); |
| 148 | + assert_eq!( |
| 149 | + "http://localhost:9001/2018-06-01/runtime/invocation/next", |
| 150 | + &req.uri().to_string() |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + #[test] |
| 155 | + fn test_set_origin_with_base_path() { |
| 156 | + let base = "http://localhost:9001/foo"; |
| 157 | + let client = Client::builder().with_endpoint(base.parse().unwrap()).build().unwrap(); |
| 158 | + let req = build_request() |
| 159 | + .uri("/2018-06-01/runtime/invocation/next") |
| 160 | + .body(()) |
| 161 | + .unwrap(); |
| 162 | + let req = client.set_origin(req).unwrap(); |
| 163 | + assert_eq!( |
| 164 | + "http://localhost:9001/foo/2018-06-01/runtime/invocation/next", |
| 165 | + &req.uri().to_string() |
| 166 | + ); |
| 167 | + |
| 168 | + let base = "http://localhost:9001/foo/"; |
| 169 | + let client = Client::builder().with_endpoint(base.parse().unwrap()).build().unwrap(); |
| 170 | + let req = build_request() |
| 171 | + .uri("/2018-06-01/runtime/invocation/next") |
| 172 | + .body(()) |
| 173 | + .unwrap(); |
| 174 | + let req = client.set_origin(req).unwrap(); |
| 175 | + assert_eq!( |
| 176 | + "http://localhost:9001/foo/2018-06-01/runtime/invocation/next", |
| 177 | + &req.uri().to_string() |
| 178 | + ); |
| 179 | + } |
| 180 | +} |
0 commit comments