Skip to content

Commit c948939

Browse files
authored
feat: inject user agent in Lambda runtime API calls (#363)
1 parent dcc33ea commit c948939

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

lambda-runtime/src/requests.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use hyper::Body;
44
use serde::Serialize;
55
use std::str::FromStr;
66

7+
const USER_AGENT: &str = concat!("aws-lambda-rust/", env!("CARGO_PKG_VERSION"));
8+
79
pub(crate) trait IntoRequest {
810
fn into_req(self) -> Result<Request<Body>, Error>;
911
}
@@ -20,6 +22,7 @@ impl IntoRequest for NextEventRequest {
2022
fn into_req(self) -> Result<Request<Body>, Error> {
2123
let req = Request::builder()
2224
.method(Method::GET)
25+
.header("User-Agent", USER_AGENT)
2326
.uri(Uri::from_static("/2018-06-01/runtime/invocation/next"))
2427
.body(Body::empty())?;
2528
Ok(req)
@@ -57,6 +60,10 @@ fn test_next_event_request() {
5760
let req = req.into_req().unwrap();
5861
assert_eq!(req.method(), Method::GET);
5962
assert_eq!(req.uri(), &Uri::from_static("/2018-06-01/runtime/invocation/next"));
63+
assert!(match req.headers().get("User-Agent") {
64+
Some(header) => header.to_str().unwrap().starts_with("aws-lambda-rust/"),
65+
None => false,
66+
});
6067
}
6168

6269
// /runtime/invocation/{AwsRequestId}/response
@@ -75,7 +82,11 @@ where
7582
let body = serde_json::to_vec(&self.body)?;
7683
let body = Body::from(body);
7784

78-
let req = Request::builder().method(Method::POST).uri(uri).body(body)?;
85+
let req = Request::builder()
86+
.header("User-Agent", USER_AGENT)
87+
.method(Method::POST)
88+
.uri(uri)
89+
.body(body)?;
7990
Ok(req)
8091
}
8192
}
@@ -90,6 +101,10 @@ fn test_event_completion_request() {
90101
let expected = Uri::from_static("/2018-06-01/runtime/invocation/id/response");
91102
assert_eq!(req.method(), Method::POST);
92103
assert_eq!(req.uri(), &expected);
104+
assert!(match req.headers().get("User-Agent") {
105+
Some(header) => header.to_str().unwrap().starts_with("aws-lambda-rust/"),
106+
None => false,
107+
});
93108
}
94109

95110
// /runtime/invocation/{AwsRequestId}/error
@@ -108,6 +123,7 @@ impl<'a> IntoRequest for EventErrorRequest<'a> {
108123
let req = Request::builder()
109124
.method(Method::POST)
110125
.uri(uri)
126+
.header("User-Agent", USER_AGENT)
111127
.header("lambda-runtime-function-error-type", "unhandled")
112128
.body(body)?;
113129
Ok(req)
@@ -127,6 +143,10 @@ fn test_event_error_request() {
127143
let expected = Uri::from_static("/2018-06-01/runtime/invocation/id/error");
128144
assert_eq!(req.method(), Method::POST);
129145
assert_eq!(req.uri(), &expected);
146+
assert!(match req.headers().get("User-Agent") {
147+
Some(header) => header.to_str().unwrap().starts_with("aws-lambda-rust/"),
148+
None => false,
149+
});
130150
}
131151

132152
// /runtime/init/error
@@ -140,6 +160,7 @@ impl IntoRequest for InitErrorRequest {
140160
let req = Request::builder()
141161
.method(Method::POST)
142162
.uri(uri)
163+
.header("User-Agent", USER_AGENT)
143164
.header("lambda-runtime-function-error-type", "unhandled")
144165
.body(Body::empty())?;
145166
Ok(req)
@@ -153,4 +174,8 @@ fn test_init_error_request() {
153174
let expected = Uri::from_static("/2018-06-01/runtime/init/error");
154175
assert_eq!(req.method(), Method::POST);
155176
assert_eq!(req.uri(), &expected);
177+
assert!(match req.headers().get("User-Agent") {
178+
Some(header) => header.to_str().unwrap().starts_with("aws-lambda-rust/"),
179+
None => false,
180+
});
156181
}

0 commit comments

Comments
 (0)