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.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +21 -6
  3. data/ext/spikard_rb/Cargo.toml +2 -2
  4. data/lib/spikard/app.rb +33 -14
  5. data/lib/spikard/testing.rb +47 -12
  6. data/lib/spikard/version.rb +1 -1
  7. data/vendor/crates/spikard-bindings-shared/Cargo.toml +63 -0
  8. data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +132 -0
  9. data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +752 -0
  10. data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +194 -0
  11. data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +246 -0
  12. data/vendor/crates/spikard-bindings-shared/src/error_response.rs +401 -0
  13. data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +238 -0
  14. data/vendor/crates/spikard-bindings-shared/src/lib.rs +24 -0
  15. data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +292 -0
  16. data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +616 -0
  17. data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +305 -0
  18. data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +248 -0
  19. data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +351 -0
  20. data/vendor/crates/spikard-bindings-shared/tests/comprehensive_coverage.rs +454 -0
  21. data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +383 -0
  22. data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +280 -0
  23. data/vendor/crates/spikard-core/Cargo.toml +4 -4
  24. data/vendor/crates/spikard-core/src/debug.rs +64 -0
  25. data/vendor/crates/spikard-core/src/di/container.rs +3 -27
  26. data/vendor/crates/spikard-core/src/di/factory.rs +1 -5
  27. data/vendor/crates/spikard-core/src/di/graph.rs +8 -47
  28. data/vendor/crates/spikard-core/src/di/mod.rs +1 -1
  29. data/vendor/crates/spikard-core/src/di/resolved.rs +1 -7
  30. data/vendor/crates/spikard-core/src/di/value.rs +2 -4
  31. data/vendor/crates/spikard-core/src/errors.rs +30 -0
  32. data/vendor/crates/spikard-core/src/http.rs +262 -0
  33. data/vendor/crates/spikard-core/src/lib.rs +1 -1
  34. data/vendor/crates/spikard-core/src/lifecycle.rs +764 -0
  35. data/vendor/crates/spikard-core/src/metadata.rs +389 -0
  36. data/vendor/crates/spikard-core/src/parameters.rs +1962 -159
  37. data/vendor/crates/spikard-core/src/problem.rs +34 -0
  38. data/vendor/crates/spikard-core/src/request_data.rs +966 -1
  39. data/vendor/crates/spikard-core/src/router.rs +263 -2
  40. data/vendor/crates/spikard-core/src/validation/error_mapper.rs +688 -0
  41. data/vendor/crates/spikard-core/src/{validation.rs → validation/mod.rs} +26 -268
  42. data/vendor/crates/spikard-http/Cargo.toml +12 -16
  43. data/vendor/crates/spikard-http/examples/sse-notifications.rs +148 -0
  44. data/vendor/crates/spikard-http/examples/websocket-chat.rs +92 -0
  45. data/vendor/crates/spikard-http/src/auth.rs +65 -16
  46. data/vendor/crates/spikard-http/src/background.rs +1614 -3
  47. data/vendor/crates/spikard-http/src/cors.rs +515 -0
  48. data/vendor/crates/spikard-http/src/debug.rs +65 -0
  49. data/vendor/crates/spikard-http/src/di_handler.rs +1322 -77
  50. data/vendor/crates/spikard-http/src/handler_response.rs +711 -0
  51. data/vendor/crates/spikard-http/src/handler_trait.rs +607 -5
  52. data/vendor/crates/spikard-http/src/handler_trait_tests.rs +6 -0
  53. data/vendor/crates/spikard-http/src/lib.rs +33 -28
  54. data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +81 -0
  55. data/vendor/crates/spikard-http/src/lifecycle.rs +765 -0
  56. data/vendor/crates/spikard-http/src/middleware/mod.rs +372 -117
  57. data/vendor/crates/spikard-http/src/middleware/multipart.rs +836 -10
  58. data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +409 -43
  59. data/vendor/crates/spikard-http/src/middleware/validation.rs +513 -65
  60. data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +345 -0
  61. data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +1055 -0
  62. data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +473 -3
  63. data/vendor/crates/spikard-http/src/query_parser.rs +455 -31
  64. data/vendor/crates/spikard-http/src/response.rs +321 -0
  65. data/vendor/crates/spikard-http/src/server/handler.rs +1572 -9
  66. data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +136 -0
  67. data/vendor/crates/spikard-http/src/server/mod.rs +875 -178
  68. data/vendor/crates/spikard-http/src/server/request_extraction.rs +674 -23
  69. data/vendor/crates/spikard-http/src/server/routing_factory.rs +599 -0
  70. data/vendor/crates/spikard-http/src/sse.rs +983 -21
  71. data/vendor/crates/spikard-http/src/testing/form.rs +38 -0
  72. data/vendor/crates/spikard-http/src/testing/test_client.rs +0 -2
  73. data/vendor/crates/spikard-http/src/testing.rs +7 -7
  74. data/vendor/crates/spikard-http/src/websocket.rs +1055 -4
  75. data/vendor/crates/spikard-http/tests/background_behavior.rs +832 -0
  76. data/vendor/crates/spikard-http/tests/common/handlers.rs +309 -0
  77. data/vendor/crates/spikard-http/tests/common/mod.rs +26 -0
  78. data/vendor/crates/spikard-http/tests/di_integration.rs +192 -0
  79. data/vendor/crates/spikard-http/tests/doc_snippets.rs +5 -0
  80. data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +1093 -0
  81. data/vendor/crates/spikard-http/tests/multipart_behavior.rs +656 -0
  82. data/vendor/crates/spikard-http/tests/server_config_builder.rs +314 -0
  83. data/vendor/crates/spikard-http/tests/sse_behavior.rs +620 -0
  84. data/vendor/crates/spikard-http/tests/websocket_behavior.rs +663 -0
  85. data/vendor/crates/spikard-rb/Cargo.toml +10 -4
  86. data/vendor/crates/spikard-rb/build.rs +196 -5
  87. data/vendor/crates/spikard-rb/src/config/mod.rs +5 -0
  88. data/vendor/crates/spikard-rb/src/{config.rs → config/server_config.rs} +100 -109
  89. data/vendor/crates/spikard-rb/src/conversion.rs +121 -20
  90. data/vendor/crates/spikard-rb/src/di/builder.rs +100 -0
  91. data/vendor/crates/spikard-rb/src/{di.rs → di/mod.rs} +12 -46
  92. data/vendor/crates/spikard-rb/src/handler.rs +100 -107
  93. data/vendor/crates/spikard-rb/src/integration/mod.rs +3 -0
  94. data/vendor/crates/spikard-rb/src/lib.rs +467 -1428
  95. data/vendor/crates/spikard-rb/src/lifecycle.rs +1 -0
  96. data/vendor/crates/spikard-rb/src/metadata/mod.rs +5 -0
  97. data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +447 -0
  98. data/vendor/crates/spikard-rb/src/runtime/mod.rs +5 -0
  99. data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +324 -0
  100. data/vendor/crates/spikard-rb/src/server.rs +47 -22
  101. data/vendor/crates/spikard-rb/src/{test_client.rs → testing/client.rs} +187 -40
  102. data/vendor/crates/spikard-rb/src/testing/mod.rs +7 -0
  103. data/vendor/crates/spikard-rb/src/testing/websocket.rs +635 -0
  104. data/vendor/crates/spikard-rb/src/websocket.rs +178 -37
  105. metadata +46 -13
  106. data/vendor/crates/spikard-http/src/parameters.rs +0 -1
  107. data/vendor/crates/spikard-http/src/problem.rs +0 -1
  108. data/vendor/crates/spikard-http/src/router.rs +0 -1
  109. data/vendor/crates/spikard-http/src/schema_registry.rs +0 -1
  110. data/vendor/crates/spikard-http/src/type_hints.rs +0 -1
  111. data/vendor/crates/spikard-http/src/validation.rs +0 -1
  112. data/vendor/crates/spikard-rb/src/test_websocket.rs +0 -221
  113. /data/vendor/crates/spikard-rb/src/{test_sse.rs → testing/sse.rs} +0 -0
