spikard 0.3.6 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +21 -6
- data/ext/spikard_rb/Cargo.toml +2 -2
- data/lib/spikard/app.rb +33 -14
- data/lib/spikard/testing.rb +47 -12
- data/lib/spikard/version.rb +1 -1
- data/vendor/crates/spikard-bindings-shared/Cargo.toml +63 -0
- data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +132 -0
- data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +752 -0
- data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +194 -0
- data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +246 -0
- data/vendor/crates/spikard-bindings-shared/src/error_response.rs +401 -0
- data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +238 -0
- data/vendor/crates/spikard-bindings-shared/src/lib.rs +24 -0
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +292 -0
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +616 -0
- data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +305 -0
- data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +248 -0
- data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +351 -0
- data/vendor/crates/spikard-bindings-shared/tests/comprehensive_coverage.rs +454 -0
- data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +383 -0
- data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +280 -0
- data/vendor/crates/spikard-core/Cargo.toml +4 -4
- data/vendor/crates/spikard-core/src/debug.rs +64 -0
- data/vendor/crates/spikard-core/src/di/container.rs +3 -27
- data/vendor/crates/spikard-core/src/di/factory.rs +1 -5
- data/vendor/crates/spikard-core/src/di/graph.rs +8 -47
- data/vendor/crates/spikard-core/src/di/mod.rs +1 -1
- data/vendor/crates/spikard-core/src/di/resolved.rs +1 -7
- data/vendor/crates/spikard-core/src/di/value.rs +2 -4
- data/vendor/crates/spikard-core/src/errors.rs +30 -0
- data/vendor/crates/spikard-core/src/http.rs +262 -0
- data/vendor/crates/spikard-core/src/lib.rs +1 -1
- data/vendor/crates/spikard-core/src/lifecycle.rs +764 -0
- data/vendor/crates/spikard-core/src/metadata.rs +389 -0
- data/vendor/crates/spikard-core/src/parameters.rs +1962 -159
- data/vendor/crates/spikard-core/src/problem.rs +34 -0
- data/vendor/crates/spikard-core/src/request_data.rs +966 -1
- data/vendor/crates/spikard-core/src/router.rs +263 -2
- data/vendor/crates/spikard-core/src/validation/error_mapper.rs +688 -0
- data/vendor/crates/spikard-core/src/{validation.rs → validation/mod.rs} +26 -268
- data/vendor/crates/spikard-http/Cargo.toml +12 -16
- data/vendor/crates/spikard-http/examples/sse-notifications.rs +148 -0
- data/vendor/crates/spikard-http/examples/websocket-chat.rs +92 -0
- data/vendor/crates/spikard-http/src/auth.rs +65 -16
- data/vendor/crates/spikard-http/src/background.rs +1614 -3
- data/vendor/crates/spikard-http/src/cors.rs +515 -0
- data/vendor/crates/spikard-http/src/debug.rs +65 -0
- data/vendor/crates/spikard-http/src/di_handler.rs +1322 -77
- data/vendor/crates/spikard-http/src/handler_response.rs +711 -0
- data/vendor/crates/spikard-http/src/handler_trait.rs +607 -5
- data/vendor/crates/spikard-http/src/handler_trait_tests.rs +6 -0
- data/vendor/crates/spikard-http/src/lib.rs +33 -28
- data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +81 -0
- data/vendor/crates/spikard-http/src/lifecycle.rs +765 -0
- data/vendor/crates/spikard-http/src/middleware/mod.rs +372 -117
- data/vendor/crates/spikard-http/src/middleware/multipart.rs +836 -10
- data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +409 -43
- data/vendor/crates/spikard-http/src/middleware/validation.rs +513 -65
- data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +345 -0
- data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +1055 -0
- data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +473 -3
- data/vendor/crates/spikard-http/src/query_parser.rs +455 -31
- data/vendor/crates/spikard-http/src/response.rs +321 -0
- data/vendor/crates/spikard-http/src/server/handler.rs +1572 -9
- data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +136 -0
- data/vendor/crates/spikard-http/src/server/mod.rs +875 -178
- data/vendor/crates/spikard-http/src/server/request_extraction.rs +674 -23
- data/vendor/crates/spikard-http/src/server/routing_factory.rs +599 -0
- data/vendor/crates/spikard-http/src/sse.rs +983 -21
- data/vendor/crates/spikard-http/src/testing/form.rs +38 -0
- data/vendor/crates/spikard-http/src/testing/test_client.rs +0 -2
- data/vendor/crates/spikard-http/src/testing.rs +7 -7
- data/vendor/crates/spikard-http/src/websocket.rs +1055 -4
- data/vendor/crates/spikard-http/tests/background_behavior.rs +832 -0
- data/vendor/crates/spikard-http/tests/common/handlers.rs +309 -0
- data/vendor/crates/spikard-http/tests/common/mod.rs +26 -0
- data/vendor/crates/spikard-http/tests/di_integration.rs +192 -0
- data/vendor/crates/spikard-http/tests/doc_snippets.rs +5 -0
- data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +1093 -0
- data/vendor/crates/spikard-http/tests/multipart_behavior.rs +656 -0
- data/vendor/crates/spikard-http/tests/server_config_builder.rs +314 -0
- data/vendor/crates/spikard-http/tests/sse_behavior.rs +620 -0
- data/vendor/crates/spikard-http/tests/websocket_behavior.rs +663 -0
- data/vendor/crates/spikard-rb/Cargo.toml +10 -4
- data/vendor/crates/spikard-rb/build.rs +196 -5
- data/vendor/crates/spikard-rb/src/config/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/{config.rs → config/server_config.rs} +100 -109
- data/vendor/crates/spikard-rb/src/conversion.rs +121 -20
- data/vendor/crates/spikard-rb/src/di/builder.rs +100 -0
- data/vendor/crates/spikard-rb/src/{di.rs → di/mod.rs} +12 -46
- data/vendor/crates/spikard-rb/src/handler.rs +100 -107
- data/vendor/crates/spikard-rb/src/integration/mod.rs +3 -0
- data/vendor/crates/spikard-rb/src/lib.rs +467 -1428
- data/vendor/crates/spikard-rb/src/lifecycle.rs +1 -0
- data/vendor/crates/spikard-rb/src/metadata/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +447 -0
- data/vendor/crates/spikard-rb/src/runtime/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +324 -0
- data/vendor/crates/spikard-rb/src/server.rs +47 -22
- data/vendor/crates/spikard-rb/src/{test_client.rs → testing/client.rs} +187 -40
- data/vendor/crates/spikard-rb/src/testing/mod.rs +7 -0
- data/vendor/crates/spikard-rb/src/testing/websocket.rs +635 -0
- data/vendor/crates/spikard-rb/src/websocket.rs +178 -37
- metadata +46 -13
- data/vendor/crates/spikard-http/src/parameters.rs +0 -1
- data/vendor/crates/spikard-http/src/problem.rs +0 -1
- data/vendor/crates/spikard-http/src/router.rs +0 -1
- data/vendor/crates/spikard-http/src/schema_registry.rs +0 -1
- data/vendor/crates/spikard-http/src/type_hints.rs +0 -1
- data/vendor/crates/spikard-http/src/validation.rs +0 -1
- data/vendor/crates/spikard-rb/src/test_websocket.rs +0 -221
- /data/vendor/crates/spikard-rb/src/{test_sse.rs → testing/sse.rs} +0 -0
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
//! Edge case tests for error_response module
|
|
2
|
+
//!
|
|
3
|
+
//! These tests cover serialization edge cases and ensure fallback
|
|
4
|
+
//! behavior works correctly when serialization fails.
|
|
5
|
+
|
|
6
|
+
use axum::http::StatusCode;
|
|
7
|
+
use pretty_assertions::assert_eq;
|
|
8
|
+
use serde_json::{Value, json};
|
|
9
|
+
use spikard_bindings_shared::ErrorResponseBuilder;
|
|
10
|
+
use spikard_core::errors::StructuredError;
|
|
11
|
+
use spikard_core::problem::ProblemDetails;
|
|
12
|
+
use spikard_core::validation::{ValidationError, ValidationErrorDetail};
|
|
13
|
+
|
|
14
|
+
#[test]
|
|
15
|
+
fn test_structured_error_with_empty_message() {
|
|
16
|
+
let (status, body) = ErrorResponseBuilder::structured_error(StatusCode::BAD_REQUEST, "empty_msg", "");
|
|
17
|
+
assert_eq!(status, StatusCode::BAD_REQUEST);
|
|
18
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
19
|
+
assert_eq!(parsed["error"], "");
|
|
20
|
+
assert_eq!(parsed["code"], "empty_msg");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
#[test]
|
|
24
|
+
fn test_structured_error_with_unicode() {
|
|
25
|
+
let (status, body) = ErrorResponseBuilder::structured_error(
|
|
26
|
+
StatusCode::BAD_REQUEST,
|
|
27
|
+
"unicode_test",
|
|
28
|
+
"Error with emoji: 😀 and special chars: 中文",
|
|
29
|
+
);
|
|
30
|
+
assert_eq!(status, StatusCode::BAD_REQUEST);
|
|
31
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
32
|
+
assert!(parsed["error"].as_str().unwrap().contains("😀"));
|
|
33
|
+
assert!(parsed["error"].as_str().unwrap().contains("中文"));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
#[test]
|
|
37
|
+
fn test_with_details_empty_details() {
|
|
38
|
+
let (status, body) =
|
|
39
|
+
ErrorResponseBuilder::with_details(StatusCode::NOT_FOUND, "not_found", "Resource not found", json!({}));
|
|
40
|
+
assert_eq!(status, StatusCode::NOT_FOUND);
|
|
41
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
42
|
+
assert_eq!(parsed["code"], "not_found");
|
|
43
|
+
assert!(parsed["details"].is_object());
|
|
44
|
+
assert_eq!(parsed["details"].as_object().unwrap().len(), 0);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
#[test]
|
|
48
|
+
fn test_with_details_nested_objects() {
|
|
49
|
+
let details = json!({
|
|
50
|
+
"level1": {
|
|
51
|
+
"level2": {
|
|
52
|
+
"level3": {
|
|
53
|
+
"message": "deeply nested"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
let (status, body) = ErrorResponseBuilder::with_details(
|
|
60
|
+
StatusCode::INTERNAL_SERVER_ERROR,
|
|
61
|
+
"nested_error",
|
|
62
|
+
"Error with nested details",
|
|
63
|
+
details.clone(),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
|
|
67
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
68
|
+
assert_eq!(
|
|
69
|
+
parsed["details"]["level1"]["level2"]["level3"]["message"],
|
|
70
|
+
"deeply nested"
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
#[test]
|
|
75
|
+
fn test_with_details_array_values() {
|
|
76
|
+
let details = json!({
|
|
77
|
+
"errors": ["error1", "error2", "error3"],
|
|
78
|
+
"codes": [400, 401, 403]
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
let (status, body) = ErrorResponseBuilder::with_details(
|
|
82
|
+
StatusCode::BAD_REQUEST,
|
|
83
|
+
"multiple_errors",
|
|
84
|
+
"Multiple validation errors",
|
|
85
|
+
details,
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
assert_eq!(status, StatusCode::BAD_REQUEST);
|
|
89
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
90
|
+
assert_eq!(parsed["details"]["errors"][0], "error1");
|
|
91
|
+
assert_eq!(parsed["details"]["codes"][2], 403);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
#[test]
|
|
95
|
+
fn test_from_structured_error_with_details() {
|
|
96
|
+
let error = StructuredError::new(
|
|
97
|
+
"custom_error".to_string(),
|
|
98
|
+
"Custom error occurred".to_string(),
|
|
99
|
+
json!({"field": "username", "reason": "already_exists"}),
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
let (status, body) = ErrorResponseBuilder::from_structured_error(error);
|
|
103
|
+
assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
|
|
104
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
105
|
+
assert_eq!(parsed["code"], "custom_error");
|
|
106
|
+
assert_eq!(parsed["error"], "Custom error occurred");
|
|
107
|
+
assert_eq!(parsed["details"]["field"], "username");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
#[test]
|
|
111
|
+
fn test_validation_error_multiple_errors() {
|
|
112
|
+
let validation_error = ValidationError {
|
|
113
|
+
errors: vec![
|
|
114
|
+
ValidationErrorDetail {
|
|
115
|
+
error_type: "missing".to_string(),
|
|
116
|
+
loc: vec!["body".to_string(), "username".to_string()],
|
|
117
|
+
msg: "Field required".to_string(),
|
|
118
|
+
input: Value::Null,
|
|
119
|
+
ctx: None,
|
|
120
|
+
},
|
|
121
|
+
ValidationErrorDetail {
|
|
122
|
+
error_type: "type_error".to_string(),
|
|
123
|
+
loc: vec!["body".to_string(), "age".to_string()],
|
|
124
|
+
msg: "Value must be a number".to_string(),
|
|
125
|
+
input: Value::String("abc".to_string()),
|
|
126
|
+
ctx: None,
|
|
127
|
+
},
|
|
128
|
+
ValidationErrorDetail {
|
|
129
|
+
error_type: "value_error".to_string(),
|
|
130
|
+
loc: vec!["body".to_string(), "email".to_string()],
|
|
131
|
+
msg: "Invalid email format".to_string(),
|
|
132
|
+
input: Value::String("not-an-email".to_string()),
|
|
133
|
+
ctx: None,
|
|
134
|
+
},
|
|
135
|
+
],
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
let (status, body) = ErrorResponseBuilder::validation_error(&validation_error);
|
|
139
|
+
assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY);
|
|
140
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
141
|
+
assert_eq!(parsed["status"], 422);
|
|
142
|
+
assert_eq!(parsed["errors"].as_array().unwrap().len(), 3);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
#[test]
|
|
146
|
+
fn test_validation_error_with_context() {
|
|
147
|
+
let validation_error = ValidationError {
|
|
148
|
+
errors: vec![ValidationErrorDetail {
|
|
149
|
+
error_type: "value_error".to_string(),
|
|
150
|
+
loc: vec!["query".to_string(), "page".to_string()],
|
|
151
|
+
msg: "Value must be greater than 0".to_string(),
|
|
152
|
+
input: json!(0),
|
|
153
|
+
ctx: Some(json!({"min": 1, "max": 100})),
|
|
154
|
+
}],
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
let (status, body) = ErrorResponseBuilder::validation_error(&validation_error);
|
|
158
|
+
assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY);
|
|
159
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
160
|
+
assert!(parsed["errors"].is_array());
|
|
161
|
+
assert_eq!(parsed["errors"][0]["type"], "value_error");
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
#[test]
|
|
165
|
+
fn test_problem_details_with_instance() {
|
|
166
|
+
let mut problem = ProblemDetails::not_found("User not found");
|
|
167
|
+
problem.instance = Some("/users/12345".to_string());
|
|
168
|
+
|
|
169
|
+
let (status, body) = ErrorResponseBuilder::problem_details_response(&problem);
|
|
170
|
+
assert_eq!(status, StatusCode::NOT_FOUND);
|
|
171
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
172
|
+
assert_eq!(parsed["instance"], "/users/12345");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
#[test]
|
|
176
|
+
fn test_problem_details_with_extensions() {
|
|
177
|
+
let mut problem = ProblemDetails::internal_server_error("Database connection failed");
|
|
178
|
+
problem.extensions.insert("retry_after".to_string(), json!(30));
|
|
179
|
+
problem.extensions.insert("error_id".to_string(), json!("ERR-2024-001"));
|
|
180
|
+
|
|
181
|
+
let (status, body) = ErrorResponseBuilder::problem_details_response(&problem);
|
|
182
|
+
assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
|
|
183
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
184
|
+
assert_eq!(parsed["retry_after"], 30);
|
|
185
|
+
assert_eq!(parsed["error_id"], "ERR-2024-001");
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
#[test]
|
|
189
|
+
fn test_all_convenience_methods_return_valid_json() {
|
|
190
|
+
let test_cases = vec![
|
|
191
|
+
ErrorResponseBuilder::bad_request("Bad request"),
|
|
192
|
+
ErrorResponseBuilder::internal_error("Internal error"),
|
|
193
|
+
ErrorResponseBuilder::unauthorized("Unauthorized"),
|
|
194
|
+
ErrorResponseBuilder::forbidden("Forbidden"),
|
|
195
|
+
ErrorResponseBuilder::not_found("Not found"),
|
|
196
|
+
ErrorResponseBuilder::method_not_allowed("Method not allowed"),
|
|
197
|
+
ErrorResponseBuilder::unprocessable_entity("Unprocessable entity"),
|
|
198
|
+
ErrorResponseBuilder::conflict("Conflict"),
|
|
199
|
+
ErrorResponseBuilder::service_unavailable("Service unavailable"),
|
|
200
|
+
ErrorResponseBuilder::request_timeout("Request timeout"),
|
|
201
|
+
];
|
|
202
|
+
|
|
203
|
+
for (status, body) in test_cases {
|
|
204
|
+
let parsed: Value = serde_json::from_str(&body)
|
|
205
|
+
.unwrap_or_else(|_| panic!("Failed to parse JSON for status {}: {}", status, body));
|
|
206
|
+
|
|
207
|
+
assert!(
|
|
208
|
+
parsed.get("error").is_some(),
|
|
209
|
+
"Missing 'error' field for status {}",
|
|
210
|
+
status
|
|
211
|
+
);
|
|
212
|
+
assert!(
|
|
213
|
+
parsed.get("code").is_some(),
|
|
214
|
+
"Missing 'code' field for status {}",
|
|
215
|
+
status
|
|
216
|
+
);
|
|
217
|
+
assert!(
|
|
218
|
+
parsed.get("details").is_some(),
|
|
219
|
+
"Missing 'details' field for status {}",
|
|
220
|
+
status
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
#[test]
|
|
226
|
+
fn test_error_response_status_code_mapping() {
|
|
227
|
+
assert_eq!(ErrorResponseBuilder::bad_request("msg").0, StatusCode::BAD_REQUEST);
|
|
228
|
+
assert_eq!(
|
|
229
|
+
ErrorResponseBuilder::internal_error("msg").0,
|
|
230
|
+
StatusCode::INTERNAL_SERVER_ERROR
|
|
231
|
+
);
|
|
232
|
+
assert_eq!(ErrorResponseBuilder::unauthorized("msg").0, StatusCode::UNAUTHORIZED);
|
|
233
|
+
assert_eq!(ErrorResponseBuilder::forbidden("msg").0, StatusCode::FORBIDDEN);
|
|
234
|
+
assert_eq!(ErrorResponseBuilder::not_found("msg").0, StatusCode::NOT_FOUND);
|
|
235
|
+
assert_eq!(
|
|
236
|
+
ErrorResponseBuilder::method_not_allowed("msg").0,
|
|
237
|
+
StatusCode::METHOD_NOT_ALLOWED
|
|
238
|
+
);
|
|
239
|
+
assert_eq!(
|
|
240
|
+
ErrorResponseBuilder::unprocessable_entity("msg").0,
|
|
241
|
+
StatusCode::UNPROCESSABLE_ENTITY
|
|
242
|
+
);
|
|
243
|
+
assert_eq!(ErrorResponseBuilder::conflict("msg").0, StatusCode::CONFLICT);
|
|
244
|
+
assert_eq!(
|
|
245
|
+
ErrorResponseBuilder::service_unavailable("msg").0,
|
|
246
|
+
StatusCode::SERVICE_UNAVAILABLE
|
|
247
|
+
);
|
|
248
|
+
assert_eq!(
|
|
249
|
+
ErrorResponseBuilder::request_timeout("msg").0,
|
|
250
|
+
StatusCode::REQUEST_TIMEOUT
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
#[test]
|
|
255
|
+
fn test_structured_error_with_special_characters() {
|
|
256
|
+
let message = r#"Error with quotes: "test" and backslashes: \\ and newlines:
|
|
257
|
+
line1
|
|
258
|
+
line2"#;
|
|
259
|
+
|
|
260
|
+
let (status, body) = ErrorResponseBuilder::structured_error(StatusCode::BAD_REQUEST, "special_chars", message);
|
|
261
|
+
assert_eq!(status, StatusCode::BAD_REQUEST);
|
|
262
|
+
|
|
263
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
264
|
+
assert!(parsed["error"].as_str().unwrap().contains("quotes"));
|
|
265
|
+
assert!(parsed["error"].as_str().unwrap().contains("backslashes"));
|
|
266
|
+
assert!(parsed["error"].as_str().unwrap().contains("newlines"));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
#[test]
|
|
270
|
+
fn test_with_details_null_values() {
|
|
271
|
+
let details = json!({
|
|
272
|
+
"field1": null,
|
|
273
|
+
"field2": "value",
|
|
274
|
+
"field3": null
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
let (status, body) =
|
|
278
|
+
ErrorResponseBuilder::with_details(StatusCode::BAD_REQUEST, "null_test", "Testing null values", details);
|
|
279
|
+
|
|
280
|
+
assert_eq!(status, StatusCode::BAD_REQUEST);
|
|
281
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
282
|
+
assert!(parsed["details"]["field1"].is_null());
|
|
283
|
+
assert_eq!(parsed["details"]["field2"], "value");
|
|
284
|
+
assert!(parsed["details"]["field3"].is_null());
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
#[test]
|
|
288
|
+
fn test_validation_error_empty_location() {
|
|
289
|
+
let validation_error = ValidationError {
|
|
290
|
+
errors: vec![ValidationErrorDetail {
|
|
291
|
+
error_type: "missing".to_string(),
|
|
292
|
+
loc: vec![],
|
|
293
|
+
msg: "Field required".to_string(),
|
|
294
|
+
input: Value::Null,
|
|
295
|
+
ctx: None,
|
|
296
|
+
}],
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
let (status, body) = ErrorResponseBuilder::validation_error(&validation_error);
|
|
300
|
+
assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY);
|
|
301
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
302
|
+
assert!(parsed["errors"][0]["loc"].is_array());
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
#[test]
|
|
306
|
+
fn test_problem_details_all_standard_types() {
|
|
307
|
+
let test_cases = vec![
|
|
308
|
+
(ProblemDetails::bad_request("Bad request"), StatusCode::BAD_REQUEST),
|
|
309
|
+
(ProblemDetails::not_found("Not found"), StatusCode::NOT_FOUND),
|
|
310
|
+
(
|
|
311
|
+
ProblemDetails::method_not_allowed("Method not allowed"),
|
|
312
|
+
StatusCode::METHOD_NOT_ALLOWED,
|
|
313
|
+
),
|
|
314
|
+
(
|
|
315
|
+
ProblemDetails::internal_server_error("Internal error"),
|
|
316
|
+
StatusCode::INTERNAL_SERVER_ERROR,
|
|
317
|
+
),
|
|
318
|
+
];
|
|
319
|
+
|
|
320
|
+
for (problem, expected_status) in test_cases {
|
|
321
|
+
let (status, body) = ErrorResponseBuilder::problem_details_response(&problem);
|
|
322
|
+
assert_eq!(status, expected_status);
|
|
323
|
+
|
|
324
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
325
|
+
assert_eq!(parsed["status"], expected_status.as_u16());
|
|
326
|
+
assert!(parsed.get("type").is_some());
|
|
327
|
+
assert!(parsed.get("title").is_some());
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
#[test]
|
|
332
|
+
fn test_error_message_types() {
|
|
333
|
+
let (_, body) = ErrorResponseBuilder::bad_request("test".to_string());
|
|
334
|
+
assert!(body.contains("test"));
|
|
335
|
+
|
|
336
|
+
let (_, body) = ErrorResponseBuilder::bad_request("test");
|
|
337
|
+
assert!(body.contains("test"));
|
|
338
|
+
|
|
339
|
+
let (_, body) = ErrorResponseBuilder::bad_request(format!("Error: {}", 123));
|
|
340
|
+
assert!(body.contains("Error: 123"));
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
#[test]
|
|
344
|
+
fn test_with_details_boolean_values() {
|
|
345
|
+
let details = json!({
|
|
346
|
+
"is_active": true,
|
|
347
|
+
"is_admin": false,
|
|
348
|
+
"has_permission": true
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
let (status, body) =
|
|
352
|
+
ErrorResponseBuilder::with_details(StatusCode::FORBIDDEN, "permission_error", "Permission denied", details);
|
|
353
|
+
|
|
354
|
+
assert_eq!(status, StatusCode::FORBIDDEN);
|
|
355
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
356
|
+
assert_eq!(parsed["details"]["is_active"], true);
|
|
357
|
+
assert_eq!(parsed["details"]["is_admin"], false);
|
|
358
|
+
assert_eq!(parsed["details"]["has_permission"], true);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
#[test]
|
|
362
|
+
fn test_with_details_numeric_values() {
|
|
363
|
+
let details = json!({
|
|
364
|
+
"integer": 42,
|
|
365
|
+
"float": 3.2,
|
|
366
|
+
"negative": -100,
|
|
367
|
+
"zero": 0
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
let (status, body) = ErrorResponseBuilder::with_details(
|
|
371
|
+
StatusCode::BAD_REQUEST,
|
|
372
|
+
"numeric_test",
|
|
373
|
+
"Numeric validation failed",
|
|
374
|
+
details,
|
|
375
|
+
);
|
|
376
|
+
|
|
377
|
+
assert_eq!(status, StatusCode::BAD_REQUEST);
|
|
378
|
+
let parsed: Value = serde_json::from_str(&body).unwrap();
|
|
379
|
+
assert_eq!(parsed["details"]["integer"], 42);
|
|
380
|
+
assert_eq!(parsed["details"]["float"], 3.2);
|
|
381
|
+
assert_eq!(parsed["details"]["negative"], -100);
|
|
382
|
+
assert_eq!(parsed["details"]["zero"], 0);
|
|
383
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
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::validation::{SchemaValidator, ValidationError, ValidationErrorDetail};
|
|
11
|
+
use spikard_http::handler_trait::{Handler, RequestData};
|
|
12
|
+
use std::collections::HashMap;
|
|
13
|
+
use std::future::Future;
|
|
14
|
+
use std::pin::Pin;
|
|
15
|
+
use std::sync::Arc;
|
|
16
|
+
|
|
17
|
+
struct MockHandler {
|
|
18
|
+
should_fail_prepare: bool,
|
|
19
|
+
should_fail_invoke: bool,
|
|
20
|
+
should_fail_interpret: bool,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
impl LanguageHandler for MockHandler {
|
|
24
|
+
type Input = String;
|
|
25
|
+
type Output = String;
|
|
26
|
+
|
|
27
|
+
fn prepare_request(&self, _data: &RequestData) -> Result<Self::Input, HandlerError> {
|
|
28
|
+
if self.should_fail_prepare {
|
|
29
|
+
Err(HandlerError::Internal("Prepare failed".to_string()))
|
|
30
|
+
} else {
|
|
31
|
+
Ok("prepared".to_string())
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fn invoke_handler(
|
|
36
|
+
&self,
|
|
37
|
+
input: Self::Input,
|
|
38
|
+
) -> Pin<Box<dyn Future<Output = Result<Self::Output, HandlerError>> + Send + '_>> {
|
|
39
|
+
let should_fail = self.should_fail_invoke;
|
|
40
|
+
Box::pin(async move {
|
|
41
|
+
if should_fail {
|
|
42
|
+
Err(HandlerError::Execution("Handler failed".to_string()))
|
|
43
|
+
} else {
|
|
44
|
+
Ok(format!("output:{}", input))
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
fn interpret_response(&self, output: Self::Output) -> Result<axum::http::Response<Body>, HandlerError> {
|
|
50
|
+
if self.should_fail_interpret {
|
|
51
|
+
Err(HandlerError::ResponseConversion("Interpret failed".to_string()))
|
|
52
|
+
} else {
|
|
53
|
+
Ok(axum::http::Response::builder()
|
|
54
|
+
.status(200)
|
|
55
|
+
.body(Body::from(output))
|
|
56
|
+
.unwrap())
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
#[tokio::test]
|
|
62
|
+
async fn test_handler_executor_with_validation_error() {
|
|
63
|
+
let schema = json!({
|
|
64
|
+
"type": "object",
|
|
65
|
+
"properties": {
|
|
66
|
+
"username": {"type": "string"},
|
|
67
|
+
"age": {"type": "number"}
|
|
68
|
+
},
|
|
69
|
+
"required": ["username", "age"]
|
|
70
|
+
});
|
|
71
|
+
let validator = Arc::new(SchemaValidator::new(schema).unwrap());
|
|
72
|
+
|
|
73
|
+
let handler = Arc::new(MockHandler {
|
|
74
|
+
should_fail_prepare: false,
|
|
75
|
+
should_fail_invoke: false,
|
|
76
|
+
should_fail_interpret: false,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
let executor = HandlerExecutor::new(handler, Some(validator));
|
|
80
|
+
|
|
81
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
82
|
+
let request_data = RequestData {
|
|
83
|
+
path_params: Arc::new(HashMap::new()),
|
|
84
|
+
query_params: json!({}),
|
|
85
|
+
validated_params: None,
|
|
86
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
87
|
+
body: json!({"username": "john"}),
|
|
88
|
+
raw_body: None,
|
|
89
|
+
headers: Arc::new(HashMap::new()),
|
|
90
|
+
cookies: Arc::new(HashMap::new()),
|
|
91
|
+
method: "POST".to_string(),
|
|
92
|
+
path: "/test".to_string(),
|
|
93
|
+
dependencies: None,
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
let result = executor.call(request, request_data).await;
|
|
97
|
+
assert!(result.is_err());
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
#[tokio::test]
|
|
101
|
+
async fn test_handler_executor_prepare_failure() {
|
|
102
|
+
let handler = Arc::new(MockHandler {
|
|
103
|
+
should_fail_prepare: true,
|
|
104
|
+
should_fail_invoke: false,
|
|
105
|
+
should_fail_interpret: false,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
let executor = HandlerExecutor::with_handler(handler);
|
|
109
|
+
|
|
110
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
111
|
+
let request_data = RequestData {
|
|
112
|
+
path_params: Arc::new(HashMap::new()),
|
|
113
|
+
query_params: json!({}),
|
|
114
|
+
validated_params: None,
|
|
115
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
116
|
+
body: json!({}),
|
|
117
|
+
raw_body: None,
|
|
118
|
+
headers: Arc::new(HashMap::new()),
|
|
119
|
+
cookies: Arc::new(HashMap::new()),
|
|
120
|
+
method: "GET".to_string(),
|
|
121
|
+
path: "/test".to_string(),
|
|
122
|
+
dependencies: None,
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
let result = executor.call(request, request_data).await;
|
|
126
|
+
assert!(result.is_err());
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
#[tokio::test]
|
|
130
|
+
async fn test_handler_executor_invoke_failure() {
|
|
131
|
+
let handler = Arc::new(MockHandler {
|
|
132
|
+
should_fail_prepare: false,
|
|
133
|
+
should_fail_invoke: true,
|
|
134
|
+
should_fail_interpret: false,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
let executor = HandlerExecutor::with_handler(handler);
|
|
138
|
+
|
|
139
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
140
|
+
let request_data = RequestData {
|
|
141
|
+
path_params: Arc::new(HashMap::new()),
|
|
142
|
+
query_params: json!({}),
|
|
143
|
+
validated_params: None,
|
|
144
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
145
|
+
body: json!({}),
|
|
146
|
+
raw_body: None,
|
|
147
|
+
headers: Arc::new(HashMap::new()),
|
|
148
|
+
cookies: Arc::new(HashMap::new()),
|
|
149
|
+
method: "GET".to_string(),
|
|
150
|
+
path: "/test".to_string(),
|
|
151
|
+
dependencies: None,
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
let result = executor.call(request, request_data).await;
|
|
155
|
+
assert!(result.is_err());
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
#[tokio::test]
|
|
159
|
+
async fn test_handler_executor_interpret_failure() {
|
|
160
|
+
let handler = Arc::new(MockHandler {
|
|
161
|
+
should_fail_prepare: false,
|
|
162
|
+
should_fail_invoke: false,
|
|
163
|
+
should_fail_interpret: true,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
let executor = HandlerExecutor::with_handler(handler);
|
|
167
|
+
|
|
168
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
169
|
+
let request_data = RequestData {
|
|
170
|
+
path_params: Arc::new(HashMap::new()),
|
|
171
|
+
query_params: json!({}),
|
|
172
|
+
validated_params: None,
|
|
173
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
174
|
+
body: json!({}),
|
|
175
|
+
raw_body: None,
|
|
176
|
+
headers: Arc::new(HashMap::new()),
|
|
177
|
+
cookies: Arc::new(HashMap::new()),
|
|
178
|
+
method: "GET".to_string(),
|
|
179
|
+
path: "/test".to_string(),
|
|
180
|
+
dependencies: None,
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
let result = executor.call(request, request_data).await;
|
|
184
|
+
assert!(result.is_err());
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
#[tokio::test]
|
|
188
|
+
async fn test_handler_executor_with_request_validator() {
|
|
189
|
+
let schema = json!({
|
|
190
|
+
"type": "object",
|
|
191
|
+
"properties": {
|
|
192
|
+
"name": {"type": "string"}
|
|
193
|
+
},
|
|
194
|
+
"required": ["name"]
|
|
195
|
+
});
|
|
196
|
+
let request_validator = Arc::new(SchemaValidator::new(schema).unwrap());
|
|
197
|
+
|
|
198
|
+
let handler = Arc::new(MockHandler {
|
|
199
|
+
should_fail_prepare: false,
|
|
200
|
+
should_fail_invoke: false,
|
|
201
|
+
should_fail_interpret: false,
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
let executor = HandlerExecutor::new(handler, Some(request_validator));
|
|
205
|
+
|
|
206
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
207
|
+
|
|
208
|
+
let mut headers = HashMap::new();
|
|
209
|
+
headers.insert("x-api-key".to_string(), "test-key".to_string());
|
|
210
|
+
|
|
211
|
+
let request_data = RequestData {
|
|
212
|
+
path_params: Arc::new(HashMap::new()),
|
|
213
|
+
query_params: json!({}),
|
|
214
|
+
validated_params: None,
|
|
215
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
216
|
+
body: json!({"name": "test"}),
|
|
217
|
+
raw_body: None,
|
|
218
|
+
headers: Arc::new(headers),
|
|
219
|
+
cookies: Arc::new(HashMap::new()),
|
|
220
|
+
method: "POST".to_string(),
|
|
221
|
+
path: "/test".to_string(),
|
|
222
|
+
dependencies: None,
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
let result = executor.call(request, request_data).await;
|
|
226
|
+
assert!(result.is_ok());
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
#[test]
|
|
230
|
+
fn test_handler_error_from_validation_error() {
|
|
231
|
+
let validation_error = ValidationError {
|
|
232
|
+
errors: vec![ValidationErrorDetail {
|
|
233
|
+
error_type: "missing".to_string(),
|
|
234
|
+
loc: vec!["body".to_string(), "field".to_string()],
|
|
235
|
+
msg: "Field required".to_string(),
|
|
236
|
+
input: json!(null),
|
|
237
|
+
ctx: None,
|
|
238
|
+
}],
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
let handler_error: HandlerError = validation_error.into();
|
|
242
|
+
assert!(matches!(handler_error, HandlerError::Validation(_)));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
#[tokio::test]
|
|
246
|
+
async fn test_handler_executor_builder_pattern() {
|
|
247
|
+
let schema = json!({
|
|
248
|
+
"type": "object",
|
|
249
|
+
"properties": {
|
|
250
|
+
"email": {"type": "string", "format": "email"}
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
let request_validator = Arc::new(SchemaValidator::new(schema).unwrap());
|
|
254
|
+
|
|
255
|
+
let handler = Arc::new(MockHandler {
|
|
256
|
+
should_fail_prepare: false,
|
|
257
|
+
should_fail_invoke: false,
|
|
258
|
+
should_fail_interpret: false,
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
let executor = HandlerExecutor::with_handler(handler).with_request_validator(request_validator);
|
|
262
|
+
|
|
263
|
+
let request = Request::builder().body(Body::empty()).unwrap();
|
|
264
|
+
let request_data = RequestData {
|
|
265
|
+
path_params: Arc::new(HashMap::new()),
|
|
266
|
+
query_params: json!({}),
|
|
267
|
+
validated_params: None,
|
|
268
|
+
raw_query_params: Arc::new(HashMap::new()),
|
|
269
|
+
body: json!({"email": "test@example.com"}),
|
|
270
|
+
raw_body: None,
|
|
271
|
+
headers: Arc::new(HashMap::new()),
|
|
272
|
+
cookies: Arc::new(HashMap::new()),
|
|
273
|
+
method: "POST".to_string(),
|
|
274
|
+
path: "/test".to_string(),
|
|
275
|
+
dependencies: None,
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
let result = executor.call(request, request_data).await;
|
|
279
|
+
assert!(result.is_ok());
|
|
280
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "spikard-core"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.5.0"
|
|
4
4
|
edition = "2024"
|
|
5
5
|
authors = ["Na'aman Hirschfeld <nhirschfeld@gmail.com>"]
|
|
6
6
|
license = "MIT"
|
|
@@ -19,16 +19,16 @@ tracing = "0.1"
|
|
|
19
19
|
anyhow = "1.0"
|
|
20
20
|
jsonschema = { version = "0.37", default-features = false }
|
|
21
21
|
regex = "1"
|
|
22
|
-
flate2 = "1.1"
|
|
22
|
+
flate2 = { version = "=1.1.5", default-features = false, features = ["rust_backend"] }
|
|
23
23
|
brotli = "8.0"
|
|
24
24
|
http = "1.4"
|
|
25
25
|
base64 = "0.22"
|
|
26
26
|
serde_qs = "0.15"
|
|
27
27
|
url = "2.5"
|
|
28
28
|
jiff = "0.2"
|
|
29
|
-
uuid = "1.
|
|
29
|
+
uuid = "1.19"
|
|
30
30
|
indexmap = "2.12"
|
|
31
|
-
tokio = { version = "1", features = ["
|
|
31
|
+
tokio = { version = "1", optional = true, default-features = false, features = ["rt", "macros", "sync", "time"] }
|
|
32
32
|
bytes = { version = "1.11", optional = true }
|
|
33
33
|
thiserror = { version = "2.0", optional = true }
|
|
34
34
|
|