Skip to content

Commit 7b2c48a

Browse files
authored
Allow base paths in the runtime api uri (#393)
This change allows to set the runtime api location with a base path. This helps tools outside production to set their own full locations that might not be in the root of the uri. Signed-off-by: David Calavera <[email protected]>
1 parent 5e324ae commit 7b2c48a

File tree

1 file changed

+60
-15
lines changed
  • lambda-runtime-api-client/src

1 file changed

+60
-15
lines changed

lambda-runtime-api-client/src/lib.rs

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
//! This crate includes a base HTTP client to interact with
66
//! the AWS Lambda Runtime API.
7-
use http::{uri::Scheme, Request, Response, Uri};
7+
use http::{uri::PathAndQuery, uri::Scheme, Request, Response, Uri};
88
use hyper::{
99
client::{connect::Connection, HttpConnector},
1010
Body,
@@ -58,26 +58,24 @@ where
5858

5959
fn set_origin<B>(&self, req: Request<B>) -> Result<Request<B>, Error> {
6060
let (mut parts, body) = req.into_parts();
61-
let (scheme, authority) = {
61+
let (scheme, authority, base_path) = {
6262
let scheme = self.base.scheme().unwrap_or(&Scheme::HTTP);
6363
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)
6566
};
6667
let path = parts.uri.path_and_query().expect("PathAndQuery not found");
68+
let pq: PathAndQuery = format!("{}{}", base_path, path).parse().expect("PathAndQuery invalid");
6769

6870
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))
8179
}
8280
}
8381

@@ -133,3 +131,50 @@ where
133131
pub fn build_request() -> http::request::Builder {
134132
http::Request::builder().header(USER_AGENT_HEADER, USER_AGENT)
135133
}
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

Comments
 (0)