-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathmain.rs
790 lines (663 loc) · 26.4 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
pub mod common;
use std::env;
use std::io;
use std::io::prelude::*;
use std::sync::Arc;
use crate::common::LambdaEventBuilder;
use http::HeaderMap;
use http::Request;
use http::{Method, Response};
use httpmock::{
Method::{DELETE, GET, POST, PUT},
MockServer,
};
use hyper::body::Incoming;
use lambda_http::Body;
use lambda_http::Context;
use lambda_web_adapter::{Adapter, AdapterOptions, LambdaInvokeMode, Protocol};
use tower::{Service, ServiceBuilder};
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use flate2::Compression;
use http_body_util::BodyExt;
use lambda_http::lambda_runtime::Config;
use serde_json::json;
use tower_http::compression::{CompressionBody, CompressionLayer};
#[test]
fn test_adapter_options_from_env() {
env::set_var("PORT", "3000");
env::set_var("HOST", "localhost");
env::set_var("READINESS_CHECK_PORT", "8000");
env::set_var("READINESS_CHECK_PROTOCOL", "TCP");
env::set_var("READINESS_CHECK_PATH", "/healthcheck");
env::set_var("REMOVE_BASE_PATH", "/prod");
env::set_var("ASYNC_INIT", "true");
env::set_var("AWS_LWA_ENABLE_COMPRESSION", "true");
env::set_var("AWS_LWA_ENABLE_TLS", "true");
env::set_var("AWS_LWA_TLS_SERVER_NAME", "api.example.com");
env::remove_var("AWS_LWA_TLS_CERT_FILE");
env::set_var("AWS_LWA_INVOKE_MODE", "buffered");
env::set_var("AWS_LWA_AUTHORIZATION_SOURCE", "auth-token");
// Initialize adapter with env options
let options = AdapterOptions::default();
Adapter::new(&options);
assert_eq!("3000", options.port);
assert_eq!("localhost", options.host);
assert_eq!("8000", options.readiness_check_port);
assert_eq!("/healthcheck", options.readiness_check_path);
assert_eq!(Protocol::Tcp, options.readiness_check_protocol);
assert_eq!(Some("/prod".into()), options.base_path);
assert!(options.async_init);
assert!(options.compression);
assert_eq!(LambdaInvokeMode::Buffered, options.invoke_mode);
assert_eq!(Some("auth-token".into()), options.authorization_source);
}
#[test]
fn test_adapter_options_from_namespaced_env() {
env::set_var("AWS_LWA_PORT", "3000");
env::set_var("AWS_LWA_HOST", "localhost");
env::set_var("AWS_LWA_READINESS_CHECK_MIN_UNHEALTHY_STATUS", "400");
env::set_var("AWS_LWA_READINESS_CHECK_PORT", "8000");
env::set_var("AWS_LWA_READINESS_CHECK_PROTOCOL", "TCP");
env::set_var("AWS_LWA_READINESS_CHECK_PATH", "/healthcheck");
env::set_var("AWS_LWA_REMOVE_BASE_PATH", "/prod");
env::set_var("AWS_LWA_ASYNC_INIT", "true");
env::set_var("AWS_LWA_ENABLE_COMPRESSION", "true");
env::set_var("AWS_LWA_INVOKE_MODE", "response_stream");
env::set_var("AWS_LWA_AUTHORIZATION_SOURCE", "auth-token");
// Initialize adapter with env options
let options = AdapterOptions::default();
Adapter::new(&options);
assert_eq!("3000", options.port);
assert_eq!(400, options.readiness_check_min_unhealthy_status);
assert_eq!("localhost", options.host);
assert_eq!("8000", options.readiness_check_port);
assert_eq!("/healthcheck", options.readiness_check_path);
assert_eq!(Protocol::Tcp, options.readiness_check_protocol);
assert_eq!(Some("/prod".into()), options.base_path);
assert!(options.async_init);
assert!(options.compression);
assert_eq!(LambdaInvokeMode::ResponseStream, options.invoke_mode);
assert_eq!(Some("auth-token".into()), options.authorization_source);
}
#[test]
fn test_readiness_check_port_fallback_to_lwa_port() {
env::remove_var("AWS_LWA_READINESS_CHECK_PORT");
env::remove_var("READINESS_CHECK_PORT");
env::set_var("AWS_LWA_PORT", "3000");
// Initialize adapter with env options
let options = AdapterOptions::default();
Adapter::new(&options);
assert_eq!("3000", options.readiness_check_port);
}
#[tokio::test]
async fn test_http_readiness_check() {
// Start app server
let app_server = MockServer::start();
let healthcheck = app_server.mock(|when, then| {
when.method(GET).path("/healthcheck");
then.status(200).body("OK");
});
// Prepare adapter configuration
let options = AdapterOptions {
host: app_server.host(),
port: app_server.port().to_string(),
readiness_check_port: app_server.port().to_string(),
readiness_check_path: "/healthcheck".to_string(),
..Default::default()
};
// Initialize adapter and do readiness check
let mut adapter = Adapter::new(&options);
adapter.check_init_health().await;
// Assert app server's healthcheck endpoint got called
healthcheck.assert();
}
#[tokio::test]
async fn test_http_basic_request() {
// Start app server
let app_server = MockServer::start();
let hello = app_server.mock(|when, then| {
when.method(GET).path("/hello");
then.status(200).body("Hello World");
});
// Initialize adapter
let mut adapter = Adapter::new(&AdapterOptions {
host: app_server.host(),
port: app_server.port().to_string(),
readiness_check_port: app_server.port().to_string(),
readiness_check_path: "/healthcheck".to_string(),
..Default::default()
});
// // Call the adapter service with basic request
let req = LambdaEventBuilder::new().with_path("/hello").build();
// We convert to Request object because it allows us to add
// the lambda Context
let mut request = Request::from(req);
add_lambda_context_to_request(&mut request);
let response = adapter.call(request).await.expect("Request failed");
// Assert endpoint was called once
hello.assert();
// and response has expected content
assert_eq!(200, response.status());
assert_eq!(response.headers().get("content-length").unwrap(), "11");
assert_eq!("Hello World", body_to_string(response).await);
}
#[tokio::test]
async fn test_http_headers() {
// Start app server
let app_server = MockServer::start();
// An endpoint that expects and returns headers
let test_endpoint = app_server.mock(|when, then| {
when.method(GET).path("/").header("foo", "bar");
then.status(200).header("fizz", "buzz").body("OK");
});
// Initialize adapter and do readiness check
let mut adapter = Adapter::new(&AdapterOptions {
host: app_server.host(),
port: app_server.port().to_string(),
readiness_check_port: app_server.port().to_string(),
readiness_check_path: "/healthcheck".to_string(),
..Default::default()
});
// Prepare request
let req = LambdaEventBuilder::new()
.with_path("/")
.with_header("foo", "bar")
.build();
// We convert to Request object because it allows us to add
// the Lambda Context
let mut request = Request::from(req);
add_lambda_context_to_request(&mut request);
// Call the adapter service with request
let response = adapter.call(request).await.expect("Request failed");
// Assert endpoint was called once
test_endpoint.assert();
// and response has expected content
assert_eq!(200, response.status());
assert!(response.headers().contains_key("fizz"));
assert_eq!("buzz", response.headers().get("fizz").unwrap());
assert_eq!("OK", body_to_string(response).await);
}
#[tokio::test]
async fn test_http_path_encoding() {
// Start app server
let app_server = MockServer::start();
// An endpoint that expects and returns headers
let test_endpoint = app_server.mock(|when, then| {
when.method(GET).path("/A%C3%B1o_1234");
then.status(200).body("Ok");
});
// Initialize adapter and do readiness check
let mut adapter = Adapter::new(&AdapterOptions {
host: app_server.host(),
port: app_server.port().to_string(),
readiness_check_port: app_server.port().to_string(),
readiness_check_path: "/healthcheck".to_string(),
..Default::default()
});
// Prepare request
let req = LambdaEventBuilder::new().with_path("/Año_1234").build();
// We convert to Request object because it allows us to add
// the lambda Context
let mut request = Request::from(req);
add_lambda_context_to_request(&mut request);
// Call the adapter service with request
let response = adapter.call(request).await.expect("Request failed");
// Assert endpoint was called once
test_endpoint.assert();
// and response has expected content
assert_eq!(200, response.status());
assert_eq!("Ok", body_to_string(response).await);
}
#[tokio::test]
async fn test_http_query_params() {
// Start app server
let app_server = MockServer::start();
// An endpoint that expects and returns headers
let test_endpoint = app_server.mock(|when, then| {
when.method(GET)
.path("/")
.query_param("foo", "bar")
.query_param("fizz", "buzz");
then.status(200).body("OK");
});
// Initialize adapter
let mut adapter = Adapter::new(&AdapterOptions {
host: app_server.host(),
port: app_server.port().to_string(),
readiness_check_port: app_server.port().to_string(),
readiness_check_path: "/healthcheck".to_string(),
..Default::default()
});
// Prepare request
let req = LambdaEventBuilder::new()
.with_path("/")
.with_query("foo", "bar")
.with_query("fizz", "buzz")
.build();
// We convert to Request object because it allows us to add
// the lambda Context
let mut request = Request::from(req);
add_lambda_context_to_request(&mut request);
// Call the adapter service with request
let response = adapter.call(request).await.expect("Request failed");
// Assert endpoint was called once
test_endpoint.assert();
// and response has expected content
assert_eq!(200, response.status());
assert_eq!("OK", body_to_string(response).await);
}
#[tokio::test]
async fn test_http_post_put_delete() {
// Start app server
let app_server = MockServer::start();
let post_endpoint = app_server.mock(|when, then| {
when.path("/").method(POST);
then.status(200).body("POST Success");
});
let put_endpoint = app_server.mock(|when, then| {
when.path("/").method(PUT);
then.status(200).body("PUT Success");
});
let delete_endpoint = app_server.mock(|when, then| {
when.path("/").method(DELETE);
then.status(200).body("DELETE Success");
});
// Initialize adapter
let mut adapter = Adapter::new(&AdapterOptions {
host: app_server.host(),
port: app_server.port().to_string(),
readiness_check_port: app_server.port().to_string(),
readiness_check_path: "/healthcheck".to_string(),
..Default::default()
});
// Prepare requests
let post_req = LambdaEventBuilder::new()
.with_method(Method::POST)
.with_path("/")
.build();
let put_req = LambdaEventBuilder::new()
.with_method(Method::PUT)
.with_path("/")
.build();
let delete_req = LambdaEventBuilder::new()
.with_method(Method::DELETE)
.with_path("/")
.build();
// We convert to Request object because it allows us to add the lambda Context
let mut post_request = Request::from(post_req);
add_lambda_context_to_request(&mut post_request);
let mut put_request = Request::from(put_req);
add_lambda_context_to_request(&mut put_request);
let mut delete_request = Request::from(delete_req);
add_lambda_context_to_request(&mut delete_request);
// Call the adapter service with requests
let post_response = adapter.call(post_request).await.expect("Request failed");
let put_response = adapter.call(put_request).await.expect("Request failed");
let delete_response = adapter.call(delete_request).await.expect("Request failed");
// Assert endpoints were called
post_endpoint.assert();
put_endpoint.assert();
delete_endpoint.assert();
assert_eq!(200, post_response.status());
assert_eq!(200, put_response.status());
assert_eq!(200, delete_response.status());
assert_eq!("POST Success", body_to_string(post_response).await);
assert_eq!("PUT Success", body_to_string(put_response).await);
assert_eq!("DELETE Success", body_to_string(delete_response).await);
}
#[tokio::test]
async fn test_http_compress() {
// Start app server
let app_server = MockServer::start();
let hello = app_server.mock(|when, then| {
when.method(GET).path("/hello");
then.status(200)
.header("content-type", "text/plain")
.body("Hello World Hello World Hello World Hello World Hello World");
});
// Initialize adapter
let adapter = Adapter::new(&AdapterOptions {
host: app_server.host(),
port: app_server.port().to_string(),
readiness_check_port: app_server.port().to_string(),
readiness_check_path: "/healthcheck".to_string(),
compression: true,
..Default::default()
});
let mut svc = ServiceBuilder::new().layer(CompressionLayer::new()).service(adapter);
// // Call the adapter service with basic request
let req = LambdaEventBuilder::new()
.with_path("/hello")
.with_header("accept-encoding", "gzip")
.build();
// We convert to Request object because it allows us to add
// the lambda Context
let mut request = Request::from(req);
add_lambda_context_to_request(&mut request);
let response = svc.call(request).await.expect("Request failed");
// Assert endpoint was called once
hello.assert();
// and response has expected content
assert_eq!(200, response.status());
assert_eq!(response.headers().get("content-encoding").unwrap(), "gzip");
assert_eq!(
"Hello World Hello World Hello World Hello World Hello World",
compressed_body_to_string(response).await
);
}
#[tokio::test]
async fn test_http_compress_disallowed_type() {
// Start app server
let app_server = MockServer::start();
let hello = app_server.mock(|when, then| {
when.method(GET).path("/hello");
then.status(200)
.header("content-type", "application/octet-stream")
.body("Hello World Hello World Hello World Hello World Hello World");
});
// Initialize adapter
let mut adapter = Adapter::new(&AdapterOptions {
host: app_server.host(),
port: app_server.port().to_string(),
readiness_check_port: app_server.port().to_string(),
readiness_check_path: "/healthcheck".to_string(),
compression: true,
..Default::default()
});
// // Call the adapter service with basic request
let req = LambdaEventBuilder::new()
.with_path("/hello")
.with_header("accept-encoding", "gzip")
.build();
// We convert to Request object because it allows us to add
// the lambda Context
let mut request = Request::from(req);
add_lambda_context_to_request(&mut request);
let response = adapter.call(request).await.expect("Request failed");
// Assert endpoint was called once
hello.assert();
// and response has expected content
assert_eq!(200, response.status());
assert_eq!(response.headers().get("content-length").unwrap(), "59"); // uncompressed: 59
assert!(!response.headers().contains_key("content-encoding"));
assert_eq!(
"Hello World Hello World Hello World Hello World Hello World",
body_to_string(response).await
);
}
#[tokio::test]
async fn test_http_compress_already_compressed() {
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
encoder
.write_all(b"Hello World Hello World Hello World Hello World Hello World")
.unwrap();
let gzipped_body = encoder.finish().unwrap();
// Start app server
let app_server = MockServer::start();
let hello = app_server.mock(|when, then| {
when.method(GET).path("/hello");
then.status(200).header("content-encoding", "gzip").body(&gzipped_body);
});
// Initialize adapter
let adapter = Adapter::new(&AdapterOptions {
host: app_server.host(),
port: app_server.port().to_string(),
readiness_check_port: app_server.port().to_string(),
readiness_check_path: "/healthcheck".to_string(),
compression: true,
..Default::default()
});
let mut svc = ServiceBuilder::new().layer(CompressionLayer::new()).service(adapter);
// Call the adapter service with basic request
let req = LambdaEventBuilder::new()
.with_path("/hello")
.with_header("accept-encoding", "gzip")
.build();
// We convert to Request object because it allows us to add
// the lambda Context
let mut request = Request::from(req);
add_lambda_context_to_request(&mut request);
let response = svc.call(request).await.expect("Request failed");
// Assert endpoint was called once
hello.assert();
// and response has expected content
assert_eq!(200, response.status());
assert_eq!(response.headers().get("content-length").unwrap(), "48"); // uncompressed: 59
assert_eq!(response.headers().get("content-encoding").unwrap(), "gzip");
assert_eq!(
"Hello World Hello World Hello World Hello World Hello World",
compressed_body_to_string(response).await
);
}
#[tokio::test]
async fn test_http_context_headers() {
// Start app server
let app_server = MockServer::start();
// An endpoint that expects and returns headers
let test_endpoint = app_server.mock(|when, then| {
when.method(GET)
.path("/")
.header_exists("x-amzn-lambda-context")
.header_exists("x-amzn-request-context");
then.status(200).header("fizz", "buzz").body("OK");
});
// Initialize adapter and do readiness check
let mut adapter = Adapter::new(&AdapterOptions {
host: app_server.host(),
port: app_server.port().to_string(),
readiness_check_port: app_server.port().to_string(),
readiness_check_path: "/healthcheck".to_string(),
..Default::default()
});
// Prepare request
let req = LambdaEventBuilder::new().with_path("/").build();
// We convert to Request object because it allows us to add
// the Lambda Context
let mut request = Request::from(req);
add_lambda_context_to_request(&mut request);
// Call the adapter service with request
let response = adapter.call(request).await.expect("Request failed");
// Assert endpoint was called once
test_endpoint.assert();
// and response has expected content
assert_eq!(200, response.status());
assert!(response.headers().contains_key("fizz"));
assert_eq!("buzz", response.headers().get("fizz").unwrap());
assert_eq!("OK", body_to_string(response).await);
}
#[tokio::test]
async fn test_http_content_encoding_suffix() {
// Start app server
let app_server = MockServer::start();
let json_data = json!({
"name": "John Doe",
"age": 43,
"gender": "Male"
})
.to_string();
// An endpoint that expects and returns headers
let test_endpoint = app_server.mock(|when, then| {
when.method(GET).path("/json");
then.status(200)
.header("content-type", "application/graphql-response+json; charset=utf-8")
.body(json_data.to_owned());
});
// Initialize adapter and do readiness check
let mut adapter = Adapter::new(&AdapterOptions {
host: app_server.host(),
port: app_server.port().to_string(),
readiness_check_port: app_server.port().to_string(),
readiness_check_path: "/healthcheck".to_string(),
..Default::default()
});
// Prepare request
let req = LambdaEventBuilder::new().with_path("/json").build();
// We convert to Request object because it allows us to add
// the Lambda Context
let mut request = Request::from(req);
add_lambda_context_to_request(&mut request);
// Call the adapter service with request
let response = adapter.call(request).await.expect("Request failed");
// Assert endpoint was called once
test_endpoint.assert();
// and response has expected content
assert_eq!(200, response.status());
assert!(response.headers().contains_key("content-type"));
assert_eq!(
"application/graphql-response+json; charset=utf-8",
response.headers().get("content-type").unwrap()
);
assert_eq!(json_data.to_owned(), body_to_string(response).await);
}
#[tokio::test]
async fn test_http_error_status_codes() {
// Start app server
let app_server = MockServer::start();
let error_endpoint = app_server.mock(|when, then| {
when.method(GET).path("/error");
then.status(502).body("Bad Gateway");
});
// Initialize adapter with error status codes
let mut adapter = Adapter::new(&AdapterOptions {
host: app_server.host(),
port: app_server.port().to_string(),
readiness_check_port: app_server.port().to_string(),
readiness_check_path: "/healthcheck".to_string(),
error_status_codes: Some(vec![500, 502, 503, 504]),
..Default::default()
});
// Call the adapter service with request that should trigger error
let req = LambdaEventBuilder::new().with_path("/error").build();
let mut request = Request::from(req);
add_lambda_context_to_request(&mut request);
let result = adapter.call(request).await;
assert!(result.is_err(), "Expected error response for status code 502");
assert!(result.unwrap_err().to_string().contains("502"));
// Assert endpoint was called
error_endpoint.assert();
}
#[tokio::test]
async fn test_http_authorization_source() {
// Start app server
let app_server = MockServer::start();
let hello = app_server.mock(|when, then| {
when.method(GET).path("/hello").header_exists("Authorization");
then.status(200).body("Hello World");
});
// Initialize adapter
let mut adapter = Adapter::new(&AdapterOptions {
host: app_server.host(),
port: app_server.port().to_string(),
readiness_check_port: app_server.port().to_string(),
readiness_check_path: "/healthcheck".to_string(),
authorization_source: Some("auth-token".to_string()),
..Default::default()
});
// // Call the adapter service with basic request
let req = LambdaEventBuilder::new()
.with_path("/hello")
.with_header("auth-token", "Bearer token")
.build();
// We convert to Request object because it allows us to add
// the lambda Context
let mut request = Request::from(req);
add_lambda_context_to_request(&mut request);
let response = adapter.call(request).await.expect("Request failed");
// Assert endpoint was called once
hello.assert();
// and response has expected content
assert_eq!(200, response.status());
assert_eq!(response.headers().get("content-length").unwrap(), "11");
assert_eq!("Hello World", body_to_string(response).await);
}
#[tokio::test]
async fn test_http_context_multi_headers() {
// Start app server
let app_server = MockServer::start();
// An endpoint that expects and returns headers
let test_endpoint = app_server.mock(|when, then| {
when.method(GET)
.path("/")
.matches(|req| {
req.headers
.as_ref()
.unwrap()
.iter()
.filter(|(key, _value)| key == "x-amzn-lambda-context")
.count()
== 1
})
.matches(|req| {
req.headers
.as_ref()
.unwrap()
.iter()
.filter(|(key, _value)| key == "x-amzn-request-context")
.count()
== 1
});
then.status(200).header("fizz", "buzz").body("OK");
});
// Initialize adapter and do readiness check
let mut adapter = Adapter::new(&AdapterOptions {
host: app_server.host(),
port: app_server.port().to_string(),
readiness_check_port: app_server.port().to_string(),
readiness_check_path: "/healthcheck".to_string(),
..Default::default()
});
// Prepare request
let req = LambdaEventBuilder::new()
.with_path("/")
.with_header("x-amzn-lambda-context", "header_from_client_1")
.with_header("x-amzn-lambda-context", "header_from_client_2")
.with_header("x-amzn-request-context", "header_from_client_1")
.with_header("x-amzn-request-context", "header_from_client_2")
.build();
// We convert to Request object because it allows us to add
// the Lambda Context
let mut request = Request::from(req);
add_lambda_context_to_request(&mut request);
// Call the adapter service with request
let response = adapter.call(request).await.expect("Request failed");
// Assert endpoint was called once
test_endpoint.assert();
// and response has expected content
assert_eq!(200, response.status());
assert!(response.headers().contains_key("fizz"));
assert_eq!("buzz", response.headers().get("fizz").unwrap());
assert_eq!("OK", body_to_string(response).await);
}
async fn body_to_string(res: Response<Incoming>) -> String {
let body_bytes = res.collect().await.unwrap().to_bytes();
String::from_utf8_lossy(&body_bytes).to_string()
}
async fn compressed_body_to_string(res: Response<CompressionBody<Incoming>>) -> String {
let body_bytes = res.collect().await.unwrap().to_bytes();
decode_reader(&body_bytes).unwrap()
}
fn decode_reader(bytes: &[u8]) -> io::Result<String> {
let mut gz = GzDecoder::new(bytes);
let mut s = String::new();
gz.read_to_string(&mut s)?;
Ok(s)
}
fn add_lambda_context_to_request(request: &mut Request<Body>) {
// create a HeaderMap to build the lambda context
let mut headers = HeaderMap::new();
headers.insert("lambda-runtime-aws-request-id", "my_id".parse().unwrap());
headers.insert("lambda-runtime-deadline-ms", "123".parse().unwrap());
headers.insert("lambda-runtime-client-context", "{}".parse().unwrap());
let conf = Config {
function_name: "test_function".into(),
memory: 128,
version: "latest".into(),
log_stream: "/aws/lambda/test_function".into(),
log_group: "2023/09/15/[$LATEST]ab831cef03e94457a94b6efcbe22406a".into(),
};
// converts HeaderMap to Context
let context = Context::new("my_id", Arc::new(conf), &headers).expect("Couldn't convert HeaderMap to Context");
// add Context to the request
request.extensions_mut().insert(context);
}