stateset-http 0.7.20

HTTP service layer (REST + SSE) for the StateSet commerce engine
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
//! HTTP middleware — request ID, logging, CORS, rate limiting, caching.

use std::sync::{Arc, Mutex};
use std::time::Instant;

use axum::{
    Router,
    body::Body,
    extract::State,
    http::{
        HeaderName, HeaderValue, Method, Request, StatusCode,
        header::{AUTHORIZATION, CONTENT_TYPE},
    },
    middleware::{Next, from_fn, from_fn_with_state},
    response::{IntoResponse, Response},
};
use http_body_util::BodyExt;
use tower_http::{
    cors::{AllowOrigin, CorsLayer},
    request_id::{MakeRequestUuid, PropagateRequestIdLayer, SetRequestIdLayer},
    trace::TraceLayer,
};

use crate::error::HttpError;

/// Header name for request IDs.
pub(crate) static X_REQUEST_ID: HeaderName = HeaderName::from_static("x-request-id");
pub(crate) static X_TENANT_ID: HeaderName = HeaderName::from_static("x-tenant-id");
const CORS_ORIGINS_ENV: &str = "STATESET_HTTP_ALLOWED_ORIGINS";
const DEFAULT_CORS_ORIGINS: [&str; 2] = ["http://localhost:3000", "http://127.0.0.1:3000"];

#[derive(Clone, Debug)]
struct BearerAuthConfig {
    token: Arc<str>,
    bound_tenant_id: Option<Arc<str>>,
}

impl BearerAuthConfig {
    fn new(token: String, bound_tenant_id: Option<String>) -> Self {
        Self {
            token: Arc::<str>::from(token),
            bound_tenant_id: bound_tenant_id.map(Arc::<str>::from),
        }
    }
}

fn bearer_token_from_header(value: &str) -> Option<&str> {
    let mut parts = value.splitn(2, ' ');
    let scheme = parts.next()?;
    let token = parts.next()?.trim();
    if scheme.eq_ignore_ascii_case("bearer") && !token.is_empty() { Some(token) } else { None }
}

fn constant_time_eq(a: &str, b: &str) -> bool {
    let a_bytes = a.as_bytes();
    let b_bytes = b.as_bytes();
    if a_bytes.len() != b_bytes.len() {
        return false;
    }

    let mut diff = 0u8;
    for (&left, &right) in a_bytes.iter().zip(b_bytes.iter()) {
        diff |= left ^ right;
    }
    diff == 0
}

fn is_valid_tenant_id(value: &str) -> bool {
    let trimmed = value.trim();
    if trimmed.is_empty() || trimmed.len() > 64 {
        return false;
    }
    trimmed.chars().all(|ch| ch.is_ascii_alphanumeric() || ch == '-' || ch == '_' || ch == '.')
}

async fn require_bearer_auth(
    State(auth): State<BearerAuthConfig>,
    request: Request<Body>,
    next: Next,
) -> Response {
    if !request.uri().path().starts_with("/api/v1") {
        return next.run(request).await;
    }

    let provided = request
        .headers()
        .get(AUTHORIZATION)
        .and_then(|value| value.to_str().ok())
        .and_then(bearer_token_from_header);

    match provided {
        Some(provided) if constant_time_eq(provided, auth.token.as_ref()) => {}
        _ => {
            return HttpError::Unauthorized("missing or invalid bearer token".to_string())
                .into_response();
        }
    }

    let tenant_id = request
        .headers()
        .get(&X_TENANT_ID)
        .and_then(|value| value.to_str().ok())
        .map(str::trim)
        .filter(|value| !value.is_empty());

    match tenant_id {
        Some(value) if is_valid_tenant_id(value) => {
            if let Some(bound_tenant_id) = auth.bound_tenant_id.as_deref() {
                if !constant_time_eq(value, bound_tenant_id) {
                    return HttpError::Forbidden(
                        "tenant is not authorized for supplied bearer token".to_string(),
                    )
                    .into_response();
                }
            }
            next.run(request).await
        }
        Some(_) => HttpError::BadRequest("invalid x-tenant-id header".to_string()).into_response(),
        None => HttpError::BadRequest("missing x-tenant-id header".to_string()).into_response(),
    }
}

