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,861 @@
|
|
|
1
|
+
//! Handler trait for language-agnostic request handling
|
|
2
|
+
//!
|
|
3
|
+
//! This module defines the core trait that all language bindings must implement.
|
|
4
|
+
//! It's completely language-agnostic - no Python, Node, or WASM knowledge.
|
|
5
|
+
|
|
6
|
+
use axum::{
|
|
7
|
+
body::Body,
|
|
8
|
+
http::{Request, Response, StatusCode},
|
|
9
|
+
};
|
|
10
|
+
use serde::{Deserialize, Serialize};
|
|
11
|
+
use serde_json::Value;
|
|
12
|
+
use std::collections::HashMap;
|
|
13
|
+
use std::future::Future;
|
|
14
|
+
use std::pin::Pin;
|
|
15
|
+
|
|
16
|
+
/// Request data extracted from HTTP request
|
|
17
|
+
/// This is the language-agnostic representation passed to handlers
|
|
18
|
+
///
|
|
19
|
+
/// Uses Arc for HashMaps to enable cheap cloning without duplicating data.
|
|
20
|
+
/// When RequestData is cloned, only the Arc pointers are cloned, not the underlying data.
|
|
21
|
+
///
|
|
22
|
+
/// Performance optimization: raw_body stores the unparsed request body bytes.
|
|
23
|
+
/// Language bindings should use raw_body when possible to avoid double-parsing.
|
|
24
|
+
/// The body field is lazily parsed only when needed for validation.
|
|
25
|
+
#[derive(Debug, Clone)]
|
|
26
|
+
pub struct RequestData {
|
|
27
|
+
pub path_params: std::sync::Arc<HashMap<String, String>>,
|
|
28
|
+
pub query_params: Value,
|
|
29
|
+
pub raw_query_params: std::sync::Arc<HashMap<String, Vec<String>>>,
|
|
30
|
+
pub body: Value,
|
|
31
|
+
pub raw_body: Option<bytes::Bytes>,
|
|
32
|
+
pub headers: std::sync::Arc<HashMap<String, String>>,
|
|
33
|
+
pub cookies: std::sync::Arc<HashMap<String, String>>,
|
|
34
|
+
pub method: String,
|
|
35
|
+
pub path: String,
|
|
36
|
+
/// Resolved dependencies for this request (populated by DependencyInjectingHandler)
|
|
37
|
+
#[cfg(feature = "di")]
|
|
38
|
+
pub dependencies: Option<std::sync::Arc<spikard_core::di::ResolvedDependencies>>,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
impl Serialize for RequestData {
|
|
42
|
+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
43
|
+
where
|
|
44
|
+
S: serde::Serializer,
|
|
45
|
+
{
|
|
46
|
+
use serde::ser::SerializeStruct;
|
|
47
|
+
#[cfg(feature = "di")]
|
|
48
|
+
let field_count = 10;
|
|
49
|
+
#[cfg(not(feature = "di"))]
|
|
50
|
+
let field_count = 9;
|
|
51
|
+
|
|
52
|
+
let mut state = serializer.serialize_struct("RequestData", field_count)?;
|
|
53
|
+
state.serialize_field("path_params", &*self.path_params)?;
|
|
54
|
+
state.serialize_field("query_params", &self.query_params)?;
|
|
55
|
+
state.serialize_field("raw_query_params", &*self.raw_query_params)?;
|
|
56
|
+
state.serialize_field("body", &self.body)?;
|
|
57
|
+
state.serialize_field("raw_body", &self.raw_body.as_ref().map(|b| b.as_ref()))?;
|
|
58
|
+
state.serialize_field("headers", &*self.headers)?;
|
|
59
|
+
state.serialize_field("cookies", &*self.cookies)?;
|
|
60
|
+
state.serialize_field("method", &self.method)?;
|
|
61
|
+
state.serialize_field("path", &self.path)?;
|
|
62
|
+
|
|
63
|
+
#[cfg(feature = "di")]
|
|
64
|
+
{
|
|
65
|
+
// Dependencies field is not serialized (contains Arc<dyn Any>)
|
|
66
|
+
// We just serialize a marker indicating whether dependencies exist
|
|
67
|
+
state.serialize_field("has_dependencies", &self.dependencies.is_some())?;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
state.end()
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
impl<'de> Deserialize<'de> for RequestData {
|
|
75
|
+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
76
|
+
where
|
|
77
|
+
D: serde::Deserializer<'de>,
|
|
78
|
+
{
|
|
79
|
+
#[derive(Deserialize)]
|
|
80
|
+
#[serde(field_identifier, rename_all = "snake_case")]
|
|
81
|
+
enum Field {
|
|
82
|
+
PathParams,
|
|
83
|
+
QueryParams,
|
|
84
|
+
RawQueryParams,
|
|
85
|
+
Body,
|
|
86
|
+
RawBody,
|
|
87
|
+
Headers,
|
|
88
|
+
Cookies,
|
|
89
|
+
Method,
|
|
90
|
+
Path,
|
|
91
|
+
#[cfg(feature = "di")]
|
|
92
|
+
HasDependencies,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
struct RequestDataVisitor;
|
|
96
|
+
|
|
97
|
+
impl<'de> serde::de::Visitor<'de> for RequestDataVisitor {
|
|
98
|
+
type Value = RequestData;
|
|
99
|
+
|
|
100
|
+
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
101
|
+
formatter.write_str("struct RequestData")
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
fn visit_map<V>(self, mut map: V) -> Result<RequestData, V::Error>
|
|
105
|
+
where
|
|
106
|
+
V: serde::de::MapAccess<'de>,
|
|
107
|
+
{
|
|
108
|
+
let mut path_params = None;
|
|
109
|
+
let mut query_params = None;
|
|
110
|
+
let mut raw_query_params = None;
|
|
111
|
+
let mut body = None;
|
|
112
|
+
let mut raw_body = None;
|
|
113
|
+
let mut headers = None;
|
|
114
|
+
let mut cookies = None;
|
|
115
|
+
let mut method = None;
|
|
116
|
+
let mut path = None;
|
|
117
|
+
|
|
118
|
+
while let Some(key) = map.next_key()? {
|
|
119
|
+
match key {
|
|
120
|
+
Field::PathParams => {
|
|
121
|
+
path_params = Some(std::sync::Arc::new(map.next_value()?));
|
|
122
|
+
}
|
|
123
|
+
Field::QueryParams => {
|
|
124
|
+
query_params = Some(map.next_value()?);
|
|
125
|
+
}
|
|
126
|
+
Field::RawQueryParams => {
|
|
127
|
+
raw_query_params = Some(std::sync::Arc::new(map.next_value()?));
|
|
128
|
+
}
|
|
129
|
+
Field::Body => {
|
|
130
|
+
body = Some(map.next_value()?);
|
|
131
|
+
}
|
|
132
|
+
Field::RawBody => {
|
|
133
|
+
let bytes_vec: Option<Vec<u8>> = map.next_value()?;
|
|
134
|
+
raw_body = bytes_vec.map(bytes::Bytes::from);
|
|
135
|
+
}
|
|
136
|
+
Field::Headers => {
|
|
137
|
+
headers = Some(std::sync::Arc::new(map.next_value()?));
|
|
138
|
+
}
|
|
139
|
+
Field::Cookies => {
|
|
140
|
+
cookies = Some(std::sync::Arc::new(map.next_value()?));
|
|
141
|
+
}
|
|
142
|
+
Field::Method => {
|
|
143
|
+
method = Some(map.next_value()?);
|
|
144
|
+
}
|
|
145
|
+
Field::Path => {
|
|
146
|
+
path = Some(map.next_value()?);
|
|
147
|
+
}
|
|
148
|
+
#[cfg(feature = "di")]
|
|
149
|
+
Field::HasDependencies => {
|
|
150
|
+
// We skip this field as dependencies can't be deserialized
|
|
151
|
+
let _: bool = map.next_value()?;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
Ok(RequestData {
|
|
157
|
+
path_params: path_params.ok_or_else(|| serde::de::Error::missing_field("path_params"))?,
|
|
158
|
+
query_params: query_params.ok_or_else(|| serde::de::Error::missing_field("query_params"))?,
|
|
159
|
+
raw_query_params: raw_query_params
|
|
160
|
+
.ok_or_else(|| serde::de::Error::missing_field("raw_query_params"))?,
|
|
161
|
+
body: body.ok_or_else(|| serde::de::Error::missing_field("body"))?,
|
|
162
|
+
raw_body,
|
|
163
|
+
headers: headers.ok_or_else(|| serde::de::Error::missing_field("headers"))?,
|
|
164
|
+
cookies: cookies.ok_or_else(|| serde::de::Error::missing_field("cookies"))?,
|
|
165
|
+
method: method.ok_or_else(|| serde::de::Error::missing_field("method"))?,
|
|
166
|
+
path: path.ok_or_else(|| serde::de::Error::missing_field("path"))?,
|
|
167
|
+
#[cfg(feature = "di")]
|
|
168
|
+
dependencies: None,
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
#[cfg(feature = "di")]
|
|
174
|
+
const FIELDS: &[&str] = &[
|
|
175
|
+
"path_params",
|
|
176
|
+
"query_params",
|
|
177
|
+
"raw_query_params",
|
|
178
|
+
"body",
|
|
179
|
+
"raw_body",
|
|
180
|
+
"headers",
|
|
181
|
+
"cookies",
|
|
182
|
+
"method",
|
|
183
|
+
"path",
|
|
184
|
+
"has_dependencies",
|
|
185
|
+
];
|
|
186
|
+
|
|
187
|
+
#[cfg(not(feature = "di"))]
|
|
188
|
+
const FIELDS: &[&str] = &[
|
|
189
|
+
"path_params",
|
|
190
|
+
"query_params",
|
|
191
|
+
"raw_query_params",
|
|
192
|
+
"body",
|
|
193
|
+
"raw_body",
|
|
194
|
+
"headers",
|
|
195
|
+
"cookies",
|
|
196
|
+
"method",
|
|
197
|
+
"path",
|
|
198
|
+
];
|
|
199
|
+
|
|
200
|
+
deserializer.deserialize_struct("RequestData", FIELDS, RequestDataVisitor)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/// Result type for handlers
|
|
205
|
+
pub type HandlerResult = Result<Response<Body>, (StatusCode, String)>;
|
|
206
|
+
|
|
207
|
+
/// Handler trait that all language bindings must implement
|
|
208
|
+
///
|
|
209
|
+
/// This trait is completely language-agnostic. Each binding (Python, Node, WASM)
|
|
210
|
+
/// implements this trait to bridge their runtime to our HTTP server.
|
|
211
|
+
pub trait Handler: Send + Sync {
|
|
212
|
+
/// Handle an HTTP request
|
|
213
|
+
///
|
|
214
|
+
/// Takes the extracted request data and returns a future that resolves to either:
|
|
215
|
+
/// - Ok(Response): A successful HTTP response
|
|
216
|
+
/// - Err((StatusCode, String)): An error with status code and message
|
|
217
|
+
fn call(
|
|
218
|
+
&self,
|
|
219
|
+
request: Request<Body>,
|
|
220
|
+
request_data: RequestData,
|
|
221
|
+
) -> Pin<Box<dyn Future<Output = HandlerResult> + Send + '_>>;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/// Validated parameters from request (path, query, headers, cookies)
|
|
225
|
+
#[derive(Debug, Clone)]
|
|
226
|
+
pub struct ValidatedParams {
|
|
227
|
+
pub params: HashMap<String, Value>,
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
#[cfg(test)]
|
|
231
|
+
mod tests {
|
|
232
|
+
use super::*;
|
|
233
|
+
use std::collections::HashMap;
|
|
234
|
+
|
|
235
|
+
// Helper function to create a minimal RequestData for testing
|
|
236
|
+
fn minimal_request_data() -> RequestData {
|
|
237
|
+
RequestData {
|
|
238
|
+
path_params: std::sync::Arc::new(HashMap::new()),
|
|
239
|
+
query_params: Value::Object(serde_json::Map::new()),
|
|
240
|
+
raw_query_params: std::sync::Arc::new(HashMap::new()),
|
|
241
|
+
body: Value::Null,
|
|
242
|
+
raw_body: None,
|
|
243
|
+
headers: std::sync::Arc::new(HashMap::new()),
|
|
244
|
+
cookies: std::sync::Arc::new(HashMap::new()),
|
|
245
|
+
method: "GET".to_string(),
|
|
246
|
+
path: "/".to_string(),
|
|
247
|
+
#[cfg(feature = "di")]
|
|
248
|
+
dependencies: None,
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
#[test]
|
|
253
|
+
fn test_request_data_serialization_minimal() {
|
|
254
|
+
// Arrange: Create minimal RequestData
|
|
255
|
+
let data = minimal_request_data();
|
|
256
|
+
|
|
257
|
+
// Act: Serialize to JSON
|
|
258
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
259
|
+
|
|
260
|
+
// Assert: All required fields are present
|
|
261
|
+
assert!(json["path_params"].is_object());
|
|
262
|
+
assert!(json["query_params"].is_object());
|
|
263
|
+
assert!(json["raw_query_params"].is_object());
|
|
264
|
+
assert!(json["body"].is_null());
|
|
265
|
+
assert!(json["headers"].is_object());
|
|
266
|
+
assert!(json["cookies"].is_object());
|
|
267
|
+
assert_eq!(json["method"], "GET");
|
|
268
|
+
assert_eq!(json["path"], "/");
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
#[test]
|
|
272
|
+
fn test_request_data_serialization_with_path_params() {
|
|
273
|
+
// Arrange
|
|
274
|
+
let mut path_params = HashMap::new();
|
|
275
|
+
path_params.insert("id".to_string(), "123".to_string());
|
|
276
|
+
path_params.insert("username".to_string(), "alice".to_string());
|
|
277
|
+
|
|
278
|
+
let data = RequestData {
|
|
279
|
+
path_params: std::sync::Arc::new(path_params),
|
|
280
|
+
..minimal_request_data()
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
// Act
|
|
284
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
285
|
+
|
|
286
|
+
// Assert
|
|
287
|
+
assert_eq!(json["path_params"]["id"], "123");
|
|
288
|
+
assert_eq!(json["path_params"]["username"], "alice");
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
#[test]
|
|
292
|
+
fn test_request_data_serialization_with_query_params() {
|
|
293
|
+
// Arrange
|
|
294
|
+
let query_params = serde_json::json!({
|
|
295
|
+
"filter": "active",
|
|
296
|
+
"limit": 10,
|
|
297
|
+
"sort": "name"
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
let data = RequestData {
|
|
301
|
+
query_params,
|
|
302
|
+
..minimal_request_data()
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
// Act
|
|
306
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
307
|
+
|
|
308
|
+
// Assert
|
|
309
|
+
assert_eq!(json["query_params"]["filter"], "active");
|
|
310
|
+
assert_eq!(json["query_params"]["limit"], 10);
|
|
311
|
+
assert_eq!(json["query_params"]["sort"], "name");
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
#[test]
|
|
315
|
+
fn test_request_data_serialization_with_raw_query_params() {
|
|
316
|
+
// Arrange
|
|
317
|
+
let mut raw_query_params = HashMap::new();
|
|
318
|
+
raw_query_params.insert("tags".to_string(), vec!["rust".to_string(), "web".to_string()]);
|
|
319
|
+
raw_query_params.insert("category".to_string(), vec!["backend".to_string()]);
|
|
320
|
+
|
|
321
|
+
let data = RequestData {
|
|
322
|
+
raw_query_params: std::sync::Arc::new(raw_query_params),
|
|
323
|
+
..minimal_request_data()
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
// Act
|
|
327
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
328
|
+
|
|
329
|
+
// Assert
|
|
330
|
+
assert!(json["raw_query_params"]["tags"].is_array());
|
|
331
|
+
assert_eq!(json["raw_query_params"]["tags"][0], "rust");
|
|
332
|
+
assert_eq!(json["raw_query_params"]["tags"][1], "web");
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
#[test]
|
|
336
|
+
fn test_request_data_serialization_with_headers() {
|
|
337
|
+
// Arrange
|
|
338
|
+
let mut headers = HashMap::new();
|
|
339
|
+
headers.insert("content-type".to_string(), "application/json".to_string());
|
|
340
|
+
headers.insert("authorization".to_string(), "Bearer token123".to_string());
|
|
341
|
+
headers.insert("user-agent".to_string(), "test-client/1.0".to_string());
|
|
342
|
+
|
|
343
|
+
let data = RequestData {
|
|
344
|
+
headers: std::sync::Arc::new(headers),
|
|
345
|
+
..minimal_request_data()
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
// Act
|
|
349
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
350
|
+
|
|
351
|
+
// Assert
|
|
352
|
+
assert_eq!(json["headers"]["content-type"], "application/json");
|
|
353
|
+
assert_eq!(json["headers"]["authorization"], "Bearer token123");
|
|
354
|
+
assert_eq!(json["headers"]["user-agent"], "test-client/1.0");
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
#[test]
|
|
358
|
+
fn test_request_data_serialization_with_cookies() {
|
|
359
|
+
// Arrange
|
|
360
|
+
let mut cookies = HashMap::new();
|
|
361
|
+
cookies.insert("session_id".to_string(), "abc123def456".to_string());
|
|
362
|
+
cookies.insert("preferences".to_string(), "dark_mode=true".to_string());
|
|
363
|
+
|
|
364
|
+
let data = RequestData {
|
|
365
|
+
cookies: std::sync::Arc::new(cookies),
|
|
366
|
+
..minimal_request_data()
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
// Act
|
|
370
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
371
|
+
|
|
372
|
+
// Assert
|
|
373
|
+
assert_eq!(json["cookies"]["session_id"], "abc123def456");
|
|
374
|
+
assert_eq!(json["cookies"]["preferences"], "dark_mode=true");
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
#[test]
|
|
378
|
+
fn test_request_data_serialization_with_json_body() {
|
|
379
|
+
// Arrange
|
|
380
|
+
let body = serde_json::json!({
|
|
381
|
+
"name": "test",
|
|
382
|
+
"age": 30,
|
|
383
|
+
"active": true,
|
|
384
|
+
"tags": ["a", "b"]
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
let data = RequestData {
|
|
388
|
+
body,
|
|
389
|
+
..minimal_request_data()
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
// Act
|
|
393
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
394
|
+
|
|
395
|
+
// Assert
|
|
396
|
+
assert_eq!(json["body"]["name"], "test");
|
|
397
|
+
assert_eq!(json["body"]["age"], 30);
|
|
398
|
+
assert_eq!(json["body"]["active"], true);
|
|
399
|
+
assert!(json["body"]["tags"].is_array());
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
#[test]
|
|
403
|
+
fn test_request_data_serialization_with_raw_body() {
|
|
404
|
+
// Arrange
|
|
405
|
+
let raw_body = bytes::Bytes::from("raw body content");
|
|
406
|
+
let data = RequestData {
|
|
407
|
+
raw_body: Some(raw_body),
|
|
408
|
+
..minimal_request_data()
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
// Act
|
|
412
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
413
|
+
|
|
414
|
+
// Assert
|
|
415
|
+
assert!(json["raw_body"].is_array());
|
|
416
|
+
// Bytes are serialized as arrays of u8
|
|
417
|
+
let serialized_bytes: Vec<u8> =
|
|
418
|
+
serde_json::from_value(json["raw_body"].clone()).expect("failed to deserialize bytes");
|
|
419
|
+
assert_eq!(serialized_bytes, b"raw body content".to_vec());
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
#[test]
|
|
423
|
+
fn test_request_data_serialization_with_empty_strings() {
|
|
424
|
+
// Arrange
|
|
425
|
+
let mut headers = HashMap::new();
|
|
426
|
+
headers.insert("x-empty".to_string(), "".to_string());
|
|
427
|
+
|
|
428
|
+
let data = RequestData {
|
|
429
|
+
method: "".to_string(),
|
|
430
|
+
path: "".to_string(),
|
|
431
|
+
headers: std::sync::Arc::new(headers),
|
|
432
|
+
..minimal_request_data()
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
// Act
|
|
436
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
437
|
+
|
|
438
|
+
// Assert
|
|
439
|
+
assert_eq!(json["method"], "");
|
|
440
|
+
assert_eq!(json["path"], "");
|
|
441
|
+
assert_eq!(json["headers"]["x-empty"], "");
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
#[test]
|
|
445
|
+
fn test_request_data_serialization_with_nested_json_body() {
|
|
446
|
+
// Arrange
|
|
447
|
+
let body = serde_json::json!({
|
|
448
|
+
"user": {
|
|
449
|
+
"profile": {
|
|
450
|
+
"name": "Alice",
|
|
451
|
+
"contact": {
|
|
452
|
+
"email": "alice@example.com",
|
|
453
|
+
"phone": null
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
},
|
|
457
|
+
"settings": {
|
|
458
|
+
"notifications": [true, false, true]
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
let data = RequestData {
|
|
463
|
+
body,
|
|
464
|
+
..minimal_request_data()
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
// Act
|
|
468
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
469
|
+
|
|
470
|
+
// Assert
|
|
471
|
+
assert_eq!(json["body"]["user"]["profile"]["name"], "Alice");
|
|
472
|
+
assert_eq!(json["body"]["user"]["profile"]["contact"]["email"], "alice@example.com");
|
|
473
|
+
assert!(json["body"]["user"]["profile"]["contact"]["phone"].is_null());
|
|
474
|
+
assert_eq!(json["body"]["settings"]["notifications"][0], true);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
#[test]
|
|
478
|
+
fn test_request_data_serialization_all_fields_complete() {
|
|
479
|
+
// Arrange: Create RequestData with all fields populated
|
|
480
|
+
let mut path_params = HashMap::new();
|
|
481
|
+
path_params.insert("id".to_string(), "42".to_string());
|
|
482
|
+
|
|
483
|
+
let mut raw_query_params = HashMap::new();
|
|
484
|
+
raw_query_params.insert("filter".to_string(), vec!["active".to_string()]);
|
|
485
|
+
|
|
486
|
+
let mut headers = HashMap::new();
|
|
487
|
+
headers.insert("content-type".to_string(), "application/json".to_string());
|
|
488
|
+
|
|
489
|
+
let mut cookies = HashMap::new();
|
|
490
|
+
cookies.insert("session".to_string(), "xyz789".to_string());
|
|
491
|
+
|
|
492
|
+
let body = serde_json::json!({"action": "create"});
|
|
493
|
+
let raw_body = bytes::Bytes::from("body bytes");
|
|
494
|
+
|
|
495
|
+
let data = RequestData {
|
|
496
|
+
path_params: std::sync::Arc::new(path_params),
|
|
497
|
+
query_params: serde_json::json!({"page": 1}),
|
|
498
|
+
raw_query_params: std::sync::Arc::new(raw_query_params),
|
|
499
|
+
body,
|
|
500
|
+
raw_body: Some(raw_body),
|
|
501
|
+
headers: std::sync::Arc::new(headers),
|
|
502
|
+
cookies: std::sync::Arc::new(cookies),
|
|
503
|
+
method: "POST".to_string(),
|
|
504
|
+
path: "/api/users".to_string(),
|
|
505
|
+
#[cfg(feature = "di")]
|
|
506
|
+
dependencies: None,
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
// Act
|
|
510
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
511
|
+
|
|
512
|
+
// Assert: Verify all fields are serialized correctly
|
|
513
|
+
assert_eq!(json["path_params"]["id"], "42");
|
|
514
|
+
assert_eq!(json["query_params"]["page"], 1);
|
|
515
|
+
assert_eq!(json["raw_query_params"]["filter"][0], "active");
|
|
516
|
+
assert_eq!(json["body"]["action"], "create");
|
|
517
|
+
assert!(json["raw_body"].is_array());
|
|
518
|
+
assert_eq!(json["headers"]["content-type"], "application/json");
|
|
519
|
+
assert_eq!(json["cookies"]["session"], "xyz789");
|
|
520
|
+
assert_eq!(json["method"], "POST");
|
|
521
|
+
assert_eq!(json["path"], "/api/users");
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
#[test]
|
|
525
|
+
fn test_request_data_clone_shares_arc_data() {
|
|
526
|
+
// Arrange
|
|
527
|
+
let mut path_params = HashMap::new();
|
|
528
|
+
path_params.insert("id".to_string(), "original".to_string());
|
|
529
|
+
|
|
530
|
+
let data1 = RequestData {
|
|
531
|
+
path_params: std::sync::Arc::new(path_params),
|
|
532
|
+
..minimal_request_data()
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
// Act: Clone the data
|
|
536
|
+
let data2 = data1.clone();
|
|
537
|
+
|
|
538
|
+
// Assert: Both share the same Arc data (pointer equality)
|
|
539
|
+
assert!(std::sync::Arc::ptr_eq(&data1.path_params, &data2.path_params));
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
#[test]
|
|
543
|
+
fn test_request_data_deserialization_complete() {
|
|
544
|
+
// Arrange
|
|
545
|
+
let json = serde_json::json!({
|
|
546
|
+
"path_params": {"id": "123"},
|
|
547
|
+
"query_params": {"filter": "active"},
|
|
548
|
+
"raw_query_params": {"tags": ["rust", "web"]},
|
|
549
|
+
"body": {"name": "test"},
|
|
550
|
+
"raw_body": null,
|
|
551
|
+
"headers": {"content-type": "application/json"},
|
|
552
|
+
"cookies": {"session": "abc"},
|
|
553
|
+
"method": "POST",
|
|
554
|
+
"path": "/api/test"
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
// Act
|
|
558
|
+
let data: RequestData = serde_json::from_value(json).expect("deserialization failed");
|
|
559
|
+
|
|
560
|
+
// Assert
|
|
561
|
+
assert_eq!(data.path_params.get("id").unwrap(), "123");
|
|
562
|
+
assert_eq!(data.query_params["filter"], "active");
|
|
563
|
+
assert_eq!(data.raw_query_params.get("tags").unwrap()[0], "rust");
|
|
564
|
+
assert_eq!(data.body["name"], "test");
|
|
565
|
+
assert!(data.raw_body.is_none());
|
|
566
|
+
assert_eq!(data.headers.get("content-type").unwrap(), "application/json");
|
|
567
|
+
assert_eq!(data.cookies.get("session").unwrap(), "abc");
|
|
568
|
+
assert_eq!(data.method, "POST");
|
|
569
|
+
assert_eq!(data.path, "/api/test");
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
#[test]
|
|
573
|
+
fn test_request_data_deserialization_with_raw_body_bytes() {
|
|
574
|
+
// Arrange: raw_body is serialized as array of u8
|
|
575
|
+
let json = serde_json::json!({
|
|
576
|
+
"path_params": {},
|
|
577
|
+
"query_params": {},
|
|
578
|
+
"raw_query_params": {},
|
|
579
|
+
"body": null,
|
|
580
|
+
"raw_body": [72, 101, 108, 108, 111], // "Hello"
|
|
581
|
+
"headers": {},
|
|
582
|
+
"cookies": {},
|
|
583
|
+
"method": "GET",
|
|
584
|
+
"path": "/"
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
// Act
|
|
588
|
+
let data: RequestData = serde_json::from_value(json).expect("deserialization failed");
|
|
589
|
+
|
|
590
|
+
// Assert
|
|
591
|
+
assert!(data.raw_body.is_some());
|
|
592
|
+
assert_eq!(data.raw_body.unwrap().as_ref(), b"Hello");
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
#[test]
|
|
596
|
+
fn test_request_data_deserialization_missing_required_field_path_params() {
|
|
597
|
+
// Arrange: Missing path_params
|
|
598
|
+
let json = serde_json::json!({
|
|
599
|
+
"query_params": {},
|
|
600
|
+
"raw_query_params": {},
|
|
601
|
+
"body": null,
|
|
602
|
+
"headers": {},
|
|
603
|
+
"cookies": {},
|
|
604
|
+
"method": "GET",
|
|
605
|
+
"path": "/"
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
// Act & Assert
|
|
609
|
+
let result: Result<RequestData, _> = serde_json::from_value(json);
|
|
610
|
+
assert!(result.is_err());
|
|
611
|
+
assert!(result.unwrap_err().to_string().contains("path_params"));
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
#[test]
|
|
615
|
+
fn test_request_data_deserialization_missing_required_field_method() {
|
|
616
|
+
// Arrange: Missing method
|
|
617
|
+
let json = serde_json::json!({
|
|
618
|
+
"path_params": {},
|
|
619
|
+
"query_params": {},
|
|
620
|
+
"raw_query_params": {},
|
|
621
|
+
"body": null,
|
|
622
|
+
"headers": {},
|
|
623
|
+
"cookies": {},
|
|
624
|
+
"path": "/"
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
// Act & Assert
|
|
628
|
+
let result: Result<RequestData, _> = serde_json::from_value(json);
|
|
629
|
+
assert!(result.is_err());
|
|
630
|
+
assert!(result.unwrap_err().to_string().contains("method"));
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
#[test]
|
|
634
|
+
fn test_request_data_serialization_roundtrip() {
|
|
635
|
+
// Arrange: Create complex RequestData
|
|
636
|
+
let original = RequestData {
|
|
637
|
+
path_params: std::sync::Arc::new({
|
|
638
|
+
let mut map = HashMap::new();
|
|
639
|
+
map.insert("id".to_string(), "999".to_string());
|
|
640
|
+
map
|
|
641
|
+
}),
|
|
642
|
+
query_params: serde_json::json!({"limit": 50, "offset": 10}),
|
|
643
|
+
raw_query_params: std::sync::Arc::new({
|
|
644
|
+
let mut map = HashMap::new();
|
|
645
|
+
map.insert("sort".to_string(), vec!["name".to_string(), "date".to_string()]);
|
|
646
|
+
map
|
|
647
|
+
}),
|
|
648
|
+
body: serde_json::json!({"title": "New Post", "content": "Hello World"}),
|
|
649
|
+
raw_body: None,
|
|
650
|
+
headers: std::sync::Arc::new({
|
|
651
|
+
let mut map = HashMap::new();
|
|
652
|
+
map.insert("accept".to_string(), "application/json".to_string());
|
|
653
|
+
map
|
|
654
|
+
}),
|
|
655
|
+
cookies: std::sync::Arc::new({
|
|
656
|
+
let mut map = HashMap::new();
|
|
657
|
+
map.insert("user_id".to_string(), "user42".to_string());
|
|
658
|
+
map
|
|
659
|
+
}),
|
|
660
|
+
method: "PUT".to_string(),
|
|
661
|
+
path: "/blog/posts/999".to_string(),
|
|
662
|
+
#[cfg(feature = "di")]
|
|
663
|
+
dependencies: None,
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
// Act: Serialize then deserialize
|
|
667
|
+
let json = serde_json::to_value(&original).expect("serialization failed");
|
|
668
|
+
let restored: RequestData = serde_json::from_value(json).expect("deserialization failed");
|
|
669
|
+
|
|
670
|
+
// Assert: All fields match
|
|
671
|
+
assert_eq!(*original.path_params, *restored.path_params);
|
|
672
|
+
assert_eq!(original.query_params, restored.query_params);
|
|
673
|
+
assert_eq!(*original.raw_query_params, *restored.raw_query_params);
|
|
674
|
+
assert_eq!(original.body, restored.body);
|
|
675
|
+
assert_eq!(original.raw_body, restored.raw_body);
|
|
676
|
+
assert_eq!(*original.headers, *restored.headers);
|
|
677
|
+
assert_eq!(*original.cookies, *restored.cookies);
|
|
678
|
+
assert_eq!(original.method, restored.method);
|
|
679
|
+
assert_eq!(original.path, restored.path);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
#[test]
|
|
683
|
+
fn test_request_data_serialization_large_body() {
|
|
684
|
+
// Arrange: Create RequestData with large nested body
|
|
685
|
+
let mut large_object = serde_json::Map::new();
|
|
686
|
+
for i in 0..100 {
|
|
687
|
+
large_object.insert(format!("key_{}", i), serde_json::Value::String(format!("value_{}", i)));
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
let data = RequestData {
|
|
691
|
+
body: Value::Object(large_object),
|
|
692
|
+
..minimal_request_data()
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
// Act
|
|
696
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
697
|
+
|
|
698
|
+
// Assert
|
|
699
|
+
assert!(json["body"].is_object());
|
|
700
|
+
assert_eq!(json["body"].get("key_0").unwrap(), "value_0");
|
|
701
|
+
assert_eq!(json["body"].get("key_99").unwrap(), "value_99");
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
#[test]
|
|
705
|
+
fn test_request_data_empty_collections() {
|
|
706
|
+
// Arrange: All collections are empty
|
|
707
|
+
let data = RequestData {
|
|
708
|
+
path_params: std::sync::Arc::new(HashMap::new()),
|
|
709
|
+
query_params: Value::Object(serde_json::Map::new()),
|
|
710
|
+
raw_query_params: std::sync::Arc::new(HashMap::new()),
|
|
711
|
+
body: Value::Object(serde_json::Map::new()),
|
|
712
|
+
raw_body: None,
|
|
713
|
+
headers: std::sync::Arc::new(HashMap::new()),
|
|
714
|
+
cookies: std::sync::Arc::new(HashMap::new()),
|
|
715
|
+
method: "GET".to_string(),
|
|
716
|
+
path: "/".to_string(),
|
|
717
|
+
#[cfg(feature = "di")]
|
|
718
|
+
dependencies: None,
|
|
719
|
+
};
|
|
720
|
+
|
|
721
|
+
// Act
|
|
722
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
723
|
+
|
|
724
|
+
// Assert
|
|
725
|
+
assert_eq!(json["path_params"].as_object().unwrap().len(), 0);
|
|
726
|
+
assert_eq!(json["query_params"].as_object().unwrap().len(), 0);
|
|
727
|
+
assert_eq!(json["raw_query_params"].as_object().unwrap().len(), 0);
|
|
728
|
+
assert_eq!(json["body"].as_object().unwrap().len(), 0);
|
|
729
|
+
assert!(json["raw_body"].is_null());
|
|
730
|
+
assert_eq!(json["headers"].as_object().unwrap().len(), 0);
|
|
731
|
+
assert_eq!(json["cookies"].as_object().unwrap().len(), 0);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
#[test]
|
|
735
|
+
fn test_request_data_special_characters_in_strings() {
|
|
736
|
+
// Arrange: Test special characters and unicode
|
|
737
|
+
let mut headers = HashMap::new();
|
|
738
|
+
headers.insert("x-custom".to_string(), "value with \"quotes\"".to_string());
|
|
739
|
+
headers.insert("x-unicode".to_string(), "Café ☕ 🚀".to_string());
|
|
740
|
+
|
|
741
|
+
let data = RequestData {
|
|
742
|
+
method: "POST".to_string(),
|
|
743
|
+
path: "/api/v1/users\\test".to_string(),
|
|
744
|
+
headers: std::sync::Arc::new(headers),
|
|
745
|
+
body: serde_json::json!({"note": "Contains\nnewline"}),
|
|
746
|
+
..minimal_request_data()
|
|
747
|
+
};
|
|
748
|
+
|
|
749
|
+
// Act
|
|
750
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
751
|
+
|
|
752
|
+
// Assert
|
|
753
|
+
assert_eq!(json["headers"]["x-custom"], "value with \"quotes\"");
|
|
754
|
+
assert_eq!(json["headers"]["x-unicode"], "Café ☕ 🚀");
|
|
755
|
+
assert_eq!(json["path"], "/api/v1/users\\test");
|
|
756
|
+
assert_eq!(json["body"]["note"], "Contains\nnewline");
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
#[test]
|
|
760
|
+
#[cfg(feature = "di")]
|
|
761
|
+
fn test_request_data_serialization_with_di_feature_no_dependencies() {
|
|
762
|
+
// Arrange: With DI feature but no dependencies set
|
|
763
|
+
let data = minimal_request_data();
|
|
764
|
+
|
|
765
|
+
// Act
|
|
766
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
767
|
+
|
|
768
|
+
// Assert: has_dependencies field should be present and false
|
|
769
|
+
assert_eq!(json["has_dependencies"], false);
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
#[test]
|
|
773
|
+
fn test_request_data_method_variants() {
|
|
774
|
+
// Arrange & Act: Test various HTTP methods
|
|
775
|
+
let methods = vec!["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"];
|
|
776
|
+
|
|
777
|
+
for method in methods {
|
|
778
|
+
let data = RequestData {
|
|
779
|
+
method: method.to_string(),
|
|
780
|
+
..minimal_request_data()
|
|
781
|
+
};
|
|
782
|
+
|
|
783
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
784
|
+
|
|
785
|
+
// Assert
|
|
786
|
+
assert_eq!(json["method"], method);
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
#[test]
|
|
791
|
+
fn test_request_data_serialization_null_body() {
|
|
792
|
+
// Arrange
|
|
793
|
+
let data = RequestData {
|
|
794
|
+
body: Value::Null,
|
|
795
|
+
..minimal_request_data()
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
// Act
|
|
799
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
800
|
+
|
|
801
|
+
// Assert
|
|
802
|
+
assert!(json["body"].is_null());
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
#[test]
|
|
806
|
+
fn test_request_data_serialization_array_body() {
|
|
807
|
+
// Arrange
|
|
808
|
+
let data = RequestData {
|
|
809
|
+
body: serde_json::json!([1, 2, 3, "four", {"five": 5}]),
|
|
810
|
+
..minimal_request_data()
|
|
811
|
+
};
|
|
812
|
+
|
|
813
|
+
// Act
|
|
814
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
815
|
+
|
|
816
|
+
// Assert
|
|
817
|
+
assert!(json["body"].is_array());
|
|
818
|
+
assert_eq!(json["body"][0], 1);
|
|
819
|
+
assert_eq!(json["body"][1], 2);
|
|
820
|
+
assert_eq!(json["body"][3], "four");
|
|
821
|
+
assert_eq!(json["body"][4]["five"], 5);
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
#[test]
|
|
825
|
+
fn test_request_data_serialization_numeric_edge_cases() {
|
|
826
|
+
// Arrange
|
|
827
|
+
let data = RequestData {
|
|
828
|
+
body: serde_json::json!({
|
|
829
|
+
"zero": 0,
|
|
830
|
+
"negative": -42,
|
|
831
|
+
"large": 9223372036854775807i64,
|
|
832
|
+
"float": 3.14159
|
|
833
|
+
}),
|
|
834
|
+
..minimal_request_data()
|
|
835
|
+
};
|
|
836
|
+
|
|
837
|
+
// Act
|
|
838
|
+
let json = serde_json::to_value(&data).expect("serialization failed");
|
|
839
|
+
|
|
840
|
+
// Assert
|
|
841
|
+
assert_eq!(json["body"]["zero"], 0);
|
|
842
|
+
assert_eq!(json["body"]["negative"], -42);
|
|
843
|
+
assert_eq!(json["body"]["large"], 9223372036854775807i64);
|
|
844
|
+
assert_eq!(json["body"]["float"], 3.14159);
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
#[test]
|
|
848
|
+
fn test_validated_params_basic_creation() {
|
|
849
|
+
// Arrange
|
|
850
|
+
let mut params = HashMap::new();
|
|
851
|
+
params.insert("id".to_string(), Value::String("123".to_string()));
|
|
852
|
+
params.insert("active".to_string(), Value::Bool(true));
|
|
853
|
+
|
|
854
|
+
// Act
|
|
855
|
+
let validated = ValidatedParams { params };
|
|
856
|
+
|
|
857
|
+
// Assert
|
|
858
|
+
assert_eq!(validated.params.get("id").unwrap(), &Value::String("123".to_string()));
|
|
859
|
+
assert_eq!(validated.params.get("active").unwrap(), &Value::Bool(true));
|
|
860
|
+
}
|
|
861
|
+
}
|