spikard 0.3.5 → 0.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +659 -659
- data/ext/spikard_rb/Cargo.toml +17 -17
- data/ext/spikard_rb/extconf.rb +10 -10
- data/ext/spikard_rb/src/lib.rs +6 -6
- data/lib/spikard/app.rb +386 -386
- data/lib/spikard/background.rb +27 -27
- data/lib/spikard/config.rb +396 -396
- data/lib/spikard/converters.rb +13 -13
- data/lib/spikard/handler_wrapper.rb +113 -113
- data/lib/spikard/provide.rb +214 -214
- data/lib/spikard/response.rb +173 -173
- data/lib/spikard/schema.rb +243 -243
- data/lib/spikard/sse.rb +111 -111
- data/lib/spikard/streaming_response.rb +44 -44
- data/lib/spikard/testing.rb +221 -221
- data/lib/spikard/upload_file.rb +131 -131
- data/lib/spikard/version.rb +5 -5
- data/lib/spikard/websocket.rb +59 -59
- data/lib/spikard.rb +43 -43
- data/sig/spikard.rbs +366 -360
- data/vendor/crates/spikard-core/Cargo.toml +40 -40
- data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -3
- data/vendor/crates/spikard-core/src/bindings/response.rs +133 -133
- data/vendor/crates/spikard-core/src/debug.rs +63 -63
- data/vendor/crates/spikard-core/src/di/container.rs +726 -726
- data/vendor/crates/spikard-core/src/di/dependency.rs +273 -273
- data/vendor/crates/spikard-core/src/di/error.rs +118 -118
- data/vendor/crates/spikard-core/src/di/factory.rs +538 -538
- data/vendor/crates/spikard-core/src/di/graph.rs +545 -545
- data/vendor/crates/spikard-core/src/di/mod.rs +192 -192
- data/vendor/crates/spikard-core/src/di/resolved.rs +411 -411
- data/vendor/crates/spikard-core/src/di/value.rs +283 -283
- data/vendor/crates/spikard-core/src/errors.rs +39 -39
- data/vendor/crates/spikard-core/src/http.rs +153 -153
- data/vendor/crates/spikard-core/src/lib.rs +29 -29
- data/vendor/crates/spikard-core/src/lifecycle.rs +422 -422
- data/vendor/crates/spikard-core/src/parameters.rs +722 -722
- data/vendor/crates/spikard-core/src/problem.rs +310 -310
- data/vendor/crates/spikard-core/src/request_data.rs +189 -189
- data/vendor/crates/spikard-core/src/router.rs +249 -249
- data/vendor/crates/spikard-core/src/schema_registry.rs +183 -183
- data/vendor/crates/spikard-core/src/type_hints.rs +304 -304
- data/vendor/crates/spikard-core/src/validation.rs +699 -699
- data/vendor/crates/spikard-http/Cargo.toml +68 -68
- data/vendor/crates/spikard-http/src/auth.rs +247 -247
- data/vendor/crates/spikard-http/src/background.rs +249 -249
- data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -3
- data/vendor/crates/spikard-http/src/bindings/response.rs +1 -1
- data/vendor/crates/spikard-http/src/body_metadata.rs +8 -8
- data/vendor/crates/spikard-http/src/cors.rs +490 -490
- data/vendor/crates/spikard-http/src/debug.rs +63 -63
- data/vendor/crates/spikard-http/src/di_handler.rs +423 -423
- data/vendor/crates/spikard-http/src/handler_response.rs +190 -190
- data/vendor/crates/spikard-http/src/handler_trait.rs +228 -228
- data/vendor/crates/spikard-http/src/handler_trait_tests.rs +284 -284
- data/vendor/crates/spikard-http/src/lib.rs +529 -529
- data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +149 -149
- data/vendor/crates/spikard-http/src/lifecycle.rs +428 -428
- data/vendor/crates/spikard-http/src/middleware/mod.rs +285 -285
- data/vendor/crates/spikard-http/src/middleware/multipart.rs +86 -86
- data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +147 -147
- data/vendor/crates/spikard-http/src/middleware/validation.rs +287 -287
- data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -309
- data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +190 -190
- data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +308 -308
- data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +195 -195
- data/vendor/crates/spikard-http/src/parameters.rs +1 -1
- data/vendor/crates/spikard-http/src/problem.rs +1 -1
- data/vendor/crates/spikard-http/src/query_parser.rs +369 -369
- data/vendor/crates/spikard-http/src/response.rs +399 -399
- data/vendor/crates/spikard-http/src/router.rs +1 -1
- data/vendor/crates/spikard-http/src/schema_registry.rs +1 -1
- data/vendor/crates/spikard-http/src/server/handler.rs +87 -87
- data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +98 -98
- data/vendor/crates/spikard-http/src/server/mod.rs +805 -805
- data/vendor/crates/spikard-http/src/server/request_extraction.rs +119 -119
- data/vendor/crates/spikard-http/src/sse.rs +447 -447
- data/vendor/crates/spikard-http/src/testing/form.rs +14 -14
- data/vendor/crates/spikard-http/src/testing/multipart.rs +60 -60
- data/vendor/crates/spikard-http/src/testing/test_client.rs +285 -285
- data/vendor/crates/spikard-http/src/testing.rs +377 -377
- data/vendor/crates/spikard-http/src/type_hints.rs +1 -1
- data/vendor/crates/spikard-http/src/validation.rs +1 -1
- data/vendor/crates/spikard-http/src/websocket.rs +324 -324
- data/vendor/crates/spikard-rb/Cargo.toml +42 -42
- data/vendor/crates/spikard-rb/build.rs +8 -8
- data/vendor/crates/spikard-rb/src/background.rs +63 -63
- data/vendor/crates/spikard-rb/src/config.rs +294 -294
- data/vendor/crates/spikard-rb/src/conversion.rs +453 -453
- data/vendor/crates/spikard-rb/src/di.rs +409 -409
- data/vendor/crates/spikard-rb/src/handler.rs +625 -625
- data/vendor/crates/spikard-rb/src/lib.rs +2771 -2771
- data/vendor/crates/spikard-rb/src/lifecycle.rs +274 -274
- data/vendor/crates/spikard-rb/src/server.rs +283 -283
- data/vendor/crates/spikard-rb/src/sse.rs +231 -231
- data/vendor/crates/spikard-rb/src/test_client.rs +404 -404
- data/vendor/crates/spikard-rb/src/test_sse.rs +143 -143
- data/vendor/crates/spikard-rb/src/test_websocket.rs +221 -221
- data/vendor/crates/spikard-rb/src/websocket.rs +233 -233
- metadata +1 -1
|
@@ -1,399 +1,399 @@
|
|
|
1
|
-
//! HTTP Response types
|
|
2
|
-
//!
|
|
3
|
-
//! Response types for returning custom responses with status codes, headers, and content
|
|
4
|
-
|
|
5
|
-
use serde_json::Value;
|
|
6
|
-
use std::collections::HashMap;
|
|
7
|
-
|
|
8
|
-
/// HTTP Response with custom status code, headers, and content
|
|
9
|
-
#[derive(Debug, Clone)]
|
|
10
|
-
pub struct Response {
|
|
11
|
-
/// Response body content
|
|
12
|
-
pub content: Option<Value>,
|
|
13
|
-
/// HTTP status code (defaults to 200)
|
|
14
|
-
pub status_code: u16,
|
|
15
|
-
/// Response headers
|
|
16
|
-
pub headers: HashMap<String, String>,
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
impl Response {
|
|
20
|
-
/// Create a new Response with default status 200
|
|
21
|
-
pub fn new(content: Option<Value>) -> Self {
|
|
22
|
-
Self {
|
|
23
|
-
content,
|
|
24
|
-
status_code: 200,
|
|
25
|
-
headers: HashMap::new(),
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/// Create a response with a specific status code
|
|
30
|
-
pub fn with_status(content: Option<Value>, status_code: u16) -> Self {
|
|
31
|
-
Self {
|
|
32
|
-
content,
|
|
33
|
-
status_code,
|
|
34
|
-
headers: HashMap::new(),
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/// Set a header
|
|
39
|
-
pub fn set_header(&mut self, key: String, value: String) {
|
|
40
|
-
self.headers.insert(key, value);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/// Set a cookie in the response
|
|
44
|
-
#[allow(clippy::too_many_arguments)]
|
|
45
|
-
pub fn set_cookie(
|
|
46
|
-
&mut self,
|
|
47
|
-
key: String,
|
|
48
|
-
value: String,
|
|
49
|
-
max_age: Option<i64>,
|
|
50
|
-
domain: Option<String>,
|
|
51
|
-
path: Option<String>,
|
|
52
|
-
secure: bool,
|
|
53
|
-
http_only: bool,
|
|
54
|
-
same_site: Option<String>,
|
|
55
|
-
) {
|
|
56
|
-
let mut cookie_value = format!("{}={}", key, value);
|
|
57
|
-
|
|
58
|
-
if let Some(age) = max_age {
|
|
59
|
-
cookie_value.push_str(&format!("; Max-Age={}", age));
|
|
60
|
-
}
|
|
61
|
-
if let Some(d) = domain {
|
|
62
|
-
cookie_value.push_str(&format!("; Domain={}", d));
|
|
63
|
-
}
|
|
64
|
-
if let Some(p) = path {
|
|
65
|
-
cookie_value.push_str(&format!("; Path={}", p));
|
|
66
|
-
}
|
|
67
|
-
if secure {
|
|
68
|
-
cookie_value.push_str("; Secure");
|
|
69
|
-
}
|
|
70
|
-
if http_only {
|
|
71
|
-
cookie_value.push_str("; HttpOnly");
|
|
72
|
-
}
|
|
73
|
-
if let Some(ss) = same_site {
|
|
74
|
-
cookie_value.push_str(&format!("; SameSite={}", ss));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
self.headers.insert("set-cookie".to_string(), cookie_value);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
impl Default for Response {
|
|
82
|
-
fn default() -> Self {
|
|
83
|
-
Self::new(None)
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
#[cfg(test)]
|
|
88
|
-
mod tests {
|
|
89
|
-
use super::*;
|
|
90
|
-
use serde_json::json;
|
|
91
|
-
|
|
92
|
-
#[test]
|
|
93
|
-
fn response_new_creates_default_status() {
|
|
94
|
-
let response = Response::new(None);
|
|
95
|
-
assert_eq!(response.status_code, 200);
|
|
96
|
-
assert!(response.headers.is_empty());
|
|
97
|
-
assert!(response.content.is_none());
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
#[test]
|
|
101
|
-
fn response_new_with_content() {
|
|
102
|
-
let content = json!({"key": "value"});
|
|
103
|
-
let response = Response::new(Some(content.clone()));
|
|
104
|
-
assert_eq!(response.status_code, 200);
|
|
105
|
-
assert_eq!(response.content, Some(content));
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
#[test]
|
|
109
|
-
fn response_with_status() {
|
|
110
|
-
let response = Response::with_status(None, 404);
|
|
111
|
-
assert_eq!(response.status_code, 404);
|
|
112
|
-
assert!(response.headers.is_empty());
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
#[test]
|
|
116
|
-
fn response_with_status_and_content() {
|
|
117
|
-
let content = json!({"error": "not found"});
|
|
118
|
-
let response = Response::with_status(Some(content.clone()), 404);
|
|
119
|
-
assert_eq!(response.status_code, 404);
|
|
120
|
-
assert_eq!(response.content, Some(content));
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
#[test]
|
|
124
|
-
fn response_set_header() {
|
|
125
|
-
let mut response = Response::new(None);
|
|
126
|
-
response.set_header("X-Custom".to_string(), "custom-value".to_string());
|
|
127
|
-
assert_eq!(response.headers.get("X-Custom"), Some(&"custom-value".to_string()));
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
#[test]
|
|
131
|
-
fn response_set_multiple_headers() {
|
|
132
|
-
let mut response = Response::new(None);
|
|
133
|
-
response.set_header("Content-Type".to_string(), "application/json".to_string());
|
|
134
|
-
response.set_header("X-Custom".to_string(), "custom-value".to_string());
|
|
135
|
-
assert_eq!(response.headers.len(), 2);
|
|
136
|
-
assert_eq!(
|
|
137
|
-
response.headers.get("Content-Type"),
|
|
138
|
-
Some(&"application/json".to_string())
|
|
139
|
-
);
|
|
140
|
-
assert_eq!(response.headers.get("X-Custom"), Some(&"custom-value".to_string()));
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
#[test]
|
|
144
|
-
fn response_set_header_overwrites() {
|
|
145
|
-
let mut response = Response::new(None);
|
|
146
|
-
response.set_header("X-Custom".to_string(), "value1".to_string());
|
|
147
|
-
response.set_header("X-Custom".to_string(), "value2".to_string());
|
|
148
|
-
assert_eq!(response.headers.get("X-Custom"), Some(&"value2".to_string()));
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
#[test]
|
|
152
|
-
fn response_set_cookie_minimal() {
|
|
153
|
-
let mut response = Response::new(None);
|
|
154
|
-
response.set_cookie(
|
|
155
|
-
"session_id".to_string(),
|
|
156
|
-
"abc123".to_string(),
|
|
157
|
-
None,
|
|
158
|
-
None,
|
|
159
|
-
None,
|
|
160
|
-
false,
|
|
161
|
-
false,
|
|
162
|
-
None,
|
|
163
|
-
);
|
|
164
|
-
let cookie = response.headers.get("set-cookie").unwrap();
|
|
165
|
-
assert_eq!(cookie, "session_id=abc123");
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
#[test]
|
|
169
|
-
fn response_set_cookie_with_max_age() {
|
|
170
|
-
let mut response = Response::new(None);
|
|
171
|
-
response.set_cookie(
|
|
172
|
-
"session".to_string(),
|
|
173
|
-
"token".to_string(),
|
|
174
|
-
Some(3600),
|
|
175
|
-
None,
|
|
176
|
-
None,
|
|
177
|
-
false,
|
|
178
|
-
false,
|
|
179
|
-
None,
|
|
180
|
-
);
|
|
181
|
-
let cookie = response.headers.get("set-cookie").unwrap();
|
|
182
|
-
assert!(cookie.contains("session=token"));
|
|
183
|
-
assert!(cookie.contains("Max-Age=3600"));
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
#[test]
|
|
187
|
-
fn response_set_cookie_with_domain() {
|
|
188
|
-
let mut response = Response::new(None);
|
|
189
|
-
response.set_cookie(
|
|
190
|
-
"session".to_string(),
|
|
191
|
-
"token".to_string(),
|
|
192
|
-
None,
|
|
193
|
-
Some("example.com".to_string()),
|
|
194
|
-
None,
|
|
195
|
-
false,
|
|
196
|
-
false,
|
|
197
|
-
None,
|
|
198
|
-
);
|
|
199
|
-
let cookie = response.headers.get("set-cookie").unwrap();
|
|
200
|
-
assert!(cookie.contains("Domain=example.com"));
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
#[test]
|
|
204
|
-
fn response_set_cookie_with_path() {
|
|
205
|
-
let mut response = Response::new(None);
|
|
206
|
-
response.set_cookie(
|
|
207
|
-
"session".to_string(),
|
|
208
|
-
"token".to_string(),
|
|
209
|
-
None,
|
|
210
|
-
None,
|
|
211
|
-
Some("/app".to_string()),
|
|
212
|
-
false,
|
|
213
|
-
false,
|
|
214
|
-
None,
|
|
215
|
-
);
|
|
216
|
-
let cookie = response.headers.get("set-cookie").unwrap();
|
|
217
|
-
assert!(cookie.contains("Path=/app"));
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
#[test]
|
|
221
|
-
fn response_set_cookie_secure() {
|
|
222
|
-
let mut response = Response::new(None);
|
|
223
|
-
response.set_cookie(
|
|
224
|
-
"session".to_string(),
|
|
225
|
-
"token".to_string(),
|
|
226
|
-
None,
|
|
227
|
-
None,
|
|
228
|
-
None,
|
|
229
|
-
true,
|
|
230
|
-
false,
|
|
231
|
-
None,
|
|
232
|
-
);
|
|
233
|
-
let cookie = response.headers.get("set-cookie").unwrap();
|
|
234
|
-
assert!(cookie.contains("Secure"));
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
#[test]
|
|
238
|
-
fn response_set_cookie_http_only() {
|
|
239
|
-
let mut response = Response::new(None);
|
|
240
|
-
response.set_cookie(
|
|
241
|
-
"session".to_string(),
|
|
242
|
-
"token".to_string(),
|
|
243
|
-
None,
|
|
244
|
-
None,
|
|
245
|
-
None,
|
|
246
|
-
false,
|
|
247
|
-
true,
|
|
248
|
-
None,
|
|
249
|
-
);
|
|
250
|
-
let cookie = response.headers.get("set-cookie").unwrap();
|
|
251
|
-
assert!(cookie.contains("HttpOnly"));
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
#[test]
|
|
255
|
-
fn response_set_cookie_same_site() {
|
|
256
|
-
let mut response = Response::new(None);
|
|
257
|
-
response.set_cookie(
|
|
258
|
-
"session".to_string(),
|
|
259
|
-
"token".to_string(),
|
|
260
|
-
None,
|
|
261
|
-
None,
|
|
262
|
-
None,
|
|
263
|
-
false,
|
|
264
|
-
false,
|
|
265
|
-
Some("Strict".to_string()),
|
|
266
|
-
);
|
|
267
|
-
let cookie = response.headers.get("set-cookie").unwrap();
|
|
268
|
-
assert!(cookie.contains("SameSite=Strict"));
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
#[test]
|
|
272
|
-
fn response_set_cookie_all_attributes() {
|
|
273
|
-
let mut response = Response::new(None);
|
|
274
|
-
response.set_cookie(
|
|
275
|
-
"session".to_string(),
|
|
276
|
-
"token123".to_string(),
|
|
277
|
-
Some(3600),
|
|
278
|
-
Some("example.com".to_string()),
|
|
279
|
-
Some("/app".to_string()),
|
|
280
|
-
true,
|
|
281
|
-
true,
|
|
282
|
-
Some("Lax".to_string()),
|
|
283
|
-
);
|
|
284
|
-
let cookie = response.headers.get("set-cookie").unwrap();
|
|
285
|
-
assert!(cookie.contains("session=token123"));
|
|
286
|
-
assert!(cookie.contains("Max-Age=3600"));
|
|
287
|
-
assert!(cookie.contains("Domain=example.com"));
|
|
288
|
-
assert!(cookie.contains("Path=/app"));
|
|
289
|
-
assert!(cookie.contains("Secure"));
|
|
290
|
-
assert!(cookie.contains("HttpOnly"));
|
|
291
|
-
assert!(cookie.contains("SameSite=Lax"));
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
#[test]
|
|
295
|
-
fn response_set_cookie_overwrites_previous() {
|
|
296
|
-
let mut response = Response::new(None);
|
|
297
|
-
response.set_cookie(
|
|
298
|
-
"session".to_string(),
|
|
299
|
-
"old_token".to_string(),
|
|
300
|
-
None,
|
|
301
|
-
None,
|
|
302
|
-
None,
|
|
303
|
-
false,
|
|
304
|
-
false,
|
|
305
|
-
None,
|
|
306
|
-
);
|
|
307
|
-
response.set_cookie(
|
|
308
|
-
"session".to_string(),
|
|
309
|
-
"new_token".to_string(),
|
|
310
|
-
None,
|
|
311
|
-
None,
|
|
312
|
-
None,
|
|
313
|
-
false,
|
|
314
|
-
false,
|
|
315
|
-
None,
|
|
316
|
-
);
|
|
317
|
-
let cookie = response.headers.get("set-cookie").unwrap();
|
|
318
|
-
assert!(cookie.contains("new_token"));
|
|
319
|
-
assert!(!cookie.contains("old_token"));
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
#[test]
|
|
323
|
-
fn response_default() {
|
|
324
|
-
let response = Response::default();
|
|
325
|
-
assert_eq!(response.status_code, 200);
|
|
326
|
-
assert!(response.headers.is_empty());
|
|
327
|
-
assert!(response.content.is_none());
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
#[test]
|
|
331
|
-
fn response_cookie_with_special_chars_in_value() {
|
|
332
|
-
let mut response = Response::new(None);
|
|
333
|
-
response.set_cookie(
|
|
334
|
-
"name".to_string(),
|
|
335
|
-
"value%3D123".to_string(),
|
|
336
|
-
None,
|
|
337
|
-
None,
|
|
338
|
-
None,
|
|
339
|
-
false,
|
|
340
|
-
false,
|
|
341
|
-
None,
|
|
342
|
-
);
|
|
343
|
-
let cookie = response.headers.get("set-cookie").unwrap();
|
|
344
|
-
assert_eq!(cookie, "name=value%3D123");
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
#[test]
|
|
348
|
-
fn response_same_site_variants() {
|
|
349
|
-
for same_site in &["Strict", "Lax", "None"] {
|
|
350
|
-
let mut response = Response::new(None);
|
|
351
|
-
response.set_cookie(
|
|
352
|
-
"test".to_string(),
|
|
353
|
-
"value".to_string(),
|
|
354
|
-
None,
|
|
355
|
-
None,
|
|
356
|
-
None,
|
|
357
|
-
false,
|
|
358
|
-
false,
|
|
359
|
-
Some(same_site.to_string()),
|
|
360
|
-
);
|
|
361
|
-
let cookie = response.headers.get("set-cookie").unwrap();
|
|
362
|
-
assert!(cookie.contains(&format!("SameSite={}", same_site)));
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
#[test]
|
|
367
|
-
fn response_zero_max_age() {
|
|
368
|
-
let mut response = Response::new(None);
|
|
369
|
-
response.set_cookie(
|
|
370
|
-
"session".to_string(),
|
|
371
|
-
"token".to_string(),
|
|
372
|
-
Some(0),
|
|
373
|
-
None,
|
|
374
|
-
None,
|
|
375
|
-
false,
|
|
376
|
-
false,
|
|
377
|
-
None,
|
|
378
|
-
);
|
|
379
|
-
let cookie = response.headers.get("set-cookie").unwrap();
|
|
380
|
-
assert!(cookie.contains("Max-Age=0"));
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
#[test]
|
|
384
|
-
fn response_negative_max_age() {
|
|
385
|
-
let mut response = Response::new(None);
|
|
386
|
-
response.set_cookie(
|
|
387
|
-
"session".to_string(),
|
|
388
|
-
"token".to_string(),
|
|
389
|
-
Some(-1),
|
|
390
|
-
None,
|
|
391
|
-
None,
|
|
392
|
-
false,
|
|
393
|
-
false,
|
|
394
|
-
None,
|
|
395
|
-
);
|
|
396
|
-
let cookie = response.headers.get("set-cookie").unwrap();
|
|
397
|
-
assert!(cookie.contains("Max-Age=-1"));
|
|
398
|
-
}
|
|
399
|
-
}
|
|
1
|
+
//! HTTP Response types
|
|
2
|
+
//!
|
|
3
|
+
//! Response types for returning custom responses with status codes, headers, and content
|
|
4
|
+
|
|
5
|
+
use serde_json::Value;
|
|
6
|
+
use std::collections::HashMap;
|
|
7
|
+
|
|
8
|
+
/// HTTP Response with custom status code, headers, and content
|
|
9
|
+
#[derive(Debug, Clone)]
|
|
10
|
+
pub struct Response {
|
|
11
|
+
/// Response body content
|
|
12
|
+
pub content: Option<Value>,
|
|
13
|
+
/// HTTP status code (defaults to 200)
|
|
14
|
+
pub status_code: u16,
|
|
15
|
+
/// Response headers
|
|
16
|
+
pub headers: HashMap<String, String>,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
impl Response {
|
|
20
|
+
/// Create a new Response with default status 200
|
|
21
|
+
pub fn new(content: Option<Value>) -> Self {
|
|
22
|
+
Self {
|
|
23
|
+
content,
|
|
24
|
+
status_code: 200,
|
|
25
|
+
headers: HashMap::new(),
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// Create a response with a specific status code
|
|
30
|
+
pub fn with_status(content: Option<Value>, status_code: u16) -> Self {
|
|
31
|
+
Self {
|
|
32
|
+
content,
|
|
33
|
+
status_code,
|
|
34
|
+
headers: HashMap::new(),
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/// Set a header
|
|
39
|
+
pub fn set_header(&mut self, key: String, value: String) {
|
|
40
|
+
self.headers.insert(key, value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/// Set a cookie in the response
|
|
44
|
+
#[allow(clippy::too_many_arguments)]
|
|
45
|
+
pub fn set_cookie(
|
|
46
|
+
&mut self,
|
|
47
|
+
key: String,
|
|
48
|
+
value: String,
|
|
49
|
+
max_age: Option<i64>,
|
|
50
|
+
domain: Option<String>,
|
|
51
|
+
path: Option<String>,
|
|
52
|
+
secure: bool,
|
|
53
|
+
http_only: bool,
|
|
54
|
+
same_site: Option<String>,
|
|
55
|
+
) {
|
|
56
|
+
let mut cookie_value = format!("{}={}", key, value);
|
|
57
|
+
|
|
58
|
+
if let Some(age) = max_age {
|
|
59
|
+
cookie_value.push_str(&format!("; Max-Age={}", age));
|
|
60
|
+
}
|
|
61
|
+
if let Some(d) = domain {
|
|
62
|
+
cookie_value.push_str(&format!("; Domain={}", d));
|
|
63
|
+
}
|
|
64
|
+
if let Some(p) = path {
|
|
65
|
+
cookie_value.push_str(&format!("; Path={}", p));
|
|
66
|
+
}
|
|
67
|
+
if secure {
|
|
68
|
+
cookie_value.push_str("; Secure");
|
|
69
|
+
}
|
|
70
|
+
if http_only {
|
|
71
|
+
cookie_value.push_str("; HttpOnly");
|
|
72
|
+
}
|
|
73
|
+
if let Some(ss) = same_site {
|
|
74
|
+
cookie_value.push_str(&format!("; SameSite={}", ss));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
self.headers.insert("set-cookie".to_string(), cookie_value);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
impl Default for Response {
|
|
82
|
+
fn default() -> Self {
|
|
83
|
+
Self::new(None)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
#[cfg(test)]
|
|
88
|
+
mod tests {
|
|
89
|
+
use super::*;
|
|
90
|
+
use serde_json::json;
|
|
91
|
+
|
|
92
|
+
#[test]
|
|
93
|
+
fn response_new_creates_default_status() {
|
|
94
|
+
let response = Response::new(None);
|
|
95
|
+
assert_eq!(response.status_code, 200);
|
|
96
|
+
assert!(response.headers.is_empty());
|
|
97
|
+
assert!(response.content.is_none());
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
#[test]
|
|
101
|
+
fn response_new_with_content() {
|
|
102
|
+
let content = json!({"key": "value"});
|
|
103
|
+
let response = Response::new(Some(content.clone()));
|
|
104
|
+
assert_eq!(response.status_code, 200);
|
|
105
|
+
assert_eq!(response.content, Some(content));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
#[test]
|
|
109
|
+
fn response_with_status() {
|
|
110
|
+
let response = Response::with_status(None, 404);
|
|
111
|
+
assert_eq!(response.status_code, 404);
|
|
112
|
+
assert!(response.headers.is_empty());
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
#[test]
|
|
116
|
+
fn response_with_status_and_content() {
|
|
117
|
+
let content = json!({"error": "not found"});
|
|
118
|
+
let response = Response::with_status(Some(content.clone()), 404);
|
|
119
|
+
assert_eq!(response.status_code, 404);
|
|
120
|
+
assert_eq!(response.content, Some(content));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
#[test]
|
|
124
|
+
fn response_set_header() {
|
|
125
|
+
let mut response = Response::new(None);
|
|
126
|
+
response.set_header("X-Custom".to_string(), "custom-value".to_string());
|
|
127
|
+
assert_eq!(response.headers.get("X-Custom"), Some(&"custom-value".to_string()));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
#[test]
|
|
131
|
+
fn response_set_multiple_headers() {
|
|
132
|
+
let mut response = Response::new(None);
|
|
133
|
+
response.set_header("Content-Type".to_string(), "application/json".to_string());
|
|
134
|
+
response.set_header("X-Custom".to_string(), "custom-value".to_string());
|
|
135
|
+
assert_eq!(response.headers.len(), 2);
|
|
136
|
+
assert_eq!(
|
|
137
|
+
response.headers.get("Content-Type"),
|
|
138
|
+
Some(&"application/json".to_string())
|
|
139
|
+
);
|
|
140
|
+
assert_eq!(response.headers.get("X-Custom"), Some(&"custom-value".to_string()));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
#[test]
|
|
144
|
+
fn response_set_header_overwrites() {
|
|
145
|
+
let mut response = Response::new(None);
|
|
146
|
+
response.set_header("X-Custom".to_string(), "value1".to_string());
|
|
147
|
+
response.set_header("X-Custom".to_string(), "value2".to_string());
|
|
148
|
+
assert_eq!(response.headers.get("X-Custom"), Some(&"value2".to_string()));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
#[test]
|
|
152
|
+
fn response_set_cookie_minimal() {
|
|
153
|
+
let mut response = Response::new(None);
|
|
154
|
+
response.set_cookie(
|
|
155
|
+
"session_id".to_string(),
|
|
156
|
+
"abc123".to_string(),
|
|
157
|
+
None,
|
|
158
|
+
None,
|
|
159
|
+
None,
|
|
160
|
+
false,
|
|
161
|
+
false,
|
|
162
|
+
None,
|
|
163
|
+
);
|
|
164
|
+
let cookie = response.headers.get("set-cookie").unwrap();
|
|
165
|
+
assert_eq!(cookie, "session_id=abc123");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
#[test]
|
|
169
|
+
fn response_set_cookie_with_max_age() {
|
|
170
|
+
let mut response = Response::new(None);
|
|
171
|
+
response.set_cookie(
|
|
172
|
+
"session".to_string(),
|
|
173
|
+
"token".to_string(),
|
|
174
|
+
Some(3600),
|
|
175
|
+
None,
|
|
176
|
+
None,
|
|
177
|
+
false,
|
|
178
|
+
false,
|
|
179
|
+
None,
|
|
180
|
+
);
|
|
181
|
+
let cookie = response.headers.get("set-cookie").unwrap();
|
|
182
|
+
assert!(cookie.contains("session=token"));
|
|
183
|
+
assert!(cookie.contains("Max-Age=3600"));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
#[test]
|
|
187
|
+
fn response_set_cookie_with_domain() {
|
|
188
|
+
let mut response = Response::new(None);
|
|
189
|
+
response.set_cookie(
|
|
190
|
+
"session".to_string(),
|
|
191
|
+
"token".to_string(),
|
|
192
|
+
None,
|
|
193
|
+
Some("example.com".to_string()),
|
|
194
|
+
None,
|
|
195
|
+
false,
|
|
196
|
+
false,
|
|
197
|
+
None,
|
|
198
|
+
);
|
|
199
|
+
let cookie = response.headers.get("set-cookie").unwrap();
|
|
200
|
+
assert!(cookie.contains("Domain=example.com"));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
#[test]
|
|
204
|
+
fn response_set_cookie_with_path() {
|
|
205
|
+
let mut response = Response::new(None);
|
|
206
|
+
response.set_cookie(
|
|
207
|
+
"session".to_string(),
|
|
208
|
+
"token".to_string(),
|
|
209
|
+
None,
|
|
210
|
+
None,
|
|
211
|
+
Some("/app".to_string()),
|
|
212
|
+
false,
|
|
213
|
+
false,
|
|
214
|
+
None,
|
|
215
|
+
);
|
|
216
|
+
let cookie = response.headers.get("set-cookie").unwrap();
|
|
217
|
+
assert!(cookie.contains("Path=/app"));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
#[test]
|
|
221
|
+
fn response_set_cookie_secure() {
|
|
222
|
+
let mut response = Response::new(None);
|
|
223
|
+
response.set_cookie(
|
|
224
|
+
"session".to_string(),
|
|
225
|
+
"token".to_string(),
|
|
226
|
+
None,
|
|
227
|
+
None,
|
|
228
|
+
None,
|
|
229
|
+
true,
|
|
230
|
+
false,
|
|
231
|
+
None,
|
|
232
|
+
);
|
|
233
|
+
let cookie = response.headers.get("set-cookie").unwrap();
|
|
234
|
+
assert!(cookie.contains("Secure"));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
#[test]
|
|
238
|
+
fn response_set_cookie_http_only() {
|
|
239
|
+
let mut response = Response::new(None);
|
|
240
|
+
response.set_cookie(
|
|
241
|
+
"session".to_string(),
|
|
242
|
+
"token".to_string(),
|
|
243
|
+
None,
|
|
244
|
+
None,
|
|
245
|
+
None,
|
|
246
|
+
false,
|
|
247
|
+
true,
|
|
248
|
+
None,
|
|
249
|
+
);
|
|
250
|
+
let cookie = response.headers.get("set-cookie").unwrap();
|
|
251
|
+
assert!(cookie.contains("HttpOnly"));
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
#[test]
|
|
255
|
+
fn response_set_cookie_same_site() {
|
|
256
|
+
let mut response = Response::new(None);
|
|
257
|
+
response.set_cookie(
|
|
258
|
+
"session".to_string(),
|
|
259
|
+
"token".to_string(),
|
|
260
|
+
None,
|
|
261
|
+
None,
|
|
262
|
+
None,
|
|
263
|
+
false,
|
|
264
|
+
false,
|
|
265
|
+
Some("Strict".to_string()),
|
|
266
|
+
);
|
|
267
|
+
let cookie = response.headers.get("set-cookie").unwrap();
|
|
268
|
+
assert!(cookie.contains("SameSite=Strict"));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
#[test]
|
|
272
|
+
fn response_set_cookie_all_attributes() {
|
|
273
|
+
let mut response = Response::new(None);
|
|
274
|
+
response.set_cookie(
|
|
275
|
+
"session".to_string(),
|
|
276
|
+
"token123".to_string(),
|
|
277
|
+
Some(3600),
|
|
278
|
+
Some("example.com".to_string()),
|
|
279
|
+
Some("/app".to_string()),
|
|
280
|
+
true,
|
|
281
|
+
true,
|
|
282
|
+
Some("Lax".to_string()),
|
|
283
|
+
);
|
|
284
|
+
let cookie = response.headers.get("set-cookie").unwrap();
|
|
285
|
+
assert!(cookie.contains("session=token123"));
|
|
286
|
+
assert!(cookie.contains("Max-Age=3600"));
|
|
287
|
+
assert!(cookie.contains("Domain=example.com"));
|
|
288
|
+
assert!(cookie.contains("Path=/app"));
|
|
289
|
+
assert!(cookie.contains("Secure"));
|
|
290
|
+
assert!(cookie.contains("HttpOnly"));
|
|
291
|
+
assert!(cookie.contains("SameSite=Lax"));
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
#[test]
|
|
295
|
+
fn response_set_cookie_overwrites_previous() {
|
|
296
|
+
let mut response = Response::new(None);
|
|
297
|
+
response.set_cookie(
|
|
298
|
+
"session".to_string(),
|
|
299
|
+
"old_token".to_string(),
|
|
300
|
+
None,
|
|
301
|
+
None,
|
|
302
|
+
None,
|
|
303
|
+
false,
|
|
304
|
+
false,
|
|
305
|
+
None,
|
|
306
|
+
);
|
|
307
|
+
response.set_cookie(
|
|
308
|
+
"session".to_string(),
|
|
309
|
+
"new_token".to_string(),
|
|
310
|
+
None,
|
|
311
|
+
None,
|
|
312
|
+
None,
|
|
313
|
+
false,
|
|
314
|
+
false,
|
|
315
|
+
None,
|
|
316
|
+
);
|
|
317
|
+
let cookie = response.headers.get("set-cookie").unwrap();
|
|
318
|
+
assert!(cookie.contains("new_token"));
|
|
319
|
+
assert!(!cookie.contains("old_token"));
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
#[test]
|
|
323
|
+
fn response_default() {
|
|
324
|
+
let response = Response::default();
|
|
325
|
+
assert_eq!(response.status_code, 200);
|
|
326
|
+
assert!(response.headers.is_empty());
|
|
327
|
+
assert!(response.content.is_none());
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
#[test]
|
|
331
|
+
fn response_cookie_with_special_chars_in_value() {
|
|
332
|
+
let mut response = Response::new(None);
|
|
333
|
+
response.set_cookie(
|
|
334
|
+
"name".to_string(),
|
|
335
|
+
"value%3D123".to_string(),
|
|
336
|
+
None,
|
|
337
|
+
None,
|
|
338
|
+
None,
|
|
339
|
+
false,
|
|
340
|
+
false,
|
|
341
|
+
None,
|
|
342
|
+
);
|
|
343
|
+
let cookie = response.headers.get("set-cookie").unwrap();
|
|
344
|
+
assert_eq!(cookie, "name=value%3D123");
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
#[test]
|
|
348
|
+
fn response_same_site_variants() {
|
|
349
|
+
for same_site in &["Strict", "Lax", "None"] {
|
|
350
|
+
let mut response = Response::new(None);
|
|
351
|
+
response.set_cookie(
|
|
352
|
+
"test".to_string(),
|
|
353
|
+
"value".to_string(),
|
|
354
|
+
None,
|
|
355
|
+
None,
|
|
356
|
+
None,
|
|
357
|
+
false,
|
|
358
|
+
false,
|
|
359
|
+
Some(same_site.to_string()),
|
|
360
|
+
);
|
|
361
|
+
let cookie = response.headers.get("set-cookie").unwrap();
|
|
362
|
+
assert!(cookie.contains(&format!("SameSite={}", same_site)));
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
#[test]
|
|
367
|
+
fn response_zero_max_age() {
|
|
368
|
+
let mut response = Response::new(None);
|
|
369
|
+
response.set_cookie(
|
|
370
|
+
"session".to_string(),
|
|
371
|
+
"token".to_string(),
|
|
372
|
+
Some(0),
|
|
373
|
+
None,
|
|
374
|
+
None,
|
|
375
|
+
false,
|
|
376
|
+
false,
|
|
377
|
+
None,
|
|
378
|
+
);
|
|
379
|
+
let cookie = response.headers.get("set-cookie").unwrap();
|
|
380
|
+
assert!(cookie.contains("Max-Age=0"));
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
#[test]
|
|
384
|
+
fn response_negative_max_age() {
|
|
385
|
+
let mut response = Response::new(None);
|
|
386
|
+
response.set_cookie(
|
|
387
|
+
"session".to_string(),
|
|
388
|
+
"token".to_string(),
|
|
389
|
+
Some(-1),
|
|
390
|
+
None,
|
|
391
|
+
None,
|
|
392
|
+
false,
|
|
393
|
+
false,
|
|
394
|
+
None,
|
|
395
|
+
);
|
|
396
|
+
let cookie = response.headers.get("set-cookie").unwrap();
|
|
397
|
+
assert!(cookie.contains("Max-Age=-1"));
|
|
398
|
+
}
|
|
399
|
+
}
|