/// Build the standard CORS middleware for development.
///
/// Allows a configurable set of origins with explicit methods and headers.
pub(crate) fn cors_layer() -> CorsLayer {
    let configured = std::env::var(CORS_ORIGINS_ENV).ok();
    let allowed_origins = configured
        .as_deref()
        .map(|value| value.split(',').map(str::trim).filter(|origin| !origin.is_empty()))
        .into_iter()
        .flatten()
        .filter_map(|origin| HeaderValue::from_str(origin).ok())
        .collect::<Vec<_>>();
    let origins = if allowed_origins.is_empty() {
        DEFAULT_CORS_ORIGINS
            .iter()
            .filter_map(|origin| HeaderValue::from_str(origin).ok())
            .collect::<Vec<_>>()
    } else {
        allowed_origins
    };

    CorsLayer::new()
        .allow_origin(AllowOrigin::list(origins))
        .allow_methods([
            Method::GET,
            Method::POST,
            Method::PUT,
            Method::PATCH,
            Method::DELETE,
            Method::OPTIONS,
        ])
        .allow_headers([AUTHORIZATION, CONTENT_TYPE, X_TENANT_ID.clone()])
}

// ============================================================================
// Rate Limiting
// ============================================================================

/// Configuration for the token bucket rate limiter.
#[derive(Clone, Debug)]
pub(crate) struct RateLimitConfig {
    /// Sustained requests per second.
    pub requests_per_second: u64,
    /// Maximum burst size (token bucket capacity).
    pub burst_size: u64,
}

/// Token bucket for rate limiting.
///
/// Tokens refill at `rate` per second up to `capacity`. Each request consumes
/// one token; if none are available the request is rejected with HTTP 429.
pub(crate) struct TokenBucket {
    tokens: f64,
    last_refill: Instant,
    rate: f64,
    capacity: f64,
}

impl TokenBucket {
    fn new(rate: u64, capacity: u64) -> Self {
        Self {
            tokens: capacity as f64,
            last_refill: Instant::now(),
            rate: rate as f64,
            capacity: capacity as f64,
        }
    }

    fn try_acquire(&mut self) -> bool {
        let now = Instant::now();
        let elapsed = now.duration_since(self.last_refill).as_secs_f64();
        self.tokens = (self.tokens + elapsed * self.rate).min(self.capacity);
        self.last_refill = now;
        if self.tokens >= 1.0 {
            self.tokens -= 1.0;
            true
        } else {
            false
        }
    }
}

/// Create a shared token bucket wrapped in an `Arc<Mutex<_>>`.
pub(crate) fn create_rate_limiter(config: &RateLimitConfig) -> Arc<Mutex<TokenBucket>> {
    Arc::new(Mutex::new(TokenBucket::new(config.requests_per_second, config.burst_size)))
}

/// Middleware that enforces a global request rate limit.
///
/// Returns HTTP 429 with a `Retry-After` header when the bucket is empty.
async fn rate_limit(
    State(bucket): State<Arc<Mutex<TokenBucket>>>,
    request: Request<Body>,
    next: Next,
) -> Response {
    let allowed = {
        let mut guard = bucket.lock().unwrap_or_else(|e| e.into_inner());
        guard.try_acquire()
    };
    if allowed {
        next.run(request).await
    } else {
        let mut response =
            HttpError::TooManyRequests("rate limit exceeded".to_string()).into_response();
        response.headers_mut().insert("retry-after", HeaderValue::from_static("1"));
        response
    }
}

/// Build the request-ID middleware layers.
///
/// - Assigns a `x-request-id` UUID if the incoming request lacks one.
/// - Propagates the `x-request-id` into the response.
pub(crate) fn request_id_layers() -> (SetRequestIdLayer<MakeRequestUuid>, PropagateRequestIdLayer) {
    (
        SetRequestIdLayer::new(X_REQUEST_ID.clone(), MakeRequestUuid),
        PropagateRequestIdLayer::new(X_REQUEST_ID.clone()),
    )
}

// ============================================================================
// HTTP Caching (ETag + Cache-Control)
// ============================================================================

/// Compute a weak `ETag` from response body bytes using FNV-1a for speed.
fn compute_etag(body: &[u8]) -> String {
    // FNV-1a 64-bit — fast, non-cryptographic hash ideal for ETags.
    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
    for &byte in body {
        hash ^= u64::from(byte);
        hash = hash.wrapping_mul(0x0100_0000_01b3);
    }
    format!("W/\"{hash:016x}\"")
}

