@@ -57,15 +57,18 @@ struct MobileClientIdentity(String);
57
57
#[ derive( Serialize , Deserialize , Clone , Debug , PartialEq ) ]
58
58
pub struct ClientContext {
59
59
/// Information about the mobile application invoking the function.
60
+ #[ serde( default ) ]
60
61
pub client : ClientApplication ,
61
62
/// Custom properties attached to the mobile event context.
63
+ #[ serde( default ) ]
62
64
pub custom : HashMap < String , String > ,
63
65
/// Environment settings from the mobile client.
66
+ #[ serde( default ) ]
64
67
pub environment : HashMap < String , String > ,
65
68
}
66
69
67
70
/// AWS Mobile SDK client fields.
68
- #[ derive( Serialize , Deserialize , Clone , Debug , PartialEq ) ]
71
+ #[ derive( Serialize , Deserialize , Default , Clone , Debug , PartialEq ) ]
69
72
#[ serde( rename_all = "camelCase" ) ]
70
73
pub struct ClientApplication {
71
74
/// The mobile app installation id
@@ -119,6 +122,18 @@ pub struct Context {
119
122
impl TryFrom < HeaderMap > for Context {
120
123
type Error = Error ;
121
124
fn try_from ( headers : HeaderMap ) -> Result < Self , Self :: Error > {
125
+ let client_context: Option < ClientContext > = if let Some ( value) = headers. get ( "lambda-runtime-client-context" ) {
126
+ serde_json:: from_str ( value. to_str ( ) ?) ?
127
+ } else {
128
+ None
129
+ } ;
130
+
131
+ let identity: Option < CognitoIdentity > = if let Some ( value) = headers. get ( "lambda-runtime-cognito-identity" ) {
132
+ serde_json:: from_str ( value. to_str ( ) ?) ?
133
+ } else {
134
+ None
135
+ } ;
136
+
122
137
let ctx = Context {
123
138
request_id : headers
124
139
. get ( "lambda-runtime-aws-request-id" )
@@ -142,8 +157,11 @@ impl TryFrom<HeaderMap> for Context {
142
157
. unwrap_or ( & HeaderValue :: from_static ( "" ) )
143
158
. to_str ( ) ?
144
159
. to_owned ( ) ,
160
+ client_context,
161
+ identity,
145
162
..Default :: default ( )
146
163
} ;
164
+
147
165
Ok ( ctx)
148
166
}
149
167
}
@@ -175,6 +193,70 @@ mod test {
175
193
assert ! ( tried. is_ok( ) ) ;
176
194
}
177
195
196
+ #[ test]
197
+ fn context_with_client_context_resolves ( ) {
198
+ let mut custom = HashMap :: new ( ) ;
199
+ custom. insert ( "key" . to_string ( ) , "value" . to_string ( ) ) ;
200
+ let mut environment = HashMap :: new ( ) ;
201
+ environment. insert ( "key" . to_string ( ) , "value" . to_string ( ) ) ;
202
+ let client_context = ClientContext {
203
+ client : ClientApplication {
204
+ installation_id : String :: new ( ) ,
205
+ app_title : String :: new ( ) ,
206
+ app_version_name : String :: new ( ) ,
207
+ app_version_code : String :: new ( ) ,
208
+ app_package_name : String :: new ( ) ,
209
+ } ,
210
+ custom,
211
+ environment,
212
+ } ;
213
+ let client_context_str = serde_json:: to_string ( & client_context) . unwrap ( ) ;
214
+ let mut headers = HeaderMap :: new ( ) ;
215
+ headers. insert ( "lambda-runtime-aws-request-id" , HeaderValue :: from_static ( "my-id" ) ) ;
216
+ headers. insert ( "lambda-runtime-deadline-ms" , HeaderValue :: from_static ( "123" ) ) ;
217
+ headers. insert (
218
+ "lambda-runtime-client-context" ,
219
+ HeaderValue :: from_str ( & client_context_str) . unwrap ( ) ,
220
+ ) ;
221
+ let tried = Context :: try_from ( headers) ;
222
+ assert ! ( tried. is_ok( ) ) ;
223
+ let tried = tried. unwrap ( ) ;
224
+ assert ! ( tried. client_context. is_some( ) ) ;
225
+ assert_eq ! ( tried. client_context. unwrap( ) , client_context) ;
226
+ }
227
+
228
+ #[ test]
229
+ fn context_with_empty_client_context_resolves ( ) {
230
+ let mut headers = HeaderMap :: new ( ) ;
231
+ headers. insert ( "lambda-runtime-aws-request-id" , HeaderValue :: from_static ( "my-id" ) ) ;
232
+ headers. insert ( "lambda-runtime-deadline-ms" , HeaderValue :: from_static ( "123" ) ) ;
233
+ headers. insert ( "lambda-runtime-client-context" , HeaderValue :: from_static ( "{}" ) ) ;
234
+ let tried = Context :: try_from ( headers) ;
235
+ assert ! ( tried. is_ok( ) ) ;
236
+ assert ! ( tried. unwrap( ) . client_context. is_some( ) ) ;
237
+ }
238
+
239
+ #[ test]
240
+ fn context_with_identity_resolves ( ) {
241
+ let cognito_identity = CognitoIdentity {
242
+ identity_id : String :: new ( ) ,
243
+ identity_pool_id : String :: new ( ) ,
244
+ } ;
245
+ let cognito_identity_str = serde_json:: to_string ( & cognito_identity) . unwrap ( ) ;
246
+ let mut headers = HeaderMap :: new ( ) ;
247
+ headers. insert ( "lambda-runtime-aws-request-id" , HeaderValue :: from_static ( "my-id" ) ) ;
248
+ headers. insert ( "lambda-runtime-deadline-ms" , HeaderValue :: from_static ( "123" ) ) ;
249
+ headers. insert (
250
+ "lambda-runtime-cognito-identity" ,
251
+ HeaderValue :: from_str ( & cognito_identity_str) . unwrap ( ) ,
252
+ ) ;
253
+ let tried = Context :: try_from ( headers) ;
254
+ assert ! ( tried. is_ok( ) ) ;
255
+ let tried = tried. unwrap ( ) ;
256
+ assert ! ( tried. identity. is_some( ) ) ;
257
+ assert_eq ! ( tried. identity. unwrap( ) , cognito_identity) ;
258
+ }
259
+
178
260
#[ test]
179
261
fn context_with_bad_deadline_type_is_err ( ) {
180
262
let mut headers = HeaderMap :: new ( ) ;
@@ -192,6 +274,42 @@ mod test {
192
274
assert ! ( tried. is_err( ) ) ;
193
275
}
194
276
277
+ #[ test]
278
+ fn context_with_bad_client_context_is_err ( ) {
279
+ let mut headers = HeaderMap :: new ( ) ;
280
+ headers. insert ( "lambda-runtime-aws-request-id" , HeaderValue :: from_static ( "my-id" ) ) ;
281
+ headers. insert ( "lambda-runtime-deadline-ms" , HeaderValue :: from_static ( "123" ) ) ;
282
+ headers. insert (
283
+ "lambda-runtime-client-context" ,
284
+ HeaderValue :: from_static ( "BAD-Type,not JSON" ) ,
285
+ ) ;
286
+ let tried = Context :: try_from ( headers) ;
287
+ assert ! ( tried. is_err( ) ) ;
288
+ }
289
+
290
+ #[ test]
291
+ fn context_with_empty_identity_is_err ( ) {
292
+ let mut headers = HeaderMap :: new ( ) ;
293
+ headers. insert ( "lambda-runtime-aws-request-id" , HeaderValue :: from_static ( "my-id" ) ) ;
294
+ headers. insert ( "lambda-runtime-deadline-ms" , HeaderValue :: from_static ( "123" ) ) ;
295
+ headers. insert ( "lambda-runtime-cognito-identity" , HeaderValue :: from_static ( "{}" ) ) ;
296
+ let tried = Context :: try_from ( headers) ;
297
+ assert ! ( tried. is_err( ) ) ;
298
+ }
299
+
300
+ #[ test]
301
+ fn context_with_bad_identity_is_err ( ) {
302
+ let mut headers = HeaderMap :: new ( ) ;
303
+ headers. insert ( "lambda-runtime-aws-request-id" , HeaderValue :: from_static ( "my-id" ) ) ;
304
+ headers. insert ( "lambda-runtime-deadline-ms" , HeaderValue :: from_static ( "123" ) ) ;
305
+ headers. insert (
306
+ "lambda-runtime-cognito-identity" ,
307
+ HeaderValue :: from_static ( "BAD-Type,not JSON" ) ,
308
+ ) ;
309
+ let tried = Context :: try_from ( headers) ;
310
+ assert ! ( tried. is_err( ) ) ;
311
+ }
312
+
195
313
#[ test]
196
314
#[ should_panic]
197
315
#[ allow( unused_must_use) ]
0 commit comments