rustapi-core 0.1.450

The core engine of the RustAPI framework. Provides the hyper-based HTTP server, router, extraction logic, and foundational traits.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Request/Response Interceptor System for RustAPI
//!
//! This module provides interceptors that can modify requests before handlers
//! and responses after handlers, without the complexity of Tower layers.
//!
//! # Overview
//!
//! Interceptors provide a simpler alternative to middleware for common use cases:
//! - Adding headers to all requests/responses
//! - Logging and metrics
//! - Request/response transformation
//!
//! # Execution Order
//!
//! Request interceptors execute in registration order (1 → 2 → 3 → Handler).
//! Response interceptors execute in reverse order (Handler → 3 → 2 → 1).
//!
//! # Example
//!
//! ```rust,ignore
//! use rustapi_core::{RustApi, interceptor::{RequestInterceptor, ResponseInterceptor}};
//!
//! struct AddRequestId;
//!
//! impl RequestInterceptor for AddRequestId {
//!     fn intercept(&self, mut req: Request) -> Request {
//!         req.extensions_mut().insert(uuid::Uuid::new_v4());
//!         req
//!     }
//! }
//!
//! struct AddServerHeader;
//!
//! impl ResponseInterceptor for AddServerHeader {
//!     fn intercept(&self, mut res: Response) -> Response {
//!         res.headers_mut().insert("X-Server", "RustAPI".parse().unwrap());
//!         res
//!     }
//! }
//!
//! RustApi::new()
//!     .request_interceptor(AddRequestId)
//!     .response_interceptor(AddServerHeader)
//!     .route("/", get(handler))
//!     .run("127.0.0.1:8080")
//!     .await
//! ```

use crate::request::Request;
use crate::response::Response;

/// Trait for intercepting and modifying requests before they reach handlers.
///
/// Request interceptors are executed in the order they are registered.
/// Each interceptor receives the request, can modify it, and returns the
/// (potentially modified) request for the next interceptor or handler.
///
/// # Example
///
/// ```rust,ignore
/// use rustapi_core::interceptor::RequestInterceptor;
/// use rustapi_core::Request;
///
/// struct LoggingInterceptor;
///
/// impl RequestInterceptor for LoggingInterceptor {
///     fn intercept(&self, req: Request) -> Request {
///         println!("Request: {} {}", req.method(), req.path());
///         req
///     }
/// }
/// ```
pub trait RequestInterceptor: Send + Sync + 'static {
    /// Intercept and optionally modify the request.
    ///
    /// The returned request will be passed to the next interceptor or handler.
    fn intercept(&self, request: Request) -> Request;

    /// Clone this interceptor into a boxed trait object.
    fn clone_box(&self) -> Box<dyn RequestInterceptor>;
}

impl Clone for Box<dyn RequestInterceptor> {
    fn clone(&self) -> Self {
        self.clone_box()
    }
}

/// Trait for intercepting and modifying responses after handlers complete.
///
/// Response interceptors are executed in reverse registration order.
/// Each interceptor receives the response, can modify it, and returns the
/// (potentially modified) response for the previous interceptor or client.
///
/// # Example
///
/// ```rust,ignore
/// use rustapi_core::interceptor::ResponseInterceptor;
/// use rustapi_core::Response;
///
/// struct AddCorsHeaders;
///
/// impl ResponseInterceptor for AddCorsHeaders {
///     fn intercept(&self, mut res: Response) -> Response {
///         res.headers_mut().insert(
///             "Access-Control-Allow-Origin",
///             "*".parse().unwrap()
///         );
///         res
///     }
/// }
/// ```
pub trait ResponseInterceptor: Send + Sync + 'static {
    /// Intercept and optionally modify the response.
    ///
    /// The returned response will be passed to the previous interceptor or client.
    fn intercept(&self, response: Response) -> Response;

    /// Clone this interceptor into a boxed trait object.
    fn clone_box(&self) -> Box<dyn ResponseInterceptor>;
}

impl Clone for Box<dyn ResponseInterceptor> {
    fn clone(&self) -> Self {
        self.clone_box()
    }
}