/// Determine the `Cache-Control` value based on the request path.
///
/// - Health endpoints: `no-cache` (always revalidate)
/// - List endpoints (`/api/v1/<resource>`): short TTL (10s) + revalidation
/// - Single resource (`/api/v1/<resource>/<id>`): moderate TTL (60s) + revalidation
/// - `OpenAPI` spec: long TTL (1 hour, public)
fn cache_control_for_path(path: &str) -> &'static str {
    if path.starts_with("/health") {
        "no-cache"
    } else if path.ends_with("/events/stream") {
        "no-cache"
    } else if path.contains("/openapi.json") {
        "public, max-age=3600"
    } else if let Some(rest) = path.strip_prefix("/api/v1/") {
        // Count path segments after /api/v1/
        let segments = rest.split('/').filter(|s| !s.is_empty()).count();
        if segments <= 1 {
            // List endpoint (e.g. /api/v1/orders)
            "private, max-age=10, must-revalidate"
        } else {
            // Single resource (e.g. /api/v1/orders/{id})
            "private, max-age=60, must-revalidate"
        }
    } else {
        "no-cache"
    }
}

/// Middleware that adds `ETag` and `Cache-Control` headers to GET responses,
/// and handles `If-None-Match` conditional requests (returns 304 Not Modified).
///
/// Only applied to successful GET responses with a response body.
async fn http_cache(request: Request<Body>, next: Next) -> Response {
    let method = request.method().clone();
    let path = request.uri().path().to_owned();

    // Only cache GET requests
    if method != Method::GET {
        return next.run(request).await;
    }

    // Extract If-None-Match header before passing the request
    let if_none_match =
        request.headers().get("if-none-match").and_then(|v| v.to_str().ok()).map(String::from);

    let response = next.run(request).await;

    // Only cache successful responses
    if !response.status().is_success() {
        return response;
    }

    let (mut parts, body) = response.into_parts();

    // Do not buffer indefinitely streaming responses such as SSE.
    let is_sse = parts
        .headers
        .get(CONTENT_TYPE)
        .and_then(|value| value.to_str().ok())
        .is_some_and(|value| value.starts_with("text/event-stream"));
    if is_sse {
        parts.headers.insert("cache-control", HeaderValue::from_static("no-cache"));
        return Response::from_parts(parts, body);
    }

    // Collect the body bytes
    let body_bytes = match body.collect().await {
        Ok(collected) => collected.to_bytes(),
        Err(_) => return Response::from_parts(parts, Body::empty()),
    };

    // Compute ETag
    let etag = compute_etag(&body_bytes);

    // Check If-None-Match
    if let Some(client_etag) = if_none_match {
        // Strip quotes and W/ prefix for comparison
        let normalize = |s: &str| s.replace("W/", "").replace('"', "");
        if normalize(&client_etag) == normalize(&etag) {
            let mut not_modified = Response::new(Body::empty());
            *not_modified.status_mut() = StatusCode::NOT_MODIFIED;
            not_modified.headers_mut().insert(
                "etag",
                HeaderValue::from_str(&etag).unwrap_or(HeaderValue::from_static("")),
            );
            not_modified
                .headers_mut()
                .insert("cache-control", HeaderValue::from_static(cache_control_for_path(&path)));
            return not_modified;
        }
    }

    // Add caching headers
    parts
        .headers
        .insert("etag", HeaderValue::from_str(&etag).unwrap_or(HeaderValue::from_static("")));
    parts.headers.insert("cache-control", HeaderValue::from_static(cache_control_for_path(&path)));

    Response::from_parts(parts, Body::from(body_bytes))
}

