spikard 0.4.0-x86_64-linux
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 +7 -0
- data/LICENSE +1 -0
- data/README.md +659 -0
- data/ext/spikard_rb/Cargo.toml +17 -0
- data/ext/spikard_rb/extconf.rb +10 -0
- data/ext/spikard_rb/src/lib.rs +6 -0
- data/lib/spikard/app.rb +405 -0
- data/lib/spikard/background.rb +27 -0
- data/lib/spikard/config.rb +396 -0
- data/lib/spikard/converters.rb +13 -0
- data/lib/spikard/handler_wrapper.rb +113 -0
- data/lib/spikard/provide.rb +214 -0
- data/lib/spikard/response.rb +173 -0
- data/lib/spikard/schema.rb +243 -0
- data/lib/spikard/sse.rb +111 -0
- data/lib/spikard/streaming_response.rb +44 -0
- data/lib/spikard/testing.rb +221 -0
- data/lib/spikard/upload_file.rb +131 -0
- data/lib/spikard/version.rb +5 -0
- data/lib/spikard/websocket.rb +59 -0
- data/lib/spikard.rb +43 -0
- data/sig/spikard.rbs +366 -0
- data/vendor/bundle/ruby/3.4.0/gems/diff-lcs-1.6.2/mise.toml +5 -0
- data/vendor/bundle/ruby/3.4.0/gems/rake-compiler-dock-1.10.0/build/buildkitd.toml +2 -0
- data/vendor/crates/spikard-bindings-shared/Cargo.toml +63 -0
- data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +139 -0
- data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +561 -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 +403 -0
- data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +274 -0
- data/vendor/crates/spikard-bindings-shared/src/lib.rs +25 -0
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +298 -0
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +637 -0
- data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +309 -0
- data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +248 -0
- data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +355 -0
- data/vendor/crates/spikard-bindings-shared/tests/comprehensive_coverage.rs +502 -0
- data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +389 -0
- data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +413 -0
- data/vendor/crates/spikard-core/Cargo.toml +40 -0
- data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -0
- data/vendor/crates/spikard-core/src/bindings/response.rs +133 -0
- data/vendor/crates/spikard-core/src/debug.rs +63 -0
- data/vendor/crates/spikard-core/src/di/container.rs +726 -0
- data/vendor/crates/spikard-core/src/di/dependency.rs +273 -0
- data/vendor/crates/spikard-core/src/di/error.rs +118 -0
- data/vendor/crates/spikard-core/src/di/factory.rs +538 -0
- data/vendor/crates/spikard-core/src/di/graph.rs +545 -0
- data/vendor/crates/spikard-core/src/di/mod.rs +192 -0
- data/vendor/crates/spikard-core/src/di/resolved.rs +411 -0
- data/vendor/crates/spikard-core/src/di/value.rs +283 -0
- data/vendor/crates/spikard-core/src/errors.rs +39 -0
- data/vendor/crates/spikard-core/src/http.rs +153 -0
- data/vendor/crates/spikard-core/src/lib.rs +29 -0
- data/vendor/crates/spikard-core/src/lifecycle.rs +422 -0
- data/vendor/crates/spikard-core/src/metadata.rs +397 -0
- data/vendor/crates/spikard-core/src/parameters.rs +723 -0
- data/vendor/crates/spikard-core/src/problem.rs +310 -0
- data/vendor/crates/spikard-core/src/request_data.rs +189 -0
- data/vendor/crates/spikard-core/src/router.rs +249 -0
- data/vendor/crates/spikard-core/src/schema_registry.rs +183 -0
- data/vendor/crates/spikard-core/src/type_hints.rs +304 -0
- data/vendor/crates/spikard-core/src/validation/error_mapper.rs +689 -0
- data/vendor/crates/spikard-core/src/validation/mod.rs +459 -0
- data/vendor/crates/spikard-http/Cargo.toml +58 -0
- data/vendor/crates/spikard-http/examples/sse-notifications.rs +147 -0
- data/vendor/crates/spikard-http/examples/websocket-chat.rs +91 -0
- data/vendor/crates/spikard-http/src/auth.rs +247 -0
- data/vendor/crates/spikard-http/src/background.rs +1562 -0
- data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -0
- data/vendor/crates/spikard-http/src/bindings/response.rs +1 -0
- data/vendor/crates/spikard-http/src/body_metadata.rs +8 -0
- data/vendor/crates/spikard-http/src/cors.rs +490 -0
- data/vendor/crates/spikard-http/src/debug.rs +63 -0
- data/vendor/crates/spikard-http/src/di_handler.rs +1878 -0
- data/vendor/crates/spikard-http/src/handler_response.rs +532 -0
- data/vendor/crates/spikard-http/src/handler_trait.rs +861 -0
- data/vendor/crates/spikard-http/src/handler_trait_tests.rs +284 -0
- data/vendor/crates/spikard-http/src/lib.rs +524 -0
- data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +149 -0
- data/vendor/crates/spikard-http/src/lifecycle.rs +428 -0
- data/vendor/crates/spikard-http/src/middleware/mod.rs +285 -0
- data/vendor/crates/spikard-http/src/middleware/multipart.rs +930 -0
- data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +541 -0
- data/vendor/crates/spikard-http/src/middleware/validation.rs +287 -0
- data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -0
- data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +535 -0
- data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +867 -0
- data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +678 -0
- data/vendor/crates/spikard-http/src/query_parser.rs +369 -0
- data/vendor/crates/spikard-http/src/response.rs +399 -0
- data/vendor/crates/spikard-http/src/server/handler.rs +1557 -0
- data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +98 -0
- data/vendor/crates/spikard-http/src/server/mod.rs +806 -0
- data/vendor/crates/spikard-http/src/server/request_extraction.rs +630 -0
- data/vendor/crates/spikard-http/src/server/routing_factory.rs +497 -0
- data/vendor/crates/spikard-http/src/sse.rs +961 -0
- data/vendor/crates/spikard-http/src/testing/form.rs +14 -0
- data/vendor/crates/spikard-http/src/testing/multipart.rs +60 -0
- data/vendor/crates/spikard-http/src/testing/test_client.rs +285 -0
- data/vendor/crates/spikard-http/src/testing.rs +377 -0
- data/vendor/crates/spikard-http/src/websocket.rs +831 -0
- data/vendor/crates/spikard-http/tests/background_behavior.rs +918 -0
- data/vendor/crates/spikard-http/tests/common/handlers.rs +308 -0
- data/vendor/crates/spikard-http/tests/common/mod.rs +21 -0
- data/vendor/crates/spikard-http/tests/di_integration.rs +202 -0
- data/vendor/crates/spikard-http/tests/doc_snippets.rs +4 -0
- data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +1135 -0
- data/vendor/crates/spikard-http/tests/multipart_behavior.rs +688 -0
- data/vendor/crates/spikard-http/tests/server_config_builder.rs +324 -0
- data/vendor/crates/spikard-http/tests/sse_behavior.rs +728 -0
- data/vendor/crates/spikard-http/tests/websocket_behavior.rs +724 -0
- data/vendor/crates/spikard-rb/Cargo.toml +43 -0
- data/vendor/crates/spikard-rb/build.rs +199 -0
- data/vendor/crates/spikard-rb/src/background.rs +63 -0
- data/vendor/crates/spikard-rb/src/config/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/config/server_config.rs +283 -0
- data/vendor/crates/spikard-rb/src/conversion.rs +459 -0
- data/vendor/crates/spikard-rb/src/di/builder.rs +105 -0
- data/vendor/crates/spikard-rb/src/di/mod.rs +413 -0
- data/vendor/crates/spikard-rb/src/handler.rs +612 -0
- data/vendor/crates/spikard-rb/src/integration/mod.rs +3 -0
- data/vendor/crates/spikard-rb/src/lib.rs +1857 -0
- data/vendor/crates/spikard-rb/src/lifecycle.rs +275 -0
- data/vendor/crates/spikard-rb/src/metadata/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +427 -0
- data/vendor/crates/spikard-rb/src/runtime/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +326 -0
- data/vendor/crates/spikard-rb/src/server.rs +283 -0
- data/vendor/crates/spikard-rb/src/sse.rs +231 -0
- data/vendor/crates/spikard-rb/src/testing/client.rs +404 -0
- data/vendor/crates/spikard-rb/src/testing/mod.rs +7 -0
- data/vendor/crates/spikard-rb/src/testing/sse.rs +143 -0
- data/vendor/crates/spikard-rb/src/testing/websocket.rs +221 -0
- data/vendor/crates/spikard-rb/src/websocket.rs +233 -0
- data/vendor/crates/spikard-rb/tests/magnus_ffi_tests.rs +14 -0
- metadata +213 -0
|
@@ -0,0 +1,1135 @@
|
|
|
1
|
+
//! Comprehensive tests for lifecycle hooks execution
|
|
2
|
+
//!
|
|
3
|
+
//! Tests the execute_with_lifecycle_hooks function covering:
|
|
4
|
+
//! - No hooks and empty hooks fast path
|
|
5
|
+
//! - Hook execution order and continuation
|
|
6
|
+
//! - Short-circuit behavior
|
|
7
|
+
//! - Error handling and onError transformation
|
|
8
|
+
//! - Success path with onResponse
|
|
9
|
+
//! - Response modifications
|
|
10
|
+
|
|
11
|
+
mod common;
|
|
12
|
+
|
|
13
|
+
use axum::body::Body;
|
|
14
|
+
use axum::http::{Request, Response, StatusCode};
|
|
15
|
+
use serde_json::json;
|
|
16
|
+
use spikard_http::lifecycle::HookResult;
|
|
17
|
+
use spikard_http::server::lifecycle_execution::execute_with_lifecycle_hooks;
|
|
18
|
+
use spikard_http::{Handler, HandlerResult, RequestData};
|
|
19
|
+
use std::collections::HashMap;
|
|
20
|
+
use std::future::Future;
|
|
21
|
+
use std::pin::Pin;
|
|
22
|
+
use std::sync::Arc;
|
|
23
|
+
|
|
24
|
+
/// Test request data factory
|
|
25
|
+
fn test_request_data() -> RequestData {
|
|
26
|
+
RequestData {
|
|
27
|
+
path_params: Arc::new(HashMap::new()),
|
|
28
|
+
query_params: serde_json::Value::Null,
|
|
29
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
30
|
+
body: json!({"test": "data"}),
|
|
31
|
+
raw_body: None,
|
|
32
|
+
headers: Arc::new(HashMap::new()),
|
|
33
|
+
cookies: Arc::new(HashMap::new()),
|
|
34
|
+
method: "GET".to_string(),
|
|
35
|
+
path: "/test".to_string(),
|
|
36
|
+
#[cfg(feature = "di")]
|
|
37
|
+
dependencies: None,
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/// Handler that returns 200 OK with custom body
|
|
42
|
+
struct OkHandler;
|
|
43
|
+
|
|
44
|
+
impl Handler for OkHandler {
|
|
45
|
+
fn call(
|
|
46
|
+
&self,
|
|
47
|
+
_request: Request<Body>,
|
|
48
|
+
_request_data: RequestData,
|
|
49
|
+
) -> Pin<Box<dyn Future<Output = HandlerResult> + Send + '_>> {
|
|
50
|
+
Box::pin(async move {
|
|
51
|
+
let response = Response::builder()
|
|
52
|
+
.status(StatusCode::OK)
|
|
53
|
+
.body(Body::from("OK"))
|
|
54
|
+
.unwrap();
|
|
55
|
+
Ok(response)
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/// Handler that returns 400 Bad Request error
|
|
61
|
+
struct BadRequestHandler;
|
|
62
|
+
|
|
63
|
+
impl Handler for BadRequestHandler {
|
|
64
|
+
fn call(
|
|
65
|
+
&self,
|
|
66
|
+
_request: Request<Body>,
|
|
67
|
+
_request_data: RequestData,
|
|
68
|
+
) -> Pin<Box<dyn Future<Output = HandlerResult> + Send + '_>> {
|
|
69
|
+
Box::pin(async move { Err((StatusCode::BAD_REQUEST, "{\"error\": \"Bad Request\"}".to_string())) })
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ============================================================================
|
|
74
|
+
// Test Scenarios
|
|
75
|
+
// ============================================================================
|
|
76
|
+
|
|
77
|
+
#[tokio::test]
|
|
78
|
+
async fn test_no_hooks_none_variant() {
|
|
79
|
+
// Test 1: No hooks (None variant) → direct handler call
|
|
80
|
+
let handler = Arc::new(OkHandler);
|
|
81
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
82
|
+
let request_data = test_request_data();
|
|
83
|
+
|
|
84
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, None).await;
|
|
85
|
+
|
|
86
|
+
assert!(result.is_ok());
|
|
87
|
+
let response = result.unwrap();
|
|
88
|
+
assert_eq!(response.status(), StatusCode::OK);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
#[tokio::test]
|
|
92
|
+
async fn test_empty_hooks_list() {
|
|
93
|
+
// Test 2: Empty hooks (is_empty() returns true) → direct handler call
|
|
94
|
+
let handler = Arc::new(OkHandler);
|
|
95
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
96
|
+
let request_data = test_request_data();
|
|
97
|
+
let hooks = Arc::new(spikard_http::lifecycle::LifecycleHooks::new());
|
|
98
|
+
|
|
99
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(hooks)).await;
|
|
100
|
+
|
|
101
|
+
assert!(result.is_ok());
|
|
102
|
+
let response = result.unwrap();
|
|
103
|
+
assert_eq!(response.status(), StatusCode::OK);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
#[tokio::test]
|
|
107
|
+
async fn test_pre_validation_hook_continues() {
|
|
108
|
+
// Test 3: preValidation hook continues → proceeds to handler
|
|
109
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
110
|
+
|
|
111
|
+
hooks.add_pre_validation(spikard_http::lifecycle::request_hook(
|
|
112
|
+
"continue_hook",
|
|
113
|
+
|req| async move { Ok(HookResult::Continue(req)) },
|
|
114
|
+
));
|
|
115
|
+
|
|
116
|
+
let handler = Arc::new(OkHandler);
|
|
117
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
118
|
+
let request_data = test_request_data();
|
|
119
|
+
|
|
120
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
121
|
+
|
|
122
|
+
assert!(result.is_ok());
|
|
123
|
+
let response = result.unwrap();
|
|
124
|
+
assert_eq!(response.status(), StatusCode::OK);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
#[tokio::test]
|
|
128
|
+
async fn test_pre_validation_hook_short_circuits() {
|
|
129
|
+
// Test 4: preValidation hook short-circuits → handler not called, response returned
|
|
130
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
131
|
+
|
|
132
|
+
hooks.add_pre_validation(spikard_http::lifecycle::request_hook(
|
|
133
|
+
"short_circuit",
|
|
134
|
+
|_req| async move {
|
|
135
|
+
let response = Response::builder()
|
|
136
|
+
.status(StatusCode::UNAUTHORIZED)
|
|
137
|
+
.body(Body::from("{\"error\": \"Unauthorized\"}"))
|
|
138
|
+
.unwrap();
|
|
139
|
+
Ok(HookResult::ShortCircuit(response))
|
|
140
|
+
},
|
|
141
|
+
));
|
|
142
|
+
|
|
143
|
+
let handler = Arc::new(OkHandler);
|
|
144
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
145
|
+
let request_data = test_request_data();
|
|
146
|
+
|
|
147
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
148
|
+
|
|
149
|
+
assert!(result.is_ok());
|
|
150
|
+
let response = result.unwrap();
|
|
151
|
+
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
#[tokio::test]
|
|
155
|
+
async fn test_pre_validation_hook_error_calls_on_error() {
|
|
156
|
+
// Test 5: preValidation hook fails → onError hook called
|
|
157
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
158
|
+
|
|
159
|
+
hooks.add_pre_validation(spikard_http::lifecycle::request_hook(
|
|
160
|
+
"failing_hook",
|
|
161
|
+
|_req| async move { Err("Validation failed".to_string()) },
|
|
162
|
+
));
|
|
163
|
+
|
|
164
|
+
hooks.add_on_error(spikard_http::lifecycle::response_hook(
|
|
165
|
+
"error_handler",
|
|
166
|
+
|mut resp| async move {
|
|
167
|
+
resp.headers_mut()
|
|
168
|
+
.insert("X-Error-Handled", axum::http::HeaderValue::from_static("true"));
|
|
169
|
+
Ok(HookResult::Continue(resp))
|
|
170
|
+
},
|
|
171
|
+
));
|
|
172
|
+
|
|
173
|
+
let handler = Arc::new(OkHandler);
|
|
174
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
175
|
+
let request_data = test_request_data();
|
|
176
|
+
|
|
177
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
178
|
+
|
|
179
|
+
assert!(result.is_ok());
|
|
180
|
+
let response = result.unwrap();
|
|
181
|
+
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
182
|
+
assert_eq!(
|
|
183
|
+
response.headers().get("X-Error-Handled").map(|v| v.to_str().unwrap()),
|
|
184
|
+
Some("true")
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
#[tokio::test]
|
|
189
|
+
async fn test_pre_handler_hook_continues() {
|
|
190
|
+
// Test 6: preHandler hook continues → proceeds to handler
|
|
191
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
192
|
+
|
|
193
|
+
hooks.add_pre_handler(spikard_http::lifecycle::request_hook(
|
|
194
|
+
"continue_hook",
|
|
195
|
+
|req| async move { Ok(HookResult::Continue(req)) },
|
|
196
|
+
));
|
|
197
|
+
|
|
198
|
+
let handler = Arc::new(OkHandler);
|
|
199
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
200
|
+
let request_data = test_request_data();
|
|
201
|
+
|
|
202
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
203
|
+
|
|
204
|
+
assert!(result.is_ok());
|
|
205
|
+
let response = result.unwrap();
|
|
206
|
+
assert_eq!(response.status(), StatusCode::OK);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
#[tokio::test]
|
|
210
|
+
async fn test_pre_handler_hook_short_circuits() {
|
|
211
|
+
// Test 7: preHandler hook short-circuits → handler not called
|
|
212
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
213
|
+
|
|
214
|
+
hooks.add_pre_handler(spikard_http::lifecycle::request_hook(
|
|
215
|
+
"short_circuit",
|
|
216
|
+
|_req| async move {
|
|
217
|
+
let response = Response::builder()
|
|
218
|
+
.status(StatusCode::FORBIDDEN)
|
|
219
|
+
.body(Body::from("{\"error\": \"Forbidden\"}"))
|
|
220
|
+
.unwrap();
|
|
221
|
+
Ok(HookResult::ShortCircuit(response))
|
|
222
|
+
},
|
|
223
|
+
));
|
|
224
|
+
|
|
225
|
+
let handler = Arc::new(OkHandler);
|
|
226
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
227
|
+
let request_data = test_request_data();
|
|
228
|
+
|
|
229
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
230
|
+
|
|
231
|
+
assert!(result.is_ok());
|
|
232
|
+
let response = result.unwrap();
|
|
233
|
+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
#[tokio::test]
|
|
237
|
+
async fn test_pre_handler_hook_error_calls_on_error() {
|
|
238
|
+
// Test 8: preHandler hook fails → onError hook called
|
|
239
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
240
|
+
|
|
241
|
+
hooks.add_pre_handler(spikard_http::lifecycle::request_hook(
|
|
242
|
+
"failing_hook",
|
|
243
|
+
|_req| async move { Err("Handler preparation failed".to_string()) },
|
|
244
|
+
));
|
|
245
|
+
|
|
246
|
+
hooks.add_on_error(spikard_http::lifecycle::response_hook(
|
|
247
|
+
"error_handler",
|
|
248
|
+
|mut resp| async move {
|
|
249
|
+
resp.headers_mut()
|
|
250
|
+
.insert("X-PreHandler-Error", axum::http::HeaderValue::from_static("caught"));
|
|
251
|
+
Ok(HookResult::Continue(resp))
|
|
252
|
+
},
|
|
253
|
+
));
|
|
254
|
+
|
|
255
|
+
let handler = Arc::new(OkHandler);
|
|
256
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
257
|
+
let request_data = test_request_data();
|
|
258
|
+
|
|
259
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
260
|
+
|
|
261
|
+
assert!(result.is_ok());
|
|
262
|
+
let response = result.unwrap();
|
|
263
|
+
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
264
|
+
assert_eq!(
|
|
265
|
+
response
|
|
266
|
+
.headers()
|
|
267
|
+
.get("X-PreHandler-Error")
|
|
268
|
+
.map(|v| v.to_str().unwrap()),
|
|
269
|
+
Some("caught")
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
#[tokio::test]
|
|
274
|
+
async fn test_handler_error_calls_on_error() {
|
|
275
|
+
// Test 9: Handler fails → onError hook called and transforms response
|
|
276
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
277
|
+
|
|
278
|
+
hooks.add_on_error(spikard_http::lifecycle::response_hook(
|
|
279
|
+
"error_logger",
|
|
280
|
+
|mut resp| async move {
|
|
281
|
+
resp.headers_mut()
|
|
282
|
+
.insert("X-Handler-Error", axum::http::HeaderValue::from_static("logged"));
|
|
283
|
+
Ok(HookResult::Continue(resp))
|
|
284
|
+
},
|
|
285
|
+
));
|
|
286
|
+
|
|
287
|
+
let handler = Arc::new(BadRequestHandler);
|
|
288
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
289
|
+
let request_data = test_request_data();
|
|
290
|
+
|
|
291
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
292
|
+
|
|
293
|
+
assert!(result.is_ok());
|
|
294
|
+
let response = result.unwrap();
|
|
295
|
+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
|
296
|
+
assert_eq!(
|
|
297
|
+
response.headers().get("X-Handler-Error").map(|v| v.to_str().unwrap()),
|
|
298
|
+
Some("logged")
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
#[tokio::test]
|
|
303
|
+
async fn test_success_handler_calls_on_response() {
|
|
304
|
+
// Test 10: Handler succeeds → onResponse hook called and modifies response
|
|
305
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
306
|
+
|
|
307
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook(
|
|
308
|
+
"response_modifier",
|
|
309
|
+
|mut resp| async move {
|
|
310
|
+
resp.headers_mut()
|
|
311
|
+
.insert("X-Response-Modified", axum::http::HeaderValue::from_static("yes"));
|
|
312
|
+
Ok(HookResult::Continue(resp))
|
|
313
|
+
},
|
|
314
|
+
));
|
|
315
|
+
|
|
316
|
+
let handler = Arc::new(OkHandler);
|
|
317
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
318
|
+
let request_data = test_request_data();
|
|
319
|
+
|
|
320
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
321
|
+
|
|
322
|
+
assert!(result.is_ok());
|
|
323
|
+
let response = result.unwrap();
|
|
324
|
+
assert_eq!(response.status(), StatusCode::OK);
|
|
325
|
+
assert_eq!(
|
|
326
|
+
response
|
|
327
|
+
.headers()
|
|
328
|
+
.get("X-Response-Modified")
|
|
329
|
+
.map(|v| v.to_str().unwrap()),
|
|
330
|
+
Some("yes")
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
#[tokio::test]
|
|
335
|
+
async fn test_on_response_hook_error_returns_500() {
|
|
336
|
+
// Test 11: onResponse hook fails → returns 500
|
|
337
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
338
|
+
|
|
339
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook(
|
|
340
|
+
"failing_response_hook",
|
|
341
|
+
|_resp| async move { Err("Response processing failed".to_string()) },
|
|
342
|
+
));
|
|
343
|
+
|
|
344
|
+
let handler = Arc::new(OkHandler);
|
|
345
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
346
|
+
let request_data = test_request_data();
|
|
347
|
+
|
|
348
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
349
|
+
|
|
350
|
+
assert!(result.is_ok());
|
|
351
|
+
let response = result.unwrap();
|
|
352
|
+
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
#[tokio::test]
|
|
356
|
+
async fn test_on_error_hook_fails_fallback_500() {
|
|
357
|
+
// Test 12: onError hook itself fails → fallback 500 response
|
|
358
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
359
|
+
|
|
360
|
+
hooks.add_pre_validation(spikard_http::lifecycle::request_hook(
|
|
361
|
+
"failing_validation",
|
|
362
|
+
|_req| async move { Err("Validation error".to_string()) },
|
|
363
|
+
));
|
|
364
|
+
|
|
365
|
+
hooks.add_on_error(spikard_http::lifecycle::response_hook(
|
|
366
|
+
"failing_error_hook",
|
|
367
|
+
|_resp| async move { Err("Error hook failed".to_string()) },
|
|
368
|
+
));
|
|
369
|
+
|
|
370
|
+
let handler = Arc::new(OkHandler);
|
|
371
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
372
|
+
let request_data = test_request_data();
|
|
373
|
+
|
|
374
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
375
|
+
|
|
376
|
+
assert!(result.is_ok());
|
|
377
|
+
let response = result.unwrap();
|
|
378
|
+
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
#[tokio::test]
|
|
382
|
+
async fn test_hook_execution_order_preserved() {
|
|
383
|
+
// Test 13: Hook execution order preserved across phases
|
|
384
|
+
use std::sync::Mutex;
|
|
385
|
+
|
|
386
|
+
let execution_log = Arc::new(Mutex::new(Vec::new()));
|
|
387
|
+
let execution_log_clone1 = execution_log.clone();
|
|
388
|
+
let execution_log_clone2 = execution_log.clone();
|
|
389
|
+
let execution_log_clone3 = execution_log.clone();
|
|
390
|
+
|
|
391
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
392
|
+
|
|
393
|
+
// Pre-validation hook
|
|
394
|
+
hooks.add_pre_validation(spikard_http::lifecycle::request_hook("pre_validation", move |req| {
|
|
395
|
+
let log = execution_log_clone1.clone();
|
|
396
|
+
async move {
|
|
397
|
+
log.lock().unwrap().push("pre_validation");
|
|
398
|
+
Ok(HookResult::Continue(req))
|
|
399
|
+
}
|
|
400
|
+
}));
|
|
401
|
+
|
|
402
|
+
// Pre-handler hook
|
|
403
|
+
hooks.add_pre_handler(spikard_http::lifecycle::request_hook("pre_handler", move |req| {
|
|
404
|
+
let log = execution_log_clone2.clone();
|
|
405
|
+
async move {
|
|
406
|
+
log.lock().unwrap().push("pre_handler");
|
|
407
|
+
Ok(HookResult::Continue(req))
|
|
408
|
+
}
|
|
409
|
+
}));
|
|
410
|
+
|
|
411
|
+
// On-response hook
|
|
412
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook("on_response", move |resp| {
|
|
413
|
+
let log = execution_log_clone3.clone();
|
|
414
|
+
async move {
|
|
415
|
+
log.lock().unwrap().push("on_response");
|
|
416
|
+
Ok(HookResult::Continue(resp))
|
|
417
|
+
}
|
|
418
|
+
}));
|
|
419
|
+
|
|
420
|
+
let handler = Arc::new(OkHandler);
|
|
421
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
422
|
+
let request_data = test_request_data();
|
|
423
|
+
|
|
424
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
425
|
+
|
|
426
|
+
assert!(result.is_ok());
|
|
427
|
+
|
|
428
|
+
let log = execution_log.lock().unwrap();
|
|
429
|
+
assert_eq!(log.as_slice(), &["pre_validation", "pre_handler", "on_response"]);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
#[tokio::test]
|
|
433
|
+
async fn test_pre_validation_short_circuit_skips_handler_and_on_response() {
|
|
434
|
+
// Test 14: preValidation short-circuit skips handler and on_response
|
|
435
|
+
use std::sync::Mutex;
|
|
436
|
+
|
|
437
|
+
let handler_called = Arc::new(Mutex::new(false));
|
|
438
|
+
let response_hook_called = Arc::new(Mutex::new(false));
|
|
439
|
+
|
|
440
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
441
|
+
|
|
442
|
+
hooks.add_pre_validation(spikard_http::lifecycle::request_hook(
|
|
443
|
+
"short_circuit",
|
|
444
|
+
|_req| async move {
|
|
445
|
+
let response = Response::builder()
|
|
446
|
+
.status(StatusCode::UNAUTHORIZED)
|
|
447
|
+
.body(Body::from("Unauthorized"))
|
|
448
|
+
.unwrap();
|
|
449
|
+
Ok(HookResult::ShortCircuit(response))
|
|
450
|
+
},
|
|
451
|
+
));
|
|
452
|
+
|
|
453
|
+
let response_hook_called_clone = response_hook_called.clone();
|
|
454
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook(
|
|
455
|
+
"response_tracker",
|
|
456
|
+
move |resp| {
|
|
457
|
+
let flag = response_hook_called_clone.clone();
|
|
458
|
+
async move {
|
|
459
|
+
*flag.lock().unwrap() = true;
|
|
460
|
+
Ok(HookResult::Continue(resp))
|
|
461
|
+
}
|
|
462
|
+
},
|
|
463
|
+
));
|
|
464
|
+
|
|
465
|
+
struct TrackingHandler {
|
|
466
|
+
called: Arc<Mutex<bool>>,
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
impl Handler for TrackingHandler {
|
|
470
|
+
fn call(
|
|
471
|
+
&self,
|
|
472
|
+
_request: Request<Body>,
|
|
473
|
+
_request_data: RequestData,
|
|
474
|
+
) -> Pin<Box<dyn Future<Output = HandlerResult> + Send + '_>> {
|
|
475
|
+
let called = self.called.clone();
|
|
476
|
+
Box::pin(async move {
|
|
477
|
+
*called.lock().unwrap() = true;
|
|
478
|
+
let response = Response::builder()
|
|
479
|
+
.status(StatusCode::OK)
|
|
480
|
+
.body(Body::from("OK"))
|
|
481
|
+
.unwrap();
|
|
482
|
+
Ok(response)
|
|
483
|
+
})
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
let handler = Arc::new(TrackingHandler {
|
|
488
|
+
called: handler_called.clone(),
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
492
|
+
let request_data = test_request_data();
|
|
493
|
+
|
|
494
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
495
|
+
|
|
496
|
+
assert!(result.is_ok());
|
|
497
|
+
assert!(!*handler_called.lock().unwrap(), "Handler should not be called");
|
|
498
|
+
assert!(
|
|
499
|
+
!*response_hook_called.lock().unwrap(),
|
|
500
|
+
"on_response should not be called"
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
#[tokio::test]
|
|
505
|
+
async fn test_multiple_hooks_same_phase_executed_in_order() {
|
|
506
|
+
// Test 15: Multiple hooks in same phase executed in order
|
|
507
|
+
use std::sync::Mutex;
|
|
508
|
+
|
|
509
|
+
let execution_log = Arc::new(Mutex::new(Vec::new()));
|
|
510
|
+
let log1 = execution_log.clone();
|
|
511
|
+
let log2 = execution_log.clone();
|
|
512
|
+
let log3 = execution_log.clone();
|
|
513
|
+
|
|
514
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
515
|
+
|
|
516
|
+
// Add multiple on_response hooks
|
|
517
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook("first_hook", move |resp| {
|
|
518
|
+
let log = log1.clone();
|
|
519
|
+
async move {
|
|
520
|
+
log.lock().unwrap().push("first");
|
|
521
|
+
Ok(HookResult::Continue(resp))
|
|
522
|
+
}
|
|
523
|
+
}));
|
|
524
|
+
|
|
525
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook("second_hook", move |resp| {
|
|
526
|
+
let log = log2.clone();
|
|
527
|
+
async move {
|
|
528
|
+
log.lock().unwrap().push("second");
|
|
529
|
+
Ok(HookResult::Continue(resp))
|
|
530
|
+
}
|
|
531
|
+
}));
|
|
532
|
+
|
|
533
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook("third_hook", move |resp| {
|
|
534
|
+
let log = log3.clone();
|
|
535
|
+
async move {
|
|
536
|
+
log.lock().unwrap().push("third");
|
|
537
|
+
Ok(HookResult::Continue(resp))
|
|
538
|
+
}
|
|
539
|
+
}));
|
|
540
|
+
|
|
541
|
+
let handler = Arc::new(OkHandler);
|
|
542
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
543
|
+
let request_data = test_request_data();
|
|
544
|
+
|
|
545
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
546
|
+
|
|
547
|
+
assert!(result.is_ok());
|
|
548
|
+
|
|
549
|
+
let log = execution_log.lock().unwrap();
|
|
550
|
+
assert_eq!(log.as_slice(), &["first", "second", "third"]);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
#[tokio::test]
|
|
554
|
+
async fn test_hook_can_modify_request() {
|
|
555
|
+
// Test 16: Hook can modify request and handler receives modified version
|
|
556
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
557
|
+
|
|
558
|
+
hooks.add_pre_handler(spikard_http::lifecycle::request_hook(
|
|
559
|
+
"add_header",
|
|
560
|
+
|mut req| async move {
|
|
561
|
+
req.headers_mut()
|
|
562
|
+
.insert("X-Added-By-Hook", axum::http::HeaderValue::from_static("hook-value"));
|
|
563
|
+
Ok(HookResult::Continue(req))
|
|
564
|
+
},
|
|
565
|
+
));
|
|
566
|
+
|
|
567
|
+
struct HeaderCheckHandler;
|
|
568
|
+
|
|
569
|
+
impl Handler for HeaderCheckHandler {
|
|
570
|
+
fn call(
|
|
571
|
+
&self,
|
|
572
|
+
request: Request<Body>,
|
|
573
|
+
_request_data: RequestData,
|
|
574
|
+
) -> Pin<Box<dyn Future<Output = HandlerResult> + Send + '_>> {
|
|
575
|
+
Box::pin(async move {
|
|
576
|
+
let has_header = request.headers().contains_key("X-Added-By-Hook");
|
|
577
|
+
let body = if has_header { "Header found" } else { "Header not found" };
|
|
578
|
+
|
|
579
|
+
let response = Response::builder()
|
|
580
|
+
.status(StatusCode::OK)
|
|
581
|
+
.body(Body::from(body))
|
|
582
|
+
.unwrap();
|
|
583
|
+
Ok(response)
|
|
584
|
+
})
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
let handler = Arc::new(HeaderCheckHandler);
|
|
589
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
590
|
+
let request_data = test_request_data();
|
|
591
|
+
|
|
592
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
593
|
+
|
|
594
|
+
assert!(result.is_ok());
|
|
595
|
+
let response = result.unwrap();
|
|
596
|
+
assert_eq!(response.status(), StatusCode::OK);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
#[tokio::test]
|
|
600
|
+
async fn test_error_response_contains_hook_error_message() {
|
|
601
|
+
// Test 17: Error response contains hook error message
|
|
602
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
603
|
+
|
|
604
|
+
hooks.add_pre_validation(spikard_http::lifecycle::request_hook(
|
|
605
|
+
"failing_hook",
|
|
606
|
+
|_req| async move { Err("Custom validation error".to_string()) },
|
|
607
|
+
));
|
|
608
|
+
|
|
609
|
+
let handler = Arc::new(OkHandler);
|
|
610
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
611
|
+
let request_data = test_request_data();
|
|
612
|
+
|
|
613
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
614
|
+
|
|
615
|
+
assert!(result.is_ok());
|
|
616
|
+
let response = result.unwrap();
|
|
617
|
+
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
618
|
+
|
|
619
|
+
// Verify error message is in response body
|
|
620
|
+
let body_bytes = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
|
621
|
+
let body_str = String::from_utf8(body_bytes.to_vec()).unwrap();
|
|
622
|
+
assert!(body_str.contains("Custom validation error"));
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
#[tokio::test]
|
|
626
|
+
async fn test_chained_hook_modifications() {
|
|
627
|
+
// Test 18: Multiple hooks can chain modifications
|
|
628
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
629
|
+
|
|
630
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook(
|
|
631
|
+
"add_header_1",
|
|
632
|
+
|mut resp| async move {
|
|
633
|
+
resp.headers_mut()
|
|
634
|
+
.insert("X-Header-1", axum::http::HeaderValue::from_static("value1"));
|
|
635
|
+
Ok(HookResult::Continue(resp))
|
|
636
|
+
},
|
|
637
|
+
));
|
|
638
|
+
|
|
639
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook(
|
|
640
|
+
"add_header_2",
|
|
641
|
+
|mut resp| async move {
|
|
642
|
+
resp.headers_mut()
|
|
643
|
+
.insert("X-Header-2", axum::http::HeaderValue::from_static("value2"));
|
|
644
|
+
Ok(HookResult::Continue(resp))
|
|
645
|
+
},
|
|
646
|
+
));
|
|
647
|
+
|
|
648
|
+
let handler = Arc::new(OkHandler);
|
|
649
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
650
|
+
let request_data = test_request_data();
|
|
651
|
+
|
|
652
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
653
|
+
|
|
654
|
+
assert!(result.is_ok());
|
|
655
|
+
let response = result.unwrap();
|
|
656
|
+
assert_eq!(
|
|
657
|
+
response.headers().get("X-Header-1").map(|v| v.to_str().unwrap()),
|
|
658
|
+
Some("value1")
|
|
659
|
+
);
|
|
660
|
+
assert_eq!(
|
|
661
|
+
response.headers().get("X-Header-2").map(|v| v.to_str().unwrap()),
|
|
662
|
+
Some("value2")
|
|
663
|
+
);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
#[tokio::test]
|
|
667
|
+
async fn test_on_response_not_called_when_handler_fails() {
|
|
668
|
+
// Test 19: Verify on_response hook is NOT called when handler fails (bug-prone area)
|
|
669
|
+
use std::sync::Mutex;
|
|
670
|
+
|
|
671
|
+
let response_hook_called = Arc::new(Mutex::new(false));
|
|
672
|
+
let response_hook_called_clone = response_hook_called.clone();
|
|
673
|
+
|
|
674
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
675
|
+
|
|
676
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook(
|
|
677
|
+
"response_tracker",
|
|
678
|
+
move |resp| {
|
|
679
|
+
let flag = response_hook_called_clone.clone();
|
|
680
|
+
async move {
|
|
681
|
+
*flag.lock().unwrap() = true;
|
|
682
|
+
Ok(HookResult::Continue(resp))
|
|
683
|
+
}
|
|
684
|
+
},
|
|
685
|
+
));
|
|
686
|
+
|
|
687
|
+
let handler = Arc::new(BadRequestHandler);
|
|
688
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
689
|
+
let request_data = test_request_data();
|
|
690
|
+
|
|
691
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
692
|
+
|
|
693
|
+
assert!(result.is_ok());
|
|
694
|
+
assert!(
|
|
695
|
+
!*response_hook_called.lock().unwrap(),
|
|
696
|
+
"on_response should NOT be called when handler fails"
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
#[tokio::test]
|
|
701
|
+
async fn test_on_error_called_when_handler_fails() {
|
|
702
|
+
// Test 20: Verify on_error hook IS called when handler fails
|
|
703
|
+
use std::sync::Mutex;
|
|
704
|
+
|
|
705
|
+
let error_hook_called = Arc::new(Mutex::new(false));
|
|
706
|
+
let error_hook_called_clone = error_hook_called.clone();
|
|
707
|
+
|
|
708
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
709
|
+
|
|
710
|
+
hooks.add_on_error(spikard_http::lifecycle::response_hook("error_tracker", move |resp| {
|
|
711
|
+
let flag = error_hook_called_clone.clone();
|
|
712
|
+
async move {
|
|
713
|
+
*flag.lock().unwrap() = true;
|
|
714
|
+
Ok(HookResult::Continue(resp))
|
|
715
|
+
}
|
|
716
|
+
}));
|
|
717
|
+
|
|
718
|
+
let handler = Arc::new(BadRequestHandler);
|
|
719
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
720
|
+
let request_data = test_request_data();
|
|
721
|
+
|
|
722
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
723
|
+
|
|
724
|
+
assert!(result.is_ok());
|
|
725
|
+
assert!(
|
|
726
|
+
*error_hook_called.lock().unwrap(),
|
|
727
|
+
"on_error should be called when handler fails"
|
|
728
|
+
);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
#[tokio::test]
|
|
732
|
+
async fn test_on_error_hook_preserves_original_handler_error_status() {
|
|
733
|
+
// Test 21: on_error hook receives original error status code
|
|
734
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
735
|
+
|
|
736
|
+
hooks.add_on_error(spikard_http::lifecycle::response_hook(
|
|
737
|
+
"error_checker",
|
|
738
|
+
|resp| async move {
|
|
739
|
+
// Verify we receive 400 from the handler, not 500
|
|
740
|
+
if resp.status() == StatusCode::BAD_REQUEST {
|
|
741
|
+
Ok(HookResult::Continue(resp))
|
|
742
|
+
} else {
|
|
743
|
+
Err("Expected 400 status".to_string())
|
|
744
|
+
}
|
|
745
|
+
},
|
|
746
|
+
));
|
|
747
|
+
|
|
748
|
+
let handler = Arc::new(BadRequestHandler);
|
|
749
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
750
|
+
let request_data = test_request_data();
|
|
751
|
+
|
|
752
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
753
|
+
|
|
754
|
+
// Should succeed because on_error processed the response correctly
|
|
755
|
+
assert!(result.is_ok());
|
|
756
|
+
let response = result.unwrap();
|
|
757
|
+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
#[tokio::test]
|
|
761
|
+
async fn test_all_three_error_paths_call_on_error() {
|
|
762
|
+
// Test 22: All error paths (preValidation, preHandler, handler) call on_error
|
|
763
|
+
use std::sync::Mutex;
|
|
764
|
+
|
|
765
|
+
let error_hook_count = Arc::new(Mutex::new(0));
|
|
766
|
+
|
|
767
|
+
// Test preValidation error path
|
|
768
|
+
{
|
|
769
|
+
let error_hook_count_clone = error_hook_count.clone();
|
|
770
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
771
|
+
|
|
772
|
+
hooks.add_pre_validation(spikard_http::lifecycle::request_hook("failing", |_req| async move {
|
|
773
|
+
Err("preValidation error".to_string())
|
|
774
|
+
}));
|
|
775
|
+
|
|
776
|
+
hooks.add_on_error(spikard_http::lifecycle::response_hook("counter", move |resp| {
|
|
777
|
+
let count = error_hook_count_clone.clone();
|
|
778
|
+
async move {
|
|
779
|
+
*count.lock().unwrap() += 1;
|
|
780
|
+
Ok(HookResult::Continue(resp))
|
|
781
|
+
}
|
|
782
|
+
}));
|
|
783
|
+
|
|
784
|
+
let handler = Arc::new(OkHandler);
|
|
785
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
786
|
+
let request_data = test_request_data();
|
|
787
|
+
|
|
788
|
+
let _result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
assert_eq!(
|
|
792
|
+
*error_hook_count.lock().unwrap(),
|
|
793
|
+
1,
|
|
794
|
+
"on_error called for preValidation error"
|
|
795
|
+
);
|
|
796
|
+
|
|
797
|
+
// Reset and test preHandler error path
|
|
798
|
+
*error_hook_count.lock().unwrap() = 0;
|
|
799
|
+
{
|
|
800
|
+
let error_hook_count_clone = error_hook_count.clone();
|
|
801
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
802
|
+
|
|
803
|
+
hooks.add_pre_handler(spikard_http::lifecycle::request_hook("failing", |_req| async move {
|
|
804
|
+
Err("preHandler error".to_string())
|
|
805
|
+
}));
|
|
806
|
+
|
|
807
|
+
hooks.add_on_error(spikard_http::lifecycle::response_hook("counter", move |resp| {
|
|
808
|
+
let count = error_hook_count_clone.clone();
|
|
809
|
+
async move {
|
|
810
|
+
*count.lock().unwrap() += 1;
|
|
811
|
+
Ok(HookResult::Continue(resp))
|
|
812
|
+
}
|
|
813
|
+
}));
|
|
814
|
+
|
|
815
|
+
let handler = Arc::new(OkHandler);
|
|
816
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
817
|
+
let request_data = test_request_data();
|
|
818
|
+
|
|
819
|
+
let _result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
assert_eq!(
|
|
823
|
+
*error_hook_count.lock().unwrap(),
|
|
824
|
+
1,
|
|
825
|
+
"on_error called for preHandler error"
|
|
826
|
+
);
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
#[tokio::test]
|
|
830
|
+
async fn test_on_error_fallback_response_when_error_hook_fails() {
|
|
831
|
+
// Test 23: If on_error hook itself fails, fallback response returned
|
|
832
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
833
|
+
|
|
834
|
+
hooks.add_pre_validation(spikard_http::lifecycle::request_hook(
|
|
835
|
+
"failing_validation",
|
|
836
|
+
|_req| async move { Err("Validation error".to_string()) },
|
|
837
|
+
));
|
|
838
|
+
|
|
839
|
+
hooks.add_on_error(spikard_http::lifecycle::response_hook(
|
|
840
|
+
"failing_error_hook",
|
|
841
|
+
|_resp| async move { Err("on_error itself failed".to_string()) },
|
|
842
|
+
));
|
|
843
|
+
|
|
844
|
+
let handler = Arc::new(OkHandler);
|
|
845
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
846
|
+
let request_data = test_request_data();
|
|
847
|
+
|
|
848
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
849
|
+
|
|
850
|
+
assert!(result.is_ok());
|
|
851
|
+
let response = result.unwrap();
|
|
852
|
+
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
853
|
+
|
|
854
|
+
let body_bytes = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
|
855
|
+
let body_str = String::from_utf8(body_bytes.to_vec()).unwrap();
|
|
856
|
+
assert!(body_str.contains("Hook execution failed"));
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
#[tokio::test]
|
|
860
|
+
async fn test_full_hook_chain_with_success() {
|
|
861
|
+
// Test 24: Full chain: preValidation → preHandler → handler → onResponse
|
|
862
|
+
use std::sync::Mutex;
|
|
863
|
+
|
|
864
|
+
let execution_log = Arc::new(Mutex::new(Vec::new()));
|
|
865
|
+
let log1 = execution_log.clone();
|
|
866
|
+
let log2 = execution_log.clone();
|
|
867
|
+
let log3 = execution_log.clone();
|
|
868
|
+
|
|
869
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
870
|
+
|
|
871
|
+
hooks.add_pre_validation(spikard_http::lifecycle::request_hook("pv", move |req| {
|
|
872
|
+
let log = log1.clone();
|
|
873
|
+
async move {
|
|
874
|
+
log.lock().unwrap().push("preValidation");
|
|
875
|
+
Ok(HookResult::Continue(req))
|
|
876
|
+
}
|
|
877
|
+
}));
|
|
878
|
+
|
|
879
|
+
hooks.add_pre_handler(spikard_http::lifecycle::request_hook("ph", move |req| {
|
|
880
|
+
let log = log2.clone();
|
|
881
|
+
async move {
|
|
882
|
+
log.lock().unwrap().push("preHandler");
|
|
883
|
+
Ok(HookResult::Continue(req))
|
|
884
|
+
}
|
|
885
|
+
}));
|
|
886
|
+
|
|
887
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook("or", move |resp| {
|
|
888
|
+
let log = log3.clone();
|
|
889
|
+
async move {
|
|
890
|
+
log.lock().unwrap().push("onResponse");
|
|
891
|
+
Ok(HookResult::Continue(resp))
|
|
892
|
+
}
|
|
893
|
+
}));
|
|
894
|
+
|
|
895
|
+
let handler = Arc::new(OkHandler);
|
|
896
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
897
|
+
let request_data = test_request_data();
|
|
898
|
+
|
|
899
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
900
|
+
|
|
901
|
+
assert!(result.is_ok());
|
|
902
|
+
let log = execution_log.lock().unwrap();
|
|
903
|
+
assert_eq!(log.as_slice(), &["preValidation", "preHandler", "onResponse"]);
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
#[tokio::test]
|
|
907
|
+
async fn test_pre_handler_short_circuit_skips_on_response() {
|
|
908
|
+
// Test 25: preHandler short-circuit skips handler and on_response
|
|
909
|
+
use std::sync::Mutex;
|
|
910
|
+
|
|
911
|
+
let response_hook_called = Arc::new(Mutex::new(false));
|
|
912
|
+
let response_hook_called_clone = response_hook_called.clone();
|
|
913
|
+
|
|
914
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
915
|
+
|
|
916
|
+
hooks.add_pre_handler(spikard_http::lifecycle::request_hook(
|
|
917
|
+
"short_circuit",
|
|
918
|
+
|_req| async move {
|
|
919
|
+
let response = Response::builder()
|
|
920
|
+
.status(StatusCode::FORBIDDEN)
|
|
921
|
+
.body(Body::from("Forbidden"))
|
|
922
|
+
.unwrap();
|
|
923
|
+
Ok(HookResult::ShortCircuit(response))
|
|
924
|
+
},
|
|
925
|
+
));
|
|
926
|
+
|
|
927
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook(
|
|
928
|
+
"response_tracker",
|
|
929
|
+
move |resp| {
|
|
930
|
+
let flag = response_hook_called_clone.clone();
|
|
931
|
+
async move {
|
|
932
|
+
*flag.lock().unwrap() = true;
|
|
933
|
+
Ok(HookResult::Continue(resp))
|
|
934
|
+
}
|
|
935
|
+
},
|
|
936
|
+
));
|
|
937
|
+
|
|
938
|
+
let handler = Arc::new(OkHandler);
|
|
939
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
940
|
+
let request_data = test_request_data();
|
|
941
|
+
|
|
942
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
943
|
+
|
|
944
|
+
assert!(result.is_ok());
|
|
945
|
+
let response = result.unwrap();
|
|
946
|
+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
|
|
947
|
+
assert!(
|
|
948
|
+
!*response_hook_called.lock().unwrap(),
|
|
949
|
+
"on_response should not be called"
|
|
950
|
+
);
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
#[tokio::test]
|
|
954
|
+
async fn test_on_response_hook_can_transform_success_response() {
|
|
955
|
+
// Test 26: on_response can completely transform the response
|
|
956
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
957
|
+
|
|
958
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook(
|
|
959
|
+
"transform",
|
|
960
|
+
|_old_resp| async move {
|
|
961
|
+
let new_response = Response::builder()
|
|
962
|
+
.status(StatusCode::CREATED)
|
|
963
|
+
.header("X-Transformed", "yes")
|
|
964
|
+
.body(Body::from("Transformed"))
|
|
965
|
+
.unwrap();
|
|
966
|
+
Ok(HookResult::Continue(new_response))
|
|
967
|
+
},
|
|
968
|
+
));
|
|
969
|
+
|
|
970
|
+
let handler = Arc::new(OkHandler);
|
|
971
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
972
|
+
let request_data = test_request_data();
|
|
973
|
+
|
|
974
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
975
|
+
|
|
976
|
+
assert!(result.is_ok());
|
|
977
|
+
let response = result.unwrap();
|
|
978
|
+
assert_eq!(response.status(), StatusCode::CREATED);
|
|
979
|
+
assert_eq!(
|
|
980
|
+
response.headers().get("X-Transformed").map(|v| v.to_str().unwrap()),
|
|
981
|
+
Some("yes")
|
|
982
|
+
);
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
#[tokio::test]
|
|
986
|
+
async fn test_error_response_body_contains_correct_hook_context() {
|
|
987
|
+
// Test 27: Error responses include context about which hook phase failed
|
|
988
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
989
|
+
|
|
990
|
+
hooks.add_pre_handler(spikard_http::lifecycle::request_hook("failing", |_req| async move {
|
|
991
|
+
Err("Specific handler setup error".to_string())
|
|
992
|
+
}));
|
|
993
|
+
|
|
994
|
+
let handler = Arc::new(OkHandler);
|
|
995
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
996
|
+
let request_data = test_request_data();
|
|
997
|
+
|
|
998
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
999
|
+
|
|
1000
|
+
assert!(result.is_ok());
|
|
1001
|
+
let response = result.unwrap();
|
|
1002
|
+
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
1003
|
+
|
|
1004
|
+
let body_bytes = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
|
1005
|
+
let body_str = String::from_utf8(body_bytes.to_vec()).unwrap();
|
|
1006
|
+
|
|
1007
|
+
// Should mention preHandler and the error message
|
|
1008
|
+
assert!(body_str.contains("preHandler"));
|
|
1009
|
+
assert!(body_str.contains("Specific handler setup error"));
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
#[tokio::test]
|
|
1013
|
+
async fn test_multiple_error_hooks_execute_in_sequence() {
|
|
1014
|
+
// Test 28: Multiple on_error hooks execute in sequence
|
|
1015
|
+
use std::sync::Mutex;
|
|
1016
|
+
|
|
1017
|
+
let execution_log = Arc::new(Mutex::new(Vec::new()));
|
|
1018
|
+
let log1 = execution_log.clone();
|
|
1019
|
+
let log2 = execution_log.clone();
|
|
1020
|
+
|
|
1021
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
1022
|
+
|
|
1023
|
+
hooks.add_pre_validation(spikard_http::lifecycle::request_hook("failing", |_req| async move {
|
|
1024
|
+
Err("Validation failed".to_string())
|
|
1025
|
+
}));
|
|
1026
|
+
|
|
1027
|
+
hooks.add_on_error(spikard_http::lifecycle::response_hook("error_handler_1", move |resp| {
|
|
1028
|
+
let log = log1.clone();
|
|
1029
|
+
async move {
|
|
1030
|
+
log.lock().unwrap().push("error_handler_1");
|
|
1031
|
+
Ok(HookResult::Continue(resp))
|
|
1032
|
+
}
|
|
1033
|
+
}));
|
|
1034
|
+
|
|
1035
|
+
hooks.add_on_error(spikard_http::lifecycle::response_hook("error_handler_2", move |resp| {
|
|
1036
|
+
let log = log2.clone();
|
|
1037
|
+
async move {
|
|
1038
|
+
log.lock().unwrap().push("error_handler_2");
|
|
1039
|
+
Ok(HookResult::Continue(resp))
|
|
1040
|
+
}
|
|
1041
|
+
}));
|
|
1042
|
+
|
|
1043
|
+
let handler = Arc::new(OkHandler);
|
|
1044
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
1045
|
+
let request_data = test_request_data();
|
|
1046
|
+
|
|
1047
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
1048
|
+
|
|
1049
|
+
assert!(result.is_ok());
|
|
1050
|
+
let log = execution_log.lock().unwrap();
|
|
1051
|
+
assert_eq!(log.as_slice(), &["error_handler_1", "error_handler_2"]);
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
#[tokio::test]
|
|
1055
|
+
async fn test_handler_result_error_becomes_500_with_on_error_handling() {
|
|
1056
|
+
// Test 29: Handler errors are wrapped with on_error hook capability
|
|
1057
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
1058
|
+
|
|
1059
|
+
hooks.add_on_error(spikard_http::lifecycle::response_hook(
|
|
1060
|
+
"error_modifier",
|
|
1061
|
+
|mut resp| async move {
|
|
1062
|
+
resp.headers_mut()
|
|
1063
|
+
.insert("X-Error-Detected", axum::http::HeaderValue::from_static("true"));
|
|
1064
|
+
Ok(HookResult::Continue(resp))
|
|
1065
|
+
},
|
|
1066
|
+
));
|
|
1067
|
+
|
|
1068
|
+
let handler = Arc::new(BadRequestHandler);
|
|
1069
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
1070
|
+
let request_data = test_request_data();
|
|
1071
|
+
|
|
1072
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
1073
|
+
|
|
1074
|
+
assert!(result.is_ok());
|
|
1075
|
+
let response = result.unwrap();
|
|
1076
|
+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
|
1077
|
+
assert_eq!(
|
|
1078
|
+
response.headers().get("X-Error-Detected").map(|v| v.to_str().unwrap()),
|
|
1079
|
+
Some("true")
|
|
1080
|
+
);
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
#[tokio::test]
|
|
1084
|
+
async fn test_on_response_with_multiple_sequential_transforms() {
|
|
1085
|
+
// Test 30: Multiple on_response hooks can sequentially transform response
|
|
1086
|
+
let mut hooks = spikard_http::lifecycle::LifecycleHooks::new();
|
|
1087
|
+
|
|
1088
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook(
|
|
1089
|
+
"add_header_1",
|
|
1090
|
+
|mut resp| async move {
|
|
1091
|
+
resp.headers_mut()
|
|
1092
|
+
.insert("X-Step-1", axum::http::HeaderValue::from_static("done"));
|
|
1093
|
+
Ok(HookResult::Continue(resp))
|
|
1094
|
+
},
|
|
1095
|
+
));
|
|
1096
|
+
|
|
1097
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook(
|
|
1098
|
+
"add_header_2",
|
|
1099
|
+
|mut resp| async move {
|
|
1100
|
+
resp.headers_mut()
|
|
1101
|
+
.insert("X-Step-2", axum::http::HeaderValue::from_static("done"));
|
|
1102
|
+
Ok(HookResult::Continue(resp))
|
|
1103
|
+
},
|
|
1104
|
+
));
|
|
1105
|
+
|
|
1106
|
+
hooks.add_on_response(spikard_http::lifecycle::response_hook(
|
|
1107
|
+
"add_header_3",
|
|
1108
|
+
|mut resp| async move {
|
|
1109
|
+
resp.headers_mut()
|
|
1110
|
+
.insert("X-Step-3", axum::http::HeaderValue::from_static("done"));
|
|
1111
|
+
Ok(HookResult::Continue(resp))
|
|
1112
|
+
},
|
|
1113
|
+
));
|
|
1114
|
+
|
|
1115
|
+
let handler = Arc::new(OkHandler);
|
|
1116
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
1117
|
+
let request_data = test_request_data();
|
|
1118
|
+
|
|
1119
|
+
let result = execute_with_lifecycle_hooks(request, request_data, handler, Some(Arc::new(hooks))).await;
|
|
1120
|
+
|
|
1121
|
+
assert!(result.is_ok());
|
|
1122
|
+
let response = result.unwrap();
|
|
1123
|
+
assert_eq!(
|
|
1124
|
+
response.headers().get("X-Step-1").map(|v| v.to_str().unwrap()),
|
|
1125
|
+
Some("done")
|
|
1126
|
+
);
|
|
1127
|
+
assert_eq!(
|
|
1128
|
+
response.headers().get("X-Step-2").map(|v| v.to_str().unwrap()),
|
|
1129
|
+
Some("done")
|
|
1130
|
+
);
|
|
1131
|
+
assert_eq!(
|
|
1132
|
+
response.headers().get("X-Step-3").map(|v| v.to_str().unwrap()),
|
|
1133
|
+
Some("done")
|
|
1134
|
+
);
|
|
1135
|
+
}
|