/// Chain of request and response interceptors.
///
/// Manages the execution of multiple interceptors in the correct order:
/// - Request interceptors: executed in registration order (first registered = first executed)
/// - Response interceptors: executed in reverse order (last registered = first executed)
#[derive(Clone, Default)]
pub struct InterceptorChain {
    request_interceptors: Vec<Box<dyn RequestInterceptor>>,
    response_interceptors: Vec<Box<dyn ResponseInterceptor>>,
}

impl InterceptorChain {
    /// Create a new empty interceptor chain.
    pub fn new() -> Self {
        Self {
            request_interceptors: Vec::new(),
            response_interceptors: Vec::new(),
        }
    }

    /// Add a request interceptor to the chain.
    ///
    /// Interceptors are executed in the order they are added.
    pub fn add_request_interceptor<I: RequestInterceptor>(&mut self, interceptor: I) {
        self.request_interceptors.push(Box::new(interceptor));
    }

    /// Add a response interceptor to the chain.
    ///
    /// Interceptors are executed in reverse order (last added = first executed after handler).
    pub fn add_response_interceptor<I: ResponseInterceptor>(&mut self, interceptor: I) {
        self.response_interceptors.push(Box::new(interceptor));
    }

    /// Get the number of request interceptors.
    pub fn request_interceptor_count(&self) -> usize {
        self.request_interceptors.len()
    }

    /// Get the number of response interceptors.
    pub fn response_interceptor_count(&self) -> usize {
        self.response_interceptors.len()
    }

    /// Check if the chain has any interceptors.
    pub fn is_empty(&self) -> bool {
        self.request_interceptors.is_empty() && self.response_interceptors.is_empty()
    }

    /// Execute all request interceptors on the given request.
    ///
    /// Interceptors are executed in registration order.
    /// Each interceptor receives the output of the previous one.
    pub fn intercept_request(&self, mut request: Request) -> Request {
        for interceptor in &self.request_interceptors {
            request = interceptor.intercept(request);
        }
        request
    }