/// Apply all standard middleware to a router.
pub(crate) fn apply_middleware(
    router: Router,
    with_cors: bool,
    with_request_id: bool,
    auth_config: Option<(String, Option<String>)>,
    rate_limit_config: Option<RateLimitConfig>,
) -> Router {
    let mut router = router.layer(TraceLayer::new_for_http());

    // HTTP caching (ETag + Cache-Control) — innermost layer for GET responses
    router = router.layer(from_fn(http_cache));

    if let Some(config) = rate_limit_config {
        let bucket = create_rate_limiter(&config);
        router = router.layer(from_fn_with_state(bucket, rate_limit));
    }

    if let Some((token, bound_tenant_id)) = auth_config {
        router = router.layer(from_fn_with_state(
            BearerAuthConfig::new(token, bound_tenant_id),
            require_bearer_auth,
        ));
    }

    if with_cors {
        router = router.layer(cors_layer());
    }

    if with_request_id {
        let (set_id, propagate_id) = request_id_layers();
        router = router.layer(propagate_id).layer(set_id);
    }

    router
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::body::Body;
    use axum::http::{Method, Request, StatusCode};
    use axum::response::sse::{Event, KeepAlive, Sse};
    use axum::routing::get;
    use std::{convert::Infallible, time::Duration};
    use tokio_stream::{StreamExt as _, wrappers::IntervalStream};
    use tower::ServiceExt;

    #[test]
    fn x_request_id_header_name() {
        assert_eq!(X_REQUEST_ID.as_str(), "x-request-id");
    }

    #[test]
    fn cors_layer_builds() {
        let _layer = cors_layer();
    }

    #[test]
    fn request_id_layers_build() {
        let (_set, _propagate) = request_id_layers();
    }

    #[test]
    fn apply_middleware_no_extras() {
        let router = Router::new();
        let _router = apply_middleware(router, false, false, None, None);
    }

    #[test]
    fn apply_middleware_all() {
        let router = Router::new();
        let _router = apply_middleware(router, true, true, Some(("token".to_string(), None)), None);
    }

    #[test]
    fn apply_middleware_cors_only() {
        let router = Router::new();
        let _router = apply_middleware(router, true, false, None, None);
    }

    #[test]
    fn apply_middleware_request_id_only() {
        let router = Router::new();
        let _router = apply_middleware(router, false, true, None, None);
    }

    #[test]
    fn constant_time_eq_behaves_like_string_equality() {
        assert!(constant_time_eq("secret", "secret"));
        assert!(!constant_time_eq("secret", "secreu"));
        assert!(!constant_time_eq("secret", "secret1"));
        assert!(!constant_time_eq("", "a"));
    }

    #[test]
    fn tenant_id_validation() {
        assert!(is_valid_tenant_id("tenant-1"));
        assert!(is_valid_tenant_id("tenant.alpha_01"));
        assert!(!is_valid_tenant_id(""));
        assert!(!is_valid_tenant_id("  "));
        assert!(!is_valid_tenant_id("../etc/passwd"));
        assert!(!is_valid_tenant_id("tenant/one"));
    }

    #[tokio::test]
    async fn auth_blocks_unauthorized_api_requests() {
        let router = Router::new().route("/api/v1/orders", get(|| async { "ok" }));
        let app = apply_middleware(router, false, false, Some(("secret".to_string(), None)), None);

        let response =
            app.oneshot(Request::get("/api/v1/orders").body(Body::empty()).unwrap()).await.unwrap();
        assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn auth_allows_authorized_api_requests() {
        let router = Router::new().route("/api/v1/orders", get(|| async { "ok" }));
        let app = apply_middleware(router, false, false, Some(("secret".to_string(), None)), None);

        let response = app
            .oneshot(
                Request::get("/api/v1/orders")
                    .header("authorization", "Bearer secret")
                    .header("x-tenant-id", "tenant-1")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn auth_blocks_missing_tenant_header() {
        let router = Router::new().route("/api/v1/orders", get(|| async { "ok" }));
        let app = apply_middleware(router, false, false, Some(("secret".to_string(), None)), None);

        let response = app
            .oneshot(
                Request::get("/api/v1/orders")
                    .header("authorization", "Bearer secret")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    #[tokio::test]
    async fn auth_blocks_tenant_mismatch_when_token_is_bound() {
        let router = Router::new().route("/api/v1/orders", get(|| async { "ok" }));
        let app = apply_middleware(
            router,
            false,
            false,
            Some(("secret".to_string(), Some("tenant-a".to_string()))),
            None,
        );

        let response = app
            .oneshot(
                Request::get("/api/v1/orders")
                    .header("authorization", "Bearer secret")
                    .header("x-tenant-id", "tenant-b")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::FORBIDDEN);
    }

    #[tokio::test]
    async fn auth_skips_non_api_routes() {
        let router = Router::new().route("/health", get(|| async { "ok" }));
        let app = apply_middleware(router, false, false, Some(("secret".to_string(), None)), None);

        let response =
            app.oneshot(Request::get("/health").body(Body::empty()).unwrap()).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn cors_allows_localhost_origin_by_default() {
        let router = Router::new().route("/health", get(|| async { "ok" }));
        let app = apply_middleware(router, true, false, None, None);

        let response = app
            .oneshot(
                Request::builder()
                    .method(Method::OPTIONS)
                    .uri("/health")
                    .header("origin", "http://localhost:3000")
                    .header("access-control-request-method", "GET")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(
            response
                .headers()
                .get("access-control-allow-origin")
                .and_then(|value| value.to_str().ok()),
            Some("http://localhost:3000")
        );
    }

    #[tokio::test]
    async fn cors_rejects_unconfigured_origin_by_default() {
        let router = Router::new().route("/health", get(|| async { "ok" }));
        let app = apply_middleware(router, true, false, None, None);

        let response = app
            .oneshot(
                Request::builder()
                    .method(Method::OPTIONS)
                    .uri("/health")
                    .header("origin", "https://evil.example")
                    .header("access-control-request-method", "GET")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);
        assert!(response.headers().get("access-control-allow-origin").is_none());
    }

    #[test]
    fn token_bucket_allows_within_capacity() {
        let mut bucket = TokenBucket::new(10, 5);
        for _ in 0..5 {
            assert!(bucket.try_acquire(), "should allow requests within burst capacity");
        }
    }

    #[test]
    fn token_bucket_rejects_over_capacity() {
        let mut bucket = TokenBucket::new(10, 2);
        assert!(bucket.try_acquire());
        assert!(bucket.try_acquire());
        assert!(!bucket.try_acquire(), "should reject requests beyond burst");
    }

    #[test]
    fn token_bucket_refills_over_time() {
        let mut bucket = TokenBucket::new(1000, 1);
        assert!(bucket.try_acquire());
        assert!(!bucket.try_acquire());
        // Manually advance the last_refill to simulate time passing
        bucket.last_refill -= std::time::Duration::from_millis(10);
        assert!(bucket.try_acquire(), "should refill tokens over time");
    }

    #[test]
    fn rate_limit_config_builds() {
        let config = RateLimitConfig { requests_per_second: 100, burst_size: 200 };
        let _limiter = create_rate_limiter(&config);
    }

    #[tokio::test]
    async fn rate_limit_allows_requests_within_limit() {
        let router = Router::new().route("/health", get(|| async { "ok" }));
        let config = Some(RateLimitConfig { requests_per_second: 100, burst_size: 10 });
        let app = apply_middleware(router, false, false, None, config);

        let response =
            app.oneshot(Request::get("/health").body(Body::empty()).unwrap()).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn rate_limit_returns_429_when_exceeded() {
        let config = RateLimitConfig { requests_per_second: 1, burst_size: 1 };
        let bucket = create_rate_limiter(&config);

        // Exhaust the bucket
        {
            let mut guard = bucket.lock().unwrap();
            guard.try_acquire();
        }

        // Build the middleware with a pre-exhausted bucket
        let app: Router<()> = Router::new()
            .route("/health", get(|| async { "ok" }))
            .layer(from_fn_with_state(bucket, rate_limit));

        let response =
            app.oneshot(Request::get("/health").body(Body::empty()).unwrap()).await.unwrap();
        assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
        assert_eq!(response.headers().get("retry-after").and_then(|v| v.to_str().ok()), Some("1"));
    }

    #[test]
    fn apply_middleware_with_rate_limit() {
        let router = Router::new();
        let config = Some(RateLimitConfig { requests_per_second: 50, burst_size: 100 });
        let _router = apply_middleware(router, false, false, None, config);
    }

    // ========================================================================
    // HTTP Caching Tests
    // ========================================================================

    #[test]
    fn compute_etag_deterministic() {
        let etag1 = compute_etag(b"hello world");
        let etag2 = compute_etag(b"hello world");
        assert_eq!(etag1, etag2, "same input should produce same ETag");
    }

    #[test]
    fn compute_etag_differs_for_different_input() {
        let etag1 = compute_etag(b"hello");
        let etag2 = compute_etag(b"world");
        assert_ne!(etag1, etag2, "different input should produce different ETag");
    }

    #[test]
    fn compute_etag_is_weak() {
        let etag = compute_etag(b"test");
        assert!(etag.starts_with("W/\""), "ETag should be a weak validator");
    }

    #[test]
    fn cache_control_health_is_no_cache() {
        assert_eq!(cache_control_for_path("/health"), "no-cache");
        assert_eq!(cache_control_for_path("/health/ready"), "no-cache");
    }

    #[test]
    fn cache_control_openapi_is_public() {
        assert_eq!(cache_control_for_path("/api/v1/openapi.json"), "public, max-age=3600");
    }

    #[test]
    fn cache_control_list_endpoints() {
        assert_eq!(
            cache_control_for_path("/api/v1/orders"),
            "private, max-age=10, must-revalidate"
        );
        assert_eq!(
            cache_control_for_path("/api/v1/customers"),
            "private, max-age=10, must-revalidate"
        );
    }

    #[test]
    fn cache_control_single_resource() {
        assert_eq!(
            cache_control_for_path("/api/v1/orders/abc-123"),
            "private, max-age=60, must-revalidate"
        );
        assert_eq!(
            cache_control_for_path("/api/v1/customers/cust-1"),
            "private, max-age=60, must-revalidate"
        );
    }

    #[test]
    fn cache_control_event_stream_is_no_cache() {
        assert_eq!(cache_control_for_path("/api/v1/events/stream"), "no-cache");
    }

    #[tokio::test]
    async fn http_cache_adds_etag_to_get_response() {
        let app: Router<()> = Router::new()
            .route("/api/v1/orders", get(|| async { "[]" }))
            .layer(from_fn(http_cache));

        let response =
            app.oneshot(Request::get("/api/v1/orders").body(Body::empty()).unwrap()).await.unwrap();

        assert_eq!(response.status(), StatusCode::OK);
        assert!(response.headers().get("etag").is_some(), "should have ETag header");
        assert!(
            response.headers().get("cache-control").is_some(),
            "should have Cache-Control header"
        );
    }

    #[tokio::test]
    async fn http_cache_returns_304_on_matching_etag() {
        let app: Router<()> = Router::new()
            .route("/api/v1/orders", get(|| async { "[]" }))
            .layer(from_fn(http_cache));

        // First request to get the ETag
        let response = app
            .clone()
            .oneshot(Request::get("/api/v1/orders").body(Body::empty()).unwrap())
            .await
            .unwrap();
        let etag = response.headers().get("etag").unwrap().to_str().unwrap().to_string();

        // Second request with If-None-Match
        let response = app
            .oneshot(
                Request::get("/api/v1/orders")
                    .header("if-none-match", &etag)
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::NOT_MODIFIED);
    }

    #[tokio::test]
    async fn http_cache_skips_post_requests() {
        use axum::routing::post;

        let app: Router<()> = Router::new()
            .route("/api/v1/orders", post(|| async { "created" }))
            .layer(from_fn(http_cache));

        let response = app
            .oneshot(Request::post("/api/v1/orders").body(Body::empty()).unwrap())
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);
        assert!(response.headers().get("etag").is_none(), "POST should not have ETag");
    }

    #[tokio::test]
    async fn http_cache_does_not_cache_error_responses() {
        async fn handler() -> (StatusCode, &'static str) {
            (StatusCode::NOT_FOUND, "not found")
        }

        let app: Router<()> =
            Router::new().route("/api/v1/orders/missing", get(handler)).layer(from_fn(http_cache));

        let response = app
            .oneshot(Request::get("/api/v1/orders/missing").body(Body::empty()).unwrap())
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::NOT_FOUND);
        assert!(response.headers().get("etag").is_none(), "error responses should not have ETag");
    }

    #[tokio::test]
    async fn http_cache_returns_200_on_mismatched_etag() {
        let app: Router<()> = Router::new()
            .route("/api/v1/orders", get(|| async { "[]" }))
            .layer(from_fn(http_cache));

        let response = app
            .oneshot(
                Request::get("/api/v1/orders")
                    .header("if-none-match", "W/\"stale-etag\"")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);
        assert!(response.headers().get("etag").is_some());
    }

    #[tokio::test]
    async fn http_cache_skips_sse_responses() {
        let app: Router<()> = Router::new()
            .route(
                "/api/v1/events/stream",
                get(|| async {
                    let stream =
                        IntervalStream::new(tokio::time::interval(Duration::from_secs(60)))
                            .map(|_| Ok::<_, Infallible>(Event::default().data("tick")));
                    Sse::new(stream).keep_alive(KeepAlive::default())
                }),
            )
            .layer(from_fn(http_cache));

        let response = tokio::time::timeout(
            Duration::from_millis(200),
            app.oneshot(Request::get("/api/v1/events/stream").body(Body::empty()).unwrap()),
        )
        .await
        .expect("SSE response should not be buffered by cache middleware")
        .unwrap();

        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(
            response.headers().get(CONTENT_TYPE).and_then(|value| value.to_str().ok()),
            Some("text/event-stream")
        );
        assert_eq!(
            response.headers().get("cache-control").and_then(|value| value.to_str().ok()),
            Some("no-cache")
        );
        assert!(
            response.headers().get("etag").is_none(),
            "streaming responses should not have ETags"
        );
    }
}