Skip to content

Commit b28b868

Browse files
authored
Add with_request_context to RequestExt (#523)
The trait `RequestExt` already includes functions to add query string parameters, path parameters and stage variables to a request. This commit adds another function to allow adding a `RequestContext` to the request. This is useful in cases where a lambda function is called by the API Gateway with a custom authorizer that adds parameters to the `RequestContext`. Related to #522 Co-authored-by: Simon Gasse <[email protected]>
1 parent 9b27da0 commit b28b868

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

lambda-http/src/ext.rs

+11
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ pub trait RequestExt {
165165
/// Return request context data assocaited with the ALB or API gateway request
166166
fn request_context(&self) -> RequestContext;
167167

168+
/// Configures instance with request context
169+
///
170+
/// This is intended for use in mock testing contexts.
171+
fn with_request_context(self, context: RequestContext) -> Self;
172+
168173
/// Return the Result of a payload parsed into a serde Deserializeable
169174
/// type
170175
///
@@ -255,6 +260,12 @@ impl RequestExt for http::Request<Body> {
255260
.expect("Request did not contain a request context")
256261
}
257262

263+
fn with_request_context(self, context: RequestContext) -> Self {
264+
let mut s = self;
265+
s.extensions_mut().insert(context);
266+
s
267+
}
268+
258269
fn lambda_context(&self) -> Context {
259270
self.extensions()
260271
.get::<Context>()

0 commit comments

Comments
 (0)