    /// Execute all response interceptors on the given response.
    ///
    /// Interceptors are executed in reverse registration order.
    /// Each interceptor receives the output of the previous one.
    pub fn intercept_response(&self, mut response: Response) -> Response {
        // Execute in reverse order (last registered = first to process response)
        for interceptor in self.response_interceptors.iter().rev() {
            response = interceptor.intercept(response);
        }
        response
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::path_params::PathParams;
    use bytes::Bytes;
    use http::{Extensions, Method, StatusCode};

    use proptest::prelude::*;
    use std::sync::Arc;

    /// Create a test request with the given method and path
    fn create_test_request(method: Method, path: &str) -> Request {
        let uri: http::Uri = path.parse().unwrap();
        let builder = http::Request::builder().method(method).uri(uri);

        let req = builder.body(()).unwrap();
        let (parts, _) = req.into_parts();

        Request::new(
            parts,
            crate::request::BodyVariant::Buffered(Bytes::new()),
            Arc::new(Extensions::new()),
            PathParams::new(),
        )
    }

    /// Create a test response with the given status
    fn create_test_response(status: StatusCode) -> Response {
        http::Response::builder()
            .status(status)
            .body(crate::response::Body::from("test"))
            .unwrap()
    }

    /// A request interceptor that adds a header tracking its ID
    #[derive(Clone)]
    struct TrackingRequestInterceptor {
        id: usize,
        order: Arc<std::sync::Mutex<Vec<usize>>>,
    }

    impl TrackingRequestInterceptor {
        fn new(id: usize, order: Arc<std::sync::Mutex<Vec<usize>>>) -> Self {
            Self { id, order }
        }
    }

    impl RequestInterceptor for TrackingRequestInterceptor {
        fn intercept(&self, request: Request) -> Request {
            self.order.lock().unwrap().push(self.id);
            request
        }

        fn clone_box(&self) -> Box<dyn RequestInterceptor> {
            Box::new(self.clone())
        }
    }

    /// A response interceptor that adds a header tracking its ID
    #[derive(Clone)]
    struct TrackingResponseInterceptor {
        id: usize,
        order: Arc<std::sync::Mutex<Vec<usize>>>,
    }

    impl TrackingResponseInterceptor {
        fn new(id: usize, order: Arc<std::sync::Mutex<Vec<usize>>>) -> Self {
            Self { id, order }
        }
    }

    impl ResponseInterceptor for TrackingResponseInterceptor {
        fn intercept(&self, response: Response) -> Response {
            self.order.lock().unwrap().push(self.id);
            response
        }

        fn clone_box(&self) -> Box<dyn ResponseInterceptor> {
            Box::new(self.clone())
        }
    }

    // **Feature: v1-features-roadmap, Property 6: Interceptor execution order**
    //
    // For any set of N registered interceptors, request interceptors SHALL execute
    // in registration order (1→N) and response interceptors SHALL execute in
    // reverse order (N→1).
    //
    // **Validates: Requirements 2.1, 2.2, 2.3**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        #[test]
        fn prop_interceptor_execution_order(num_interceptors in 1usize..10usize) {
            let request_order = Arc::new(std::sync::Mutex::new(Vec::new()));
            let response_order = Arc::new(std::sync::Mutex::new(Vec::new()));

            let mut chain = InterceptorChain::new();

            // Add interceptors in order 0, 1, 2, ..., n-1
            for i in 0..num_interceptors {
                chain.add_request_interceptor(
                    TrackingRequestInterceptor::new(i, request_order.clone())
                );
                chain.add_response_interceptor(
                    TrackingResponseInterceptor::new(i, response_order.clone())
                );
            }

            // Execute request interceptors
            let request = create_test_request(Method::GET, "/test");
            let _ = chain.intercept_request(request);

            // Execute response interceptors
            let response = create_test_response(StatusCode::OK);
            let _ = chain.intercept_response(response);

            // Verify request interceptor order: should be 0, 1, 2, ..., n-1
            let req_order = request_order.lock().unwrap();
            prop_assert_eq!(req_order.len(), num_interceptors);
            for (idx, &id) in req_order.iter().enumerate() {
                prop_assert_eq!(id, idx, "Request interceptor order mismatch at index {}", idx);
            }

            // Verify response interceptor order: should be n-1, n-2, ..., 1, 0 (reverse)
            let res_order = response_order.lock().unwrap();
            prop_assert_eq!(res_order.len(), num_interceptors);
            for (idx, &id) in res_order.iter().enumerate() {
                let expected = num_interceptors - 1 - idx;
                prop_assert_eq!(id, expected, "Response interceptor order mismatch at index {}", idx);
            }
        }
    }

    /// A response interceptor that modifies a header
    #[derive(Clone)]
    struct HeaderModifyingResponseInterceptor {
        header_name: &'static str,
        header_value: String,
    }

    impl HeaderModifyingResponseInterceptor {
        fn new(header_name: &'static str, header_value: impl Into<String>) -> Self {
            Self {
                header_name,
                header_value: header_value.into(),
            }
        }
    }

    impl ResponseInterceptor for HeaderModifyingResponseInterceptor {
        fn intercept(&self, mut response: Response) -> Response {
            if let Ok(value) = self.header_value.parse() {
                response.headers_mut().insert(self.header_name, value);
            }
            response
        }

        fn clone_box(&self) -> Box<dyn ResponseInterceptor> {
            Box::new(self.clone())
        }
    }

