spikard 0.4.0-arm64-darwin-23
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,413 @@
|
|
|
1
|
+
//! Integration tests for handler_base module
|
|
2
|
+
//!
|
|
3
|
+
//! These tests cover the validation paths and error handling that aren't
|
|
4
|
+
//! covered by unit tests in the module itself.
|
|
5
|
+
|
|
6
|
+
use axum::body::Body;
|
|
7
|
+
use axum::http::Request;
|
|
8
|
+
use serde_json::json;
|
|
9
|
+
use spikard_bindings_shared::handler_base::{HandlerError, HandlerExecutor, LanguageHandler};
|
|
10
|
+
use spikard_core::parameters::ParameterValidator;
|
|
11
|
+
use spikard_core::validation::{SchemaValidator, ValidationError, ValidationErrorDetail};
|
|
12
|
+
use spikard_http::handler_trait::{Handler, RequestData};
|
|
13
|
+
use std::collections::HashMap;
|
|
14
|
+
use std::future::Future;
|
|
15
|
+
use std::pin::Pin;
|
|
16
|
+
use std::sync::Arc;
|
|
17
|
+
|
|
18
|
+
// Mock handler that returns controlled responses
|
|
19
|
+
struct MockHandler {
|
|
20
|
+
should_fail_prepare: bool,
|
|
21
|
+
should_fail_invoke: bool,
|
|
22
|
+
should_fail_interpret: bool,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
impl LanguageHandler for MockHandler {
|
|
26
|
+
type Input = String;
|
|
27
|
+
type Output = String;
|
|
28
|
+
|
|
29
|
+
fn prepare_request(&self, _data: &RequestData) -> Result<Self::Input, HandlerError> {
|
|
30
|
+
if self.should_fail_prepare {
|
|
31
|
+
Err(HandlerError::Internal("Prepare failed".to_string()))
|
|
32
|
+
} else {
|
|
33
|
+
Ok("prepared".to_string())
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
fn invoke_handler(
|
|
38
|
+
&self,
|
|
39
|
+
input: Self::Input,
|
|
40
|
+
) -> Pin<Box<dyn Future<Output = Result<Self::Output, HandlerError>> + Send + '_>> {
|
|
41
|
+
let should_fail = self.should_fail_invoke;
|
|
42
|
+
Box::pin(async move {
|
|
43
|
+
if should_fail {
|
|
44
|
+
Err(HandlerError::Execution("Handler failed".to_string()))
|
|
45
|
+
} else {
|
|
46
|
+
Ok(format!("output:{}", input))
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
fn interpret_response(&self, output: Self::Output) -> Result<axum::http::Response<Body>, HandlerError> {
|
|
52
|
+
if self.should_fail_interpret {
|
|
53
|
+
Err(HandlerError::ResponseConversion("Interpret failed".to_string()))
|
|
54
|
+
} else {
|
|
55
|
+
Ok(axum::http::Response::builder()
|
|
56
|
+
.status(200)
|
|
57
|
+
.body(Body::from(output))
|
|
58
|
+
.unwrap())
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
#[tokio::test]
|
|
64
|
+
async fn test_handler_executor_with_validation_error() {
|
|
65
|
+
// Create a validator that will reject the request
|
|
66
|
+
let schema = json!({
|
|
67
|
+
"type": "object",
|
|
68
|
+
"properties": {
|
|
69
|
+
"username": {"type": "string"},
|
|
70
|
+
"age": {"type": "number"}
|
|
71
|
+
},
|
|
72
|
+
"required": ["username", "age"]
|
|
73
|
+
});
|
|
74
|
+
let validator = Arc::new(SchemaValidator::new(schema).unwrap());
|
|
75
|
+
|
|
76
|
+
let handler = Arc::new(MockHandler {
|
|
77
|
+
should_fail_prepare: false,
|
|
78
|
+
should_fail_invoke: false,
|
|
79
|
+
should_fail_interpret: false,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
let executor = HandlerExecutor::new(handler, Some(validator), None);
|
|
83
|
+
|
|
84
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
85
|
+
let request_data = RequestData {
|
|
86
|
+
path_params: Arc::new(HashMap::new()),
|
|
87
|
+
query_params: json!({}),
|
|
88
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
89
|
+
// Missing required field 'age'
|
|
90
|
+
body: json!({"username": "john"}),
|
|
91
|
+
raw_body: None,
|
|
92
|
+
headers: Arc::new(HashMap::new()),
|
|
93
|
+
cookies: Arc::new(HashMap::new()),
|
|
94
|
+
method: "POST".to_string(),
|
|
95
|
+
path: "/test".to_string(),
|
|
96
|
+
dependencies: None,
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
let result = executor.call(request, request_data).await;
|
|
100
|
+
assert!(result.is_err());
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
#[tokio::test]
|
|
104
|
+
async fn test_handler_executor_with_parameter_validation() {
|
|
105
|
+
// Create a parameter validator with schema
|
|
106
|
+
let param_schema = json!({
|
|
107
|
+
"type": "object",
|
|
108
|
+
"properties": {
|
|
109
|
+
"page": {"type": "number", "source": "query"}
|
|
110
|
+
},
|
|
111
|
+
"required": ["page"]
|
|
112
|
+
});
|
|
113
|
+
let param_validator = ParameterValidator::new(param_schema).unwrap();
|
|
114
|
+
|
|
115
|
+
let handler = Arc::new(MockHandler {
|
|
116
|
+
should_fail_prepare: false,
|
|
117
|
+
should_fail_invoke: false,
|
|
118
|
+
should_fail_interpret: false,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
let executor = HandlerExecutor::new(handler, None, Some(Arc::new(param_validator)));
|
|
122
|
+
|
|
123
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
124
|
+
let request_data = RequestData {
|
|
125
|
+
path_params: Arc::new(HashMap::new()),
|
|
126
|
+
query_params: json!({}),
|
|
127
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
128
|
+
body: json!({}),
|
|
129
|
+
raw_body: None,
|
|
130
|
+
headers: Arc::new(HashMap::new()),
|
|
131
|
+
cookies: Arc::new(HashMap::new()),
|
|
132
|
+
method: "GET".to_string(),
|
|
133
|
+
path: "/test".to_string(),
|
|
134
|
+
dependencies: None,
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// Missing required query param should fail validation
|
|
138
|
+
let result = executor.call(request, request_data).await;
|
|
139
|
+
assert!(result.is_err());
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
#[tokio::test]
|
|
143
|
+
async fn test_handler_executor_with_valid_parameters() {
|
|
144
|
+
// Create a parameter validator with optional param
|
|
145
|
+
let param_schema = json!({
|
|
146
|
+
"type": "object",
|
|
147
|
+
"properties": {
|
|
148
|
+
"page": {"type": "number", "source": "query"}
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
let param_validator = ParameterValidator::new(param_schema).unwrap();
|
|
152
|
+
|
|
153
|
+
let handler = Arc::new(MockHandler {
|
|
154
|
+
should_fail_prepare: false,
|
|
155
|
+
should_fail_invoke: false,
|
|
156
|
+
should_fail_interpret: false,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
let executor = HandlerExecutor::new(handler, None, Some(Arc::new(param_validator)));
|
|
160
|
+
|
|
161
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
162
|
+
let request_data = RequestData {
|
|
163
|
+
path_params: Arc::new(HashMap::new()),
|
|
164
|
+
query_params: json!({"page": 1}),
|
|
165
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
166
|
+
body: json!({}),
|
|
167
|
+
raw_body: None,
|
|
168
|
+
headers: Arc::new(HashMap::new()),
|
|
169
|
+
cookies: Arc::new(HashMap::new()),
|
|
170
|
+
method: "GET".to_string(),
|
|
171
|
+
path: "/test".to_string(),
|
|
172
|
+
dependencies: None,
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
let result = executor.call(request, request_data).await;
|
|
176
|
+
assert!(result.is_ok());
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
#[tokio::test]
|
|
180
|
+
async fn test_handler_executor_prepare_failure() {
|
|
181
|
+
let handler = Arc::new(MockHandler {
|
|
182
|
+
should_fail_prepare: true,
|
|
183
|
+
should_fail_invoke: false,
|
|
184
|
+
should_fail_interpret: false,
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
let executor = HandlerExecutor::with_handler(handler);
|
|
188
|
+
|
|
189
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
190
|
+
let request_data = RequestData {
|
|
191
|
+
path_params: Arc::new(HashMap::new()),
|
|
192
|
+
query_params: json!({}),
|
|
193
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
194
|
+
body: json!({}),
|
|
195
|
+
raw_body: None,
|
|
196
|
+
headers: Arc::new(HashMap::new()),
|
|
197
|
+
cookies: Arc::new(HashMap::new()),
|
|
198
|
+
method: "GET".to_string(),
|
|
199
|
+
path: "/test".to_string(),
|
|
200
|
+
dependencies: None,
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
let result = executor.call(request, request_data).await;
|
|
204
|
+
assert!(result.is_err());
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
#[tokio::test]
|
|
208
|
+
async fn test_handler_executor_invoke_failure() {
|
|
209
|
+
let handler = Arc::new(MockHandler {
|
|
210
|
+
should_fail_prepare: false,
|
|
211
|
+
should_fail_invoke: true,
|
|
212
|
+
should_fail_interpret: false,
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
let executor = HandlerExecutor::with_handler(handler);
|
|
216
|
+
|
|
217
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
218
|
+
let request_data = RequestData {
|
|
219
|
+
path_params: Arc::new(HashMap::new()),
|
|
220
|
+
query_params: json!({}),
|
|
221
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
222
|
+
body: json!({}),
|
|
223
|
+
raw_body: None,
|
|
224
|
+
headers: Arc::new(HashMap::new()),
|
|
225
|
+
cookies: Arc::new(HashMap::new()),
|
|
226
|
+
method: "GET".to_string(),
|
|
227
|
+
path: "/test".to_string(),
|
|
228
|
+
dependencies: None,
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
let result = executor.call(request, request_data).await;
|
|
232
|
+
assert!(result.is_err());
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
#[tokio::test]
|
|
236
|
+
async fn test_handler_executor_interpret_failure() {
|
|
237
|
+
let handler = Arc::new(MockHandler {
|
|
238
|
+
should_fail_prepare: false,
|
|
239
|
+
should_fail_invoke: false,
|
|
240
|
+
should_fail_interpret: true,
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
let executor = HandlerExecutor::with_handler(handler);
|
|
244
|
+
|
|
245
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
246
|
+
let request_data = RequestData {
|
|
247
|
+
path_params: Arc::new(HashMap::new()),
|
|
248
|
+
query_params: json!({}),
|
|
249
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
250
|
+
body: json!({}),
|
|
251
|
+
raw_body: None,
|
|
252
|
+
headers: Arc::new(HashMap::new()),
|
|
253
|
+
cookies: Arc::new(HashMap::new()),
|
|
254
|
+
method: "GET".to_string(),
|
|
255
|
+
path: "/test".to_string(),
|
|
256
|
+
dependencies: None,
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
let result = executor.call(request, request_data).await;
|
|
260
|
+
assert!(result.is_err());
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
#[tokio::test]
|
|
264
|
+
async fn test_handler_executor_with_both_validators() {
|
|
265
|
+
// Create both request and parameter validators
|
|
266
|
+
let schema = json!({
|
|
267
|
+
"type": "object",
|
|
268
|
+
"properties": {
|
|
269
|
+
"name": {"type": "string"}
|
|
270
|
+
},
|
|
271
|
+
"required": ["name"]
|
|
272
|
+
});
|
|
273
|
+
let request_validator = Arc::new(SchemaValidator::new(schema).unwrap());
|
|
274
|
+
|
|
275
|
+
let param_schema = json!({
|
|
276
|
+
"type": "object",
|
|
277
|
+
"properties": {
|
|
278
|
+
"x-api-key": {"type": "string", "source": "header"}
|
|
279
|
+
},
|
|
280
|
+
"required": ["x-api-key"]
|
|
281
|
+
});
|
|
282
|
+
let param_validator = ParameterValidator::new(param_schema).unwrap();
|
|
283
|
+
|
|
284
|
+
let handler = Arc::new(MockHandler {
|
|
285
|
+
should_fail_prepare: false,
|
|
286
|
+
should_fail_invoke: false,
|
|
287
|
+
should_fail_interpret: false,
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
let executor = HandlerExecutor::new(handler, Some(request_validator), Some(Arc::new(param_validator)));
|
|
291
|
+
|
|
292
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
293
|
+
|
|
294
|
+
// Valid request with all requirements
|
|
295
|
+
let mut headers = HashMap::new();
|
|
296
|
+
headers.insert("x-api-key".to_string(), "test-key".to_string());
|
|
297
|
+
|
|
298
|
+
let request_data = RequestData {
|
|
299
|
+
path_params: Arc::new(HashMap::new()),
|
|
300
|
+
query_params: json!({}),
|
|
301
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
302
|
+
body: json!({"name": "test"}),
|
|
303
|
+
raw_body: None,
|
|
304
|
+
headers: Arc::new(headers),
|
|
305
|
+
cookies: Arc::new(HashMap::new()),
|
|
306
|
+
method: "POST".to_string(),
|
|
307
|
+
path: "/test".to_string(),
|
|
308
|
+
dependencies: None,
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
let result = executor.call(request, request_data).await;
|
|
312
|
+
assert!(result.is_ok());
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
#[tokio::test]
|
|
316
|
+
async fn test_handler_executor_parameter_validation_failure() {
|
|
317
|
+
let param_schema = json!({
|
|
318
|
+
"type": "object",
|
|
319
|
+
"properties": {
|
|
320
|
+
"authorization": {"type": "string", "source": "header"}
|
|
321
|
+
},
|
|
322
|
+
"required": ["authorization"]
|
|
323
|
+
});
|
|
324
|
+
let param_validator = ParameterValidator::new(param_schema).unwrap();
|
|
325
|
+
|
|
326
|
+
let handler = Arc::new(MockHandler {
|
|
327
|
+
should_fail_prepare: false,
|
|
328
|
+
should_fail_invoke: false,
|
|
329
|
+
should_fail_interpret: false,
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
let executor = HandlerExecutor::with_handler(handler).with_parameter_validator(Arc::new(param_validator));
|
|
333
|
+
|
|
334
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
335
|
+
let request_data = RequestData {
|
|
336
|
+
path_params: Arc::new(HashMap::new()),
|
|
337
|
+
query_params: json!({}),
|
|
338
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
339
|
+
body: json!({}),
|
|
340
|
+
raw_body: None,
|
|
341
|
+
headers: Arc::new(HashMap::new()), // Missing required header
|
|
342
|
+
cookies: Arc::new(HashMap::new()),
|
|
343
|
+
method: "GET".to_string(),
|
|
344
|
+
path: "/test".to_string(),
|
|
345
|
+
dependencies: None,
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
let result = executor.call(request, request_data).await;
|
|
349
|
+
assert!(result.is_err());
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
#[test]
|
|
353
|
+
fn test_handler_error_from_validation_error() {
|
|
354
|
+
let validation_error = ValidationError {
|
|
355
|
+
errors: vec![ValidationErrorDetail {
|
|
356
|
+
error_type: "missing".to_string(),
|
|
357
|
+
loc: vec!["body".to_string(), "field".to_string()],
|
|
358
|
+
msg: "Field required".to_string(),
|
|
359
|
+
input: json!(null),
|
|
360
|
+
ctx: None,
|
|
361
|
+
}],
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
let handler_error: HandlerError = validation_error.into();
|
|
365
|
+
assert!(matches!(handler_error, HandlerError::Validation(_)));
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
#[tokio::test]
|
|
369
|
+
async fn test_handler_executor_builder_pattern() {
|
|
370
|
+
let schema = json!({
|
|
371
|
+
"type": "object",
|
|
372
|
+
"properties": {
|
|
373
|
+
"email": {"type": "string", "format": "email"}
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
let request_validator = Arc::new(SchemaValidator::new(schema).unwrap());
|
|
377
|
+
|
|
378
|
+
let param_schema = json!({
|
|
379
|
+
"type": "object",
|
|
380
|
+
"properties": {
|
|
381
|
+
"session_id": {"type": "string", "source": "cookie"}
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
let param_validator = ParameterValidator::new(param_schema).unwrap();
|
|
385
|
+
|
|
386
|
+
let handler = Arc::new(MockHandler {
|
|
387
|
+
should_fail_prepare: false,
|
|
388
|
+
should_fail_invoke: false,
|
|
389
|
+
should_fail_interpret: false,
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
// Test builder pattern chaining
|
|
393
|
+
let executor = HandlerExecutor::with_handler(handler)
|
|
394
|
+
.with_request_validator(request_validator)
|
|
395
|
+
.with_parameter_validator(Arc::new(param_validator));
|
|
396
|
+
|
|
397
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
398
|
+
let request_data = RequestData {
|
|
399
|
+
path_params: Arc::new(HashMap::new()),
|
|
400
|
+
query_params: json!({}),
|
|
401
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
402
|
+
body: json!({"email": "test@example.com"}),
|
|
403
|
+
raw_body: None,
|
|
404
|
+
headers: Arc::new(HashMap::new()),
|
|
405
|
+
cookies: Arc::new(HashMap::new()),
|
|
406
|
+
method: "POST".to_string(),
|
|
407
|
+
path: "/test".to_string(),
|
|
408
|
+
dependencies: None,
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
let result = executor.call(request, request_data).await;
|
|
412
|
+
assert!(result.is_ok());
|
|
413
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "spikard-core"
|
|
3
|
+
version = "0.4.0"
|
|
4
|
+
edition = "2024"
|
|
5
|
+
authors = ["Na'aman Hirschfeld <nhirschfeld@gmail.com>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
repository = "https://github.com/Goldziher/spikard"
|
|
8
|
+
homepage = "https://github.com/Goldziher/spikard"
|
|
9
|
+
description = "Shared transport-agnostic primitives for Spikard runtimes"
|
|
10
|
+
keywords = ["http", "web", "framework", "validation", "middleware"]
|
|
11
|
+
categories = ["web-programming::http-server", "web-programming", "development-tools"]
|
|
12
|
+
documentation = "https://docs.rs/spikard-core"
|
|
13
|
+
readme = "README.md"
|
|
14
|
+
|
|
15
|
+
[dependencies]
|
|
16
|
+
serde = { version = "1.0", features = ["derive"] }
|
|
17
|
+
serde_json = "1.0"
|
|
18
|
+
tracing = "0.1"
|
|
19
|
+
anyhow = "1.0"
|
|
20
|
+
jsonschema = { version = "0.37", default-features = false }
|
|
21
|
+
regex = "1"
|
|
22
|
+
flate2 = { version = "=1.1.5", default-features = false, features = ["rust_backend"] }
|
|
23
|
+
brotli = "8.0"
|
|
24
|
+
http = "1.4"
|
|
25
|
+
base64 = "0.22"
|
|
26
|
+
serde_qs = "0.15"
|
|
27
|
+
url = "2.5"
|
|
28
|
+
jiff = "0.2"
|
|
29
|
+
uuid = "1.19"
|
|
30
|
+
indexmap = "2.12"
|
|
31
|
+
tokio = { version = "1", features = ["full"], optional = true }
|
|
32
|
+
bytes = { version = "1.11", optional = true }
|
|
33
|
+
thiserror = { version = "2.0", optional = true }
|
|
34
|
+
|
|
35
|
+
[features]
|
|
36
|
+
default = []
|
|
37
|
+
di = ["dep:tokio", "dep:bytes", "dep:thiserror"]
|
|
38
|
+
|
|
39
|
+
[dev-dependencies]
|
|
40
|
+
tokio-test = "0.4"
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
use crate::CompressionConfig;
|
|
2
|
+
use brotli::CompressorWriter;
|
|
3
|
+
use flate2::Compression;
|
|
4
|
+
use flate2::write::GzEncoder;
|
|
5
|
+
use std::collections::HashMap;
|
|
6
|
+
use std::io::Write;
|
|
7
|
+
|
|
8
|
+
/// Minimal response container shared by bindings.
|
|
9
|
+
#[derive(Clone, Debug)]
|
|
10
|
+
pub struct RawResponse {
|
|
11
|
+
pub status: u16,
|
|
12
|
+
pub headers: HashMap<String, String>,
|
|
13
|
+
pub body: Vec<u8>,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
impl RawResponse {
|
|
17
|
+
/// Construct a new response.
|
|
18
|
+
pub fn new(status: u16, headers: HashMap<String, String>, body: Vec<u8>) -> Self {
|
|
19
|
+
Self { status, headers, body }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/// Apply compression filters if the response qualifies.
|
|
23
|
+
pub fn apply_compression(&mut self, request_headers: &HashMap<String, String>, compression: &CompressionConfig) {
|
|
24
|
+
if self.body.is_empty() || self.status == 206 {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if self
|
|
28
|
+
.headers
|
|
29
|
+
.keys()
|
|
30
|
+
.any(|key| key.eq_ignore_ascii_case("content-encoding"))
|
|
31
|
+
{
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if self.body.len() < compression.min_size {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let accept_encoding = header_value(request_headers, "Accept-Encoding").map(|value| value.to_ascii_lowercase());
|
|
39
|
+
let accepts_brotli = accept_encoding
|
|
40
|
+
.as_ref()
|
|
41
|
+
.map(|value| value.contains("br"))
|
|
42
|
+
.unwrap_or(false);
|
|
43
|
+
if compression.brotli && accepts_brotli && self.try_compress_brotli(compression) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let accepts_gzip = accept_encoding
|
|
48
|
+
.as_ref()
|
|
49
|
+
.map(|value| value.contains("gzip"))
|
|
50
|
+
.unwrap_or(false);
|
|
51
|
+
if compression.gzip && accepts_gzip {
|
|
52
|
+
self.try_compress_gzip(compression);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
fn try_compress_brotli(&mut self, compression: &CompressionConfig) -> bool {
|
|
57
|
+
let quality = compression.quality.min(11);
|
|
58
|
+
let mut writer = CompressorWriter::new(Vec::new(), 4096, quality, 22);
|
|
59
|
+
if writer.write_all(&self.body).is_err() || writer.flush().is_err() {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
let compressed = writer.into_inner();
|
|
63
|
+
if compressed.is_empty() {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
self.finalize_encoded_body("br", compressed);
|
|
67
|
+
true
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
fn try_compress_gzip(&mut self, compression: &CompressionConfig) -> bool {
|
|
71
|
+
let mut encoder = GzEncoder::new(Vec::new(), Compression::new(compression.quality));
|
|
72
|
+
if encoder.write_all(&self.body).is_err() {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
let compressed = encoder.finish().unwrap_or_else(|_| Vec::new());
|
|
76
|
+
if compressed.is_empty() {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
self.finalize_encoded_body("gzip", compressed);
|
|
80
|
+
true
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
fn finalize_encoded_body(&mut self, encoding: &str, compressed: Vec<u8>) {
|
|
84
|
+
self.body = compressed;
|
|
85
|
+
self.headers
|
|
86
|
+
.insert("content-encoding".to_string(), encoding.to_string());
|
|
87
|
+
self.headers.insert("vary".to_string(), "Accept-Encoding".to_string());
|
|
88
|
+
self.headers
|
|
89
|
+
.insert("content-length".to_string(), self.body.len().to_string());
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
fn header_value<'a>(headers: &'a HashMap<String, String>, name: &str) -> Option<&'a str> {
|
|
94
|
+
headers.iter().find_map(|(key, value)| {
|
|
95
|
+
if key.eq_ignore_ascii_case(name) {
|
|
96
|
+
Some(value.as_str())
|
|
97
|
+
} else {
|
|
98
|
+
None
|
|
99
|
+
}
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/// Pre-rendered static asset produced by the CLI bundler.
|
|
104
|
+
#[derive(Clone, Debug)]
|
|
105
|
+
pub struct StaticAsset {
|
|
106
|
+
pub route: String,
|
|
107
|
+
pub headers: HashMap<String, String>,
|
|
108
|
+
pub body: Vec<u8>,
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
impl StaticAsset {
|
|
112
|
+
/// Build a response snapshot if the incoming request targets this asset.
|
|
113
|
+
pub fn serve(&self, method: &str, normalized_path: &str) -> Option<RawResponse> {
|
|
114
|
+
if !method.eq_ignore_ascii_case("GET") && !method.eq_ignore_ascii_case("HEAD") {
|
|
115
|
+
return None;
|
|
116
|
+
}
|
|
117
|
+
if self.route != normalized_path {
|
|
118
|
+
return None;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let mut headers = self.headers.clone();
|
|
122
|
+
headers
|
|
123
|
+
.entry("content-length".to_string())
|
|
124
|
+
.or_insert_with(|| self.body.len().to_string());
|
|
125
|
+
let body = if method.eq_ignore_ascii_case("HEAD") {
|
|
126
|
+
Vec::new()
|
|
127
|
+
} else {
|
|
128
|
+
self.body.clone()
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
Some(RawResponse::new(200, headers, body))
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
//! Debug logging utilities for spikard-http
|
|
2
|
+
//!
|
|
3
|
+
//! This module provides debug logging that can be enabled via:
|
|
4
|
+
//! - Building in debug mode (cfg(debug_assertions))
|
|
5
|
+
//! - Setting SPIKARD_DEBUG=1 environment variable
|
|
6
|
+
|
|
7
|
+
use std::sync::atomic::{AtomicBool, Ordering};
|
|
8
|
+
|
|
9
|
+
static DEBUG_ENABLED: AtomicBool = AtomicBool::new(false);
|
|
10
|
+
|
|
11
|
+
/// Initialize debug logging based on environment and build mode
|
|
12
|
+
pub fn init() {
|
|
13
|
+
let enabled = cfg!(debug_assertions) || std::env::var("SPIKARD_DEBUG").is_ok() || std::env::var("DEBUG").is_ok();
|
|
14
|
+
|
|
15
|
+
eprintln!(
|
|
16
|
+
"[spikard-http::debug] init() called, cfg!(debug_assertions)={}, DEBUG={}, enabled={}",
|
|
17
|
+
cfg!(debug_assertions),
|
|
18
|
+
std::env::var("DEBUG").is_ok(),
|
|
19
|
+
enabled
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
DEBUG_ENABLED.store(enabled, Ordering::Relaxed);
|
|
23
|
+
|
|
24
|
+
if enabled {
|
|
25
|
+
eprintln!("[spikard-http] Debug logging enabled");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// Check if debug logging is enabled
|
|
30
|
+
#[inline]
|
|
31
|
+
pub fn is_enabled() -> bool {
|
|
32
|
+
DEBUG_ENABLED.load(Ordering::Relaxed)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// Log a debug message if debugging is enabled
|
|
36
|
+
#[macro_export]
|
|
37
|
+
macro_rules! debug_log {
|
|
38
|
+
($($arg:tt)*) => {
|
|
39
|
+
if $crate::debug::is_enabled() {
|
|
40
|
+
eprintln!("[spikard-http] {}", format!($($arg)*));
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// Log a debug message with a specific module/component name
|
|
46
|
+
#[macro_export]
|
|
47
|
+
macro_rules! debug_log_module {
|
|
48
|
+
($module:expr, $($arg:tt)*) => {
|
|
49
|
+
if $crate::debug::is_enabled() {
|
|
50
|
+
eprintln!("[spikard-http::{}] {}", $module, format!($($arg)*));
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// Log a debug value with pretty-printing
|
|
56
|
+
#[macro_export]
|
|
57
|
+
macro_rules! debug_log_value {
|
|
58
|
+
($name:expr, $value:expr) => {
|
|
59
|
+
if $crate::debug::is_enabled() {
|
|
60
|
+
eprintln!("[spikard-http] {} = {:?}", $name, $value);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|