@@ -1,12 +1,59 @@
1
1
  //! Request parsing and data extraction utilities
2
2
 
3
3
  use crate::handler_trait::RequestData;
4
- use crate::query_parser::parse_query_string_to_json;
4
+ use crate::query_parser::{parse_query_pairs_to_json, parse_query_string};
5
5
  use axum::body::Body;
6
6
  use http_body_util::BodyExt;
7
7
  use serde_json::Value;
8
8
  use std::collections::HashMap;
9
9
  use std::sync::Arc;
10
+ use std::sync::OnceLock;
11
+
12
+ #[derive(Debug, Clone, Copy)]
13
+ pub struct WithoutBodyExtractionOptions {
14
+ pub include_raw_query_params: bool,
15
+ pub include_query_params_json: bool,
16
+ pub include_headers: bool,
17
+ pub include_cookies: bool,
18
+ }
19
+
20
+ fn extract_query_params_and_raw(
21
+ uri: &axum::http::Uri,
22
+ include_raw_query_params: bool,
23
+ include_query_params_json: bool,
24
+ ) -> (Value, HashMap<String, Vec<String>>) {
25
+ let query_string = uri.query().unwrap_or("");
26
+ if query_string.is_empty() {
27
+ return (Value::Object(serde_json::Map::new()), HashMap::new());
28
+ }
29
+
30
+ match (include_raw_query_params, include_query_params_json) {
31
+ (false, false) => (Value::Null, HashMap::new()),
32
+ (false, true) => (
33
+ crate::query_parser::parse_query_string_to_json(query_string.as_bytes(), true),
34
+ HashMap::new(),
35
+ ),
36
+ (true, false) => {
37
+ let raw =
38
+ parse_query_string(query_string.as_bytes(), '&')
39
+ .into_iter()
40
+ .fold(HashMap::new(), |mut acc, (k, v)| {
41
+ acc.entry(k).or_insert_with(Vec::new).push(v);
42
+ acc
43
+ });
44
+ (Value::Null, raw)
45
+ }
46
+ (true, true) => {
47
+ let pairs = parse_query_string(query_string.as_bytes(), '&');
48
+ let json = parse_query_pairs_to_json(&pairs, true);
49
+ let raw = pairs.into_iter().fold(HashMap::new(), |mut acc, (k, v)| {
50
+ acc.entry(k).or_insert_with(Vec::new).push(v);
51
+ acc
52
+ });
53
+ (json, raw)
54
+ }
55
+ }
56
+ }
10
57
 