    // **Feature: v1-features-roadmap, Property 7: Interceptor modification propagation**
    //
    // For any modification made by an interceptor, subsequent interceptors and handlers
    // SHALL receive the modified request/response.
    //
    // **Validates: Requirements 2.4, 2.5**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        #[test]
        fn prop_interceptor_modification_propagation(
            num_interceptors in 1usize..5usize,
            header_values in prop::collection::vec("[a-zA-Z0-9]{1,10}", 1..5usize),
        ) {
            let mut chain = InterceptorChain::new();

            // Add response interceptors that each add a unique header
            for (i, value) in header_values.iter().enumerate().take(num_interceptors) {
                let header_name = Box::leak(format!("x-test-{}", i).into_boxed_str());
                chain.add_response_interceptor(
                    HeaderModifyingResponseInterceptor::new(header_name, value.clone())
                );
            }

            // Execute response interceptors
            let response = create_test_response(StatusCode::OK);
            let modified_response = chain.intercept_response(response);

            // Verify all headers were added (modifications propagated)
            for (i, value) in header_values.iter().enumerate().take(num_interceptors) {
                let header_name = format!("x-test-{}", i);
                let header_value = modified_response.headers().get(&header_name);
                prop_assert!(header_value.is_some(), "Header {} should be present", header_name);
                prop_assert_eq!(
                    header_value.unwrap().to_str().unwrap(),
                    value,
                    "Header {} should have value {}", header_name, value
                );
            }
        }
    }

    #[test]
    fn test_empty_chain() {
        let chain = InterceptorChain::new();
        assert!(chain.is_empty());
        assert_eq!(chain.request_interceptor_count(), 0);
        assert_eq!(chain.response_interceptor_count(), 0);

        // Should pass through unchanged
        let request = create_test_request(Method::GET, "/test");
        let _ = chain.intercept_request(request);

        let response = create_test_response(StatusCode::OK);
        let result = chain.intercept_response(response);
        assert_eq!(result.status(), StatusCode::OK);
    }

    #[test]
    fn test_single_request_interceptor() {
        let order = Arc::new(std::sync::Mutex::new(Vec::new()));
        let mut chain = InterceptorChain::new();
        chain.add_request_interceptor(TrackingRequestInterceptor::new(42, order.clone()));

        assert!(!chain.is_empty());
        assert_eq!(chain.request_interceptor_count(), 1);

        let request = create_test_request(Method::GET, "/test");
        let _ = chain.intercept_request(request);

        let recorded = order.lock().unwrap();
        assert_eq!(recorded.len(), 1);
        assert_eq!(recorded[0], 42);
    }

    #[test]
    fn test_single_response_interceptor() {
        let order = Arc::new(std::sync::Mutex::new(Vec::new()));
        let mut chain = InterceptorChain::new();
        chain.add_response_interceptor(TrackingResponseInterceptor::new(42, order.clone()));

        assert!(!chain.is_empty());
        assert_eq!(chain.response_interceptor_count(), 1);

        let response = create_test_response(StatusCode::OK);
        let _ = chain.intercept_response(response);

        let recorded = order.lock().unwrap();
        assert_eq!(recorded.len(), 1);
        assert_eq!(recorded[0], 42);
    }

    #[test]
    fn test_response_header_modification() {
        let mut chain = InterceptorChain::new();
        chain.add_response_interceptor(HeaderModifyingResponseInterceptor::new(
            "x-custom", "value1",
        ));
        chain.add_response_interceptor(HeaderModifyingResponseInterceptor::new(
            "x-another",
            "value2",
        ));

        let response = create_test_response(StatusCode::OK);
        let modified = chain.intercept_response(response);

        // Both headers should be present
        assert_eq!(
            modified
                .headers()
                .get("x-custom")
                .unwrap()
                .to_str()
                .unwrap(),
            "value1"
        );
        assert_eq!(
            modified
                .headers()
                .get("x-another")
                .unwrap()
                .to_str()
                .unwrap(),
            "value2"
        );
    }

    #[test]
    fn test_chain_clone() {
        let order = Arc::new(std::sync::Mutex::new(Vec::new()));
        let mut chain = InterceptorChain::new();
        chain.add_request_interceptor(TrackingRequestInterceptor::new(1, order.clone()));
        chain.add_response_interceptor(TrackingResponseInterceptor::new(2, order.clone()));

        // Clone the chain
        let cloned = chain.clone();

        assert_eq!(cloned.request_interceptor_count(), 1);
        assert_eq!(cloned.response_interceptor_count(), 1);
    }
}