spikard 0.5.0 → 0.6.1

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.
Files changed (135) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +674 -674
  4. data/ext/spikard_rb/Cargo.toml +17 -17
  5. data/ext/spikard_rb/extconf.rb +13 -10
  6. data/ext/spikard_rb/src/lib.rs +6 -6
  7. data/lib/spikard/app.rb +405 -405
  8. data/lib/spikard/background.rb +27 -27
  9. data/lib/spikard/config.rb +396 -396
  10. data/lib/spikard/converters.rb +13 -13
  11. data/lib/spikard/handler_wrapper.rb +113 -113
  12. data/lib/spikard/provide.rb +214 -214
  13. data/lib/spikard/response.rb +173 -173
  14. data/lib/spikard/schema.rb +243 -243
  15. data/lib/spikard/sse.rb +111 -111
  16. data/lib/spikard/streaming_response.rb +44 -44
  17. data/lib/spikard/testing.rb +256 -256
  18. data/lib/spikard/upload_file.rb +131 -131
  19. data/lib/spikard/version.rb +5 -5
  20. data/lib/spikard/websocket.rb +59 -59
  21. data/lib/spikard.rb +43 -43
  22. data/sig/spikard.rbs +366 -366
  23. data/vendor/crates/spikard-bindings-shared/Cargo.toml +63 -63
  24. data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +132 -132
  25. data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +752 -752
  26. data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +194 -194
  27. data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +246 -246
  28. data/vendor/crates/spikard-bindings-shared/src/error_response.rs +401 -401
  29. data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +238 -238
  30. data/vendor/crates/spikard-bindings-shared/src/lib.rs +24 -24
  31. data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +292 -292
  32. data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +616 -616
  33. data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +305 -305
  34. data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +248 -248
  35. data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +351 -351
  36. data/vendor/crates/spikard-bindings-shared/tests/comprehensive_coverage.rs +454 -454
  37. data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +383 -383
  38. data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +280 -280
  39. data/vendor/crates/spikard-core/Cargo.toml +40 -40
  40. data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -3
  41. data/vendor/crates/spikard-core/src/bindings/response.rs +133 -133
  42. data/vendor/crates/spikard-core/src/debug.rs +127 -127
  43. data/vendor/crates/spikard-core/src/di/container.rs +702 -702
  44. data/vendor/crates/spikard-core/src/di/dependency.rs +273 -273
  45. data/vendor/crates/spikard-core/src/di/error.rs +118 -118
  46. data/vendor/crates/spikard-core/src/di/factory.rs +534 -534
  47. data/vendor/crates/spikard-core/src/di/graph.rs +506 -506
  48. data/vendor/crates/spikard-core/src/di/mod.rs +192 -192
  49. data/vendor/crates/spikard-core/src/di/resolved.rs +405 -405
  50. data/vendor/crates/spikard-core/src/di/value.rs +281 -281
  51. data/vendor/crates/spikard-core/src/errors.rs +69 -69
  52. data/vendor/crates/spikard-core/src/http.rs +415 -415
  53. data/vendor/crates/spikard-core/src/lib.rs +29 -29
  54. data/vendor/crates/spikard-core/src/lifecycle.rs +1186 -1186
  55. data/vendor/crates/spikard-core/src/metadata.rs +389 -389
  56. data/vendor/crates/spikard-core/src/parameters.rs +2525 -2525
  57. data/vendor/crates/spikard-core/src/problem.rs +344 -344
  58. data/vendor/crates/spikard-core/src/request_data.rs +1154 -1154
  59. data/vendor/crates/spikard-core/src/router.rs +510 -510
  60. data/vendor/crates/spikard-core/src/schema_registry.rs +183 -183
  61. data/vendor/crates/spikard-core/src/type_hints.rs +304 -304
  62. data/vendor/crates/spikard-core/src/validation/error_mapper.rs +696 -688
  63. data/vendor/crates/spikard-core/src/validation/mod.rs +457 -457
  64. data/vendor/crates/spikard-http/Cargo.toml +62 -64
  65. data/vendor/crates/spikard-http/examples/sse-notifications.rs +148 -148
  66. data/vendor/crates/spikard-http/examples/websocket-chat.rs +92 -92
  67. data/vendor/crates/spikard-http/src/auth.rs +296 -296
  68. data/vendor/crates/spikard-http/src/background.rs +1860 -1860
  69. data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -3
  70. data/vendor/crates/spikard-http/src/bindings/response.rs +1 -1
  71. data/vendor/crates/spikard-http/src/body_metadata.rs +8 -8
  72. data/vendor/crates/spikard-http/src/cors.rs +1005 -1005
  73. data/vendor/crates/spikard-http/src/debug.rs +128 -128
  74. data/vendor/crates/spikard-http/src/di_handler.rs +1668 -1668
  75. data/vendor/crates/spikard-http/src/handler_response.rs +901 -901
  76. data/vendor/crates/spikard-http/src/handler_trait.rs +838 -830
  77. data/vendor/crates/spikard-http/src/handler_trait_tests.rs +290 -290
  78. data/vendor/crates/spikard-http/src/lib.rs +534 -534
  79. data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +230 -230
  80. data/vendor/crates/spikard-http/src/lifecycle.rs +1193 -1193
  81. data/vendor/crates/spikard-http/src/middleware/mod.rs +560 -540
  82. data/vendor/crates/spikard-http/src/middleware/multipart.rs +912 -912
  83. data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +513 -513
  84. data/vendor/crates/spikard-http/src/middleware/validation.rs +768 -735
  85. data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -309
  86. data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +535 -535
  87. data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +1363 -1363
  88. data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +665 -665
  89. data/vendor/crates/spikard-http/src/query_parser.rs +793 -793
  90. data/vendor/crates/spikard-http/src/response.rs +720 -720
  91. data/vendor/crates/spikard-http/src/server/handler.rs +1650 -1650
  92. data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +234 -234
  93. data/vendor/crates/spikard-http/src/server/mod.rs +1593 -1502
  94. data/vendor/crates/spikard-http/src/server/request_extraction.rs +789 -770
  95. data/vendor/crates/spikard-http/src/server/routing_factory.rs +629 -599
  96. data/vendor/crates/spikard-http/src/sse.rs +1409 -1409
  97. data/vendor/crates/spikard-http/src/testing/form.rs +52 -52
  98. data/vendor/crates/spikard-http/src/testing/multipart.rs +64 -60
  99. data/vendor/crates/spikard-http/src/testing/test_client.rs +311 -283
  100. data/vendor/crates/spikard-http/src/testing.rs +406 -377
  101. data/vendor/crates/spikard-http/src/websocket.rs +1404 -1375
  102. data/vendor/crates/spikard-http/tests/background_behavior.rs +832 -832
  103. data/vendor/crates/spikard-http/tests/common/handlers.rs +309 -309
  104. data/vendor/crates/spikard-http/tests/common/mod.rs +26 -26
  105. data/vendor/crates/spikard-http/tests/di_integration.rs +192 -192
  106. data/vendor/crates/spikard-http/tests/doc_snippets.rs +5 -5
  107. data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +1093 -1093
  108. data/vendor/crates/spikard-http/tests/multipart_behavior.rs +656 -656
  109. data/vendor/crates/spikard-http/tests/server_config_builder.rs +314 -314
  110. data/vendor/crates/spikard-http/tests/sse_behavior.rs +620 -620
  111. data/vendor/crates/spikard-http/tests/websocket_behavior.rs +663 -663
  112. data/vendor/crates/spikard-rb/Cargo.toml +48 -48
  113. data/vendor/crates/spikard-rb/build.rs +199 -199
  114. data/vendor/crates/spikard-rb/src/background.rs +63 -63
  115. data/vendor/crates/spikard-rb/src/config/mod.rs +5 -5
  116. data/vendor/crates/spikard-rb/src/config/server_config.rs +285 -285
  117. data/vendor/crates/spikard-rb/src/conversion.rs +554 -554
  118. data/vendor/crates/spikard-rb/src/di/builder.rs +100 -100
  119. data/vendor/crates/spikard-rb/src/di/mod.rs +375 -375
  120. data/vendor/crates/spikard-rb/src/handler.rs +618 -618
  121. data/vendor/crates/spikard-rb/src/integration/mod.rs +3 -3
  122. data/vendor/crates/spikard-rb/src/lib.rs +1806 -1810
  123. data/vendor/crates/spikard-rb/src/lifecycle.rs +275 -275
  124. data/vendor/crates/spikard-rb/src/metadata/mod.rs +5 -5
  125. data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +442 -447
  126. data/vendor/crates/spikard-rb/src/runtime/mod.rs +5 -5
  127. data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +324 -324
  128. data/vendor/crates/spikard-rb/src/server.rs +305 -308
  129. data/vendor/crates/spikard-rb/src/sse.rs +231 -231
  130. data/vendor/crates/spikard-rb/src/testing/client.rs +538 -551
  131. data/vendor/crates/spikard-rb/src/testing/mod.rs +7 -7
  132. data/vendor/crates/spikard-rb/src/testing/sse.rs +143 -143
  133. data/vendor/crates/spikard-rb/src/testing/websocket.rs +608 -635
  134. data/vendor/crates/spikard-rb/src/websocket.rs +377 -374
  135. metadata +15 -1
