spikard 0.4.0-x64-mingw-ucrt
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,309 @@
|
|
|
1
|
+
//! Response building utilities
|
|
2
|
+
|
|
3
|
+
use axum::http::{HeaderMap, StatusCode, header};
|
|
4
|
+
use serde_json::json;
|
|
5
|
+
|
|
6
|
+
/// Builder for constructing HTTP responses across bindings
|
|
7
|
+
pub struct ResponseBuilder {
|
|
8
|
+
status: StatusCode,
|
|
9
|
+
body: serde_json::Value,
|
|
10
|
+
headers: HeaderMap,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
impl ResponseBuilder {
|
|
14
|
+
/// Create a new response builder with default status 200 OK
|
|
15
|
+
pub fn new() -> Self {
|
|
16
|
+
Self {
|
|
17
|
+
status: StatusCode::OK,
|
|
18
|
+
body: json!({}),
|
|
19
|
+
headers: HeaderMap::new(),
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/// Set the HTTP status code
|
|
24
|
+
pub fn status(mut self, status: StatusCode) -> Self {
|
|
25
|
+
self.status = status;
|
|
26
|
+
self
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// Set the response body
|
|
30
|
+
pub fn body(mut self, body: serde_json::Value) -> Self {
|
|
31
|
+
self.body = body;
|
|
32
|
+
self
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// Add a response header
|
|
36
|
+
pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
|
|
37
|
+
if let Ok(name) = key.into().parse::<header::HeaderName>()
|
|
38
|
+
&& let Ok(val) = value.into().parse::<header::HeaderValue>()
|
|
39
|
+
{
|
|
40
|
+
self.headers.insert(name, val);
|
|
41
|
+
}
|
|
42
|
+
self
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// Build the response as (status, headers, body)
|
|
46
|
+
pub fn build(self) -> (StatusCode, HeaderMap, String) {
|
|
47
|
+
let body = serde_json::to_string(&self.body).unwrap_or_else(|_| "{}".to_string());
|
|
48
|
+
(self.status, self.headers, body)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
impl Default for ResponseBuilder {
|
|
53
|
+
fn default() -> Self {
|
|
54
|
+
Self::new()
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
#[cfg(test)]
|
|
59
|
+
mod tests {
|
|
60
|
+
use super::*;
|
|
61
|
+
|
|
62
|
+
#[test]
|
|
63
|
+
fn test_response_builder_default() {
|
|
64
|
+
let (status, headers, body) = ResponseBuilder::new().build();
|
|
65
|
+
|
|
66
|
+
assert_eq!(status, StatusCode::OK);
|
|
67
|
+
assert_eq!(body, "{}");
|
|
68
|
+
assert!(headers.is_empty());
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
#[test]
|
|
72
|
+
fn test_response_builder_default_trait() {
|
|
73
|
+
let (status, _, body) = ResponseBuilder::default().build();
|
|
74
|
+
|
|
75
|
+
assert_eq!(status, StatusCode::OK);
|
|
76
|
+
assert_eq!(body, "{}");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
#[test]
|
|
80
|
+
fn test_response_builder_status() {
|
|
81
|
+
let (status, _, _) = ResponseBuilder::new().status(StatusCode::CREATED).build();
|
|
82
|
+
|
|
83
|
+
assert_eq!(status, StatusCode::CREATED);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
#[test]
|
|
87
|
+
fn test_response_builder_status_chain() {
|
|
88
|
+
let (status, _, _) = ResponseBuilder::new()
|
|
89
|
+
.status(StatusCode::ACCEPTED)
|
|
90
|
+
.status(StatusCode::CREATED)
|
|
91
|
+
.build();
|
|
92
|
+
|
|
93
|
+
// Last status should win
|
|
94
|
+
assert_eq!(status, StatusCode::CREATED);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
#[test]
|
|
98
|
+
fn test_response_builder_body() {
|
|
99
|
+
let body_data = json!({ "id": 123, "name": "test" });
|
|
100
|
+
let (_, _, body) = ResponseBuilder::new().body(body_data.clone()).build();
|
|
101
|
+
|
|
102
|
+
let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
|
|
103
|
+
assert_eq!(parsed["id"], 123);
|
|
104
|
+
assert_eq!(parsed["name"], "test");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
#[test]
|
|
108
|
+
fn test_response_builder_body_chain() {
|
|
109
|
+
let first_body = json!({ "first": "value" });
|
|
110
|
+
let second_body = json!({ "second": "value" });
|
|
111
|
+
|
|
112
|
+
let (_, _, body) = ResponseBuilder::new().body(first_body).body(second_body).build();
|
|
113
|
+
|
|
114
|
+
let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
|
|
115
|
+
assert!(!parsed.get("first").is_some());
|
|
116
|
+
assert_eq!(parsed["second"], "value");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
#[test]
|
|
120
|
+
fn test_response_builder_header() {
|
|
121
|
+
let (_, headers, _) = ResponseBuilder::new()
|
|
122
|
+
.header("Content-Type", "application/json")
|
|
123
|
+
.build();
|
|
124
|
+
|
|
125
|
+
assert_eq!(
|
|
126
|
+
headers.get("content-type").unwrap().to_str().unwrap(),
|
|
127
|
+
"application/json"
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
#[test]
|
|
132
|
+
fn test_response_builder_multiple_headers() {
|
|
133
|
+
let (_, headers, _) = ResponseBuilder::new()
|
|
134
|
+
.header("Content-Type", "application/json")
|
|
135
|
+
.header("X-Custom-Header", "custom-value")
|
|
136
|
+
.header("Authorization", "Bearer token123")
|
|
137
|
+
.build();
|
|
138
|
+
|
|
139
|
+
assert_eq!(headers.len(), 3);
|
|
140
|
+
assert_eq!(
|
|
141
|
+
headers.get("content-type").unwrap().to_str().unwrap(),
|
|
142
|
+
"application/json"
|
|
143
|
+
);
|
|
144
|
+
assert_eq!(
|
|
145
|
+
headers.get("x-custom-header").unwrap().to_str().unwrap(),
|
|
146
|
+
"custom-value"
|
|
147
|
+
);
|
|
148
|
+
assert_eq!(
|
|
149
|
+
headers.get("authorization").unwrap().to_str().unwrap(),
|
|
150
|
+
"Bearer token123"
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
#[test]
|
|
155
|
+
fn test_response_builder_header_overwrite() {
|
|
156
|
+
let (_, headers, _) = ResponseBuilder::new()
|
|
157
|
+
.header("Content-Type", "text/plain")
|
|
158
|
+
.header("Content-Type", "application/json")
|
|
159
|
+
.build();
|
|
160
|
+
|
|
161
|
+
assert_eq!(
|
|
162
|
+
headers.get("content-type").unwrap().to_str().unwrap(),
|
|
163
|
+
"application/json"
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
#[test]
|
|
168
|
+
fn test_response_builder_full_chain() {
|
|
169
|
+
let (status, headers, body) = ResponseBuilder::new()
|
|
170
|
+
.status(StatusCode::CREATED)
|
|
171
|
+
.body(json!({
|
|
172
|
+
"id": 456,
|
|
173
|
+
"status": "active",
|
|
174
|
+
"items": [1, 2, 3]
|
|
175
|
+
}))
|
|
176
|
+
.header("Content-Type", "application/json")
|
|
177
|
+
.header("X-Request-Id", "req-123")
|
|
178
|
+
.build();
|
|
179
|
+
|
|
180
|
+
assert_eq!(status, StatusCode::CREATED);
|
|
181
|
+
assert_eq!(headers.len(), 2);
|
|
182
|
+
|
|
183
|
+
let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
|
|
184
|
+
assert_eq!(parsed["id"], 456);
|
|
185
|
+
assert_eq!(parsed["status"], "active");
|
|
186
|
+
assert_eq!(parsed["items"][0], 1);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
#[test]
|
|
190
|
+
fn test_response_builder() {
|
|
191
|
+
let (status, _, body) = ResponseBuilder::new()
|
|
192
|
+
.status(StatusCode::CREATED)
|
|
193
|
+
.body(json!({ "id": 123 }))
|
|
194
|
+
.build();
|
|
195
|
+
|
|
196
|
+
assert_eq!(status, StatusCode::CREATED);
|
|
197
|
+
assert!(body.contains("123"));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
#[test]
|
|
201
|
+
fn test_response_builder_complex_json() {
|
|
202
|
+
let complex_body = json!({
|
|
203
|
+
"user": {
|
|
204
|
+
"id": 1,
|
|
205
|
+
"name": "John Doe",
|
|
206
|
+
"email": "john@example.com",
|
|
207
|
+
"roles": ["admin", "user"],
|
|
208
|
+
"settings": {
|
|
209
|
+
"notifications": true,
|
|
210
|
+
"theme": "dark"
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
"success": true,
|
|
214
|
+
"timestamp": "2024-01-01T00:00:00Z"
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
let (status, _, body) = ResponseBuilder::new().status(StatusCode::OK).body(complex_body).build();
|
|
218
|
+
|
|
219
|
+
assert_eq!(status, StatusCode::OK);
|
|
220
|
+
let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
|
|
221
|
+
assert_eq!(parsed["user"]["id"], 1);
|
|
222
|
+
assert_eq!(parsed["user"]["roles"][0], "admin");
|
|
223
|
+
assert_eq!(parsed["user"]["settings"]["theme"], "dark");
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
#[test]
|
|
227
|
+
fn test_response_builder_null_body() {
|
|
228
|
+
let (_, _, body) = ResponseBuilder::new().body(serde_json::Value::Null).build();
|
|
229
|
+
|
|
230
|
+
assert_eq!(body, "null");
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
#[test]
|
|
234
|
+
fn test_response_builder_array_body() {
|
|
235
|
+
let array_body = json!([1, 2, 3, 4, 5]);
|
|
236
|
+
let (_, _, body) = ResponseBuilder::new().body(array_body).build();
|
|
237
|
+
|
|
238
|
+
let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
|
|
239
|
+
assert!(parsed.is_array());
|
|
240
|
+
assert_eq!(parsed[0], 1);
|
|
241
|
+
assert_eq!(parsed[4], 5);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
#[test]
|
|
245
|
+
fn test_response_builder_empty_object() {
|
|
246
|
+
let (_, _, body) = ResponseBuilder::new().body(json!({})).build();
|
|
247
|
+
|
|
248
|
+
let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
|
|
249
|
+
assert!(parsed.is_object());
|
|
250
|
+
assert_eq!(parsed.as_object().unwrap().len(), 0);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
#[test]
|
|
254
|
+
fn test_response_builder_all_status_codes() {
|
|
255
|
+
let status_codes = vec![
|
|
256
|
+
StatusCode::OK,
|
|
257
|
+
StatusCode::CREATED,
|
|
258
|
+
StatusCode::ACCEPTED,
|
|
259
|
+
StatusCode::BAD_REQUEST,
|
|
260
|
+
StatusCode::UNAUTHORIZED,
|
|
261
|
+
StatusCode::FORBIDDEN,
|
|
262
|
+
StatusCode::NOT_FOUND,
|
|
263
|
+
StatusCode::INTERNAL_SERVER_ERROR,
|
|
264
|
+
StatusCode::SERVICE_UNAVAILABLE,
|
|
265
|
+
];
|
|
266
|
+
|
|
267
|
+
for code in status_codes {
|
|
268
|
+
let (status, _, _) = ResponseBuilder::new().status(code).build();
|
|
269
|
+
|
|
270
|
+
assert_eq!(status, code);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
#[test]
|
|
275
|
+
fn test_response_builder_invalid_header_name() {
|
|
276
|
+
// Invalid header names should be silently ignored
|
|
277
|
+
let (_, headers, _) = ResponseBuilder::new()
|
|
278
|
+
.header("Invalid\nHeader", "value")
|
|
279
|
+
.header("Valid-Header", "value")
|
|
280
|
+
.build();
|
|
281
|
+
|
|
282
|
+
// Only valid header should be present
|
|
283
|
+
assert_eq!(headers.len(), 1);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
#[test]
|
|
287
|
+
fn test_response_builder_invalid_header_value() {
|
|
288
|
+
// Invalid header values should be silently ignored
|
|
289
|
+
let (_, headers, _) = ResponseBuilder::new().header("Valid-Header", "valid-value").build();
|
|
290
|
+
|
|
291
|
+
assert_eq!(headers.len(), 1);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
#[test]
|
|
295
|
+
fn test_response_builder_special_characters_in_json() {
|
|
296
|
+
let body_with_special_chars = json!({
|
|
297
|
+
"message": "Hello \"World\"",
|
|
298
|
+
"path": "C:\\Users\\test",
|
|
299
|
+
"unicode": "café ☕",
|
|
300
|
+
"newlines": "line1\nline2"
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
let (_, _, body) = ResponseBuilder::new().body(body_with_special_chars).build();
|
|
304
|
+
|
|
305
|
+
let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
|
|
306
|
+
assert_eq!(parsed["message"], "Hello \"World\"");
|
|
307
|
+
assert_eq!(parsed["unicode"], "café ☕");
|
|
308
|
+
}
|
|
309
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
//! Shared test client infrastructure
|
|
2
|
+
|
|
3
|
+
use std::collections::HashMap;
|
|
4
|
+
|
|
5
|
+
/// Base configuration for test clients across bindings
|
|
6
|
+
pub struct TestClientConfig {
|
|
7
|
+
/// The base URL for the test server
|
|
8
|
+
pub base_url: String,
|
|
9
|
+
/// Request timeout in milliseconds
|
|
10
|
+
pub timeout_ms: u64,
|
|
11
|
+
/// Whether to follow redirects
|
|
12
|
+
pub follow_redirects: bool,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
impl TestClientConfig {
|
|
16
|
+
/// Create a new test client configuration with custom base URL
|
|
17
|
+
pub fn new(base_url: impl Into<String>) -> Self {
|
|
18
|
+
Self {
|
|
19
|
+
base_url: base_url.into(),
|
|
20
|
+
timeout_ms: 30000,
|
|
21
|
+
follow_redirects: true,
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/// Set the timeout in milliseconds
|
|
26
|
+
pub fn with_timeout(mut self, timeout_ms: u64) -> Self {
|
|
27
|
+
self.timeout_ms = timeout_ms;
|
|
28
|
+
self
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// Set whether to follow redirects
|
|
32
|
+
pub fn with_follow_redirects(mut self, follow_redirects: bool) -> Self {
|
|
33
|
+
self.follow_redirects = follow_redirects;
|
|
34
|
+
self
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
impl Default for TestClientConfig {
|
|
39
|
+
fn default() -> Self {
|
|
40
|
+
Self {
|
|
41
|
+
base_url: "http://localhost:3000".to_string(),
|
|
42
|
+
timeout_ms: 30000,
|
|
43
|
+
follow_redirects: true,
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/// Common test response metadata
|
|
49
|
+
#[derive(Debug, Clone)]
|
|
50
|
+
pub struct TestResponseMetadata {
|
|
51
|
+
/// HTTP status code
|
|
52
|
+
pub status_code: u16,
|
|
53
|
+
/// Response headers
|
|
54
|
+
pub headers: HashMap<String, String>,
|
|
55
|
+
/// Response body size in bytes
|
|
56
|
+
pub body_size: usize,
|
|
57
|
+
/// Response time in milliseconds
|
|
58
|
+
pub response_time_ms: u64,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
impl TestResponseMetadata {
|
|
62
|
+
/// Create a new test response metadata
|
|
63
|
+
pub fn new(status_code: u16, headers: HashMap<String, String>, body_size: usize, response_time_ms: u64) -> Self {
|
|
64
|
+
Self {
|
|
65
|
+
status_code,
|
|
66
|
+
headers,
|
|
67
|
+
body_size,
|
|
68
|
+
response_time_ms,
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// Get a header value by name (case-insensitive)
|
|
73
|
+
pub fn get_header(&self, name: &str) -> Option<&String> {
|
|
74
|
+
let lower_name = name.to_lowercase();
|
|
75
|
+
self.headers
|
|
76
|
+
.iter()
|
|
77
|
+
.find(|(k, _)| k.to_lowercase() == lower_name)
|
|
78
|
+
.map(|(_, v)| v)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// Check if response was successful (2xx status code)
|
|
82
|
+
pub fn is_success(&self) -> bool {
|
|
83
|
+
self.status_code >= 200 && self.status_code < 300
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/// Check if response was a client error (4xx status code)
|
|
87
|
+
pub fn is_client_error(&self) -> bool {
|
|
88
|
+
self.status_code >= 400 && self.status_code < 500
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// Check if response was a server error (5xx status code)
|
|
92
|
+
pub fn is_server_error(&self) -> bool {
|
|
93
|
+
self.status_code >= 500 && self.status_code < 600
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
#[cfg(test)]
|
|
98
|
+
mod tests {
|
|
99
|
+
use super::*;
|
|
100
|
+
|
|
101
|
+
#[test]
|
|
102
|
+
fn test_test_client_config_default() {
|
|
103
|
+
let config = TestClientConfig::default();
|
|
104
|
+
assert_eq!(config.base_url, "http://localhost:3000");
|
|
105
|
+
assert_eq!(config.timeout_ms, 30000);
|
|
106
|
+
assert!(config.follow_redirects);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
#[test]
|
|
110
|
+
fn test_test_client_config_new() {
|
|
111
|
+
let config = TestClientConfig::new("http://example.com:8080");
|
|
112
|
+
assert_eq!(config.base_url, "http://example.com:8080");
|
|
113
|
+
assert_eq!(config.timeout_ms, 30000);
|
|
114
|
+
assert!(config.follow_redirects);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
#[test]
|
|
118
|
+
fn test_test_client_config_with_timeout() {
|
|
119
|
+
let config = TestClientConfig::default().with_timeout(5000);
|
|
120
|
+
assert_eq!(config.timeout_ms, 5000);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
#[test]
|
|
124
|
+
fn test_test_client_config_with_follow_redirects_false() {
|
|
125
|
+
let config = TestClientConfig::default().with_follow_redirects(false);
|
|
126
|
+
assert!(!config.follow_redirects);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
#[test]
|
|
130
|
+
fn test_test_client_config_chaining() {
|
|
131
|
+
let config = TestClientConfig::new("http://api.example.com")
|
|
132
|
+
.with_timeout(10000)
|
|
133
|
+
.with_follow_redirects(false);
|
|
134
|
+
|
|
135
|
+
assert_eq!(config.base_url, "http://api.example.com");
|
|
136
|
+
assert_eq!(config.timeout_ms, 10000);
|
|
137
|
+
assert!(!config.follow_redirects);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
#[test]
|
|
141
|
+
fn test_response_metadata_new() {
|
|
142
|
+
let mut headers = HashMap::new();
|
|
143
|
+
headers.insert("Content-Type".to_string(), "application/json".to_string());
|
|
144
|
+
|
|
145
|
+
let metadata = TestResponseMetadata::new(200, headers.clone(), 256, 100);
|
|
146
|
+
|
|
147
|
+
assert_eq!(metadata.status_code, 200);
|
|
148
|
+
assert_eq!(metadata.headers, headers);
|
|
149
|
+
assert_eq!(metadata.body_size, 256);
|
|
150
|
+
assert_eq!(metadata.response_time_ms, 100);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
#[test]
|
|
154
|
+
fn test_response_metadata_clone() {
|
|
155
|
+
let mut headers = HashMap::new();
|
|
156
|
+
headers.insert("Content-Type".to_string(), "application/json".to_string());
|
|
157
|
+
|
|
158
|
+
let metadata1 = TestResponseMetadata::new(201, headers, 512, 200);
|
|
159
|
+
let metadata2 = metadata1.clone();
|
|
160
|
+
|
|
161
|
+
assert_eq!(metadata1.status_code, metadata2.status_code);
|
|
162
|
+
assert_eq!(metadata1.body_size, metadata2.body_size);
|
|
163
|
+
assert_eq!(metadata1.response_time_ms, metadata2.response_time_ms);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
#[test]
|
|
167
|
+
fn test_get_header_case_insensitive() {
|
|
168
|
+
let mut headers = HashMap::new();
|
|
169
|
+
headers.insert("Content-Type".to_string(), "application/json".to_string());
|
|
170
|
+
headers.insert("Authorization".to_string(), "Bearer token123".to_string());
|
|
171
|
+
|
|
172
|
+
let metadata = TestResponseMetadata::new(200, headers, 100, 50);
|
|
173
|
+
|
|
174
|
+
assert_eq!(
|
|
175
|
+
metadata.get_header("Content-Type"),
|
|
176
|
+
Some(&"application/json".to_string())
|
|
177
|
+
);
|
|
178
|
+
assert_eq!(
|
|
179
|
+
metadata.get_header("content-type"),
|
|
180
|
+
Some(&"application/json".to_string())
|
|
181
|
+
);
|
|
182
|
+
assert_eq!(
|
|
183
|
+
metadata.get_header("CONTENT-TYPE"),
|
|
184
|
+
Some(&"application/json".to_string())
|
|
185
|
+
);
|
|
186
|
+
assert_eq!(metadata.get_header("Missing"), None);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
#[test]
|
|
190
|
+
fn test_is_success() {
|
|
191
|
+
let headers = HashMap::new();
|
|
192
|
+
let metadata_200 = TestResponseMetadata::new(200, headers.clone(), 100, 50);
|
|
193
|
+
let metadata_201 = TestResponseMetadata::new(201, headers.clone(), 100, 50);
|
|
194
|
+
let metadata_204 = TestResponseMetadata::new(204, headers.clone(), 0, 50);
|
|
195
|
+
let metadata_299 = TestResponseMetadata::new(299, headers.clone(), 100, 50);
|
|
196
|
+
let metadata_300 = TestResponseMetadata::new(300, headers.clone(), 100, 50);
|
|
197
|
+
let metadata_400 = TestResponseMetadata::new(400, headers.clone(), 100, 50);
|
|
198
|
+
|
|
199
|
+
assert!(metadata_200.is_success());
|
|
200
|
+
assert!(metadata_201.is_success());
|
|
201
|
+
assert!(metadata_204.is_success());
|
|
202
|
+
assert!(metadata_299.is_success());
|
|
203
|
+
assert!(!metadata_300.is_success());
|
|
204
|
+
assert!(!metadata_400.is_success());
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
#[test]
|
|
208
|
+
fn test_is_client_error() {
|
|
209
|
+
let headers = HashMap::new();
|
|
210
|
+
let metadata_399 = TestResponseMetadata::new(399, headers.clone(), 100, 50);
|
|
211
|
+
let metadata_400 = TestResponseMetadata::new(400, headers.clone(), 100, 50);
|
|
212
|
+
let metadata_404 = TestResponseMetadata::new(404, headers.clone(), 100, 50);
|
|
213
|
+
let metadata_499 = TestResponseMetadata::new(499, headers.clone(), 100, 50);
|
|
214
|
+
let metadata_500 = TestResponseMetadata::new(500, headers.clone(), 100, 50);
|
|
215
|
+
|
|
216
|
+
assert!(!metadata_399.is_client_error());
|
|
217
|
+
assert!(metadata_400.is_client_error());
|
|
218
|
+
assert!(metadata_404.is_client_error());
|
|
219
|
+
assert!(metadata_499.is_client_error());
|
|
220
|
+
assert!(!metadata_500.is_client_error());
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
#[test]
|
|
224
|
+
fn test_is_server_error() {
|
|
225
|
+
let headers = HashMap::new();
|
|
226
|
+
let metadata_499 = TestResponseMetadata::new(499, headers.clone(), 100, 50);
|
|
227
|
+
let metadata_500 = TestResponseMetadata::new(500, headers.clone(), 100, 50);
|
|
228
|
+
let metadata_502 = TestResponseMetadata::new(502, headers.clone(), 100, 50);
|
|
229
|
+
let metadata_599 = TestResponseMetadata::new(599, headers.clone(), 100, 50);
|
|
230
|
+
let metadata_600 = TestResponseMetadata::new(600, headers.clone(), 100, 50);
|
|
231
|
+
|
|
232
|
+
assert!(!metadata_499.is_server_error());
|
|
233
|
+
assert!(metadata_500.is_server_error());
|
|
234
|
+
assert!(metadata_502.is_server_error());
|
|
235
|
+
assert!(metadata_599.is_server_error());
|
|
236
|
+
assert!(!metadata_600.is_server_error());
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
#[test]
|
|
240
|
+
fn test_response_metadata_debug() {
|
|
241
|
+
let headers = HashMap::new();
|
|
242
|
+
let metadata = TestResponseMetadata::new(200, headers, 100, 50);
|
|
243
|
+
let debug_str = format!("{:?}", metadata);
|
|
244
|
+
assert!(debug_str.contains("200"));
|
|
245
|
+
assert!(debug_str.contains("100"));
|
|
246
|
+
assert!(debug_str.contains("50"));
|
|
247
|
+
}
|
|
248
|
+
}
|