spikard 0.4.0-arm64-darwin-23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +1 -0
- data/README.md +659 -0
- data/ext/spikard_rb/Cargo.toml +17 -0
- data/ext/spikard_rb/extconf.rb +10 -0
- data/ext/spikard_rb/src/lib.rs +6 -0
- data/lib/spikard/app.rb +405 -0
- data/lib/spikard/background.rb +27 -0
- data/lib/spikard/config.rb +396 -0
- data/lib/spikard/converters.rb +13 -0
- data/lib/spikard/handler_wrapper.rb +113 -0
- data/lib/spikard/provide.rb +214 -0
- data/lib/spikard/response.rb +173 -0
- data/lib/spikard/schema.rb +243 -0
- data/lib/spikard/sse.rb +111 -0
- data/lib/spikard/streaming_response.rb +44 -0
- data/lib/spikard/testing.rb +221 -0
- data/lib/spikard/upload_file.rb +131 -0
- data/lib/spikard/version.rb +5 -0
- data/lib/spikard/websocket.rb +59 -0
- data/lib/spikard.rb +43 -0
- data/sig/spikard.rbs +366 -0
- data/vendor/bundle/ruby/3.4.0/gems/diff-lcs-1.6.2/mise.toml +5 -0
- data/vendor/bundle/ruby/3.4.0/gems/rake-compiler-dock-1.10.0/build/buildkitd.toml +2 -0
- data/vendor/crates/spikard-bindings-shared/Cargo.toml +63 -0
- data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +139 -0
- data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +561 -0
- data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +194 -0
- data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +246 -0
- data/vendor/crates/spikard-bindings-shared/src/error_response.rs +403 -0
- data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +274 -0
- data/vendor/crates/spikard-bindings-shared/src/lib.rs +25 -0
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +298 -0
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +637 -0
- data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +309 -0
- data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +248 -0
- data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +355 -0
- data/vendor/crates/spikard-bindings-shared/tests/comprehensive_coverage.rs +502 -0
- data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +389 -0
- data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +413 -0
- data/vendor/crates/spikard-core/Cargo.toml +40 -0
- data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -0
- data/vendor/crates/spikard-core/src/bindings/response.rs +133 -0
- data/vendor/crates/spikard-core/src/debug.rs +63 -0
- data/vendor/crates/spikard-core/src/di/container.rs +726 -0
- data/vendor/crates/spikard-core/src/di/dependency.rs +273 -0
- data/vendor/crates/spikard-core/src/di/error.rs +118 -0
- data/vendor/crates/spikard-core/src/di/factory.rs +538 -0
- data/vendor/crates/spikard-core/src/di/graph.rs +545 -0
- data/vendor/crates/spikard-core/src/di/mod.rs +192 -0
- data/vendor/crates/spikard-core/src/di/resolved.rs +411 -0
- data/vendor/crates/spikard-core/src/di/value.rs +283 -0
- data/vendor/crates/spikard-core/src/errors.rs +39 -0
- data/vendor/crates/spikard-core/src/http.rs +153 -0
- data/vendor/crates/spikard-core/src/lib.rs +29 -0
- data/vendor/crates/spikard-core/src/lifecycle.rs +422 -0
- data/vendor/crates/spikard-core/src/metadata.rs +397 -0
- data/vendor/crates/spikard-core/src/parameters.rs +723 -0
- data/vendor/crates/spikard-core/src/problem.rs +310 -0
- data/vendor/crates/spikard-core/src/request_data.rs +189 -0
- data/vendor/crates/spikard-core/src/router.rs +249 -0
- data/vendor/crates/spikard-core/src/schema_registry.rs +183 -0
- data/vendor/crates/spikard-core/src/type_hints.rs +304 -0
- data/vendor/crates/spikard-core/src/validation/error_mapper.rs +689 -0
- data/vendor/crates/spikard-core/src/validation/mod.rs +459 -0
- data/vendor/crates/spikard-http/Cargo.toml +58 -0
- data/vendor/crates/spikard-http/examples/sse-notifications.rs +147 -0
- data/vendor/crates/spikard-http/examples/websocket-chat.rs +91 -0
- data/vendor/crates/spikard-http/src/auth.rs +247 -0
- data/vendor/crates/spikard-http/src/background.rs +1562 -0
- data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -0
- data/vendor/crates/spikard-http/src/bindings/response.rs +1 -0
- data/vendor/crates/spikard-http/src/body_metadata.rs +8 -0
- data/vendor/crates/spikard-http/src/cors.rs +490 -0
- data/vendor/crates/spikard-http/src/debug.rs +63 -0
- data/vendor/crates/spikard-http/src/di_handler.rs +1878 -0
- data/vendor/crates/spikard-http/src/handler_response.rs +532 -0
- data/vendor/crates/spikard-http/src/handler_trait.rs +861 -0
- data/vendor/crates/spikard-http/src/handler_trait_tests.rs +284 -0
- data/vendor/crates/spikard-http/src/lib.rs +524 -0
- data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +149 -0
- data/vendor/crates/spikard-http/src/lifecycle.rs +428 -0
- data/vendor/crates/spikard-http/src/middleware/mod.rs +285 -0
- data/vendor/crates/spikard-http/src/middleware/multipart.rs +930 -0
- data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +541 -0
- data/vendor/crates/spikard-http/src/middleware/validation.rs +287 -0
- data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -0
- data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +535 -0
- data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +867 -0
- data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +678 -0
- data/vendor/crates/spikard-http/src/query_parser.rs +369 -0
- data/vendor/crates/spikard-http/src/response.rs +399 -0
- data/vendor/crates/spikard-http/src/server/handler.rs +1557 -0
- data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +98 -0
- data/vendor/crates/spikard-http/src/server/mod.rs +806 -0
- data/vendor/crates/spikard-http/src/server/request_extraction.rs +630 -0
- data/vendor/crates/spikard-http/src/server/routing_factory.rs +497 -0
- data/vendor/crates/spikard-http/src/sse.rs +961 -0
- data/vendor/crates/spikard-http/src/testing/form.rs +14 -0
- data/vendor/crates/spikard-http/src/testing/multipart.rs +60 -0
- data/vendor/crates/spikard-http/src/testing/test_client.rs +285 -0
- data/vendor/crates/spikard-http/src/testing.rs +377 -0
- data/vendor/crates/spikard-http/src/websocket.rs +831 -0
- data/vendor/crates/spikard-http/tests/background_behavior.rs +918 -0
- data/vendor/crates/spikard-http/tests/common/handlers.rs +308 -0
- data/vendor/crates/spikard-http/tests/common/mod.rs +21 -0
- data/vendor/crates/spikard-http/tests/di_integration.rs +202 -0
- data/vendor/crates/spikard-http/tests/doc_snippets.rs +4 -0
- data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +1135 -0
- data/vendor/crates/spikard-http/tests/multipart_behavior.rs +688 -0
- data/vendor/crates/spikard-http/tests/server_config_builder.rs +324 -0
- data/vendor/crates/spikard-http/tests/sse_behavior.rs +728 -0
- data/vendor/crates/spikard-http/tests/websocket_behavior.rs +724 -0
- data/vendor/crates/spikard-rb/Cargo.toml +43 -0
- data/vendor/crates/spikard-rb/build.rs +199 -0
- data/vendor/crates/spikard-rb/src/background.rs +63 -0
- data/vendor/crates/spikard-rb/src/config/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/config/server_config.rs +283 -0
- data/vendor/crates/spikard-rb/src/conversion.rs +459 -0
- data/vendor/crates/spikard-rb/src/di/builder.rs +105 -0
- data/vendor/crates/spikard-rb/src/di/mod.rs +413 -0
- data/vendor/crates/spikard-rb/src/handler.rs +612 -0
- data/vendor/crates/spikard-rb/src/integration/mod.rs +3 -0
- data/vendor/crates/spikard-rb/src/lib.rs +1857 -0
- data/vendor/crates/spikard-rb/src/lifecycle.rs +275 -0
- data/vendor/crates/spikard-rb/src/metadata/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +427 -0
- data/vendor/crates/spikard-rb/src/runtime/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +326 -0
- data/vendor/crates/spikard-rb/src/server.rs +283 -0
- data/vendor/crates/spikard-rb/src/sse.rs +231 -0
- data/vendor/crates/spikard-rb/src/testing/client.rs +404 -0
- data/vendor/crates/spikard-rb/src/testing/mod.rs +7 -0
- data/vendor/crates/spikard-rb/src/testing/sse.rs +143 -0
- data/vendor/crates/spikard-rb/src/testing/websocket.rs +221 -0
- data/vendor/crates/spikard-rb/src/websocket.rs +233 -0
- data/vendor/crates/spikard-rb/tests/magnus_ffi_tests.rs +14 -0
- metadata +213 -0
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
use axum::{
|
|
2
|
+
BoxError,
|
|
3
|
+
body::Body,
|
|
4
|
+
http::{HeaderMap, HeaderName, HeaderValue, StatusCode},
|
|
5
|
+
response::Response as AxumResponse,
|
|
6
|
+
};
|
|
7
|
+
use bytes::Bytes;
|
|
8
|
+
use futures::{Stream, StreamExt};
|
|
9
|
+
use std::pin::Pin;
|
|
10
|
+
|
|
11
|
+
/// Unified response type that can represent either a ready response or a streaming body.
|
|
12
|
+
///
|
|
13
|
+
/// This enum allows handlers to return either:
|
|
14
|
+
/// - A complete response that's ready to send (`Response` variant)
|
|
15
|
+
/// - A streaming response with potentially unbounded data (`Stream` variant)
|
|
16
|
+
///
|
|
17
|
+
/// # Variants
|
|
18
|
+
///
|
|
19
|
+
/// * `Response` - A complete Axum response ready to send to the client. Use this for
|
|
20
|
+
/// responses where you have all the data ready (files, JSON bodies, HTML, etc.)
|
|
21
|
+
///
|
|
22
|
+
/// * `Stream` - A streaming response that produces data chunks over time. Use this for:
|
|
23
|
+
/// - Large files (avoid loading entire file in memory)
|
|
24
|
+
/// - Server-Sent Events (SSE)
|
|
25
|
+
/// - Long-polling responses
|
|
26
|
+
/// - Real-time data feeds
|
|
27
|
+
/// - Any unbounded or very large responses
|
|
28
|
+
///
|
|
29
|
+
/// # Examples
|
|
30
|
+
///
|
|
31
|
+
/// ```ignore
|
|
32
|
+
/// // Regular response
|
|
33
|
+
/// let response = AxumResponse::builder()
|
|
34
|
+
/// .status(StatusCode::OK)
|
|
35
|
+
/// .body(Body::from("Hello"))
|
|
36
|
+
/// .unwrap();
|
|
37
|
+
/// let handler_response = HandlerResponse::from(response);
|
|
38
|
+
///
|
|
39
|
+
/// // Streaming response
|
|
40
|
+
/// let stream = futures::stream::iter(vec![
|
|
41
|
+
/// Ok::<_, Box<dyn std::error::Error>>(Bytes::from("chunk1")),
|
|
42
|
+
/// Ok(Bytes::from("chunk2")),
|
|
43
|
+
/// ]);
|
|
44
|
+
/// let response = HandlerResponse::stream(stream)
|
|
45
|
+
/// .with_status(StatusCode::OK);
|
|
46
|
+
/// ```
|
|
47
|
+
pub enum HandlerResponse {
|
|
48
|
+
/// A complete response ready to send
|
|
49
|
+
Response(AxumResponse<Body>),
|
|
50
|
+
/// A streaming response with custom status and headers
|
|
51
|
+
Stream {
|
|
52
|
+
/// The byte stream that will be sent to the client
|
|
53
|
+
stream: Pin<Box<dyn Stream<Item = Result<Bytes, BoxError>> + Send + 'static>>,
|
|
54
|
+
/// HTTP status code for the response
|
|
55
|
+
status: StatusCode,
|
|
56
|
+
/// Response headers to send
|
|
57
|
+
headers: HeaderMap,
|
|
58
|
+
},
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
impl From<AxumResponse<Body>> for HandlerResponse {
|
|
62
|
+
fn from(response: AxumResponse<Body>) -> Self {
|
|
63
|
+
HandlerResponse::Response(response)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
impl HandlerResponse {
|
|
68
|
+
/// Convert the handler response into an Axum response.
|
|
69
|
+
///
|
|
70
|
+
/// Consumes the `HandlerResponse` and produces an `AxumResponse<Body>` ready
|
|
71
|
+
/// to be sent to the client. For streaming responses, wraps the stream in an
|
|
72
|
+
/// Axum Body.
|
|
73
|
+
///
|
|
74
|
+
/// # Returns
|
|
75
|
+
/// An `AxumResponse<Body>` ready to be returned from an Axum handler
|
|
76
|
+
pub fn into_response(self) -> AxumResponse<Body> {
|
|
77
|
+
match self {
|
|
78
|
+
HandlerResponse::Response(response) => response,
|
|
79
|
+
HandlerResponse::Stream {
|
|
80
|
+
stream,
|
|
81
|
+
status,
|
|
82
|
+
mut headers,
|
|
83
|
+
} => {
|
|
84
|
+
let body = Body::from_stream(stream);
|
|
85
|
+
let mut response = AxumResponse::new(body);
|
|
86
|
+
*response.status_mut() = status;
|
|
87
|
+
response.headers_mut().extend(headers.drain());
|
|
88
|
+
response
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/// Create a streaming response from any async stream of byte chunks.
|
|
94
|
+
///
|
|
95
|
+
/// Wraps an async stream of byte chunks into a `HandlerResponse::Stream`.
|
|
96
|
+
/// This is useful for large files, real-time data, or any unbounded response.
|
|
97
|
+
///
|
|
98
|
+
/// # Type Parameters
|
|
99
|
+
/// * `S` - The stream type implementing `Stream<Item = Result<Bytes, E>>`
|
|
100
|
+
/// * `E` - The error type that can be converted to `BoxError`
|
|
101
|
+
///
|
|
102
|
+
/// # Arguments
|
|
103
|
+
/// * `stream` - An async stream that yields byte chunks or errors
|
|
104
|
+
///
|
|
105
|
+
/// # Returns
|
|
106
|
+
/// A `HandlerResponse` with 200 OK status and empty headers (customize with
|
|
107
|
+
/// `with_status()` and `with_header()`)
|
|
108
|
+
///
|
|
109
|
+
/// # Example
|
|
110
|
+
///
|
|
111
|
+
/// ```ignore
|
|
112
|
+
/// use futures::stream;
|
|
113
|
+
/// use spikard_http::HandlerResponse;
|
|
114
|
+
/// use bytes::Bytes;
|
|
115
|
+
///
|
|
116
|
+
/// let stream = stream::iter(vec![
|
|
117
|
+
/// Ok::<_, Box<dyn std::error::Error>>(Bytes::from("Hello ")),
|
|
118
|
+
/// Ok(Bytes::from("World")),
|
|
119
|
+
/// ]);
|
|
120
|
+
/// let response = HandlerResponse::stream(stream)
|
|
121
|
+
/// .with_status(StatusCode::OK);
|
|
122
|
+
/// ```
|
|
123
|
+
pub fn stream<S, E>(stream: S) -> Self
|
|
124
|
+
where
|
|
125
|
+
S: Stream<Item = Result<Bytes, E>> + Send + 'static,
|
|
126
|
+
E: Into<BoxError>,
|
|
127
|
+
{
|
|
128
|
+
let mapped = stream.map(|chunk| chunk.map_err(Into::into));
|
|
129
|
+
HandlerResponse::Stream {
|
|
130
|
+
stream: Box::pin(mapped),
|
|
131
|
+
status: StatusCode::OK,
|
|
132
|
+
headers: HeaderMap::new(),
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/// Override the HTTP status code for the streaming response.
|
|
137
|
+
///
|
|
138
|
+
/// Sets the HTTP status code to be used in the response. This only affects
|
|
139
|
+
/// `Stream` variants; regular responses already have their status set.
|
|
140
|
+
///
|
|
141
|
+
/// # Arguments
|
|
142
|
+
/// * `status` - The HTTP status code to use (e.g., `StatusCode::OK`)
|
|
143
|
+
///
|
|
144
|
+
/// # Returns
|
|
145
|
+
/// Self for method chaining
|
|
146
|
+
///
|
|
147
|
+
/// # Example
|
|
148
|
+
///
|
|
149
|
+
/// ```ignore
|
|
150
|
+
/// let response = HandlerResponse::stream(my_stream)
|
|
151
|
+
/// .with_status(StatusCode::PARTIAL_CONTENT);
|
|
152
|
+
/// ```
|
|
153
|
+
pub fn with_status(mut self, status: StatusCode) -> Self {
|
|
154
|
+
if let HandlerResponse::Stream { status: s, .. } = &mut self {
|
|
155
|
+
*s = status;
|
|
156
|
+
}
|
|
157
|
+
self
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/// Insert or replace a header on the streaming response.
|
|
161
|
+
///
|
|
162
|
+
/// Adds an HTTP header to the response. This only affects `Stream` variants;
|
|
163
|
+
/// regular responses already have their headers set. If a header with the same
|
|
164
|
+
/// name already exists, it will be replaced.
|
|
165
|
+
///
|
|
166
|
+
/// # Arguments
|
|
167
|
+
/// * `name` - The header name (e.g., `HeaderName::from_static("content-type")`)
|
|
168
|
+
/// * `value` - The header value
|
|
169
|
+
///
|
|
170
|
+
/// # Returns
|
|
171
|
+
/// Self for method chaining
|
|
172
|
+
///
|
|
173
|
+
/// # Example
|
|
174
|
+
///
|
|
175
|
+
/// ```ignore
|
|
176
|
+
/// use axum::http::{HeaderName, HeaderValue};
|
|
177
|
+
///
|
|
178
|
+
/// let response = HandlerResponse::stream(my_stream)
|
|
179
|
+
/// .with_header(
|
|
180
|
+
/// HeaderName::from_static("content-type"),
|
|
181
|
+
/// HeaderValue::from_static("application/octet-stream")
|
|
182
|
+
/// );
|
|
183
|
+
/// ```
|
|
184
|
+
pub fn with_header(mut self, name: HeaderName, value: HeaderValue) -> Self {
|
|
185
|
+
if let HandlerResponse::Stream { headers, .. } = &mut self {
|
|
186
|
+
headers.insert(name, value);
|
|
187
|
+
}
|
|
188
|
+
self
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
#[cfg(test)]
|
|
193
|
+
mod tests {
|
|
194
|
+
use super::*;
|
|
195
|
+
use axum::http::header;
|
|
196
|
+
use http_body_util::BodyExt;
|
|
197
|
+
|
|
198
|
+
/// Test 1: Convert AxumResponse → HandlerResponse::Response
|
|
199
|
+
#[test]
|
|
200
|
+
fn test_from_axum_response() {
|
|
201
|
+
let axum_response = AxumResponse::new(Body::from("test body"));
|
|
202
|
+
let handler_response = HandlerResponse::from(axum_response);
|
|
203
|
+
|
|
204
|
+
match handler_response {
|
|
205
|
+
HandlerResponse::Response(_) => {}
|
|
206
|
+
HandlerResponse::Stream { .. } => panic!("Expected Response variant"),
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/// Test 2: Create stream with chunks, verify wrapping
|
|
211
|
+
#[tokio::test]
|
|
212
|
+
async fn test_stream_creation_with_chunks() {
|
|
213
|
+
let chunks = vec![
|
|
214
|
+
Ok::<_, Box<dyn std::error::Error + Send + Sync>>(Bytes::from("chunk1")),
|
|
215
|
+
Ok(Bytes::from("chunk2")),
|
|
216
|
+
Ok(Bytes::from("chunk3")),
|
|
217
|
+
];
|
|
218
|
+
let stream = futures::stream::iter(chunks);
|
|
219
|
+
let handler_response = HandlerResponse::stream(stream);
|
|
220
|
+
|
|
221
|
+
match handler_response {
|
|
222
|
+
HandlerResponse::Stream { status, headers, .. } => {
|
|
223
|
+
assert_eq!(status, StatusCode::OK);
|
|
224
|
+
assert!(headers.is_empty());
|
|
225
|
+
}
|
|
226
|
+
HandlerResponse::Response(_) => panic!("Expected Stream variant"),
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/// Test 3: Stream with custom status code (206 Partial Content)
|
|
231
|
+
#[tokio::test]
|
|
232
|
+
async fn test_stream_with_custom_status() {
|
|
233
|
+
let chunks = vec![Ok::<_, Box<dyn std::error::Error + Send + Sync>>(Bytes::from(
|
|
234
|
+
"partial",
|
|
235
|
+
))];
|
|
236
|
+
let stream = futures::stream::iter(chunks);
|
|
237
|
+
let handler_response = HandlerResponse::stream(stream).with_status(StatusCode::PARTIAL_CONTENT);
|
|
238
|
+
|
|
239
|
+
match handler_response {
|
|
240
|
+
HandlerResponse::Stream { status, .. } => {
|
|
241
|
+
assert_eq!(status, StatusCode::PARTIAL_CONTENT);
|
|
242
|
+
}
|
|
243
|
+
HandlerResponse::Response(_) => panic!("Expected Stream variant"),
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/// Test 4: Stream with headers via with_header()
|
|
248
|
+
#[tokio::test]
|
|
249
|
+
async fn test_stream_with_headers() {
|
|
250
|
+
let chunks = vec![Ok::<_, Box<dyn std::error::Error + Send + Sync>>(Bytes::from("test"))];
|
|
251
|
+
let stream = futures::stream::iter(chunks);
|
|
252
|
+
let handler_response = HandlerResponse::stream(stream)
|
|
253
|
+
.with_header(header::CONTENT_TYPE, HeaderValue::from_static("application/x-ndjson"))
|
|
254
|
+
.with_header(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
|
|
255
|
+
|
|
256
|
+
match handler_response {
|
|
257
|
+
HandlerResponse::Stream { headers, .. } => {
|
|
258
|
+
assert_eq!(headers.get(header::CONTENT_TYPE).unwrap(), "application/x-ndjson");
|
|
259
|
+
assert_eq!(headers.get(header::CACHE_CONTROL).unwrap(), "no-cache");
|
|
260
|
+
}
|
|
261
|
+
HandlerResponse::Response(_) => panic!("Expected Stream variant"),
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/// Test 5: Stream body consumption - read all chunks from stream
|
|
266
|
+
#[tokio::test]
|
|
267
|
+
async fn test_stream_body_consumption() {
|
|
268
|
+
let chunks = vec![
|
|
269
|
+
Ok::<_, Box<dyn std::error::Error + Send + Sync>>(Bytes::from("hello ")),
|
|
270
|
+
Ok(Bytes::from("world")),
|
|
271
|
+
Ok(Bytes::from("!")),
|
|
272
|
+
];
|
|
273
|
+
let stream = futures::stream::iter(chunks);
|
|
274
|
+
let handler_response = HandlerResponse::stream(stream).with_status(StatusCode::OK);
|
|
275
|
+
|
|
276
|
+
let axum_response = handler_response.into_response();
|
|
277
|
+
let body = axum_response.into_body().collect().await.unwrap();
|
|
278
|
+
let bytes = body.to_bytes();
|
|
279
|
+
|
|
280
|
+
assert_eq!(bytes, "hello world!");
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/// Test 6: Into response for Response variant - passthrough conversion
|
|
284
|
+
#[tokio::test]
|
|
285
|
+
async fn test_into_response_for_response_variant() {
|
|
286
|
+
let original_body = "test response body";
|
|
287
|
+
let axum_response = AxumResponse::new(Body::from(original_body));
|
|
288
|
+
let handler_response = HandlerResponse::from(axum_response);
|
|
289
|
+
|
|
290
|
+
let result = handler_response.into_response();
|
|
291
|
+
let body = result.into_body().collect().await.unwrap();
|
|
292
|
+
let bytes = body.to_bytes();
|
|
293
|
+
|
|
294
|
+
assert_eq!(bytes, original_body);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/// Test 7: Method chaining - with_status() → with_header() → with_header()
|
|
298
|
+
#[tokio::test]
|
|
299
|
+
async fn test_method_chaining() {
|
|
300
|
+
let chunks = vec![Ok::<_, Box<dyn std::error::Error + Send + Sync>>(Bytes::from(
|
|
301
|
+
"chained",
|
|
302
|
+
))];
|
|
303
|
+
let stream = futures::stream::iter(chunks);
|
|
304
|
+
|
|
305
|
+
let handler_response = HandlerResponse::stream(stream)
|
|
306
|
+
.with_status(StatusCode::CREATED)
|
|
307
|
+
.with_header(header::CONTENT_TYPE, HeaderValue::from_static("text/plain"))
|
|
308
|
+
.with_header(header::ETAG, HeaderValue::from_static("\"abc123\""));
|
|
309
|
+
|
|
310
|
+
match handler_response {
|
|
311
|
+
HandlerResponse::Stream { status, headers, .. } => {
|
|
312
|
+
assert_eq!(status, StatusCode::CREATED);
|
|
313
|
+
assert_eq!(headers.get(header::CONTENT_TYPE).unwrap(), "text/plain");
|
|
314
|
+
assert_eq!(headers.get(header::ETAG).unwrap(), "\"abc123\"");
|
|
315
|
+
}
|
|
316
|
+
HandlerResponse::Response(_) => panic!("Expected Stream variant"),
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/// Test 8: Empty stream - zero-byte stream handling
|
|
321
|
+
#[tokio::test]
|
|
322
|
+
async fn test_empty_stream() {
|
|
323
|
+
let chunks: Vec<Result<Bytes, Box<dyn std::error::Error + Send + Sync>>> = vec![];
|
|
324
|
+
let stream = futures::stream::iter(chunks);
|
|
325
|
+
let handler_response = HandlerResponse::stream(stream).with_status(StatusCode::NO_CONTENT);
|
|
326
|
+
|
|
327
|
+
let axum_response = handler_response.into_response();
|
|
328
|
+
let status = axum_response.status();
|
|
329
|
+
let body = axum_response.into_body().collect().await.unwrap();
|
|
330
|
+
let bytes = body.to_bytes();
|
|
331
|
+
|
|
332
|
+
assert!(bytes.is_empty());
|
|
333
|
+
assert_eq!(status, StatusCode::NO_CONTENT);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/// Test 9: Large stream - many chunks (100+ items)
|
|
337
|
+
#[tokio::test]
|
|
338
|
+
async fn test_large_stream() {
|
|
339
|
+
let chunks: Vec<Result<Bytes, Box<dyn std::error::Error + Send + Sync>>> =
|
|
340
|
+
(0..150).map(|i| Ok(Bytes::from(format!("chunk{} ", i)))).collect();
|
|
341
|
+
|
|
342
|
+
let stream = futures::stream::iter(chunks);
|
|
343
|
+
let handler_response = HandlerResponse::stream(stream);
|
|
344
|
+
|
|
345
|
+
let axum_response = handler_response.into_response();
|
|
346
|
+
let body = axum_response.into_body().collect().await.unwrap();
|
|
347
|
+
let bytes = body.to_bytes();
|
|
348
|
+
|
|
349
|
+
// Verify we got all chunks
|
|
350
|
+
assert!(bytes.len() > 1000);
|
|
351
|
+
for i in 0..150 {
|
|
352
|
+
let expected = format!("chunk{} ", i);
|
|
353
|
+
assert!(std::str::from_utf8(&bytes).unwrap().contains(&expected));
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/// Test 10: Error in stream - stream item returns Err, verify error propagation
|
|
358
|
+
#[tokio::test]
|
|
359
|
+
async fn test_stream_error_propagation() {
|
|
360
|
+
let chunks: Vec<Result<Bytes, Box<dyn std::error::Error + Send + Sync>>> = vec![
|
|
361
|
+
Ok(Bytes::from("good1 ")),
|
|
362
|
+
Err("custom error".into()),
|
|
363
|
+
Ok(Bytes::from("good2")),
|
|
364
|
+
];
|
|
365
|
+
|
|
366
|
+
let stream = futures::stream::iter(chunks);
|
|
367
|
+
let handler_response = HandlerResponse::stream(stream);
|
|
368
|
+
|
|
369
|
+
let axum_response = handler_response.into_response();
|
|
370
|
+
let result = axum_response.into_body().collect().await;
|
|
371
|
+
|
|
372
|
+
// Error should propagate when collecting
|
|
373
|
+
assert!(result.is_err());
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/// Test 11: Response variant ignores with_status()
|
|
377
|
+
#[test]
|
|
378
|
+
fn test_response_variant_ignores_with_status() {
|
|
379
|
+
let axum_response = AxumResponse::builder()
|
|
380
|
+
.status(StatusCode::OK)
|
|
381
|
+
.body(Body::from("test"))
|
|
382
|
+
.unwrap();
|
|
383
|
+
let handler_response = HandlerResponse::from(axum_response);
|
|
384
|
+
|
|
385
|
+
// Calling with_status on Response variant should be no-op
|
|
386
|
+
let result = handler_response.with_status(StatusCode::NOT_FOUND);
|
|
387
|
+
|
|
388
|
+
match result {
|
|
389
|
+
HandlerResponse::Response(resp) => {
|
|
390
|
+
assert_eq!(resp.status(), StatusCode::OK);
|
|
391
|
+
}
|
|
392
|
+
HandlerResponse::Stream { .. } => panic!("Expected Response variant"),
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/// Test 12: Response variant ignores with_header()
|
|
397
|
+
#[test]
|
|
398
|
+
fn test_response_variant_ignores_with_header() {
|
|
399
|
+
let axum_response = AxumResponse::builder()
|
|
400
|
+
.status(StatusCode::OK)
|
|
401
|
+
.header(header::CONTENT_TYPE, "text/plain")
|
|
402
|
+
.body(Body::from("test"))
|
|
403
|
+
.unwrap();
|
|
404
|
+
let handler_response = HandlerResponse::from(axum_response);
|
|
405
|
+
|
|
406
|
+
// Calling with_header on Response variant should be no-op
|
|
407
|
+
let result = handler_response.with_header(header::CACHE_CONTROL, HeaderValue::from_static("max-age=3600"));
|
|
408
|
+
|
|
409
|
+
match result {
|
|
410
|
+
HandlerResponse::Response(resp) => {
|
|
411
|
+
assert!(resp.headers().get(header::CACHE_CONTROL).is_none());
|
|
412
|
+
}
|
|
413
|
+
HandlerResponse::Stream { .. } => panic!("Expected Response variant"),
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/// Test 13: Stream into_response applies status and headers
|
|
418
|
+
#[tokio::test]
|
|
419
|
+
async fn test_stream_into_response_applies_status_and_headers() {
|
|
420
|
+
let chunks = vec![Ok::<_, Box<dyn std::error::Error + Send + Sync>>(Bytes::from(
|
|
421
|
+
"stream data",
|
|
422
|
+
))];
|
|
423
|
+
let stream = futures::stream::iter(chunks);
|
|
424
|
+
|
|
425
|
+
let handler_response = HandlerResponse::stream(stream)
|
|
426
|
+
.with_status(StatusCode::PARTIAL_CONTENT)
|
|
427
|
+
.with_header(header::CONTENT_RANGE, HeaderValue::from_static("bytes 0-10/100"));
|
|
428
|
+
|
|
429
|
+
let axum_response = handler_response.into_response();
|
|
430
|
+
|
|
431
|
+
assert_eq!(axum_response.status(), StatusCode::PARTIAL_CONTENT);
|
|
432
|
+
assert_eq!(
|
|
433
|
+
axum_response.headers().get(header::CONTENT_RANGE).unwrap(),
|
|
434
|
+
"bytes 0-10/100"
|
|
435
|
+
);
|
|
436
|
+
|
|
437
|
+
let body = axum_response.into_body().collect().await.unwrap();
|
|
438
|
+
assert_eq!(body.to_bytes(), "stream data");
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/// Test 14: Multiple header replacements via with_header()
|
|
442
|
+
#[tokio::test]
|
|
443
|
+
async fn test_multiple_header_replacements() {
|
|
444
|
+
let chunks = vec![Ok::<_, Box<dyn std::error::Error + Send + Sync>>(Bytes::from("data"))];
|
|
445
|
+
let stream = futures::stream::iter(chunks);
|
|
446
|
+
|
|
447
|
+
// Replace the same header twice - second should win
|
|
448
|
+
let handler_response = HandlerResponse::stream(stream)
|
|
449
|
+
.with_header(header::CONTENT_TYPE, HeaderValue::from_static("application/json"))
|
|
450
|
+
.with_header(header::CONTENT_TYPE, HeaderValue::from_static("application/x-ndjson"));
|
|
451
|
+
|
|
452
|
+
match handler_response {
|
|
453
|
+
HandlerResponse::Stream { headers, .. } => {
|
|
454
|
+
assert_eq!(headers.get(header::CONTENT_TYPE).unwrap(), "application/x-ndjson");
|
|
455
|
+
}
|
|
456
|
+
HandlerResponse::Response(_) => panic!("Expected Stream variant"),
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/// Test 15: Stream with various status codes
|
|
461
|
+
#[tokio::test]
|
|
462
|
+
async fn test_stream_with_various_status_codes() {
|
|
463
|
+
let status_codes = vec![
|
|
464
|
+
StatusCode::OK,
|
|
465
|
+
StatusCode::CREATED,
|
|
466
|
+
StatusCode::ACCEPTED,
|
|
467
|
+
StatusCode::PARTIAL_CONTENT,
|
|
468
|
+
StatusCode::MULTI_STATUS,
|
|
469
|
+
];
|
|
470
|
+
|
|
471
|
+
for status in status_codes {
|
|
472
|
+
let chunks = vec![Ok::<_, Box<dyn std::error::Error + Send + Sync>>(Bytes::from("test"))];
|
|
473
|
+
let stream = futures::stream::iter(chunks);
|
|
474
|
+
let handler_response = HandlerResponse::stream(stream).with_status(status);
|
|
475
|
+
|
|
476
|
+
match handler_response {
|
|
477
|
+
HandlerResponse::Stream { status: s, .. } => {
|
|
478
|
+
assert_eq!(s, status);
|
|
479
|
+
}
|
|
480
|
+
HandlerResponse::Response(_) => panic!("Expected Stream variant"),
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/// Test 16: Stream with JSON lines content (fixture-based)
|
|
486
|
+
#[tokio::test]
|
|
487
|
+
async fn test_stream_with_json_lines_content() {
|
|
488
|
+
let chunks = vec![
|
|
489
|
+
Ok::<_, Box<dyn std::error::Error + Send + Sync>>(Bytes::from(r#"{"index":0,"payload":"alpha"}"#)),
|
|
490
|
+
Ok(Bytes::from("\n")),
|
|
491
|
+
Ok(Bytes::from(r#"{"index":1,"payload":"beta"}"#)),
|
|
492
|
+
Ok(Bytes::from("\n")),
|
|
493
|
+
Ok(Bytes::from(r#"{"index":2,"payload":"gamma"}"#)),
|
|
494
|
+
Ok(Bytes::from("\n")),
|
|
495
|
+
];
|
|
496
|
+
|
|
497
|
+
let stream = futures::stream::iter(chunks);
|
|
498
|
+
let handler_response = HandlerResponse::stream(stream)
|
|
499
|
+
.with_status(StatusCode::OK)
|
|
500
|
+
.with_header(header::CONTENT_TYPE, HeaderValue::from_static("application/x-ndjson"));
|
|
501
|
+
|
|
502
|
+
let axum_response = handler_response.into_response();
|
|
503
|
+
let status = axum_response.status();
|
|
504
|
+
let body = axum_response.into_body().collect().await.unwrap();
|
|
505
|
+
let bytes = body.to_bytes();
|
|
506
|
+
let body_str = std::str::from_utf8(&bytes).unwrap();
|
|
507
|
+
|
|
508
|
+
assert_eq!(status, StatusCode::OK);
|
|
509
|
+
assert!(body_str.contains("alpha"));
|
|
510
|
+
assert!(body_str.contains("beta"));
|
|
511
|
+
assert!(body_str.contains("gamma"));
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/// Test 17: Round-trip Response → HandlerResponse → Response
|
|
515
|
+
#[tokio::test]
|
|
516
|
+
async fn test_response_roundtrip() {
|
|
517
|
+
let original = AxumResponse::builder()
|
|
518
|
+
.status(StatusCode::OK)
|
|
519
|
+
.header(header::CONTENT_TYPE, "text/plain")
|
|
520
|
+
.body(Body::from("roundtrip test"))
|
|
521
|
+
.unwrap();
|
|
522
|
+
|
|
523
|
+
let handler_response = HandlerResponse::from(original);
|
|
524
|
+
let result = handler_response.into_response();
|
|
525
|
+
|
|
526
|
+
assert_eq!(result.status(), StatusCode::OK);
|
|
527
|
+
assert_eq!(result.headers().get(header::CONTENT_TYPE).unwrap(), "text/plain");
|
|
528
|
+
|
|
529
|
+
let body = result.into_body().collect().await.unwrap();
|
|
530
|
+
assert_eq!(body.to_bytes(), "roundtrip test");
|
|
531
|
+
}
|
|
532
|
+
}
|