spikard 0.3.6 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +21 -6
- data/ext/spikard_rb/Cargo.toml +2 -2
- data/lib/spikard/app.rb +33 -14
- data/lib/spikard/testing.rb +47 -12
- data/lib/spikard/version.rb +1 -1
- data/vendor/crates/spikard-bindings-shared/Cargo.toml +63 -0
- data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +132 -0
- data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +752 -0
- data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +194 -0
- data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +246 -0
- data/vendor/crates/spikard-bindings-shared/src/error_response.rs +401 -0
- data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +238 -0
- data/vendor/crates/spikard-bindings-shared/src/lib.rs +24 -0
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +292 -0
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +616 -0
- data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +305 -0
- data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +248 -0
- data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +351 -0
- data/vendor/crates/spikard-bindings-shared/tests/comprehensive_coverage.rs +454 -0
- data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +383 -0
- data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +280 -0
- data/vendor/crates/spikard-core/Cargo.toml +4 -4
- data/vendor/crates/spikard-core/src/debug.rs +64 -0
- data/vendor/crates/spikard-core/src/di/container.rs +3 -27
- data/vendor/crates/spikard-core/src/di/factory.rs +1 -5
- data/vendor/crates/spikard-core/src/di/graph.rs +8 -47
- data/vendor/crates/spikard-core/src/di/mod.rs +1 -1
- data/vendor/crates/spikard-core/src/di/resolved.rs +1 -7
- data/vendor/crates/spikard-core/src/di/value.rs +2 -4
- data/vendor/crates/spikard-core/src/errors.rs +30 -0
- data/vendor/crates/spikard-core/src/http.rs +262 -0
- data/vendor/crates/spikard-core/src/lib.rs +1 -1
- data/vendor/crates/spikard-core/src/lifecycle.rs +764 -0
- data/vendor/crates/spikard-core/src/metadata.rs +389 -0
- data/vendor/crates/spikard-core/src/parameters.rs +1962 -159
- data/vendor/crates/spikard-core/src/problem.rs +34 -0
- data/vendor/crates/spikard-core/src/request_data.rs +966 -1
- data/vendor/crates/spikard-core/src/router.rs +263 -2
- data/vendor/crates/spikard-core/src/validation/error_mapper.rs +688 -0
- data/vendor/crates/spikard-core/src/{validation.rs → validation/mod.rs} +26 -268
- data/vendor/crates/spikard-http/Cargo.toml +12 -16
- data/vendor/crates/spikard-http/examples/sse-notifications.rs +148 -0
- data/vendor/crates/spikard-http/examples/websocket-chat.rs +92 -0
- data/vendor/crates/spikard-http/src/auth.rs +65 -16
- data/vendor/crates/spikard-http/src/background.rs +1614 -3
- data/vendor/crates/spikard-http/src/cors.rs +515 -0
- data/vendor/crates/spikard-http/src/debug.rs +65 -0
- data/vendor/crates/spikard-http/src/di_handler.rs +1322 -77
- data/vendor/crates/spikard-http/src/handler_response.rs +711 -0
- data/vendor/crates/spikard-http/src/handler_trait.rs +607 -5
- data/vendor/crates/spikard-http/src/handler_trait_tests.rs +6 -0
- data/vendor/crates/spikard-http/src/lib.rs +33 -28
- data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +81 -0
- data/vendor/crates/spikard-http/src/lifecycle.rs +765 -0
- data/vendor/crates/spikard-http/src/middleware/mod.rs +372 -117
- data/vendor/crates/spikard-http/src/middleware/multipart.rs +836 -10
- data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +409 -43
- data/vendor/crates/spikard-http/src/middleware/validation.rs +513 -65
- data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +345 -0
- data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +1055 -0
- data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +473 -3
- data/vendor/crates/spikard-http/src/query_parser.rs +455 -31
- data/vendor/crates/spikard-http/src/response.rs +321 -0
- data/vendor/crates/spikard-http/src/server/handler.rs +1572 -9
- data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +136 -0
- data/vendor/crates/spikard-http/src/server/mod.rs +875 -178
- data/vendor/crates/spikard-http/src/server/request_extraction.rs +674 -23
- data/vendor/crates/spikard-http/src/server/routing_factory.rs +599 -0
- data/vendor/crates/spikard-http/src/sse.rs +983 -21
- data/vendor/crates/spikard-http/src/testing/form.rs +38 -0
- data/vendor/crates/spikard-http/src/testing/test_client.rs +0 -2
- data/vendor/crates/spikard-http/src/testing.rs +7 -7
- data/vendor/crates/spikard-http/src/websocket.rs +1055 -4
- data/vendor/crates/spikard-http/tests/background_behavior.rs +832 -0
- data/vendor/crates/spikard-http/tests/common/handlers.rs +309 -0
- data/vendor/crates/spikard-http/tests/common/mod.rs +26 -0
- data/vendor/crates/spikard-http/tests/di_integration.rs +192 -0
- data/vendor/crates/spikard-http/tests/doc_snippets.rs +5 -0
- data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +1093 -0
- data/vendor/crates/spikard-http/tests/multipart_behavior.rs +656 -0
- data/vendor/crates/spikard-http/tests/server_config_builder.rs +314 -0
- data/vendor/crates/spikard-http/tests/sse_behavior.rs +620 -0
- data/vendor/crates/spikard-http/tests/websocket_behavior.rs +663 -0
- data/vendor/crates/spikard-rb/Cargo.toml +10 -4
- data/vendor/crates/spikard-rb/build.rs +196 -5
- data/vendor/crates/spikard-rb/src/config/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/{config.rs → config/server_config.rs} +100 -109
- data/vendor/crates/spikard-rb/src/conversion.rs +121 -20
- data/vendor/crates/spikard-rb/src/di/builder.rs +100 -0
- data/vendor/crates/spikard-rb/src/{di.rs → di/mod.rs} +12 -46
- data/vendor/crates/spikard-rb/src/handler.rs +100 -107
- data/vendor/crates/spikard-rb/src/integration/mod.rs +3 -0
- data/vendor/crates/spikard-rb/src/lib.rs +467 -1428
- data/vendor/crates/spikard-rb/src/lifecycle.rs +1 -0
- data/vendor/crates/spikard-rb/src/metadata/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +447 -0
- data/vendor/crates/spikard-rb/src/runtime/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +324 -0
- data/vendor/crates/spikard-rb/src/server.rs +47 -22
- data/vendor/crates/spikard-rb/src/{test_client.rs → testing/client.rs} +187 -40
- data/vendor/crates/spikard-rb/src/testing/mod.rs +7 -0
- data/vendor/crates/spikard-rb/src/testing/websocket.rs +635 -0
- data/vendor/crates/spikard-rb/src/websocket.rs +178 -37
- metadata +46 -13
- data/vendor/crates/spikard-http/src/parameters.rs +0 -1
- data/vendor/crates/spikard-http/src/problem.rs +0 -1
- data/vendor/crates/spikard-http/src/router.rs +0 -1
- data/vendor/crates/spikard-http/src/schema_registry.rs +0 -1
- data/vendor/crates/spikard-http/src/type_hints.rs +0 -1
- data/vendor/crates/spikard-http/src/validation.rs +0 -1
- data/vendor/crates/spikard-rb/src/test_websocket.rs +0 -221
- /data/vendor/crates/spikard-rb/src/{test_sse.rs → testing/sse.rs} +0 -0
|
@@ -425,4 +425,769 @@ mod tests {
|
|
|
425
425
|
let result = hooks.execute_on_request(req).await.unwrap();
|
|
426
426
|
assert!(matches!(result, HookResult::Continue(_)));
|
|
427
427
|
}
|
|
428
|
+
|
|
429
|
+
#[tokio::test]
|
|
430
|
+
async fn test_hook_chaining_modifies_request_sequentially() {
|
|
431
|
+
let hooks = LifecycleHooks::builder()
|
|
432
|
+
.on_request(request_hook("add_header_1", |mut req| async move {
|
|
433
|
+
req.headers_mut()
|
|
434
|
+
.insert("X-Chain-1", axum::http::HeaderValue::from_static("first"));
|
|
435
|
+
Ok(HookResult::Continue(req))
|
|
436
|
+
}))
|
|
437
|
+
.on_request(request_hook("add_header_2", |mut req| async move {
|
|
438
|
+
req.headers_mut()
|
|
439
|
+
.insert("X-Chain-2", axum::http::HeaderValue::from_static("second"));
|
|
440
|
+
Ok(HookResult::Continue(req))
|
|
441
|
+
}))
|
|
442
|
+
.on_request(request_hook("add_header_3", |mut req| async move {
|
|
443
|
+
req.headers_mut()
|
|
444
|
+
.insert("X-Chain-3", axum::http::HeaderValue::from_static("third"));
|
|
445
|
+
Ok(HookResult::Continue(req))
|
|
446
|
+
}))
|
|
447
|
+
.build();
|
|
448
|
+
|
|
449
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
450
|
+
let result = hooks.execute_on_request(req).await.unwrap();
|
|
451
|
+
|
|
452
|
+
match result {
|
|
453
|
+
HookResult::Continue(req) => {
|
|
454
|
+
assert_eq!(req.headers().get("X-Chain-1").unwrap(), "first");
|
|
455
|
+
assert_eq!(req.headers().get("X-Chain-2").unwrap(), "second");
|
|
456
|
+
assert_eq!(req.headers().get("X-Chain-3").unwrap(), "third");
|
|
457
|
+
}
|
|
458
|
+
HookResult::ShortCircuit(_) => panic!("Expected Continue"),
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
#[tokio::test]
|
|
463
|
+
async fn test_response_hook_chaining_modifies_status_and_headers() {
|
|
464
|
+
let hooks = LifecycleHooks::builder()
|
|
465
|
+
.on_response(response_hook("add_security_header", |mut resp| async move {
|
|
466
|
+
resp.headers_mut().insert(
|
|
467
|
+
"X-Content-Type-Options",
|
|
468
|
+
axum::http::HeaderValue::from_static("nosniff"),
|
|
469
|
+
);
|
|
470
|
+
Ok(HookResult::Continue(resp))
|
|
471
|
+
}))
|
|
472
|
+
.on_response(response_hook("add_cache_header", |mut resp| async move {
|
|
473
|
+
resp.headers_mut()
|
|
474
|
+
.insert("Cache-Control", axum::http::HeaderValue::from_static("no-cache"));
|
|
475
|
+
Ok(HookResult::Continue(resp))
|
|
476
|
+
}))
|
|
477
|
+
.on_response(response_hook("add_custom_header", |mut resp| async move {
|
|
478
|
+
resp.headers_mut()
|
|
479
|
+
.insert("X-Custom", axum::http::HeaderValue::from_static("value"));
|
|
480
|
+
Ok(HookResult::Continue(resp))
|
|
481
|
+
}))
|
|
482
|
+
.build();
|
|
483
|
+
|
|
484
|
+
let resp = Response::builder().status(StatusCode::OK).body(Body::empty()).unwrap();
|
|
485
|
+
|
|
486
|
+
let result = hooks.execute_on_response(resp).await.unwrap();
|
|
487
|
+
|
|
488
|
+
assert_eq!(result.status(), StatusCode::OK);
|
|
489
|
+
assert_eq!(result.headers().get("X-Content-Type-Options").unwrap(), "nosniff");
|
|
490
|
+
assert_eq!(result.headers().get("Cache-Control").unwrap(), "no-cache");
|
|
491
|
+
assert_eq!(result.headers().get("X-Custom").unwrap(), "value");
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
#[tokio::test]
|
|
495
|
+
async fn test_pre_validation_and_pre_handler_chaining() {
|
|
496
|
+
let hooks = LifecycleHooks::builder()
|
|
497
|
+
.pre_validation(request_hook("validate_auth", |mut req| async move {
|
|
498
|
+
req.headers_mut()
|
|
499
|
+
.insert("X-Validated", axum::http::HeaderValue::from_static("true"));
|
|
500
|
+
Ok(HookResult::Continue(req))
|
|
501
|
+
}))
|
|
502
|
+
.pre_handler(request_hook("prepare_handler", |mut req| async move {
|
|
503
|
+
req.headers_mut()
|
|
504
|
+
.insert("X-Prepared", axum::http::HeaderValue::from_static("true"));
|
|
505
|
+
Ok(HookResult::Continue(req))
|
|
506
|
+
}))
|
|
507
|
+
.build();
|
|
508
|
+
|
|
509
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
510
|
+
let result = hooks.execute_pre_validation(req).await.unwrap();
|
|
511
|
+
|
|
512
|
+
match result {
|
|
513
|
+
HookResult::Continue(req) => {
|
|
514
|
+
assert_eq!(req.headers().get("X-Validated").unwrap(), "true");
|
|
515
|
+
assert!(!req.headers().contains_key("X-Prepared"));
|
|
516
|
+
}
|
|
517
|
+
HookResult::ShortCircuit(_) => panic!("Expected Continue"),
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
let req = Request::builder()
|
|
521
|
+
.header("X-Validated", "true")
|
|
522
|
+
.body(Body::empty())
|
|
523
|
+
.unwrap();
|
|
524
|
+
let result = hooks.execute_pre_handler(req).await.unwrap();
|
|
525
|
+
|
|
526
|
+
match result {
|
|
527
|
+
HookResult::Continue(req) => {
|
|
528
|
+
assert_eq!(req.headers().get("X-Prepared").unwrap(), "true");
|
|
529
|
+
}
|
|
530
|
+
HookResult::ShortCircuit(_) => panic!("Expected Continue"),
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
#[tokio::test]
|
|
535
|
+
async fn test_hook_chain_with_state_passing() {
|
|
536
|
+
let hooks = LifecycleHooks::builder()
|
|
537
|
+
.on_request(request_hook("add_user_id", |mut req| async move {
|
|
538
|
+
req.headers_mut()
|
|
539
|
+
.insert("X-User-ID", axum::http::HeaderValue::from_static("123"));
|
|
540
|
+
Ok(HookResult::Continue(req))
|
|
541
|
+
}))
|
|
542
|
+
.on_request(request_hook("add_session_id", |mut req| async move {
|
|
543
|
+
if let Some(user_id) = req.headers().get("X-User-ID") {
|
|
544
|
+
if user_id == "123" {
|
|
545
|
+
req.headers_mut()
|
|
546
|
+
.insert("X-Session-ID", axum::http::HeaderValue::from_static("session_abc"));
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
Ok(HookResult::Continue(req))
|
|
550
|
+
}))
|
|
551
|
+
.build();
|
|
552
|
+
|
|
553
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
554
|
+
let result = hooks.execute_on_request(req).await.unwrap();
|
|
555
|
+
|
|
556
|
+
match result {
|
|
557
|
+
HookResult::Continue(req) => {
|
|
558
|
+
assert_eq!(req.headers().get("X-User-ID").unwrap(), "123");
|
|
559
|
+
assert_eq!(req.headers().get("X-Session-ID").unwrap(), "session_abc");
|
|
560
|
+
}
|
|
561
|
+
HookResult::ShortCircuit(_) => panic!("Expected Continue"),
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
#[tokio::test]
|
|
566
|
+
async fn test_pre_validation_short_circuit_stops_subsequent_hooks() {
|
|
567
|
+
let hooks = LifecycleHooks::builder()
|
|
568
|
+
.on_request(request_hook("on_request", |req| async move {
|
|
569
|
+
println!("on_request executed");
|
|
570
|
+
Ok(HookResult::Continue(req))
|
|
571
|
+
}))
|
|
572
|
+
.pre_validation(request_hook("pre_validation_abort", |_req| async move {
|
|
573
|
+
println!("pre_validation executed - short circuiting");
|
|
574
|
+
let response = Response::builder()
|
|
575
|
+
.status(StatusCode::BAD_REQUEST)
|
|
576
|
+
.body(Body::from("Validation failed"))
|
|
577
|
+
.unwrap();
|
|
578
|
+
Ok(HookResult::ShortCircuit(response))
|
|
579
|
+
}))
|
|
580
|
+
.pre_handler(request_hook("pre_handler", |req| async move {
|
|
581
|
+
println!("pre_handler executed - should NOT happen");
|
|
582
|
+
Ok(HookResult::Continue(req))
|
|
583
|
+
}))
|
|
584
|
+
.build();
|
|
585
|
+
|
|
586
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
587
|
+
let result = hooks.execute_pre_validation(req).await.unwrap();
|
|
588
|
+
|
|
589
|
+
match result {
|
|
590
|
+
HookResult::ShortCircuit(resp) => {
|
|
591
|
+
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
592
|
+
}
|
|
593
|
+
HookResult::Continue(_) => panic!("Expected ShortCircuit"),
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
#[tokio::test]
|
|
598
|
+
async fn test_pre_handler_short_circuit_returns_early_response() {
|
|
599
|
+
let hooks = LifecycleHooks::builder()
|
|
600
|
+
.pre_validation(request_hook("pre_validation", |req| async move {
|
|
601
|
+
Ok(HookResult::Continue(req))
|
|
602
|
+
}))
|
|
603
|
+
.pre_handler(request_hook("rate_limit_check", |_req| async move {
|
|
604
|
+
let response = Response::builder()
|
|
605
|
+
.status(StatusCode::TOO_MANY_REQUESTS)
|
|
606
|
+
.body(Body::from("Rate limit exceeded"))
|
|
607
|
+
.unwrap();
|
|
608
|
+
Ok(HookResult::ShortCircuit(response))
|
|
609
|
+
}))
|
|
610
|
+
.build();
|
|
611
|
+
|
|
612
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
613
|
+
let result = hooks.execute_pre_handler(req).await.unwrap();
|
|
614
|
+
|
|
615
|
+
match result {
|
|
616
|
+
HookResult::ShortCircuit(resp) => {
|
|
617
|
+
assert_eq!(resp.status(), StatusCode::TOO_MANY_REQUESTS);
|
|
618
|
+
}
|
|
619
|
+
HookResult::Continue(_) => panic!("Expected ShortCircuit"),
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
#[tokio::test]
|
|
624
|
+
async fn test_short_circuit_in_middle_of_chain() {
|
|
625
|
+
let hooks = LifecycleHooks::builder()
|
|
626
|
+
.on_request(request_hook("hook_1", |mut req| async move {
|
|
627
|
+
req.headers_mut()
|
|
628
|
+
.insert("X-Executed-1", axum::http::HeaderValue::from_static("yes"));
|
|
629
|
+
Ok(HookResult::Continue(req))
|
|
630
|
+
}))
|
|
631
|
+
.on_request(request_hook("hook_2_abort", |_req| async move {
|
|
632
|
+
let response = Response::builder()
|
|
633
|
+
.status(StatusCode::FORBIDDEN)
|
|
634
|
+
.body(Body::from("Access denied"))
|
|
635
|
+
.unwrap();
|
|
636
|
+
Ok(HookResult::ShortCircuit(response))
|
|
637
|
+
}))
|
|
638
|
+
.on_request(request_hook("hook_3", |mut req| async move {
|
|
639
|
+
req.headers_mut()
|
|
640
|
+
.insert("X-Executed-3", axum::http::HeaderValue::from_static("yes"));
|
|
641
|
+
Ok(HookResult::Continue(req))
|
|
642
|
+
}))
|
|
643
|
+
.build();
|
|
644
|
+
|
|
645
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
646
|
+
let result = hooks.execute_on_request(req).await.unwrap();
|
|
647
|
+
|
|
648
|
+
match result {
|
|
649
|
+
HookResult::ShortCircuit(resp) => {
|
|
650
|
+
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
|
|
651
|
+
}
|
|
652
|
+
HookResult::Continue(_) => panic!("Expected ShortCircuit"),
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
#[tokio::test]
|
|
657
|
+
async fn test_short_circuit_with_custom_response_headers() {
|
|
658
|
+
let hooks = LifecycleHooks::builder()
|
|
659
|
+
.pre_validation(request_hook("auth_check", |_req| async move {
|
|
660
|
+
let response = Response::builder()
|
|
661
|
+
.status(StatusCode::UNAUTHORIZED)
|
|
662
|
+
.header("WWW-Authenticate", "Bearer realm=\"api\"")
|
|
663
|
+
.body(Body::from("Authorization required"))
|
|
664
|
+
.unwrap();
|
|
665
|
+
Ok(HookResult::ShortCircuit(response))
|
|
666
|
+
}))
|
|
667
|
+
.build();
|
|
668
|
+
|
|
669
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
670
|
+
let result = hooks.execute_pre_validation(req).await.unwrap();
|
|
671
|
+
|
|
672
|
+
match result {
|
|
673
|
+
HookResult::ShortCircuit(resp) => {
|
|
674
|
+
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
|
|
675
|
+
assert_eq!(resp.headers().get("WWW-Authenticate").unwrap(), "Bearer realm=\"api\"");
|
|
676
|
+
}
|
|
677
|
+
HookResult::Continue(_) => panic!("Expected ShortCircuit"),
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
#[tokio::test]
|
|
682
|
+
async fn test_hook_error_propagates_through_chain() {
|
|
683
|
+
let hooks = LifecycleHooks::builder()
|
|
684
|
+
.on_request(request_hook("good_hook", |mut req| async move {
|
|
685
|
+
req.headers_mut()
|
|
686
|
+
.insert("X-Good", axum::http::HeaderValue::from_static("yes"));
|
|
687
|
+
Ok(HookResult::Continue(req))
|
|
688
|
+
}))
|
|
689
|
+
.on_request(request_hook("bad_hook", |_req| async move {
|
|
690
|
+
Err("Something went wrong in hook".to_string())
|
|
691
|
+
}))
|
|
692
|
+
.build();
|
|
693
|
+
|
|
694
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
695
|
+
let result = hooks.execute_on_request(req).await;
|
|
696
|
+
|
|
697
|
+
assert!(result.is_err());
|
|
698
|
+
assert_eq!(result.unwrap_err(), "Something went wrong in hook");
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
#[tokio::test]
|
|
702
|
+
async fn test_error_in_pre_validation_stops_chain() {
|
|
703
|
+
let hooks = LifecycleHooks::builder()
|
|
704
|
+
.pre_validation(request_hook("validation_hook", |_req| async move {
|
|
705
|
+
Err("Validation error: invalid input".to_string())
|
|
706
|
+
}))
|
|
707
|
+
.pre_handler(request_hook("handler_prep", |req| async move {
|
|
708
|
+
Ok(HookResult::Continue(req))
|
|
709
|
+
}))
|
|
710
|
+
.build();
|
|
711
|
+
|
|
712
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
713
|
+
let result = hooks.execute_pre_validation(req).await;
|
|
714
|
+
|
|
715
|
+
assert!(result.is_err());
|
|
716
|
+
assert!(result.unwrap_err().contains("Validation error"));
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
#[tokio::test]
|
|
720
|
+
async fn test_on_error_hook_transforms_response() {
|
|
721
|
+
let hooks = LifecycleHooks::builder()
|
|
722
|
+
.on_error(response_hook("transform_error", |mut resp| async move {
|
|
723
|
+
resp.headers_mut()
|
|
724
|
+
.insert("X-Error-Handled", axum::http::HeaderValue::from_static("true"));
|
|
725
|
+
|
|
726
|
+
let _status = resp.status();
|
|
727
|
+
Ok(HookResult::Continue(resp))
|
|
728
|
+
}))
|
|
729
|
+
.build();
|
|
730
|
+
|
|
731
|
+
let resp = Response::builder()
|
|
732
|
+
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
733
|
+
.body(Body::empty())
|
|
734
|
+
.unwrap();
|
|
735
|
+
|
|
736
|
+
let result = hooks.execute_on_error(resp).await.unwrap();
|
|
737
|
+
|
|
738
|
+
assert_eq!(result.headers().get("X-Error-Handled").unwrap(), "true");
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
#[tokio::test]
|
|
742
|
+
async fn test_response_hook_error_propagates() {
|
|
743
|
+
let hooks = LifecycleHooks::builder()
|
|
744
|
+
.on_response(response_hook("good_response_hook", |mut resp| async move {
|
|
745
|
+
resp.headers_mut()
|
|
746
|
+
.insert("X-Processed", axum::http::HeaderValue::from_static("yes"));
|
|
747
|
+
Ok(HookResult::Continue(resp))
|
|
748
|
+
}))
|
|
749
|
+
.on_response(response_hook("bad_response_hook", |_resp| async move {
|
|
750
|
+
Err("Error processing response".to_string())
|
|
751
|
+
}))
|
|
752
|
+
.build();
|
|
753
|
+
|
|
754
|
+
let resp = Response::builder().status(StatusCode::OK).body(Body::empty()).unwrap();
|
|
755
|
+
|
|
756
|
+
let result = hooks.execute_on_response(resp).await;
|
|
757
|
+
|
|
758
|
+
assert!(result.is_err());
|
|
759
|
+
assert_eq!(result.unwrap_err(), "Error processing response");
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
#[tokio::test]
|
|
763
|
+
async fn test_error_hook_error_propagates() {
|
|
764
|
+
let hooks = LifecycleHooks::builder()
|
|
765
|
+
.on_error(response_hook("error_hook_1", |mut resp| async move {
|
|
766
|
+
resp.headers_mut()
|
|
767
|
+
.insert("X-Error-Processed", axum::http::HeaderValue::from_static("1"));
|
|
768
|
+
Ok(HookResult::Continue(resp))
|
|
769
|
+
}))
|
|
770
|
+
.on_error(response_hook("error_hook_2_fails", |_resp| async move {
|
|
771
|
+
Err("Error in error hook".to_string())
|
|
772
|
+
}))
|
|
773
|
+
.build();
|
|
774
|
+
|
|
775
|
+
let resp = Response::builder()
|
|
776
|
+
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
777
|
+
.body(Body::empty())
|
|
778
|
+
.unwrap();
|
|
779
|
+
|
|
780
|
+
let result = hooks.execute_on_error(resp).await;
|
|
781
|
+
|
|
782
|
+
assert!(result.is_err());
|
|
783
|
+
assert_eq!(result.unwrap_err(), "Error in error hook");
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
#[tokio::test]
|
|
787
|
+
async fn test_on_request_adds_multiple_headers() {
|
|
788
|
+
let hooks = LifecycleHooks::builder()
|
|
789
|
+
.on_request(request_hook("add_request_headers", |mut req| async move {
|
|
790
|
+
req.headers_mut()
|
|
791
|
+
.insert("X-Request-ID", axum::http::HeaderValue::from_static("req_123"));
|
|
792
|
+
req.headers_mut()
|
|
793
|
+
.insert("X-Timestamp", axum::http::HeaderValue::from_static("2025-01-01"));
|
|
794
|
+
req.headers_mut()
|
|
795
|
+
.insert("X-Processed", axum::http::HeaderValue::from_static("true"));
|
|
796
|
+
Ok(HookResult::Continue(req))
|
|
797
|
+
}))
|
|
798
|
+
.build();
|
|
799
|
+
|
|
800
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
801
|
+
let result = hooks.execute_on_request(req).await.unwrap();
|
|
802
|
+
|
|
803
|
+
match result {
|
|
804
|
+
HookResult::Continue(req) => {
|
|
805
|
+
assert_eq!(req.headers().get("X-Request-ID").unwrap(), "req_123");
|
|
806
|
+
assert_eq!(req.headers().get("X-Timestamp").unwrap(), "2025-01-01");
|
|
807
|
+
assert_eq!(req.headers().get("X-Processed").unwrap(), "true");
|
|
808
|
+
}
|
|
809
|
+
HookResult::ShortCircuit(_) => panic!("Expected Continue"),
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
#[tokio::test]
|
|
814
|
+
async fn test_on_response_adds_security_headers() {
|
|
815
|
+
let hooks = LifecycleHooks::builder()
|
|
816
|
+
.on_response(response_hook("add_security_headers", |mut resp| async move {
|
|
817
|
+
resp.headers_mut()
|
|
818
|
+
.insert("X-Frame-Options", axum::http::HeaderValue::from_static("DENY"));
|
|
819
|
+
resp.headers_mut().insert(
|
|
820
|
+
"X-Content-Type-Options",
|
|
821
|
+
axum::http::HeaderValue::from_static("nosniff"),
|
|
822
|
+
);
|
|
823
|
+
resp.headers_mut().insert(
|
|
824
|
+
"Strict-Transport-Security",
|
|
825
|
+
axum::http::HeaderValue::from_static("max-age=31536000"),
|
|
826
|
+
);
|
|
827
|
+
Ok(HookResult::Continue(resp))
|
|
828
|
+
}))
|
|
829
|
+
.build();
|
|
830
|
+
|
|
831
|
+
let resp = Response::builder().status(StatusCode::OK).body(Body::empty()).unwrap();
|
|
832
|
+
|
|
833
|
+
let result = hooks.execute_on_response(resp).await.unwrap();
|
|
834
|
+
|
|
835
|
+
assert_eq!(result.headers().get("X-Frame-Options").unwrap(), "DENY");
|
|
836
|
+
assert_eq!(result.headers().get("X-Content-Type-Options").unwrap(), "nosniff");
|
|
837
|
+
assert_eq!(
|
|
838
|
+
result.headers().get("Strict-Transport-Security").unwrap(),
|
|
839
|
+
"max-age=31536000"
|
|
840
|
+
);
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
#[tokio::test]
|
|
844
|
+
async fn test_pre_handler_modifies_request_before_execution() {
|
|
845
|
+
let hooks = LifecycleHooks::builder()
|
|
846
|
+
.pre_handler(request_hook("inject_context", |mut req| async move {
|
|
847
|
+
req.headers_mut().insert(
|
|
848
|
+
"X-Handler-Context",
|
|
849
|
+
axum::http::HeaderValue::from_static("context_data"),
|
|
850
|
+
);
|
|
851
|
+
req.headers_mut()
|
|
852
|
+
.insert("X-Injected", axum::http::HeaderValue::from_static("true"));
|
|
853
|
+
Ok(HookResult::Continue(req))
|
|
854
|
+
}))
|
|
855
|
+
.build();
|
|
856
|
+
|
|
857
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
858
|
+
let result = hooks.execute_pre_handler(req).await.unwrap();
|
|
859
|
+
|
|
860
|
+
match result {
|
|
861
|
+
HookResult::Continue(req) => {
|
|
862
|
+
assert_eq!(req.headers().get("X-Handler-Context").unwrap(), "context_data");
|
|
863
|
+
assert_eq!(req.headers().get("X-Injected").unwrap(), "true");
|
|
864
|
+
}
|
|
865
|
+
HookResult::ShortCircuit(_) => panic!("Expected Continue"),
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
#[tokio::test]
|
|
870
|
+
async fn test_register_multiple_hooks_different_types() {
|
|
871
|
+
let mut hooks = LifecycleHooks::new();
|
|
872
|
+
|
|
873
|
+
hooks.add_on_request(request_hook("on_request_1", |req| async move {
|
|
874
|
+
Ok(HookResult::Continue(req))
|
|
875
|
+
}));
|
|
876
|
+
|
|
877
|
+
hooks.add_pre_validation(request_hook("pre_validation_1", |req| async move {
|
|
878
|
+
Ok(HookResult::Continue(req))
|
|
879
|
+
}));
|
|
880
|
+
|
|
881
|
+
hooks.add_pre_handler(request_hook("pre_handler_1", |req| async move {
|
|
882
|
+
Ok(HookResult::Continue(req))
|
|
883
|
+
}));
|
|
884
|
+
|
|
885
|
+
hooks.add_on_response(response_hook("on_response_1", |resp| async move {
|
|
886
|
+
Ok(HookResult::Continue(resp))
|
|
887
|
+
}));
|
|
888
|
+
|
|
889
|
+
hooks.add_on_error(response_hook("on_error_1", |resp| async move {
|
|
890
|
+
Ok(HookResult::Continue(resp))
|
|
891
|
+
}));
|
|
892
|
+
|
|
893
|
+
assert!(!hooks.is_empty());
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
#[tokio::test]
|
|
897
|
+
async fn test_builder_composition_with_request_and_response_hooks() {
|
|
898
|
+
let hooks = LifecycleHooks::builder()
|
|
899
|
+
.on_request(request_hook("req_1", |mut req| async move {
|
|
900
|
+
req.headers_mut()
|
|
901
|
+
.insert("X-R1", axum::http::HeaderValue::from_static("1"));
|
|
902
|
+
Ok(HookResult::Continue(req))
|
|
903
|
+
}))
|
|
904
|
+
.on_request(request_hook("req_2", |mut req| async move {
|
|
905
|
+
req.headers_mut()
|
|
906
|
+
.insert("X-R2", axum::http::HeaderValue::from_static("2"));
|
|
907
|
+
Ok(HookResult::Continue(req))
|
|
908
|
+
}))
|
|
909
|
+
.on_response(response_hook("resp_1", |mut resp| async move {
|
|
910
|
+
resp.headers_mut()
|
|
911
|
+
.insert("X-Resp1", axum::http::HeaderValue::from_static("resp1"));
|
|
912
|
+
Ok(HookResult::Continue(resp))
|
|
913
|
+
}))
|
|
914
|
+
.on_response(response_hook("resp_2", |mut resp| async move {
|
|
915
|
+
resp.headers_mut()
|
|
916
|
+
.insert("X-Resp2", axum::http::HeaderValue::from_static("resp2"));
|
|
917
|
+
Ok(HookResult::Continue(resp))
|
|
918
|
+
}))
|
|
919
|
+
.build();
|
|
920
|
+
|
|
921
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
922
|
+
let req_result = hooks.execute_on_request(req).await.unwrap();
|
|
923
|
+
|
|
924
|
+
match req_result {
|
|
925
|
+
HookResult::Continue(req) => {
|
|
926
|
+
assert_eq!(req.headers().get("X-R1").unwrap(), "1");
|
|
927
|
+
assert_eq!(req.headers().get("X-R2").unwrap(), "2");
|
|
928
|
+
}
|
|
929
|
+
HookResult::ShortCircuit(_) => panic!("Expected Continue"),
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
let resp = Response::builder().status(StatusCode::OK).body(Body::empty()).unwrap();
|
|
933
|
+
let resp_result = hooks.execute_on_response(resp).await.unwrap();
|
|
934
|
+
|
|
935
|
+
assert_eq!(resp_result.headers().get("X-Resp1").unwrap(), "resp1");
|
|
936
|
+
assert_eq!(resp_result.headers().get("X-Resp2").unwrap(), "resp2");
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
#[tokio::test]
|
|
940
|
+
async fn test_multiple_hooks_accumulate_state() {
|
|
941
|
+
let hooks = LifecycleHooks::builder()
|
|
942
|
+
.on_request(request_hook("init_counter", |mut req| async move {
|
|
943
|
+
req.headers_mut()
|
|
944
|
+
.insert("X-Count", axum::http::HeaderValue::from_static("0"));
|
|
945
|
+
Ok(HookResult::Continue(req))
|
|
946
|
+
}))
|
|
947
|
+
.on_request(request_hook("increment_1", |mut req| async move {
|
|
948
|
+
if let Some(count_header) = req.headers().get("X-Count") {
|
|
949
|
+
if count_header == "0" {
|
|
950
|
+
req.headers_mut()
|
|
951
|
+
.insert("X-Count", axum::http::HeaderValue::from_static("1"));
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
Ok(HookResult::Continue(req))
|
|
955
|
+
}))
|
|
956
|
+
.on_request(request_hook("increment_2", |mut req| async move {
|
|
957
|
+
if let Some(count_header) = req.headers().get("X-Count") {
|
|
958
|
+
if count_header == "1" {
|
|
959
|
+
req.headers_mut()
|
|
960
|
+
.insert("X-Count", axum::http::HeaderValue::from_static("2"));
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
Ok(HookResult::Continue(req))
|
|
964
|
+
}))
|
|
965
|
+
.build();
|
|
966
|
+
|
|
967
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
968
|
+
let result = hooks.execute_on_request(req).await.unwrap();
|
|
969
|
+
|
|
970
|
+
match result {
|
|
971
|
+
HookResult::Continue(req) => {
|
|
972
|
+
assert_eq!(req.headers().get("X-Count").unwrap(), "2");
|
|
973
|
+
}
|
|
974
|
+
HookResult::ShortCircuit(_) => panic!("Expected Continue"),
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
#[tokio::test]
|
|
979
|
+
async fn test_first_hook_short_circuits_second_continues() {
|
|
980
|
+
let hooks = LifecycleHooks::builder()
|
|
981
|
+
.on_request(request_hook("early_exit", |_req| async move {
|
|
982
|
+
let response = Response::builder()
|
|
983
|
+
.status(StatusCode::FORBIDDEN)
|
|
984
|
+
.body(Body::from("Early exit"))
|
|
985
|
+
.unwrap();
|
|
986
|
+
Ok(HookResult::ShortCircuit(response))
|
|
987
|
+
}))
|
|
988
|
+
.on_request(request_hook("never_runs", |req| async move {
|
|
989
|
+
Ok(HookResult::Continue(req))
|
|
990
|
+
}))
|
|
991
|
+
.build();
|
|
992
|
+
|
|
993
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
994
|
+
let result = hooks.execute_on_request(req).await.unwrap();
|
|
995
|
+
|
|
996
|
+
match result {
|
|
997
|
+
HookResult::ShortCircuit(resp) => {
|
|
998
|
+
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
|
|
999
|
+
}
|
|
1000
|
+
HookResult::Continue(_) => panic!("Expected ShortCircuit"),
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
#[tokio::test]
|
|
1005
|
+
async fn test_all_hook_phases_in_sequence() {
|
|
1006
|
+
let hooks = LifecycleHooks::builder()
|
|
1007
|
+
.on_request(request_hook("on_request", |req| async move {
|
|
1008
|
+
Ok(HookResult::Continue(req))
|
|
1009
|
+
}))
|
|
1010
|
+
.pre_validation(request_hook("pre_validation", |req| async move {
|
|
1011
|
+
Ok(HookResult::Continue(req))
|
|
1012
|
+
}))
|
|
1013
|
+
.pre_handler(request_hook("pre_handler", |req| async move {
|
|
1014
|
+
Ok(HookResult::Continue(req))
|
|
1015
|
+
}))
|
|
1016
|
+
.on_response(response_hook("on_response", |resp| async move {
|
|
1017
|
+
Ok(HookResult::Continue(resp))
|
|
1018
|
+
}))
|
|
1019
|
+
.on_error(response_hook("on_error", |resp| async move {
|
|
1020
|
+
Ok(HookResult::Continue(resp))
|
|
1021
|
+
}))
|
|
1022
|
+
.build();
|
|
1023
|
+
|
|
1024
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
1025
|
+
let _ = hooks.execute_on_request(req).await;
|
|
1026
|
+
|
|
1027
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
1028
|
+
let _ = hooks.execute_pre_validation(req).await;
|
|
1029
|
+
|
|
1030
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
1031
|
+
let _ = hooks.execute_pre_handler(req).await;
|
|
1032
|
+
|
|
1033
|
+
let resp = Response::builder().status(StatusCode::OK).body(Body::empty()).unwrap();
|
|
1034
|
+
let _ = hooks.execute_on_response(resp).await;
|
|
1035
|
+
|
|
1036
|
+
let resp = Response::builder()
|
|
1037
|
+
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
1038
|
+
.body(Body::empty())
|
|
1039
|
+
.unwrap();
|
|
1040
|
+
let _ = hooks.execute_on_error(resp).await;
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
#[tokio::test]
|
|
1044
|
+
async fn test_hook_with_complex_header_manipulation() {
|
|
1045
|
+
let hooks = LifecycleHooks::builder()
|
|
1046
|
+
.on_request(request_hook("parse_auth", |mut req| async move {
|
|
1047
|
+
let has_auth = req.headers().contains_key("Authorization");
|
|
1048
|
+
let auth_status = if has_auth { "authenticated" } else { "anonymous" };
|
|
1049
|
+
req.headers_mut()
|
|
1050
|
+
.insert("X-Auth-Status", axum::http::HeaderValue::from_static(auth_status));
|
|
1051
|
+
Ok(HookResult::Continue(req))
|
|
1052
|
+
}))
|
|
1053
|
+
.pre_validation(request_hook("validate_auth", |req| async move {
|
|
1054
|
+
if let Some(auth_header) = req.headers().get("X-Auth-Status") {
|
|
1055
|
+
if auth_header == "anonymous" {
|
|
1056
|
+
let response = Response::builder()
|
|
1057
|
+
.status(StatusCode::UNAUTHORIZED)
|
|
1058
|
+
.body(Body::from("Authentication required"))
|
|
1059
|
+
.unwrap();
|
|
1060
|
+
return Ok(HookResult::ShortCircuit(response));
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
Ok(HookResult::Continue(req))
|
|
1064
|
+
}))
|
|
1065
|
+
.build();
|
|
1066
|
+
|
|
1067
|
+
let auth_req = Request::builder()
|
|
1068
|
+
.header("Authorization", "Bearer token123")
|
|
1069
|
+
.body(Body::empty())
|
|
1070
|
+
.unwrap();
|
|
1071
|
+
|
|
1072
|
+
let result = hooks.execute_on_request(auth_req).await.unwrap();
|
|
1073
|
+
assert!(matches!(result, HookResult::Continue(_)));
|
|
1074
|
+
|
|
1075
|
+
let anon_req = Request::builder().body(Body::empty()).unwrap();
|
|
1076
|
+
let on_req_result = hooks.execute_on_request(anon_req).await.unwrap();
|
|
1077
|
+
|
|
1078
|
+
match on_req_result {
|
|
1079
|
+
HookResult::Continue(req) => {
|
|
1080
|
+
assert_eq!(req.headers().get("X-Auth-Status").unwrap(), "anonymous");
|
|
1081
|
+
|
|
1082
|
+
let val_result = hooks.execute_pre_validation(req).await.unwrap();
|
|
1083
|
+
assert!(matches!(val_result, HookResult::ShortCircuit(_)));
|
|
1084
|
+
}
|
|
1085
|
+
HookResult::ShortCircuit(_) => panic!("Expected Continue from on_request"),
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
#[tokio::test]
|
|
1090
|
+
async fn test_empty_hooks_no_overhead() {
|
|
1091
|
+
let hooks = LifecycleHooks::new();
|
|
1092
|
+
assert!(hooks.is_empty());
|
|
1093
|
+
|
|
1094
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
1095
|
+
let result = hooks.execute_on_request(req).await.unwrap();
|
|
1096
|
+
assert!(matches!(result, HookResult::Continue(_)));
|
|
1097
|
+
|
|
1098
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
1099
|
+
let result = hooks.execute_pre_validation(req).await.unwrap();
|
|
1100
|
+
assert!(matches!(result, HookResult::Continue(_)));
|
|
1101
|
+
|
|
1102
|
+
let resp = Response::builder().status(StatusCode::OK).body(Body::empty()).unwrap();
|
|
1103
|
+
let result = hooks.execute_on_response(resp).await.unwrap();
|
|
1104
|
+
assert_eq!(result.status(), StatusCode::OK);
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
#[tokio::test]
|
|
1108
|
+
async fn test_response_hook_short_circuit_treated_as_continue() {
|
|
1109
|
+
let hooks = LifecycleHooks::builder()
|
|
1110
|
+
.on_response(response_hook("hook_with_short_circuit", |mut resp| async move {
|
|
1111
|
+
resp.headers_mut()
|
|
1112
|
+
.insert("X-Processed", axum::http::HeaderValue::from_static("yes"));
|
|
1113
|
+
Ok(HookResult::ShortCircuit(resp))
|
|
1114
|
+
}))
|
|
1115
|
+
.on_response(response_hook("second_hook", |mut resp| async move {
|
|
1116
|
+
resp.headers_mut()
|
|
1117
|
+
.insert("X-Second", axum::http::HeaderValue::from_static("yes"));
|
|
1118
|
+
Ok(HookResult::Continue(resp))
|
|
1119
|
+
}))
|
|
1120
|
+
.build();
|
|
1121
|
+
|
|
1122
|
+
let resp = Response::builder().status(StatusCode::OK).body(Body::empty()).unwrap();
|
|
1123
|
+
|
|
1124
|
+
let result = hooks.execute_on_response(resp).await.unwrap();
|
|
1125
|
+
|
|
1126
|
+
assert_eq!(result.headers().get("X-Processed").unwrap(), "yes");
|
|
1127
|
+
assert_eq!(result.headers().get("X-Second").unwrap(), "yes");
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
#[tokio::test]
|
|
1131
|
+
async fn test_complex_pre_validation_flow_with_auth_and_content_check() {
|
|
1132
|
+
let hooks = LifecycleHooks::builder()
|
|
1133
|
+
.pre_validation(request_hook("check_auth", |req| async move {
|
|
1134
|
+
if !req.headers().contains_key("Authorization") {
|
|
1135
|
+
return Ok(HookResult::ShortCircuit(
|
|
1136
|
+
Response::builder()
|
|
1137
|
+
.status(StatusCode::UNAUTHORIZED)
|
|
1138
|
+
.body(Body::from("Missing auth"))
|
|
1139
|
+
.unwrap(),
|
|
1140
|
+
));
|
|
1141
|
+
}
|
|
1142
|
+
Ok(HookResult::Continue(req))
|
|
1143
|
+
}))
|
|
1144
|
+
.pre_validation(request_hook("check_content_type", |req| async move {
|
|
1145
|
+
if req.method() == axum::http::Method::POST {
|
|
1146
|
+
if !req.headers().contains_key("Content-Type") {
|
|
1147
|
+
return Ok(HookResult::ShortCircuit(
|
|
1148
|
+
Response::builder()
|
|
1149
|
+
.status(StatusCode::BAD_REQUEST)
|
|
1150
|
+
.body(Body::from("Missing Content-Type"))
|
|
1151
|
+
.unwrap(),
|
|
1152
|
+
));
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
Ok(HookResult::Continue(req))
|
|
1156
|
+
}))
|
|
1157
|
+
.build();
|
|
1158
|
+
|
|
1159
|
+
let req = Request::builder().body(Body::empty()).unwrap();
|
|
1160
|
+
let result = hooks.execute_pre_validation(req).await.unwrap();
|
|
1161
|
+
|
|
1162
|
+
match result {
|
|
1163
|
+
HookResult::ShortCircuit(resp) => {
|
|
1164
|
+
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
|
|
1165
|
+
}
|
|
1166
|
+
HookResult::Continue(_) => panic!("Expected ShortCircuit for missing auth"),
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
let req = Request::builder()
|
|
1170
|
+
.method(axum::http::Method::POST)
|
|
1171
|
+
.header("Authorization", "Bearer token")
|
|
1172
|
+
.body(Body::empty())
|
|
1173
|
+
.unwrap();
|
|
1174
|
+
let result = hooks.execute_pre_validation(req).await.unwrap();
|
|
1175
|
+
|
|
1176
|
+
match result {
|
|
1177
|
+
HookResult::ShortCircuit(resp) => {
|
|
1178
|
+
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
1179
|
+
}
|
|
1180
|
+
HookResult::Continue(_) => panic!("Expected ShortCircuit for missing content type"),
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
let req = Request::builder()
|
|
1184
|
+
.method(axum::http::Method::POST)
|
|
1185
|
+
.header("Authorization", "Bearer token")
|
|
1186
|
+
.header("Content-Type", "application/json")
|
|
1187
|
+
.body(Body::empty())
|
|
1188
|
+
.unwrap();
|
|
1189
|
+
let result = hooks.execute_pre_validation(req).await.unwrap();
|
|
1190
|
+
|
|
1191
|
+
assert!(matches!(result, HookResult::Continue(_)));
|
|
1192
|
+
}
|
|
428
1193
|
}
|