11
58
  /// Extract and parse query parameters from request URI
12
59
  pub fn extract_query_params(uri: &axum::http::Uri) -> Value {
@@ -14,7 +61,7 @@ pub fn extract_query_params(uri: &axum::http::Uri) -> Value {
14
61
  if query_string.is_empty() {
15
62
  Value::Object(serde_json::Map::new())
16
63
  } else {
17
- parse_query_string_to_json(query_string.as_bytes(), true)
64
+ crate::query_parser::parse_query_string_to_json(query_string.as_bytes(), true)
18
65
  }
19
66
  }
20
67
 
@@ -25,7 +72,7 @@ pub fn extract_raw_query_params(uri: &axum::http::Uri) -> HashMap<String, Vec<St
25
72
  if query_string.is_empty() {
26
73
  HashMap::new()
27
74
  } else {
28
- crate::query_parser::parse_query_string(query_string.as_bytes(), '&')
75
+ parse_query_string(query_string.as_bytes(), '&')
29
76
  .into_iter()
30
77
  .fold(HashMap::new(), |mut acc, (k, v)| {
31
78
  acc.entry(k).or_insert_with(Vec::new).push(v);
@@ -36,10 +83,11 @@ pub fn extract_raw_query_params(uri: &axum::http::Uri) -> HashMap<String, Vec<St
36
83
 
37
84
  /// Extract headers from request
38
85
  pub fn extract_headers(headers: &axum::http::HeaderMap) -> HashMap<String, String> {
39
- let mut map = HashMap::new();
86
+ let mut map = HashMap::with_capacity(headers.len());
40
87
  for (name, value) in headers.iter() {
41
88
  if let Ok(val_str) = value.to_str() {
42
- map.insert(name.as_str().to_lowercase(), val_str.to_string());
89
+ // `HeaderName::as_str()` is already normalized to lowercase.
90
+ map.insert(name.as_str().to_string(), val_str.to_string());
43
91
  }
44
92
  }
45
93
  map
@@ -58,6 +106,16 @@ pub fn extract_cookies(headers: &axum::http::HeaderMap) -> HashMap<String, Strin
58
106
  cookies
59
107
  }
60
108
 
109
+ fn empty_string_map() -> Arc<HashMap<String, String>> {
110
+ static EMPTY: OnceLock<Arc<HashMap<String, String>>> = OnceLock::new();
111
+ Arc::clone(EMPTY.get_or_init(|| Arc::new(HashMap::new())))
112
+ }
113
+
114
+ fn empty_raw_query_map() -> Arc<HashMap<String, Vec<String>>> {
115
+ static EMPTY: OnceLock<Arc<HashMap<String, Vec<String>>>> = OnceLock::new();
116
+ Arc::clone(EMPTY.get_or_init(|| Arc::new(HashMap::new())))
117
+ }
118
+
61
119
  /// Create RequestData from request parts (for requests without body)
62
120
  ///
63
121
  /// Wraps HashMaps in Arc to enable cheap cloning without duplicating data.
@@ -66,13 +124,29 @@ pub fn create_request_data_without_body(
66
124
  method: &axum::http::Method,
67
125
  headers: &axum::http::HeaderMap,
68
126
  path_params: HashMap<String, String>,
127
+ options: WithoutBodyExtractionOptions,
69
128
  ) -> RequestData {
129
+ let (query_params, raw_query_params) =
130
+ extract_query_params_and_raw(uri, options.include_raw_query_params, options.include_query_params_json);
70
131
  RequestData {
71
132
  path_params: Arc::new(path_params),
72
- query_params: extract_query_params(uri),
73
- raw_query_params: Arc::new(extract_raw_query_params(uri)),
74
- headers: Arc::new(extract_headers(headers)),
75
- cookies: Arc::new(extract_cookies(headers)),
133
+ query_params,
134
+ raw_query_params: if raw_query_params.is_empty() {
135
+ empty_raw_query_map()
136
+ } else {
137
+ Arc::new(raw_query_params)
138
+ },
139
+ validated_params: None,
140
+ headers: if options.include_headers {
141
+ Arc::new(extract_headers(headers))
142
+ } else {
143
+ empty_string_map()
144
+ },
145
+ cookies: if options.include_cookies {
146
+ Arc::new(extract_cookies(headers))
147
+ } else {
148
+ empty_string_map()
149
+ },
76
150
  body: Value::Null,
77
151
  raw_body: None,
78
152
  method: method.as_str().to_string(),
@@ -91,24 +165,47 @@ pub async fn create_request_data_with_body(
91
165
  parts: &axum::http::request::Parts,
92
166
  path_params: HashMap<String, String>,
93
167
  body: Body,
168
+ include_raw_query_params: bool,
169
+ include_query_params_json: bool,
170
+ include_headers: bool,
171
+ include_cookies: bool,
94
172
  ) -> Result<RequestData, (axum::http::StatusCode, String)> {
95
- let body_bytes = body
96
- .collect()
97
- .await
98
- .map_err(|e| {
99
- (
100
- axum::http::StatusCode::BAD_REQUEST,
101
- format!("Failed to read body: {}", e),
102
- )
103
- })?
104
- .to_bytes();
173
+ let body_bytes = if let Some(pre_read) = parts.extensions.get::<crate::middleware::PreReadBody>() {
174
+ pre_read.0.clone()
175
+ } else {
176
+ body.collect()
177
+ .await
178
+ .map_err(|e| {
179
+ (
180
+ axum::http::StatusCode::BAD_REQUEST,
181
+ format!("Failed to read body: {}", e),
182
+ )
183
+ })?
184
+ .to_bytes()
185
+ };
186
+
187
+ let (query_params, raw_query_params) =
188
+ extract_query_params_and_raw(&parts.uri, include_raw_query_params, include_query_params_json);
105
189
 
106
190
  Ok(RequestData {
107
191
  path_params: Arc::new(path_params),
108
- query_params: extract_query_params(&parts.uri),
109
- raw_query_params: Arc::new(extract_raw_query_params(&parts.uri)),
110
- headers: Arc::new(extract_headers(&parts.headers)),
111
- cookies: Arc::new(extract_cookies(&parts.headers)),
192
+ query_params,
193
+ raw_query_params: if raw_query_params.is_empty() {
194
+ empty_raw_query_map()
195
+ } else {
196
+ Arc::new(raw_query_params)
197
+ },
198
+ validated_params: None,
199
+ headers: if include_headers {
200
+ Arc::new(extract_headers(&parts.headers))
201
+ } else {
202
+ empty_string_map()
203
+ },
204
+ cookies: if include_cookies {
205
+ Arc::new(extract_cookies(&parts.headers))
206
+ } else {
207
+ empty_string_map()
208
+ },
112
209
  body: Value::Null,
113
210
  raw_body: if body_bytes.is_empty() { None } else { Some(body_bytes) },
114
211
  method: parts.method.as_str().to_string(),
@@ -117,3 +214,557 @@ pub async fn create_request_data_with_body(
117
214
  dependencies: None,
118
215
  })
119
216
  }
217
+
218
+ #[cfg(test)]
219
+ mod tests {
220
+ use super::*;
221
+ use axum::http::{HeaderMap, HeaderValue, Method, Uri};
222
+ use futures_util::stream;
223
+ use serde_json::json;
224
+
225
+ const OPTIONS_ALL: WithoutBodyExtractionOptions = WithoutBodyExtractionOptions {
226
+ include_raw_query_params: true,
227
+ include_query_params_json: true,
228
+ include_headers: true,
229
+ include_cookies: true,
230
+ };
231
+
232
+ const OPTIONS_SKIP_HEADERS: WithoutBodyExtractionOptions = WithoutBodyExtractionOptions {
233
+ include_raw_query_params: true,
234
+ include_query_params_json: true,
235
+ include_headers: false,
236
+ include_cookies: true,
237
+ };
238
+
239
+ const OPTIONS_SKIP_COOKIES: WithoutBodyExtractionOptions = WithoutBodyExtractionOptions {
240
+ include_raw_query_params: true,
241
+ include_query_params_json: true,
242
+ include_headers: true,
243
+ include_cookies: false,
244
+ };
245
+
246
+ #[test]
247
+ fn test_extract_query_params_empty() {
248
+ let uri: Uri = "/path".parse().unwrap();
249
+ let result = extract_query_params(&uri);
250
+ assert_eq!(result, json!({}));
251
+ }
252
+
253
+ #[test]
254
+ fn test_extract_query_params_single_param() {
255
+ let uri: Uri = "/path?name=value".parse().unwrap();
256
+ let result = extract_query_params(&uri);
257
+ assert_eq!(result, json!({"name": "value"}));
258
+ }
259
+
260
+ #[test]
261
+ fn test_extract_query_params_multiple_params() {
262
+ let uri: Uri = "/path?foo=1&bar=2".parse().unwrap();
263
+ let result = extract_query_params(&uri);
264
+ assert_eq!(result, json!({"foo": 1, "bar": 2}));
265
+ }
266
+
267
+ #[test]
268
+ fn test_extract_query_params_array_params() {
269
+ let uri: Uri = "/path?tags=rust&tags=http".parse().unwrap();
270
+ let result = extract_query_params(&uri);
271
+ assert_eq!(result, json!({"tags": ["rust", "http"]}));
272
+ }
273
+
274
+ #[test]
275
+ fn test_extract_query_params_mixed_array_and_single() {
276
+ let uri: Uri = "/path?tags=rust&tags=web&id=123".parse().unwrap();
277
+ let result = extract_query_params(&uri);
278
+ assert_eq!(result, json!({"tags": ["rust", "web"], "id": 123}));
279
+ }
280
+
281
+ #[test]
282
+ fn test_extract_query_params_url_encoded() {
283
+ let uri: Uri = "/path?email=test%40example.com&name=john+doe".parse().unwrap();
284
+ let result = extract_query_params(&uri);
285
+ assert_eq!(result, json!({"email": "test@example.com", "name": "john doe"}));
286
+ }
287
+
288
+ #[test]
289
+ fn test_extract_query_params_boolean_values() {
290
+ let uri: Uri = "/path?active=true&enabled=false".parse().unwrap();
291
+ let result = extract_query_params(&uri);
292
+ assert_eq!(result, json!({"active": true, "enabled": false}));
293
+ }
294
+
295
+ #[test]
296
+ fn test_extract_query_params_null_value() {
297
+ let uri: Uri = "/path?value=null".parse().unwrap();
298
+ let result = extract_query_params(&uri);
299
+ assert_eq!(result, json!({"value": null}));
300
+ }
301
+
302
+ #[test]
303
+ fn test_extract_query_params_empty_string_value() {
304
+ let uri: Uri = "/path?key=".parse().unwrap();
305
+ let result = extract_query_params(&uri);
306
+ assert_eq!(result, json!({"key": ""}));
307
+ }
308
+
309
+ #[test]
310
+ fn test_extract_raw_query_params_empty() {
311
+ let uri: Uri = "/path".parse().unwrap();
312
+ let result = extract_raw_query_params(&uri);
313
+ assert!(result.is_empty());
314
+ }
315
+
316
+ #[test]
317
+ fn test_extract_raw_query_params_single() {
318
+ let uri: Uri = "/path?name=value".parse().unwrap();
319
+ let result = extract_raw_query_params(&uri);
320
+ assert_eq!(result.get("name"), Some(&vec!["value".to_string()]));
321
+ }
322
+
323
+ #[test]
324
+ fn test_extract_raw_query_params_multiple_values() {
325
+ let uri: Uri = "/path?tag=rust&tag=http".parse().unwrap();
326
+ let result = extract_raw_query_params(&uri);
327
+ assert_eq!(result.get("tag"), Some(&vec!["rust".to_string(), "http".to_string()]));
328
+ }
329
+
330
+ #[test]
331
+ fn test_extract_raw_query_params_mixed() {
332
+ let uri: Uri = "/path?id=123&tags=rust&tags=web&active=true".parse().unwrap();
333
+ let result = extract_raw_query_params(&uri);
334
+ assert_eq!(result.get("id"), Some(&vec!["123".to_string()]));
335
+ assert_eq!(result.get("tags"), Some(&vec!["rust".to_string(), "web".to_string()]));
336
+ assert_eq!(result.get("active"), Some(&vec!["true".to_string()]));
337
+ }
338
+
339
+ #[test]
340
+ fn test_extract_raw_query_params_url_encoded() {
341
+ let uri: Uri = "/path?email=test%40example.com".parse().unwrap();
342
+ let result = extract_raw_query_params(&uri);
343
+ assert_eq!(result.get("email"), Some(&vec!["test@example.com".to_string()]));
344
+ }
345
+
346
+ #[test]
347
+ fn test_extract_headers_empty() {
348
+ let headers = HeaderMap::new();
349
+ let result = extract_headers(&headers);
350
+ assert!(result.is_empty());
351
+ }
352
+
353
+ #[test]
354
+ fn test_extract_headers_single() {
355
+ let mut headers = HeaderMap::new();
356
+ headers.insert("content-type", HeaderValue::from_static("application/json"));
357
+ let result = extract_headers(&headers);
358
+ assert_eq!(result.get("content-type"), Some(&"application/json".to_string()));
359
+ }
360
+
361
+ #[test]
362
+ fn test_extract_headers_multiple() {
363
+ let mut headers = HeaderMap::new();
364
+ headers.insert("content-type", HeaderValue::from_static("application/json"));
365
+ headers.insert("user-agent", HeaderValue::from_static("test-agent"));
366
+ headers.insert("authorization", HeaderValue::from_static("Bearer token123"));
367
+
368
+ let result = extract_headers(&headers);
369
+ assert_eq!(result.len(), 3);
370
+ assert_eq!(result.get("content-type"), Some(&"application/json".to_string()));
371
+ assert_eq!(result.get("user-agent"), Some(&"test-agent".to_string()));
372
+ assert_eq!(result.get("authorization"), Some(&"Bearer token123".to_string()));
373
+ }
374
+
375
+ #[test]
376
+ fn test_extract_headers_case_insensitive() {
377
+ let mut headers = HeaderMap::new();
378
+ headers.insert("Content-Type", HeaderValue::from_static("text/html"));
379
+ headers.insert("USER-Agent", HeaderValue::from_static("chrome"));
380
+
381
+ let result = extract_headers(&headers);
382
+ assert_eq!(result.get("content-type"), Some(&"text/html".to_string()));
383
+ assert_eq!(result.get("user-agent"), Some(&"chrome".to_string()));
384
+ }
385
+
386
+ #[test]
387
+ fn test_extract_headers_with_dashes() {
388
+ let mut headers = HeaderMap::new();
389
+ headers.insert("x-custom-header", HeaderValue::from_static("custom-value"));
390
+ headers.insert("x-request-id", HeaderValue::from_static("req-12345"));
391
+
392
+ let result = extract_headers(&headers);
393
+ assert_eq!(result.get("x-custom-header"), Some(&"custom-value".to_string()));
394
+ assert_eq!(result.get("x-request-id"), Some(&"req-12345".to_string()));
395
+ }
396
+
397
+ #[test]
398
+ fn test_extract_cookies_no_cookie_header() {
399
+ let headers = HeaderMap::new();
400
+ let result = extract_cookies(&headers);
401
+ assert!(result.is_empty());
402
+ }
403
+
404
+ #[test]
405
+ fn test_extract_cookies_single() {
406
+ let mut headers = HeaderMap::new();
407
+ headers.insert(axum::http::header::COOKIE, HeaderValue::from_static("session=abc123"));
408
+
409
+ let result = extract_cookies(&headers);
410
+ assert_eq!(result.get("session"), Some(&"abc123".to_string()));
411
+ }
412
+
413
+ #[test]
414
+ fn test_extract_cookies_multiple() {
415
+ let mut headers = HeaderMap::new();
416
+ headers.insert(
417
+ axum::http::header::COOKIE,
418
+ HeaderValue::from_static("session=abc123; user_id=42; theme=dark"),
419
+ );
420
+
421
+ let result = extract_cookies(&headers);
422
+ assert_eq!(result.len(), 3);
423
+ assert_eq!(result.get("session"), Some(&"abc123".to_string()));
424
+ assert_eq!(result.get("user_id"), Some(&"42".to_string()));
425
+ assert_eq!(result.get("theme"), Some(&"dark".to_string()));
426
+ }
427
+
428
+ #[test]
429
+ fn test_extract_cookies_with_spaces() {
430
+ let mut headers = HeaderMap::new();
431
+ headers.insert(
432
+ axum::http::header::COOKIE,
433
+ HeaderValue::from_static("session = abc123 ; theme = light"),
434
+ );
435
+
436
+ let result = extract_cookies(&headers);
437
+ assert!(result.len() >= 1);
438
+ }
439
+
440
+ #[test]
441
+ fn test_extract_cookies_empty_value() {
442
+ let mut headers = HeaderMap::new();
443
+ headers.insert(axum::http::header::COOKIE, HeaderValue::from_static("empty="));
444
+
445
+ let result = extract_cookies(&headers);
446
+ assert_eq!(result.get("empty"), Some(&String::new()));
447
+ }
448
+
449
+ #[test]
450
+ fn test_create_request_data_without_body_minimal() {
451
+ let uri: Uri = "/test".parse().unwrap();
452
+ let method = Method::GET;
453
+ let headers = HeaderMap::new();
454
+ let path_params = HashMap::new();
455
+
456
+ let result = create_request_data_without_body(&uri, &method, &headers, path_params, OPTIONS_ALL);
457
+
458
+ assert_eq!(result.method, "GET");
459
+ assert_eq!(result.path, "/test");
460
+ assert!(result.path_params.is_empty());
461
+ assert_eq!(result.query_params, json!({}));
462
+ assert!(result.raw_query_params.is_empty());
463
+ assert!(result.headers.is_empty());
464
+ assert!(result.cookies.is_empty());
465
+ assert_eq!(result.body, Value::Null);
466
+ assert!(result.raw_body.is_none());
467
+ }
468
+
469
+ #[test]
470
+ fn test_create_request_data_without_body_with_path_params() {
471
+ let uri: Uri = "/users/42".parse().unwrap();
472
+ let method = Method::GET;
473
+ let headers = HeaderMap::new();
474
+ let mut path_params = HashMap::new();
475
+ path_params.insert("user_id".to_string(), "42".to_string());
476
+
477
+ let result = create_request_data_without_body(&uri, &method, &headers, path_params, OPTIONS_ALL);
478
+
479
+ assert_eq!(result.path_params.get("user_id"), Some(&"42".to_string()));
480
+ }
481
+
482
+ #[test]
483
+ fn test_create_request_data_without_body_with_query_params() {
484
+ let uri: Uri = "/search?q=rust&limit=10".parse().unwrap();
485
+ let method = Method::GET;
486
+ let headers = HeaderMap::new();
487
+ let path_params = HashMap::new();
488
+
489
+ let result = create_request_data_without_body(&uri, &method, &headers, path_params, OPTIONS_ALL);
490
+
491
+ assert_eq!(result.query_params, json!({"q": "rust", "limit": 10}));
492
+ assert_eq!(result.raw_query_params.get("q"), Some(&vec!["rust".to_string()]));
493
+ assert_eq!(result.raw_query_params.get("limit"), Some(&vec!["10".to_string()]));
494
+ }
495
+
496
+ #[test]
497
+ fn test_create_request_data_without_body_with_headers() {
498
+ let uri: Uri = "/test".parse().unwrap();
499
+ let method = Method::POST;
500
+ let mut headers = HeaderMap::new();
501
+ headers.insert("content-type", HeaderValue::from_static("application/json"));
502
+ headers.insert("authorization", HeaderValue::from_static("Bearer token"));
503
+ let path_params = HashMap::new();
504
+
505
+ let result = create_request_data_without_body(&uri, &method, &headers, path_params, OPTIONS_ALL);
506
+
507
+ assert_eq!(
508
+ result.headers.get("content-type"),
509
+ Some(&"application/json".to_string())
510
+ );
511
+ assert_eq!(result.headers.get("authorization"), Some(&"Bearer token".to_string()));
512
+ }
513
+
514
+ #[test]
515
+ fn test_create_request_data_without_body_with_cookies() {
516
+ let uri: Uri = "/test".parse().unwrap();
517
+ let method = Method::GET;
518
+ let mut headers = HeaderMap::new();
519
+ headers.insert(
520
+ axum::http::header::COOKIE,
521
+ HeaderValue::from_static("session=xyz; theme=dark"),
522
+ );
523
+ let path_params = HashMap::new();
524
+
525
+ let result = create_request_data_without_body(&uri, &method, &headers, path_params, OPTIONS_ALL);
526
+
527
+ assert_eq!(result.cookies.get("session"), Some(&"xyz".to_string()));
528
+ assert_eq!(result.cookies.get("theme"), Some(&"dark".to_string()));
529
+ }
530
+
531
+ #[test]
532
+ fn test_create_request_data_without_body_skip_headers() {
533
+ let uri: Uri = "/test".parse().unwrap();
534
+ let method = Method::GET;
535
+ let mut headers = HeaderMap::new();
536
+ headers.insert("authorization", HeaderValue::from_static("Bearer token"));
537
+ let path_params = HashMap::new();
538
+
539
+ let result = create_request_data_without_body(&uri, &method, &headers, path_params, OPTIONS_SKIP_HEADERS);
540
+
541
+ assert!(result.headers.is_empty());
542
+ }
543
+
544
+ #[test]
545
+ fn test_create_request_data_without_body_skip_cookies() {
546
+ let uri: Uri = "/test".parse().unwrap();
547
+ let method = Method::GET;
548
+ let mut headers = HeaderMap::new();
549
+ headers.insert(axum::http::header::COOKIE, HeaderValue::from_static("session=xyz"));
550
+ let path_params = HashMap::new();
551
+
552
+ let result = create_request_data_without_body(&uri, &method, &headers, path_params, OPTIONS_SKIP_COOKIES);
553
+
554
+ assert!(result.cookies.is_empty());
555
+ }
556
+
557
+ #[test]
558
+ fn test_create_request_data_without_body_different_methods() {
559
+ let uri: Uri = "/resource".parse().unwrap();
560
+ let headers = HeaderMap::new();
561
+ let path_params = HashMap::new();
562
+
563
+ for method in &[Method::GET, Method::POST, Method::PUT, Method::DELETE, Method::PATCH] {
564
+ let result = create_request_data_without_body(&uri, method, &headers, path_params.clone(), OPTIONS_ALL);
565
+ assert_eq!(result.method, method.as_str());
566
+ }
567
+ }
568
+
569
+ #[tokio::test]
570
+ async fn test_create_request_data_with_body_empty() {
571
+ let parts = axum::http::request::Request::builder()
572
+ .method(Method::POST)
573
+ .uri("/test")
574
+ .body(Body::empty())
575
+ .unwrap()
576
+ .into_parts();
577
+
578
+ let body = Body::empty();
579
+ let path_params = HashMap::new();
580
+
581
+ let result = create_request_data_with_body(&parts.0, path_params, body, true, true, true, true)
582
+ .await
583
+ .unwrap();
584
+
585
+ assert_eq!(result.method, "POST");
586
+ assert_eq!(result.path, "/test");
587
+ assert_eq!(result.body, Value::Null);
588
+ assert!(result.raw_body.is_none());
589
+ }
590
+
591
+ #[tokio::test]
592
+ async fn test_create_request_data_with_body_json() {
593
+ let request_body = Body::from(r#"{"key":"value"}"#);
594
+ let request = axum::http::request::Request::builder()
595
+ .method(Method::POST)
596
+ .uri("/test")
597
+ .body(Body::empty())
598
+ .unwrap();
599
+
600
+ let (parts, _) = request.into_parts();
601
+ let path_params = HashMap::new();
602
+
603
+ let result = create_request_data_with_body(&parts, path_params, request_body, true, true, true, true)
604
+ .await
605
+ .unwrap();
606
+
607
+ assert_eq!(result.method, "POST");
608
+ assert_eq!(result.body, Value::Null);
609
+ assert!(result.raw_body.is_some());
610
+ assert_eq!(result.raw_body.as_ref().unwrap().as_ref(), br#"{"key":"value"}"#);
611
+ }
612
+
613
+ #[tokio::test]
614
+ async fn test_create_request_data_with_body_with_query_params() {
615
+ let request_body = Body::from("test");
616
+ let request = axum::http::request::Request::builder()
617
+ .method(Method::POST)
618
+ .uri("/test?foo=bar&baz=qux")
619
+ .body(Body::empty())
620
+ .unwrap();
621
+
622
+ let (parts, _) = request.into_parts();
623
+ let path_params = HashMap::new();
624
+
625
+ let result = create_request_data_with_body(&parts, path_params, request_body, true, true, true, true)
626
+ .await
627
+ .unwrap();
628
+
629
+ assert_eq!(result.query_params, json!({"foo": "bar", "baz": "qux"}));
630
+ }
631
+
632
+ #[tokio::test]
633
+ async fn test_create_request_data_with_body_with_headers() {
634
+ let request_body = Body::from("test");
635
+ let request = axum::http::request::Request::builder()
636
+ .method(Method::POST)
637
+ .uri("/test")
638
+ .header("content-type", "application/json")
639
+ .header("x-request-id", "req123")
640
+ .body(Body::empty())
641
+ .unwrap();
642
+
643
+ let (parts, _) = request.into_parts();
644
+ let path_params = HashMap::new();
645
+
646
+ let result = create_request_data_with_body(&parts, path_params, request_body, true, true, true, true)
647
+ .await
648
+ .unwrap();
649
+
650
+ assert_eq!(
651
+ result.headers.get("content-type"),
652
+ Some(&"application/json".to_string())
653
+ );
654
+ assert_eq!(result.headers.get("x-request-id"), Some(&"req123".to_string()));
655
+ }
656
+
657
+ #[tokio::test]
658
+ async fn test_create_request_data_with_body_with_cookies() {
659
+ let request_body = Body::from("test");
660
+ let request = axum::http::request::Request::builder()
661
+ .method(Method::POST)
662
+ .uri("/test")
663
+ .header("cookie", "session=xyz; user=123")
664
+ .body(Body::empty())
665
+ .unwrap();
666
+
667
+ let (parts, _) = request.into_parts();
668
+ let path_params = HashMap::new();
669
+
670
+ let result = create_request_data_with_body(&parts, path_params, request_body, true, true, true, true)
671
+ .await
672
+ .unwrap();
673
+
674
+ assert_eq!(result.cookies.get("session"), Some(&"xyz".to_string()));
675
+ assert_eq!(result.cookies.get("user"), Some(&"123".to_string()));
676
+ }
677
+
678
+ #[tokio::test]
679
+ async fn test_create_request_data_with_body_large_payload() {
680
+ let large_json = json!({
681
+ "data": (0..100).map(|i| json!({"id": i, "value": format!("item-{}", i)})).collect::<Vec<_>>()
682
+ });
683
+ let json_str = serde_json::to_string(&large_json).unwrap();
684
+ let request_body = Body::from(json_str.clone());
685
+
686
+ let request = axum::http::request::Request::builder()
687
+ .method(Method::POST)
688
+ .uri("/test")
689
+ .body(Body::empty())
690
+ .unwrap();
691
+
692
+ let (parts, _) = request.into_parts();
693
+ let path_params = HashMap::new();
694
+
695
+ let result = create_request_data_with_body(&parts, path_params, request_body, true, true, true, true)
696
+ .await
697
+ .unwrap();
698
+
699
+ assert!(result.raw_body.is_some());
700
+ assert_eq!(result.raw_body.as_ref().unwrap().as_ref(), json_str.as_bytes());
701
+ }
702
+
703
+ #[tokio::test]
704
+ async fn test_create_request_data_with_body_preserves_all_fields() {
705
+ let request_body = Body::from("request data");
706
+ let request = axum::http::request::Request::builder()
707
+ .method(Method::PUT)
708
+ .uri("/users/42?action=update")
709
+ .header("authorization", "Bearer token")
710
+ .header("cookie", "session=abc")
711
+ .body(Body::empty())
712
+ .unwrap();
713
+
714
+ let (parts, _) = request.into_parts();
715
+ let mut path_params = HashMap::new();
716
+ path_params.insert("user_id".to_string(), "42".to_string());
717
+
718
+ let result = create_request_data_with_body(&parts, path_params, request_body, true, true, true, true)
719
+ .await
720
+ .unwrap();
721
+
722
+ assert_eq!(result.method, "PUT");
723
+ assert_eq!(result.path, "/users/42");
724
+ assert_eq!(result.path_params.get("user_id"), Some(&"42".to_string()));
725
+ assert_eq!(result.query_params, json!({"action": "update"}));
726
+ assert!(result.headers.contains_key("authorization"));
727
+ assert!(result.cookies.contains_key("session"));
728
+ assert!(result.raw_body.is_some());
729
+ }
730
+
731
+ #[test]
732
+ fn test_arc_wrapping_for_cheap_cloning() {
733
+ let uri: Uri = "/test".parse().unwrap();
734
+ let method = Method::GET;
735
+ let mut headers = HeaderMap::new();
736
+ headers.insert(axum::http::header::COOKIE, HeaderValue::from_static("session=abc"));
737
+ let mut path_params = HashMap::new();
738
+ path_params.insert("id".to_string(), "1".to_string());
739
+
740
+ let request_data = create_request_data_without_body(&uri, &method, &headers, path_params.clone(), OPTIONS_ALL);
741
+
742
+ let cloned = request_data.clone();
743
+
744
+ assert!(Arc::ptr_eq(&request_data.path_params, &cloned.path_params));
745
+ assert!(Arc::ptr_eq(&request_data.headers, &cloned.headers));
746
+ assert!(Arc::ptr_eq(&request_data.cookies, &cloned.cookies));
747
+ assert!(Arc::ptr_eq(&request_data.raw_query_params, &cloned.raw_query_params));
748
+ }
749
+
750
+ #[tokio::test]
751
+ async fn create_request_data_with_body_returns_bad_request_when_body_stream_errors() {
752
+ let request = axum::http::Request::builder()
753
+ .method(Method::POST)
754
+ .uri("/path")
755
+ .body(Body::empty())
756
+ .unwrap();
757
+ let (parts, _) = request.into_parts();
758
+
759
+ let stream = stream::once(async move {
760
+ Err::<bytes::Bytes, std::io::Error>(std::io::Error::new(std::io::ErrorKind::Other, "boom"))
761
+ });
762
+ let body = Body::from_stream(stream);
763
+
764
+ let err = create_request_data_with_body(&parts, HashMap::new(), body, true, true, true, true)
765
+ .await
766
+ .unwrap_err();
767
+ assert_eq!(err.0, axum::http::StatusCode::BAD_REQUEST);
768
+ assert!(err.1.contains("Failed to read body:"));
769
+ }
770
+ }