spikard 0.8.3 → 0.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +19 -10
- data/ext/spikard_rb/Cargo.lock +234 -162
- data/ext/spikard_rb/Cargo.toml +2 -2
- data/ext/spikard_rb/extconf.rb +4 -3
- data/lib/spikard/config.rb +88 -12
- data/lib/spikard/testing.rb +3 -1
- data/lib/spikard/version.rb +1 -1
- data/lib/spikard.rb +11 -0
- data/vendor/crates/spikard-bindings-shared/Cargo.toml +3 -6
- data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +8 -8
- data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +2 -2
- data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +4 -4
- data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +10 -4
- data/vendor/crates/spikard-bindings-shared/src/error_response.rs +3 -3
- data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +10 -5
- data/vendor/crates/spikard-bindings-shared/src/json_conversion.rs +829 -0
- data/vendor/crates/spikard-bindings-shared/src/lazy_cache.rs +587 -0
- data/vendor/crates/spikard-bindings-shared/src/lib.rs +7 -0
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +11 -11
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +9 -37
- data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +436 -3
- data/vendor/crates/spikard-bindings-shared/src/response_interpreter.rs +944 -0
- data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +4 -4
- data/vendor/crates/spikard-bindings-shared/tests/config_extractor_behavior.rs +3 -2
- data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +13 -13
- data/vendor/crates/spikard-bindings-shared/tests/{comprehensive_coverage.rs → full_coverage.rs} +10 -5
- data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +14 -14
- data/vendor/crates/spikard-bindings-shared/tests/integration_tests.rs +669 -0
- data/vendor/crates/spikard-core/Cargo.toml +3 -3
- data/vendor/crates/spikard-core/src/di/container.rs +1 -1
- data/vendor/crates/spikard-core/src/di/factory.rs +2 -2
- data/vendor/crates/spikard-core/src/di/resolved.rs +2 -2
- data/vendor/crates/spikard-core/src/di/value.rs +1 -1
- data/vendor/crates/spikard-core/src/http.rs +75 -0
- data/vendor/crates/spikard-core/src/lifecycle.rs +43 -43
- data/vendor/crates/spikard-core/src/parameters.rs +14 -19
- data/vendor/crates/spikard-core/src/problem.rs +1 -1
- data/vendor/crates/spikard-core/src/request_data.rs +7 -16
- data/vendor/crates/spikard-core/src/router.rs +6 -0
- data/vendor/crates/spikard-core/src/schema_registry.rs +2 -3
- data/vendor/crates/spikard-core/src/type_hints.rs +3 -2
- data/vendor/crates/spikard-core/src/validation/error_mapper.rs +1 -1
- data/vendor/crates/spikard-core/src/validation/mod.rs +1 -1
- data/vendor/crates/spikard-core/tests/di_dependency_defaults.rs +1 -1
- data/vendor/crates/spikard-core/tests/error_mapper.rs +2 -2
- data/vendor/crates/spikard-core/tests/parameters_edge_cases.rs +1 -1
- data/vendor/crates/spikard-core/tests/parameters_full.rs +1 -1
- data/vendor/crates/spikard-core/tests/parameters_schema_and_formats.rs +1 -1
- data/vendor/crates/spikard-core/tests/validation_coverage.rs +4 -4
- data/vendor/crates/spikard-http/Cargo.toml +4 -2
- data/vendor/crates/spikard-http/src/cors.rs +32 -11
- data/vendor/crates/spikard-http/src/di_handler.rs +12 -8
- data/vendor/crates/spikard-http/src/grpc/framing.rs +469 -0
- data/vendor/crates/spikard-http/src/grpc/handler.rs +887 -25
- data/vendor/crates/spikard-http/src/grpc/mod.rs +114 -22
- data/vendor/crates/spikard-http/src/grpc/service.rs +232 -2
- data/vendor/crates/spikard-http/src/grpc/streaming.rs +80 -2
- data/vendor/crates/spikard-http/src/handler_trait.rs +204 -27
- data/vendor/crates/spikard-http/src/handler_trait_tests.rs +15 -15
- data/vendor/crates/spikard-http/src/jsonrpc/http_handler.rs +2 -2
- data/vendor/crates/spikard-http/src/jsonrpc/router.rs +2 -2
- data/vendor/crates/spikard-http/src/lib.rs +1 -1
- data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +2 -2
- data/vendor/crates/spikard-http/src/lifecycle.rs +4 -4
- data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +2 -0
- data/vendor/crates/spikard-http/src/server/fast_router.rs +186 -0
- data/vendor/crates/spikard-http/src/server/grpc_routing.rs +324 -23
- data/vendor/crates/spikard-http/src/server/handler.rs +33 -22
- data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +21 -2
- data/vendor/crates/spikard-http/src/server/mod.rs +125 -20
- data/vendor/crates/spikard-http/src/server/request_extraction.rs +126 -44
- data/vendor/crates/spikard-http/src/server/routing_factory.rs +80 -69
- data/vendor/crates/spikard-http/tests/common/handlers.rs +2 -2
- data/vendor/crates/spikard-http/tests/common/test_builders.rs +12 -12
- data/vendor/crates/spikard-http/tests/di_handler_error_responses.rs +2 -2
- data/vendor/crates/spikard-http/tests/di_integration.rs +6 -6
- data/vendor/crates/spikard-http/tests/grpc_bidirectional_streaming.rs +430 -0
- data/vendor/crates/spikard-http/tests/grpc_client_streaming.rs +738 -0
- data/vendor/crates/spikard-http/tests/grpc_integration_test.rs +13 -9
- data/vendor/crates/spikard-http/tests/grpc_server_streaming.rs +974 -0
- data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +2 -2
- data/vendor/crates/spikard-http/tests/request_extraction_full.rs +4 -4
- data/vendor/crates/spikard-http/tests/server_config_builder.rs +2 -2
- data/vendor/crates/spikard-http/tests/server_cors_preflight.rs +1 -0
- data/vendor/crates/spikard-http/tests/server_openapi_jsonrpc_static.rs +140 -0
- data/vendor/crates/spikard-rb/Cargo.toml +3 -1
- data/vendor/crates/spikard-rb/src/conversion.rs +138 -4
- data/vendor/crates/spikard-rb/src/grpc/handler.rs +706 -229
- data/vendor/crates/spikard-rb/src/grpc/mod.rs +6 -2
- data/vendor/crates/spikard-rb/src/gvl.rs +2 -2
- data/vendor/crates/spikard-rb/src/handler.rs +169 -91
- data/vendor/crates/spikard-rb/src/lib.rs +444 -62
- data/vendor/crates/spikard-rb/src/lifecycle.rs +29 -1
- data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +108 -43
- data/vendor/crates/spikard-rb/src/request.rs +117 -20
- data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +52 -25
- data/vendor/crates/spikard-rb/src/server.rs +23 -14
- data/vendor/crates/spikard-rb/src/testing/client.rs +5 -4
- data/vendor/crates/spikard-rb/src/testing/sse.rs +1 -36
- data/vendor/crates/spikard-rb/src/testing/websocket.rs +3 -38
- data/vendor/crates/spikard-rb/src/websocket.rs +32 -23
- data/vendor/crates/spikard-rb-macros/Cargo.toml +1 -1
- metadata +14 -4
- data/vendor/bundle/ruby/3.4.0/gems/diff-lcs-1.6.2/mise.toml +0 -5
- data/vendor/bundle/ruby/3.4.0/gems/rake-compiler-dock-1.10.0/build/buildkitd.toml +0 -2
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+
//! Full integration tests for all spikard-bindings-shared modules
|
|
2
|
+
//!
|
|
3
|
+
//! This test file ensures full code coverage across all modules in the crate,
|
|
4
|
+
//! testing edge cases, error paths, and integration scenarios.
|
|
5
|
+
|
|
6
|
+
use axum::http::{Request, StatusCode};
|
|
7
|
+
use pretty_assertions::assert_eq;
|
|
8
|
+
use serde_json::json;
|
|
9
|
+
use spikard_bindings_shared::conversion_traits::{FromLanguage, ToLanguage};
|
|
10
|
+
use spikard_bindings_shared::response_builder::ResponseBuilder;
|
|
11
|
+
use spikard_bindings_shared::*;
|
|
12
|
+
use spikard_core::RequestData as CoreRequestData;
|
|
13
|
+
use spikard_core::di::{Dependency, ResolvedDependencies};
|
|
14
|
+
use spikard_core::problem::ProblemDetails;
|
|
15
|
+
use spikard_core::validation::{ValidationError, ValidationErrorDetail};
|
|
16
|
+
use std::collections::HashMap;
|
|
17
|
+
use std::sync::Arc;
|
|
18
|
+
|
|
19
|
+
#[derive(Debug)]
|
|
20
|
+
struct CustomType {
|
|
21
|
+
value: i32,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
impl FromLanguage for CustomType {
|
|
25
|
+
type Error = String;
|
|
26
|
+
|
|
27
|
+
fn from_any(value: &(dyn std::any::Any + Send + Sync)) -> Result<Self, Self::Error> {
|
|
28
|
+
value
|
|
29
|
+
.downcast_ref::<i32>()
|
|
30
|
+
.map(|&v| Self { value: v })
|
|
31
|
+
.ok_or_else(|| "Type mismatch".to_string())
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
impl ToLanguage for CustomType {
|
|
36
|
+
type Error = String;
|
|
37
|
+
|
|
38
|
+
fn to_any(&self) -> Result<Box<dyn std::any::Any + Send + Sync>, Self::Error> {
|
|
39
|
+
Ok(Box::new(self.value))
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
#[test]
|
|
44
|
+
fn test_error_response_all_status_codes_coverage() {
|
|
45
|
+
let test_cases = vec![
|
|
46
|
+
(
|
|
47
|
+
ErrorResponseBuilder::bad_request("msg"),
|
|
48
|
+
StatusCode::BAD_REQUEST,
|
|
49
|
+
"bad_request",
|
|
50
|
+
),
|
|
51
|
+
(
|
|
52
|
+
ErrorResponseBuilder::internal_error("msg"),
|
|
53
|
+
StatusCode::INTERNAL_SERVER_ERROR,
|
|
54
|
+
"internal_error",
|
|
55
|
+
),
|
|
56
|
+
(
|
|
57
|
+
ErrorResponseBuilder::unauthorized("msg"),
|
|
58
|
+
StatusCode::UNAUTHORIZED,
|
|
59
|
+
"unauthorized",
|
|
60
|
+
),
|
|
61
|
+
(
|
|
62
|
+
ErrorResponseBuilder::forbidden("msg"),
|
|
63
|
+
StatusCode::FORBIDDEN,
|
|
64
|
+
"forbidden",
|
|
65
|
+
),
|
|
66
|
+
(
|
|
67
|
+
ErrorResponseBuilder::not_found("msg"),
|
|
68
|
+
StatusCode::NOT_FOUND,
|
|
69
|
+
"not_found",
|
|
70
|
+
),
|
|
71
|
+
(
|
|
72
|
+
ErrorResponseBuilder::method_not_allowed("msg"),
|
|
73
|
+
StatusCode::METHOD_NOT_ALLOWED,
|
|
74
|
+
"method_not_allowed",
|
|
75
|
+
),
|
|
76
|
+
(
|
|
77
|
+
ErrorResponseBuilder::unprocessable_entity("msg"),
|
|
78
|
+
StatusCode::UNPROCESSABLE_ENTITY,
|
|
79
|
+
"unprocessable_entity",
|
|
80
|
+
),
|
|
81
|
+
(ErrorResponseBuilder::conflict("msg"), StatusCode::CONFLICT, "conflict"),
|
|
82
|
+
(
|
|
83
|
+
ErrorResponseBuilder::service_unavailable("msg"),
|
|
84
|
+
StatusCode::SERVICE_UNAVAILABLE,
|
|
85
|
+
"service_unavailable",
|
|
86
|
+
),
|
|
87
|
+
(
|
|
88
|
+
ErrorResponseBuilder::request_timeout("msg"),
|
|
89
|
+
StatusCode::REQUEST_TIMEOUT,
|
|
90
|
+
"request_timeout",
|
|
91
|
+
),
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
for ((status, body), expected_status, expected_code) in test_cases {
|
|
95
|
+
assert_eq!(status, expected_status);
|
|
96
|
+
let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
|
|
97
|
+
assert_eq!(parsed["code"], expected_code);
|
|
98
|
+
assert_eq!(parsed["error"], "msg");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
#[test]
|
|
103
|
+
fn test_validation_error_with_multiple_errors() {
|
|
104
|
+
let validation_error = ValidationError {
|
|
105
|
+
errors: vec![
|
|
106
|
+
ValidationErrorDetail {
|
|
107
|
+
error_type: "missing".to_string(),
|
|
108
|
+
loc: vec!["body".to_string(), "field1".to_string()],
|
|
109
|
+
msg: "Field required".to_string(),
|
|
110
|
+
input: serde_json::Value::Null,
|
|
111
|
+
ctx: None,
|
|
112
|
+
},
|
|
113
|
+
ValidationErrorDetail {
|
|
114
|
+
error_type: "type_error".to_string(),
|
|
115
|
+
loc: vec!["body".to_string(), "field2".to_string()],
|
|
116
|
+
msg: "Invalid type".to_string(),
|
|
117
|
+
input: json!("wrong"),
|
|
118
|
+
ctx: Some(json!({"expected": "number"})),
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
let (status, body) = ErrorResponseBuilder::validation_error(&validation_error);
|
|
124
|
+
assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY);
|
|
125
|
+
|
|
126
|
+
let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
|
|
127
|
+
assert_eq!(parsed["status"], 422);
|
|
128
|
+
assert_eq!(parsed["errors"].as_array().unwrap().len(), 2);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
#[test]
|
|
132
|
+
fn test_problem_details_with_extensions() {
|
|
133
|
+
let mut problem = ProblemDetails::internal_server_error("System error");
|
|
134
|
+
problem.instance = Some("/api/users/123".to_string());
|
|
135
|
+
problem.extensions.insert("trace_id".to_string(), json!("abc-123"));
|
|
136
|
+
problem.extensions.insert("retry_after".to_string(), json!(60));
|
|
137
|
+
|
|
138
|
+
let (status, body) = ErrorResponseBuilder::problem_details_response(&problem);
|
|
139
|
+
assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
|
|
140
|
+
|
|
141
|
+
let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
|
|
142
|
+
assert_eq!(parsed["status"], 500);
|
|
143
|
+
assert_eq!(parsed["instance"], "/api/users/123");
|
|
144
|
+
assert_eq!(parsed["trace_id"], "abc-123");
|
|
145
|
+
assert_eq!(parsed["retry_after"], 60);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
#[test]
|
|
149
|
+
fn test_structured_error_with_complex_details() {
|
|
150
|
+
let details = json!({
|
|
151
|
+
"validation_errors": [
|
|
152
|
+
{"field": "email", "code": "invalid_format"},
|
|
153
|
+
{"field": "age", "code": "out_of_range"}
|
|
154
|
+
],
|
|
155
|
+
"metadata": {
|
|
156
|
+
"request_id": "req-12345",
|
|
157
|
+
"timestamp": "2024-01-01T00:00:00Z"
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
let (status, body) = ErrorResponseBuilder::with_details(
|
|
162
|
+
StatusCode::BAD_REQUEST,
|
|
163
|
+
"validation_failed",
|
|
164
|
+
"Multiple validation errors",
|
|
165
|
+
details,
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
assert_eq!(status, StatusCode::BAD_REQUEST);
|
|
169
|
+
let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
|
|
170
|
+
assert_eq!(parsed["code"], "validation_failed");
|
|
171
|
+
assert!(parsed["details"]["validation_errors"].is_array());
|
|
172
|
+
assert_eq!(parsed["details"]["metadata"]["request_id"], "req-12345");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
#[test]
|
|
176
|
+
fn test_response_builder_with_multiple_headers() {
|
|
177
|
+
let (status, headers, body) = ResponseBuilder::new()
|
|
178
|
+
.status(StatusCode::CREATED)
|
|
179
|
+
.body(json!({
|
|
180
|
+
"id": 123,
|
|
181
|
+
"name": "Test",
|
|
182
|
+
"tags": ["tag1", "tag2"],
|
|
183
|
+
"metadata": {
|
|
184
|
+
"created": "2024-01-01"
|
|
185
|
+
}
|
|
186
|
+
}))
|
|
187
|
+
.header("Content-Type", "application/json")
|
|
188
|
+
.header("X-Request-Id", "req-123")
|
|
189
|
+
.header("X-Custom", "value")
|
|
190
|
+
.build();
|
|
191
|
+
|
|
192
|
+
assert_eq!(status, StatusCode::CREATED);
|
|
193
|
+
assert_eq!(headers.len(), 3);
|
|
194
|
+
|
|
195
|
+
let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
|
|
196
|
+
assert_eq!(parsed["id"], 123);
|
|
197
|
+
assert_eq!(parsed["tags"][0], "tag1");
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
#[test]
|
|
201
|
+
fn test_response_builder_invalid_headers() {
|
|
202
|
+
let (_, headers, _) = ResponseBuilder::new()
|
|
203
|
+
.header("Invalid\nHeader", "value1")
|
|
204
|
+
.header("Valid-Header", "value2")
|
|
205
|
+
.header("Another\r\nInvalid", "value3")
|
|
206
|
+
.build();
|
|
207
|
+
|
|
208
|
+
assert_eq!(headers.len(), 1);
|
|
209
|
+
assert!(headers.get("valid-header").is_some());
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
#[test]
|
|
213
|
+
fn test_lifecycle_hook_types() {
|
|
214
|
+
use lifecycle_base::{HookResult, LifecycleHookType};
|
|
215
|
+
|
|
216
|
+
let hook_types = vec![
|
|
217
|
+
LifecycleHookType::OnRequest,
|
|
218
|
+
LifecycleHookType::PreValidation,
|
|
219
|
+
LifecycleHookType::PreHandler,
|
|
220
|
+
LifecycleHookType::OnResponse,
|
|
221
|
+
LifecycleHookType::OnError,
|
|
222
|
+
];
|
|
223
|
+
|
|
224
|
+
for hook_type in &hook_types {
|
|
225
|
+
assert_eq!(*hook_type, *hook_type);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
let continue_result = HookResult::Continue;
|
|
229
|
+
let cloned_continue = continue_result;
|
|
230
|
+
assert!(matches!(cloned_continue, HookResult::Continue));
|
|
231
|
+
|
|
232
|
+
let short_circuit = HookResult::ShortCircuit(json!({"status": "error"}));
|
|
233
|
+
let cloned_short = short_circuit;
|
|
234
|
+
assert!(matches!(cloned_short, HookResult::ShortCircuit(_)));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
#[test]
|
|
238
|
+
fn test_validation_helpers_all_field_types() {
|
|
239
|
+
use validation_helpers::{BodyValidator, FieldType, HeaderFormat, HeaderValidator};
|
|
240
|
+
|
|
241
|
+
let body = json!({
|
|
242
|
+
"string_field": "text",
|
|
243
|
+
"number_field": 42,
|
|
244
|
+
"boolean_field": true,
|
|
245
|
+
"object_field": {"key": "value"},
|
|
246
|
+
"array_field": [1, 2, 3],
|
|
247
|
+
"null_field": null
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
assert!(BodyValidator::validate_field_type(&body, "string_field", FieldType::String).is_ok());
|
|
251
|
+
assert!(BodyValidator::validate_field_type(&body, "number_field", FieldType::Number).is_ok());
|
|
252
|
+
assert!(BodyValidator::validate_field_type(&body, "boolean_field", FieldType::Boolean).is_ok());
|
|
253
|
+
assert!(BodyValidator::validate_field_type(&body, "object_field", FieldType::Object).is_ok());
|
|
254
|
+
assert!(BodyValidator::validate_field_type(&body, "array_field", FieldType::Array).is_ok());
|
|
255
|
+
|
|
256
|
+
assert!(BodyValidator::validate_field_type(&body, "null_field", FieldType::String).is_err());
|
|
257
|
+
|
|
258
|
+
assert!(HeaderValidator::validate_format("Authorization", "Bearer token", HeaderFormat::Bearer).is_ok());
|
|
259
|
+
assert!(
|
|
260
|
+
HeaderValidator::validate_format("Content-Type", "application/json; charset=utf-8", HeaderFormat::Json).is_ok()
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
#[test]
|
|
265
|
+
fn test_test_client_with_configuration() {
|
|
266
|
+
use test_client_base::{TestClientConfig, TestResponseMetadata};
|
|
267
|
+
|
|
268
|
+
let config = TestClientConfig::new("http://example.com")
|
|
269
|
+
.with_timeout(5000)
|
|
270
|
+
.with_follow_redirects(false);
|
|
271
|
+
|
|
272
|
+
assert_eq!(config.base_url, "http://example.com");
|
|
273
|
+
assert_eq!(config.timeout_ms, 5000);
|
|
274
|
+
assert!(!config.follow_redirects);
|
|
275
|
+
|
|
276
|
+
let mut headers = HashMap::new();
|
|
277
|
+
headers.insert("Content-Type".to_string(), "application/json".to_string());
|
|
278
|
+
headers.insert("X-Custom".to_string(), "value".to_string());
|
|
279
|
+
|
|
280
|
+
let metadata = TestResponseMetadata::new(201, headers, 1024, 150);
|
|
281
|
+
|
|
282
|
+
assert_eq!(metadata.status_code, 201);
|
|
283
|
+
assert_eq!(metadata.body_size, 1024);
|
|
284
|
+
assert_eq!(metadata.response_time_ms, 150);
|
|
285
|
+
assert!(metadata.is_success());
|
|
286
|
+
assert!(!metadata.is_client_error());
|
|
287
|
+
assert!(!metadata.is_server_error());
|
|
288
|
+
|
|
289
|
+
assert_eq!(
|
|
290
|
+
metadata.get_header("content-type"),
|
|
291
|
+
Some(&"application/json".to_string())
|
|
292
|
+
);
|
|
293
|
+
assert_eq!(
|
|
294
|
+
metadata.get_header("CONTENT-TYPE"),
|
|
295
|
+
Some(&"application/json".to_string())
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
#[tokio::test]
|
|
300
|
+
async fn test_di_traits_with_value_and_factory_adapters() {
|
|
301
|
+
use di_traits::{FactoryDependencyAdapter, FactoryDependencyBridge, ValueDependencyAdapter, ValueDependencyBridge};
|
|
302
|
+
use std::sync::atomic::{AtomicBool, Ordering};
|
|
303
|
+
|
|
304
|
+
struct TestValueAdapter {
|
|
305
|
+
key: String,
|
|
306
|
+
value: String,
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
impl ValueDependencyAdapter for TestValueAdapter {
|
|
310
|
+
fn key(&self) -> &str {
|
|
311
|
+
&self.key
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
fn resolve_value(
|
|
315
|
+
&self,
|
|
316
|
+
) -> std::pin::Pin<
|
|
317
|
+
Box<
|
|
318
|
+
dyn std::future::Future<
|
|
319
|
+
Output = Result<Arc<dyn std::any::Any + Send + Sync>, spikard_core::di::DependencyError>,
|
|
320
|
+
> + Send
|
|
321
|
+
+ '_,
|
|
322
|
+
>,
|
|
323
|
+
> {
|
|
324
|
+
let value = self.value.clone();
|
|
325
|
+
Box::pin(async move { Ok(Arc::new(value) as Arc<dyn std::any::Any + Send + Sync>) })
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
struct TestFactoryAdapter {
|
|
330
|
+
key: String,
|
|
331
|
+
called: Arc<AtomicBool>,
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
impl FactoryDependencyAdapter for TestFactoryAdapter {
|
|
335
|
+
fn key(&self) -> &str {
|
|
336
|
+
&self.key
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
fn invoke_factory(
|
|
340
|
+
&self,
|
|
341
|
+
_request: &http::Request<()>,
|
|
342
|
+
_request_data: &CoreRequestData,
|
|
343
|
+
_resolved: &ResolvedDependencies,
|
|
344
|
+
) -> std::pin::Pin<
|
|
345
|
+
Box<
|
|
346
|
+
dyn std::future::Future<
|
|
347
|
+
Output = Result<Arc<dyn std::any::Any + Send + Sync>, spikard_core::di::DependencyError>,
|
|
348
|
+
> + Send
|
|
349
|
+
+ '_,
|
|
350
|
+
>,
|
|
351
|
+
> {
|
|
352
|
+
let called = self.called.clone();
|
|
353
|
+
Box::pin(async move {
|
|
354
|
+
called.store(true, Ordering::SeqCst);
|
|
355
|
+
Ok(Arc::new("factory_result".to_string()) as Arc<dyn std::any::Any + Send + Sync>)
|
|
356
|
+
})
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
let value_adapter = TestValueAdapter {
|
|
361
|
+
key: "test_value".to_string(),
|
|
362
|
+
value: "test_data".to_string(),
|
|
363
|
+
};
|
|
364
|
+
let value_bridge = ValueDependencyBridge::new(value_adapter);
|
|
365
|
+
assert_eq!(value_bridge.key(), "test_value");
|
|
366
|
+
assert_eq!(value_bridge.depends_on(), Vec::<String>::new());
|
|
367
|
+
|
|
368
|
+
let request = Request::builder().body(()).unwrap();
|
|
369
|
+
let request_data = CoreRequestData {
|
|
370
|
+
path_params: Arc::new(HashMap::new()),
|
|
371
|
+
query_params: json!({}),
|
|
372
|
+
validated_params: None,
|
|
373
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
374
|
+
body: json!({}),
|
|
375
|
+
raw_body: None,
|
|
376
|
+
headers: Arc::new(HashMap::new()),
|
|
377
|
+
cookies: Arc::new(HashMap::new()),
|
|
378
|
+
method: "GET".to_string(),
|
|
379
|
+
path: "/".to_string(),
|
|
380
|
+
dependencies: None,
|
|
381
|
+
};
|
|
382
|
+
let resolved = ResolvedDependencies::new();
|
|
383
|
+
|
|
384
|
+
let result = value_bridge.resolve(&request, &request_data, &resolved).await;
|
|
385
|
+
assert!(result.is_ok());
|
|
386
|
+
|
|
387
|
+
let called_flag = Arc::new(AtomicBool::new(false));
|
|
388
|
+
let factory_adapter = TestFactoryAdapter {
|
|
389
|
+
key: "test_factory".to_string(),
|
|
390
|
+
called: called_flag.clone(),
|
|
391
|
+
};
|
|
392
|
+
let factory_bridge = FactoryDependencyBridge::new(factory_adapter);
|
|
393
|
+
|
|
394
|
+
assert_eq!(factory_bridge.key(), "test_factory");
|
|
395
|
+
assert!(!called_flag.load(Ordering::SeqCst));
|
|
396
|
+
|
|
397
|
+
let result = factory_bridge.resolve(&request, &request_data, &resolved).await;
|
|
398
|
+
assert!(result.is_ok());
|
|
399
|
+
assert!(called_flag.load(Ordering::SeqCst));
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
#[test]
|
|
403
|
+
fn test_conversion_traits_with_json_and_language_conversion() {
|
|
404
|
+
use conversion_traits::{FromLanguage, JsonConversionError, JsonConvertible, ToLanguage};
|
|
405
|
+
|
|
406
|
+
let error = JsonConversionError("Test error message".to_string());
|
|
407
|
+
assert_eq!(error.to_string(), "JSON conversion error: Test error message");
|
|
408
|
+
|
|
409
|
+
let test_cases = vec![
|
|
410
|
+
json!(null),
|
|
411
|
+
json!(true),
|
|
412
|
+
json!(false),
|
|
413
|
+
json!(42),
|
|
414
|
+
json!(3.2),
|
|
415
|
+
json!("string"),
|
|
416
|
+
json!([1, 2, 3]),
|
|
417
|
+
json!({"key": "value"}),
|
|
418
|
+
];
|
|
419
|
+
|
|
420
|
+
for test_value in test_cases {
|
|
421
|
+
let from_result = serde_json::Value::from_json(test_value.clone());
|
|
422
|
+
assert!(from_result.is_ok());
|
|
423
|
+
assert_eq!(from_result.unwrap(), test_value);
|
|
424
|
+
|
|
425
|
+
let to_result = test_value.to_json();
|
|
426
|
+
assert!(to_result.is_ok());
|
|
427
|
+
assert_eq!(to_result.unwrap(), test_value);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
let any_value: Box<dyn std::any::Any + Send + Sync> = Box::new(42i32);
|
|
431
|
+
let custom = CustomType::from_any(&*any_value).unwrap();
|
|
432
|
+
assert_eq!(custom.value, 42);
|
|
433
|
+
|
|
434
|
+
let back_to_any = custom.to_any().unwrap();
|
|
435
|
+
let back_to_i32 = back_to_any.downcast_ref::<i32>().unwrap();
|
|
436
|
+
assert_eq!(*back_to_i32, 42);
|
|
437
|
+
|
|
438
|
+
let wrong_type: Box<dyn std::any::Any + Send + Sync> = Box::new("string");
|
|
439
|
+
let result = CustomType::from_any(&*wrong_type);
|
|
440
|
+
assert!(result.is_err());
|
|
441
|
+
assert_eq!(result.unwrap_err(), "Type mismatch");
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
#[test]
|
|
445
|
+
fn test_json_conversion_types_comprehensive() {
|
|
446
|
+
use json_conversion::{JsonConversionHelper, JsonPrimitive};
|
|
447
|
+
|
|
448
|
+
// Test JsonPrimitive enum variants
|
|
449
|
+
let string_prim = JsonPrimitive::String("test");
|
|
450
|
+
let number_prim = JsonPrimitive::Number("42.5".to_string());
|
|
451
|
+
let bool_prim = JsonPrimitive::Bool(true);
|
|
452
|
+
let null_prim = JsonPrimitive::Null;
|
|
453
|
+
|
|
454
|
+
assert!(matches!(string_prim, JsonPrimitive::String(_)));
|
|
455
|
+
assert!(matches!(number_prim, JsonPrimitive::Number(_)));
|
|
456
|
+
assert!(matches!(bool_prim, JsonPrimitive::Bool(_)));
|
|
457
|
+
assert!(matches!(null_prim, JsonPrimitive::Null));
|
|
458
|
+
|
|
459
|
+
// Test JsonConversionHelper for fast path detection
|
|
460
|
+
assert!(JsonConversionHelper::is_primitive(&json!(42)));
|
|
461
|
+
assert!(JsonConversionHelper::is_primitive(&json!("text")));
|
|
462
|
+
assert!(!JsonConversionHelper::is_primitive(&json!([1, 2, 3])));
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
#[test]
|
|
466
|
+
fn test_lazy_cache_integration_with_json_conversion() {
|
|
467
|
+
use json_conversion::JsonConversionHelper;
|
|
468
|
+
use lazy_cache::LazyCache;
|
|
469
|
+
|
|
470
|
+
let helper_cache: LazyCache<JsonConversionHelper> = LazyCache::new();
|
|
471
|
+
|
|
472
|
+
// Initialize cache with JSON conversion helper
|
|
473
|
+
let _cached = helper_cache.get_or_init(|| JsonConversionHelper);
|
|
474
|
+
|
|
475
|
+
// Verify we can use it
|
|
476
|
+
let test_json = json!({"key": "value"});
|
|
477
|
+
assert!(JsonConversionHelper::is_primitive(&json!(42)));
|
|
478
|
+
assert!(!JsonConversionHelper::is_primitive(&test_json));
|
|
479
|
+
|
|
480
|
+
// Test cache state
|
|
481
|
+
assert!(helper_cache.is_cached());
|
|
482
|
+
|
|
483
|
+
// Test invalidate
|
|
484
|
+
helper_cache.invalidate();
|
|
485
|
+
assert!(!helper_cache.is_cached());
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
#[test]
|
|
489
|
+
fn test_response_interpreter_with_json_conversion() {
|
|
490
|
+
use response_interpreter::InterpretedResponse;
|
|
491
|
+
|
|
492
|
+
let response_body = json!({
|
|
493
|
+
"status": "success",
|
|
494
|
+
"data": {
|
|
495
|
+
"id": 1,
|
|
496
|
+
"name": "test"
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
// Create a Custom interpreted response
|
|
501
|
+
let interpreted = InterpretedResponse::Custom {
|
|
502
|
+
status: 200,
|
|
503
|
+
headers: HashMap::new(),
|
|
504
|
+
body: Some(response_body),
|
|
505
|
+
raw_body: None,
|
|
506
|
+
};
|
|
507
|
+
|
|
508
|
+
match interpreted {
|
|
509
|
+
InterpretedResponse::Custom { status, body, .. } => {
|
|
510
|
+
assert_eq!(status, 200);
|
|
511
|
+
assert_eq!(body.as_ref().unwrap()["status"], "success");
|
|
512
|
+
}
|
|
513
|
+
_ => panic!("Expected Custom variant"),
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
#[test]
|
|
518
|
+
fn test_build_optimized_response_bytes_function_availability() {
|
|
519
|
+
use axum::body::Bytes;
|
|
520
|
+
use axum::http::StatusCode;
|
|
521
|
+
|
|
522
|
+
let body_bytes = Bytes::from_static(br#"{"status":"ok"}"#);
|
|
523
|
+
let _response = build_optimized_response_bytes(StatusCode::OK, None, body_bytes);
|
|
524
|
+
|
|
525
|
+
// Function is available and can be called
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
#[test]
|
|
529
|
+
fn test_module_interactions_json_lazy_response() {
|
|
530
|
+
use json_conversion::JsonConversionHelper;
|
|
531
|
+
use lazy_cache::LazyCache;
|
|
532
|
+
use response_interpreter::InterpretedResponse;
|
|
533
|
+
|
|
534
|
+
// Create a lazy-cached conversion helper
|
|
535
|
+
let cache: LazyCache<JsonConversionHelper> = LazyCache::new();
|
|
536
|
+
let _ = cache.get_or_init(|| JsonConversionHelper);
|
|
537
|
+
|
|
538
|
+
// Create test JSON
|
|
539
|
+
let json_data = json!({
|
|
540
|
+
"message": "integration test",
|
|
541
|
+
"timestamp": 1_234_567_890,
|
|
542
|
+
"nested": {
|
|
543
|
+
"array": [1, 2, 3],
|
|
544
|
+
"bool": true
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
// Verify helper can detect primitives
|
|
549
|
+
let primitive_val = json!(42);
|
|
550
|
+
assert!(JsonConversionHelper::is_primitive(&primitive_val));
|
|
551
|
+
assert!(!JsonConversionHelper::is_primitive(&json_data));
|
|
552
|
+
|
|
553
|
+
// Create interpreted response
|
|
554
|
+
let response = InterpretedResponse::Custom {
|
|
555
|
+
status: 200,
|
|
556
|
+
body: Some(json_data),
|
|
557
|
+
headers: HashMap::new(),
|
|
558
|
+
raw_body: None,
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
match response {
|
|
562
|
+
InterpretedResponse::Custom { status, .. } => assert_eq!(status, 200),
|
|
563
|
+
_ => panic!("Expected Custom variant"),
|
|
564
|
+
}
|
|
565
|
+
assert!(cache.is_cached());
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
#[test]
|
|
569
|
+
fn test_response_builder_bytes_and_optimized_paths() {
|
|
570
|
+
use axum::body::Bytes;
|
|
571
|
+
use axum::http::StatusCode;
|
|
572
|
+
|
|
573
|
+
let test_body = br#"{"result":"test"}"#.to_vec();
|
|
574
|
+
|
|
575
|
+
// Test build_optimized_response_bytes path
|
|
576
|
+
let resp_bytes = build_optimized_response_bytes(StatusCode::CREATED, None, Bytes::from(test_body.clone()));
|
|
577
|
+
assert_eq!(resp_bytes.status(), StatusCode::CREATED);
|
|
578
|
+
|
|
579
|
+
// Test build_optimized_response path
|
|
580
|
+
let resp = build_optimized_response(StatusCode::OK, None, test_body);
|
|
581
|
+
assert_eq!(resp.status(), StatusCode::OK);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
#[test]
|
|
585
|
+
fn test_json_conversion_error_handling() {
|
|
586
|
+
use json_conversion::JsonConversionError;
|
|
587
|
+
|
|
588
|
+
let error = JsonConversionError::InvalidValue {
|
|
589
|
+
reason: "Invalid JSON structure".to_string(),
|
|
590
|
+
};
|
|
591
|
+
let error_str = error.to_string();
|
|
592
|
+
assert!(error_str.contains("Invalid JSON structure"));
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
#[test]
|
|
596
|
+
fn test_lazy_cache_multiple_types() {
|
|
597
|
+
use lazy_cache::LazyCache;
|
|
598
|
+
|
|
599
|
+
// Test with String
|
|
600
|
+
let cache_string: LazyCache<String> = LazyCache::new();
|
|
601
|
+
let result = cache_string.get_or_init(|| "cached value".to_string());
|
|
602
|
+
assert_eq!(result, "cached value");
|
|
603
|
+
assert!(cache_string.is_cached());
|
|
604
|
+
|
|
605
|
+
// Test with Vec
|
|
606
|
+
let cache_vec: LazyCache<Vec<i32>> = LazyCache::new();
|
|
607
|
+
let result = cache_vec.get_or_init(|| vec![1, 2, 3]);
|
|
608
|
+
assert_eq!(*result, vec![1, 2, 3]);
|
|
609
|
+
assert!(cache_vec.is_cached());
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
#[test]
|
|
613
|
+
fn test_all_new_modules_publicly_accessible() {
|
|
614
|
+
println!("All new modules successfully exported from lib.rs:");
|
|
615
|
+
println!("- json_conversion (JsonConverter, JsonConversionHelper, JsonConversionError, JsonPrimitive)");
|
|
616
|
+
println!("- lazy_cache (LazyCache)");
|
|
617
|
+
println!("- response_interpreter (InterpretedResponse, ResponseInterpreter, StreamSource)");
|
|
618
|
+
println!("- response_builder functions (build_optimized_response, build_optimized_response_bytes)");
|
|
619
|
+
|
|
620
|
+
// Verify all types are in scope by using them
|
|
621
|
+
{
|
|
622
|
+
use spikard_bindings_shared::JsonConversionHelper;
|
|
623
|
+
// Type is imported and available in scope
|
|
624
|
+
let _: &[u8] = b"";
|
|
625
|
+
let _ = JsonConversionHelper::is_primitive(&json!(null));
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
{
|
|
629
|
+
use spikard_bindings_shared::LazyCache;
|
|
630
|
+
let _cache: LazyCache<i32> = LazyCache::new();
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
{
|
|
634
|
+
use spikard_bindings_shared::InterpretedResponse;
|
|
635
|
+
let _resp = InterpretedResponse::Custom {
|
|
636
|
+
status: 200,
|
|
637
|
+
body: Some(json!({})),
|
|
638
|
+
headers: HashMap::new(),
|
|
639
|
+
raw_body: None,
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
{
|
|
644
|
+
use axum::body::Bytes;
|
|
645
|
+
use axum::http::StatusCode;
|
|
646
|
+
use spikard_bindings_shared::{build_optimized_response, build_optimized_response_bytes};
|
|
647
|
+
|
|
648
|
+
let _resp1 = build_optimized_response(StatusCode::OK, None, br"{}".to_vec());
|
|
649
|
+
let _resp2 = build_optimized_response_bytes(StatusCode::OK, None, Bytes::from_static(br"{}"));
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
println!("✓ All types successfully imported and used from public API");
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
#[test]
|
|
656
|
+
fn test_all_modules_documented() {
|
|
657
|
+
println!("All modules successfully imported and tested:");
|
|
658
|
+
println!("- ErrorResponseBuilder");
|
|
659
|
+
println!("- ResponseBuilder");
|
|
660
|
+
println!("- handler_base (LanguageHandler, HandlerExecutor, HandlerError)");
|
|
661
|
+
println!("- lifecycle_base (LifecycleHook, LifecycleConfig, HookResult)");
|
|
662
|
+
println!("- validation_helpers (HeaderValidator, BodyValidator)");
|
|
663
|
+
println!("- test_client_base (TestClientConfig, TestResponseMetadata)");
|
|
664
|
+
println!("- di_traits (ValueDependencyAdapter, FactoryDependencyAdapter)");
|
|
665
|
+
println!("- conversion_traits (FromLanguage, ToLanguage, JsonConvertible)");
|
|
666
|
+
println!("- json_conversion (JsonConverter, JsonConversionHelper)");
|
|
667
|
+
println!("- lazy_cache (LazyCache)");
|
|
668
|
+
println!("- response_interpreter (InterpretedResponse, ResponseInterpreter)");
|
|
669
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "spikard-core"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.10.2"
|
|
4
4
|
edition = "2024"
|
|
5
5
|
authors = ["Na'aman Hirschfeld <nhirschfeld@gmail.com>"]
|
|
6
6
|
license = "MIT"
|
|
@@ -34,8 +34,8 @@ base64 = "0.22"
|
|
|
34
34
|
serde_qs = "0.15"
|
|
35
35
|
url = "2.5"
|
|
36
36
|
jiff = "0.2"
|
|
37
|
-
uuid = "1.
|
|
38
|
-
indexmap = "2.
|
|
37
|
+
uuid = "1.20"
|
|
38
|
+
indexmap = "2.13"
|
|
39
39
|
tokio = { version = "1", optional = true, default-features = false, features = ["rt", "macros", "sync", "time"] }
|
|
40
40
|
bytes = { version = "1.11", optional = true }
|
|
41
41
|
thiserror = "2.0"
|
|
@@ -696,7 +696,7 @@ mod tests {
|
|
|
696
696
|
let dep = ValueDependency::new("test", 42i32);
|
|
697
697
|
container.register("test".to_string(), Arc::new(dep)).unwrap();
|
|
698
698
|
|
|
699
|
-
let debug_str = format!("{:?}"
|
|
699
|
+
let debug_str = format!("{container:?}");
|
|
700
700
|
assert!(debug_str.contains("DependencyContainer"));
|
|
701
701
|
}
|
|
702
702
|
}
|