@@ -1,292 +1,292 @@
1
- //! Lifecycle hook base implementations
2
-
3
- use std::sync::Arc;
4
-
5
- /// Lifecycle hook types supported across all bindings
6
- #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7
- pub enum LifecycleHookType {
8
- /// Called at the start of request processing
9
- OnRequest,
10
- /// Called before validation
11
- PreValidation,
12
- /// Called before handler execution
13
- PreHandler,
14
- /// Called after handler execution
15
- OnResponse,
16
- /// Called when an error occurs
17
- OnError,
18
- }
19
-
20
- /// Result type for lifecycle hooks
21
- #[derive(Clone)]
22
- pub enum HookResult {
23
- /// Continue with normal processing
24
- Continue,
25
- /// Short-circuit and return this response
26
- ShortCircuit(serde_json::Value),
27
- }
28
-
29
- /// Trait for implementing lifecycle hooks in language bindings
30
- pub trait LifecycleHook: Send + Sync {
31
- /// Execute the lifecycle hook
32
- fn execute(&self, context: serde_json::Value) -> Result<HookResult, String>;
33
-
34
- /// Get the hook type
35
- fn hook_type(&self) -> LifecycleHookType;
36
- }
37
-
38
- /// Base configuration for lifecycle hooks
39
- pub struct LifecycleConfig {
40
- /// Registered hooks by type
41
- hooks: std::collections::HashMap<LifecycleHookType, Vec<Arc<dyn LifecycleHook>>>,
42
- }
43
-
44
- impl LifecycleConfig {
45
- /// Create a new lifecycle configuration
46
- pub fn new() -> Self {
47
- Self {
48
- hooks: std::collections::HashMap::new(),
49
- }
50
- }
51
-
52
- /// Register a lifecycle hook
53
- pub fn register(&mut self, hook: Arc<dyn LifecycleHook>) {
54
- self.hooks.entry(hook.hook_type()).or_default().push(hook);
55
- }
56
-
57
- /// Get hooks for a specific type
58
- pub fn get_hooks(&self, hook_type: LifecycleHookType) -> Vec<Arc<dyn LifecycleHook>> {
59
- self.hooks.get(&hook_type).cloned().unwrap_or_default()
60
- }
61
- }
62
-
63
- impl Default for LifecycleConfig {
64
- fn default() -> Self {
65
- Self::new()
66
- }
67
- }
68
-
69
- #[cfg(test)]
70
- mod tests {
71
- use super::*;
72
- use serde_json::json;
73
-
74
- struct TestHook {
75
- hook_type: LifecycleHookType,
76
- result: HookResult,
77
- }
78
-
79
- impl LifecycleHook for TestHook {
80
- fn execute(&self, _context: serde_json::Value) -> Result<HookResult, String> {
81
- Ok(self.result.clone())
82
- }
83
-
84
- fn hook_type(&self) -> LifecycleHookType {
85
- self.hook_type
86
- }
87
- }
88
-
89
- #[test]
90
- fn test_lifecycle_hook_type_equality() {
91
- assert_eq!(LifecycleHookType::OnRequest, LifecycleHookType::OnRequest);
92
- assert_ne!(LifecycleHookType::OnRequest, LifecycleHookType::OnResponse);
93
- }
94
-
95
- #[test]
96
- fn test_lifecycle_hook_type_hash() {
97
- use std::collections::HashSet;
98
-
99
- let mut set = HashSet::new();
100
- set.insert(LifecycleHookType::OnRequest);
101
- set.insert(LifecycleHookType::PreHandler);
102
- set.insert(LifecycleHookType::OnRequest);
103
-
104
- assert_eq!(set.len(), 2);
105
- }
106
-
107
- #[test]
108
- fn test_hook_result_continue() {
109
- let result = HookResult::Continue;
110
- match result {
111
- HookResult::Continue => {}
112
- HookResult::ShortCircuit(_) => panic!("Expected Continue"),
113
- }
114
- }
115
-
116
- #[test]
117
- fn test_hook_result_short_circuit() {
118
- let response = json!({ "status": "error" });
119
- let result = HookResult::ShortCircuit(response.clone());
120
- match result {
121
- HookResult::Continue => panic!("Expected ShortCircuit"),
122
- HookResult::ShortCircuit(r) => {
123
- assert_eq!(r, response);
124
- }
125
- }
126
- }
127
-
128
- #[test]
129
- fn test_lifecycle_config_new() {
130
- let config = LifecycleConfig::new();
131
- assert_eq!(config.get_hooks(LifecycleHookType::OnRequest).len(), 0);
132
- }
133
-
134
- #[test]
135
- fn test_lifecycle_config_default() {
136
- let config = LifecycleConfig::default();
137
- assert_eq!(config.get_hooks(LifecycleHookType::OnRequest).len(), 0);
138
- }
139
-
140
- #[test]
141
- fn test_register_single_hook() {
142
- let mut config = LifecycleConfig::new();
143
- let hook = Arc::new(TestHook {
144
- hook_type: LifecycleHookType::OnRequest,
145
- result: HookResult::Continue,
146
- });
147
-
148
- config.register(hook);
149
-
150
- let hooks = config.get_hooks(LifecycleHookType::OnRequest);
151
- assert_eq!(hooks.len(), 1);
152
- }
153
-
154
- #[test]
155
- fn test_register_multiple_hooks_same_type() {
156
- let mut config = LifecycleConfig::new();
157
-
158
- for i in 0..3 {
159
- let hook = Arc::new(TestHook {
160
- hook_type: LifecycleHookType::OnRequest,
161
- result: if i == 0 {
162
- HookResult::Continue
163
- } else {
164
- HookResult::ShortCircuit(json!({ "index": i }))
165
- },
166
- });
167
- config.register(hook);
168
- }
169
-
170
- let hooks = config.get_hooks(LifecycleHookType::OnRequest);
171
- assert_eq!(hooks.len(), 3);
172
- }
173
-
174
- #[test]
175
- fn test_register_hooks_different_types() {
176
- let mut config = LifecycleConfig::new();
177
-
178
- let hook_on_request = Arc::new(TestHook {
179
- hook_type: LifecycleHookType::OnRequest,
180
- result: HookResult::Continue,
181
- });
182
-
183
- let hook_on_error = Arc::new(TestHook {
184
- hook_type: LifecycleHookType::OnError,
185
- result: HookResult::Continue,
186
- });
187
-
188
- config.register(hook_on_request);
189
- config.register(hook_on_error);
190
-
191
- assert_eq!(config.get_hooks(LifecycleHookType::OnRequest).len(), 1);
192
- assert_eq!(config.get_hooks(LifecycleHookType::OnError).len(), 1);
193
- assert_eq!(config.get_hooks(LifecycleHookType::PreHandler).len(), 0);
194
- }
195
-
196
- #[test]
197
- fn test_get_hooks_empty() {
198
- let config = LifecycleConfig::new();
199
- let hooks = config.get_hooks(LifecycleHookType::PreValidation);
200
- assert!(hooks.is_empty());
201
- }
202
-
203
- #[test]
204
- fn test_get_hooks_multiple_calls() {
205
- let mut config = LifecycleConfig::new();
206
-
207
- let hook1 = Arc::new(TestHook {
208
- hook_type: LifecycleHookType::OnResponse,
209
- result: HookResult::Continue,
210
- });
211
-
212
- let hook2 = Arc::new(TestHook {
213
- hook_type: LifecycleHookType::OnResponse,
214
- result: HookResult::Continue,
215
- });
216
-
217
- config.register(hook1);
218
- config.register(hook2);
219
-
220
- let hooks1 = config.get_hooks(LifecycleHookType::OnResponse);
221
- let hooks2 = config.get_hooks(LifecycleHookType::OnResponse);
222
-
223
- assert_eq!(hooks1.len(), 2);
224
- assert_eq!(hooks2.len(), 2);
225
- }
226
-
227
- #[test]
228
- fn test_hook_execute() {
229
- let hook = TestHook {
230
- hook_type: LifecycleHookType::OnRequest,
231
- result: HookResult::Continue,
232
- };
233
-
234
- let context = json!({ "test": "data" });
235
- let result = hook.execute(context);
236
-
237
- assert!(result.is_ok());
238
- match result.unwrap() {
239
- HookResult::Continue => {}
240
- HookResult::ShortCircuit(_) => panic!("Expected Continue"),
241
- }
242
- }
243
-
244
- #[test]
245
- fn test_hook_type_retrieval() {
246
- let hook = TestHook {
247
- hook_type: LifecycleHookType::PreValidation,
248
- result: HookResult::Continue,
249
- };
250
-
251
- assert_eq!(hook.hook_type(), LifecycleHookType::PreValidation);
252
- }
253
-
254
- #[test]
255
- fn test_all_hook_types() {
256
- let hook_types = vec![
257
- LifecycleHookType::OnRequest,
258
- LifecycleHookType::PreValidation,
259
- LifecycleHookType::PreHandler,
260
- LifecycleHookType::OnResponse,
261
- LifecycleHookType::OnError,
262
- ];
263
-
264
- let mut config = LifecycleConfig::new();
265
-
266
- for hook_type in hook_types.iter() {
267
- let hook = Arc::new(TestHook {
268
- hook_type: *hook_type,
269
- result: HookResult::Continue,
270
- });
271
- config.register(hook);
272
- }
273
-
274
- for hook_type in hook_types {
275
- let hooks = config.get_hooks(hook_type);
276
- assert_eq!(hooks.len(), 1);
277
- }
278
- }
279
-
280
- #[test]
281
- fn test_hook_result_clone() {
282
- let original = HookResult::ShortCircuit(json!({ "key": "value" }));
283
- let cloned = original.clone();
284
-
285
- match cloned {
286
- HookResult::ShortCircuit(response) => {
287
- assert_eq!(response["key"], "value");
288
- }
289
- _ => panic!("Expected ShortCircuit"),
290
- }
291
- }
292
- }
1
+ //! Lifecycle hook base implementations
2
+
3
+ use std::sync::Arc;
4
+
5
+ /// Lifecycle hook types supported across all bindings
6
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7
+ pub enum LifecycleHookType {
8
+ /// Called at the start of request processing
9
+ OnRequest,
10
+ /// Called before validation
11
+ PreValidation,
12
+ /// Called before handler execution
13
+ PreHandler,
14
+ /// Called after handler execution
15
+ OnResponse,
16
+ /// Called when an error occurs
17
+ OnError,
18
+ }
19
+
20
+ /// Result type for lifecycle hooks
21
+ #[derive(Clone)]
22
+ pub enum HookResult {
23
+ /// Continue with normal processing
24
+ Continue,
25
+ /// Short-circuit and return this response
26
+ ShortCircuit(serde_json::Value),
27
+ }
28
+
29
+ /// Trait for implementing lifecycle hooks in language bindings
30
+ pub trait LifecycleHook: Send + Sync {
31
+ /// Execute the lifecycle hook
32
+ fn execute(&self, context: serde_json::Value) -> Result<HookResult, String>;
33
+
34
+ /// Get the hook type
35
+ fn hook_type(&self) -> LifecycleHookType;
36
+ }
37
+
38
+ /// Base configuration for lifecycle hooks
39
+ pub struct LifecycleConfig {
40
+ /// Registered hooks by type
41
+ hooks: std::collections::HashMap<LifecycleHookType, Vec<Arc<dyn LifecycleHook>>>,
42
+ }
43
+
44
+ impl LifecycleConfig {
45
+ /// Create a new lifecycle configuration
46
+ pub fn new() -> Self {
47
+ Self {
48
+ hooks: std::collections::HashMap::new(),
49
+ }
50
+ }
51
+
52
+ /// Register a lifecycle hook
53
+ pub fn register(&mut self, hook: Arc<dyn LifecycleHook>) {
54
+ self.hooks.entry(hook.hook_type()).or_default().push(hook);
55
+ }
56
+
57
+ /// Get hooks for a specific type
58
+ pub fn get_hooks(&self, hook_type: LifecycleHookType) -> Vec<Arc<dyn LifecycleHook>> {
59
+ self.hooks.get(&hook_type).cloned().unwrap_or_default()
60
+ }
61
+ }
62
+
63
+ impl Default for LifecycleConfig {
64
+ fn default() -> Self {
65
+ Self::new()
66
+ }
67
+ }
68
+
69
+ #[cfg(test)]
70
+ mod tests {
71
+ use super::*;
72
+ use serde_json::json;
73
+
74
+ struct TestHook {
75
+ hook_type: LifecycleHookType,
76
+ result: HookResult,
77
+ }
78
+
79
+ impl LifecycleHook for TestHook {
80
+ fn execute(&self, _context: serde_json::Value) -> Result<HookResult, String> {
81
+ Ok(self.result.clone())
82
+ }
83
+
84
+ fn hook_type(&self) -> LifecycleHookType {
85
+ self.hook_type
86
+ }
87
+ }
88
+
89
+ #[test]
90
+ fn test_lifecycle_hook_type_equality() {
91
+ assert_eq!(LifecycleHookType::OnRequest, LifecycleHookType::OnRequest);
92
+ assert_ne!(LifecycleHookType::OnRequest, LifecycleHookType::OnResponse);
93
+ }
94
+
95
+ #[test]
96
+ fn test_lifecycle_hook_type_hash() {
97
+ use std::collections::HashSet;
98
+
99
+ let mut set = HashSet::new();
100
+ set.insert(LifecycleHookType::OnRequest);
101
+ set.insert(LifecycleHookType::PreHandler);
102
+ set.insert(LifecycleHookType::OnRequest);
103
+
104
+ assert_eq!(set.len(), 2);
105
+ }
106
+
107
+ #[test]
108
+ fn test_hook_result_continue() {
109
+ let result = HookResult::Continue;
110
+ match result {
111
+ HookResult::Continue => {}
112
+ HookResult::ShortCircuit(_) => panic!("Expected Continue"),
113
+ }
114
+ }
115
+
116
+ #[test]
117
+ fn test_hook_result_short_circuit() {
118
+ let response = json!({ "status": "error" });
119
+ let result = HookResult::ShortCircuit(response.clone());
120
+ match result {
121
+ HookResult::Continue => panic!("Expected ShortCircuit"),
122
+ HookResult::ShortCircuit(r) => {
123
+ assert_eq!(r, response);
124
+ }
125
+ }
126
+ }
127
+
128
+ #[test]
129
+ fn test_lifecycle_config_new() {
130
+ let config = LifecycleConfig::new();
131
+ assert_eq!(config.get_hooks(LifecycleHookType::OnRequest).len(), 0);
132
+ }
133
+
134
+ #[test]
135
+ fn test_lifecycle_config_default() {
136
+ let config = LifecycleConfig::default();
137
+ assert_eq!(config.get_hooks(LifecycleHookType::OnRequest).len(), 0);
138
+ }
139
+
140
+ #[test]
141
+ fn test_register_single_hook() {
142
+ let mut config = LifecycleConfig::new();
143
+ let hook = Arc::new(TestHook {
144
+ hook_type: LifecycleHookType::OnRequest,
145
+ result: HookResult::Continue,
146
+ });
147
+
148
+ config.register(hook);
149
+
150
+ let hooks = config.get_hooks(LifecycleHookType::OnRequest);
151
+ assert_eq!(hooks.len(), 1);
152
+ }
153
+
154
+ #[test]
155
+ fn test_register_multiple_hooks_same_type() {
156
+ let mut config = LifecycleConfig::new();
157
+
158
+ for i in 0..3 {
159
+ let hook = Arc::new(TestHook {
160
+ hook_type: LifecycleHookType::OnRequest,
161
+ result: if i == 0 {
162
+ HookResult::Continue
163
+ } else {
164
+ HookResult::ShortCircuit(json!({ "index": i }))
165
+ },
166
+ });
167
+ config.register(hook);
168
+ }
169
+
170
+ let hooks = config.get_hooks(LifecycleHookType::OnRequest);
171
+ assert_eq!(hooks.len(), 3);
172
+ }
173
+
174
+ #[test]
175
+ fn test_register_hooks_different_types() {
176
+ let mut config = LifecycleConfig::new();
177
+
178
+ let hook_on_request = Arc::new(TestHook {
179
+ hook_type: LifecycleHookType::OnRequest,
180
+ result: HookResult::Continue,
181
+ });
182
+
183
+ let hook_on_error = Arc::new(TestHook {
184
+ hook_type: LifecycleHookType::OnError,
185
+ result: HookResult::Continue,
186
+ });
187
+
188
+ config.register(hook_on_request);
189
+ config.register(hook_on_error);
190
+
191
+ assert_eq!(config.get_hooks(LifecycleHookType::OnRequest).len(), 1);
192
+ assert_eq!(config.get_hooks(LifecycleHookType::OnError).len(), 1);
193
+ assert_eq!(config.get_hooks(LifecycleHookType::PreHandler).len(), 0);
194
+ }
195
+
196
+ #[test]
197
+ fn test_get_hooks_empty() {
198
+ let config = LifecycleConfig::new();
199
+ let hooks = config.get_hooks(LifecycleHookType::PreValidation);
200
+ assert!(hooks.is_empty());
201
+ }
202
+
203
+ #[test]
204
+ fn test_get_hooks_multiple_calls() {
205
+ let mut config = LifecycleConfig::new();
206
+
207
+ let hook1 = Arc::new(TestHook {
208
+ hook_type: LifecycleHookType::OnResponse,
209
+ result: HookResult::Continue,
210
+ });
211
+
212
+ let hook2 = Arc::new(TestHook {
213
+ hook_type: LifecycleHookType::OnResponse,
214
+ result: HookResult::Continue,
215
+ });
216
+
217
+ config.register(hook1);
218
+ config.register(hook2);
219
+
220
+ let hooks1 = config.get_hooks(LifecycleHookType::OnResponse);
221
+ let hooks2 = config.get_hooks(LifecycleHookType::OnResponse);
222
+
223
+ assert_eq!(hooks1.len(), 2);
224
+ assert_eq!(hooks2.len(), 2);
225
+ }
226
+
227
+ #[test]
228
+ fn test_hook_execute() {
229
+ let hook = TestHook {
230
+ hook_type: LifecycleHookType::OnRequest,
231
+ result: HookResult::Continue,
232
+ };
233
+
234
+ let context = json!({ "test": "data" });
235
+ let result = hook.execute(context);
236
+
237
+ assert!(result.is_ok());
238
+ match result.unwrap() {
239
+ HookResult::Continue => {}
240
+ HookResult::ShortCircuit(_) => panic!("Expected Continue"),
241
+ }
242
+ }
243
+
244
+ #[test]
245
+ fn test_hook_type_retrieval() {
246
+ let hook = TestHook {
247
+ hook_type: LifecycleHookType::PreValidation,
248
+ result: HookResult::Continue,
249
+ };
250
+
251
+ assert_eq!(hook.hook_type(), LifecycleHookType::PreValidation);
252
+ }
253
+
254
+ #[test]
255
+ fn test_all_hook_types() {
256
+ let hook_types = vec![
257
+ LifecycleHookType::OnRequest,
258
+ LifecycleHookType::PreValidation,
259
+ LifecycleHookType::PreHandler,
260
+ LifecycleHookType::OnResponse,
261
+ LifecycleHookType::OnError,
262
+ ];
263
+
264
+ let mut config = LifecycleConfig::new();
265
+
266
+ for hook_type in hook_types.iter() {
267
+ let hook = Arc::new(TestHook {
268
+ hook_type: *hook_type,
269
+ result: HookResult::Continue,
270
+ });
271
+ config.register(hook);
272
+ }
273
+
274
+ for hook_type in hook_types {
275
+ let hooks = config.get_hooks(hook_type);
276
+ assert_eq!(hooks.len(), 1);
277
+ }
278
+ }
279
+
280
+ #[test]
281
+ fn test_hook_result_clone() {
282
+ let original = HookResult::ShortCircuit(json!({ "key": "value" }));
283
+ let cloned = original.clone();
284
+
285
+ match cloned {
286
+ HookResult::ShortCircuit(response) => {
287
+ assert_eq!(response["key"], "value");
288
+ }
289
+ _ => panic!("Expected ShortCircuit"),
290
+ }
291
+ }
292
+ }