tork 0.1.0

A FastAPI-style backend web framework for Rust, built on Hyper and Tokio. Annotation-based routers, dependency injection, and OpenAPI out of the box.
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
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
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
//! Confirms the middleware surface is reachable through the facade crate, and
//! that the `#[middleware]` macro produces a working layer.

use std::sync::Arc;
use std::time::Duration;

use bytes::Bytes;
use futures_util::stream;
use http::HeaderValue;
use http_body_util::{BodyExt, Full, StreamBody};
use tork::testing::{LogRecorder, TestClient};
use tork::{
    box_body, bytes_response, get, middleware, App, BoxFuture, DuplicatePolicy, HandlerFn,
    LoggerConfig, Method, Middleware, Next, ReqBody, Request, RequestContext, Response, Result,
    Route, Router, StatusCode,
};

struct Noop;

impl Middleware for Noop {
    fn handle(&self, request: Request, next: Next) -> BoxFuture<'static, Result<Response>> {
        next.run(request)
    }

    fn duplicate_policy(&self) -> DuplicatePolicy {
        DuplicatePolicy::Allow
    }
}

#[test]
fn middleware_types_are_exported() {
    assert!(!Noop.name().is_empty());
    assert_eq!(Noop.duplicate_policy(), DuplicatePolicy::Allow);
}

#[middleware]
async fn add_marker(req: Request, next: Next) -> Result<Response> {
    let mut res = next.run(req).await?;
    res.headers_mut()
        .insert("x-marker", HeaderValue::from_static("on"));
    Ok(res)
}

fn ok_handler() -> HandlerFn {
    std::sync::Arc::new(
        |_ctx: RequestContext| -> BoxFuture<'static, Result<Response>> {
            Box::pin(async {
                Ok(bytes_response(
                    StatusCode::OK,
                    "text/plain; charset=utf-8",
                    Bytes::from_static(b"ok"),
                ))
            })
        },
    )
}

fn request() -> http::Request<ReqBody> {
    http::Request::builder()
        .method(Method::GET)
        .uri("/")
        .body(box_body(Full::new(Bytes::new())))
        .unwrap()
}

#[tokio::test]
async fn custom_middleware_macro_runs() {
    let app = std::sync::Arc::new(
        App::new()
            .middleware(add_marker)
            .include_router(Router::new().route(tork::Route::new(Method::GET, "/", ok_handler())))
            .build()
            .unwrap(),
    );

    let response = app.handle(request()).await;
    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(
        response.headers().get("x-marker").unwrap(),
        HeaderValue::from_static("on")
    );
    let body = response.into_body().collect().await.unwrap().to_bytes();
    assert_eq!(&body[..], b"ok");
}

fn app_with<M: Middleware>(mw: M) -> std::sync::Arc<tork::AppInner> {
    std::sync::Arc::new(
        App::new()
            .middleware(mw)
            .include_router(Router::new().route(tork::Route::new(Method::GET, "/", ok_handler())))
            .build()
            .unwrap(),
    )
}

fn app_with_post<M: Middleware>(mw: M) -> std::sync::Arc<tork::AppInner> {
    std::sync::Arc::new(
        App::new()
            .middleware(mw)
            .include_router(Router::new().route(tork::Route::new(Method::POST, "/", ok_handler())))
            .build()
            .unwrap(),
    )
}

#[tokio::test]
async fn request_id_generates_when_absent() {
    use tork::middleware::RequestId;
    let response = app_with(RequestId::new()).handle(request()).await;
    let id = response
        .headers()
        .get("x-request-id")
        .unwrap()
        .to_str()
        .unwrap();
    assert!(id.starts_with("req-"), "id: {id}");
}

fn slow_handler() -> HandlerFn {
    Arc::new(
        |_ctx: RequestContext| -> BoxFuture<'static, Result<Response>> {
            Box::pin(async {
                tokio::time::sleep(Duration::from_millis(100)).await;
                Ok(bytes_response(
                    StatusCode::OK,
                    "text/plain; charset=utf-8",
                    Bytes::from_static(b"slow"),
                ))
            })
        },
    )
}

#[tokio::test]
async fn timeout_returns_504_for_slow_handler() {
    use tork::middleware::Timeout;
    let app = Arc::new(
        App::new()
            .middleware(Timeout::millis(10))
            .include_router(Router::new().route(Route::new(Method::GET, "/", slow_handler())))
            .build()
            .unwrap(),
    );
    let response = app.handle(request()).await;
    assert_eq!(response.status(), StatusCode::GATEWAY_TIMEOUT);
}

#[tokio::test]
async fn body_limit_rejects_oversized_content_length() {
    use tork::middleware::BodyLimit;
    let req = http::Request::builder()
        .method(Method::GET)
        .uri("/")
        .header("content-length", "1000000")
        .body(box_body(Full::new(Bytes::new())))
        .unwrap();
    let response = app_with(BodyLimit::bytes(10)).handle(req).await;
    assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);
}

fn get_with_headers(headers: &[(&str, &str)]) -> http::Request<ReqBody> {
    let mut builder = http::Request::builder().method(Method::GET).uri("/");
    for (name, value) in headers {
        builder = builder.header(*name, *value);
    }
    builder.body(box_body(Full::new(Bytes::new()))).unwrap()
}

#[tokio::test]
async fn trusted_host_allows_and_rejects() {
    use tork::middleware::TrustedHost;

    let allowed = app_with(TrustedHost::new(["example.com", "*.example.com"]))
        .handle(get_with_headers(&[("host", "app.example.com")]))
        .await;
    assert_eq!(allowed.status(), StatusCode::OK);

    let rejected = app_with(TrustedHost::new(["example.com"]))
        .handle(get_with_headers(&[("host", "evil.com")]))
        .await;
    assert_eq!(rejected.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn https_redirect_redirects_plain_http() {
    use tork::middleware::HttpsRedirect;

    let response = app_with(HttpsRedirect::new())
        .handle(get_with_headers(&[("host", "example.com")]))
        .await;
    assert_eq!(response.status(), StatusCode::PERMANENT_REDIRECT);
    assert_eq!(
        response.headers().get("location").unwrap(),
        "https://example.com/"
    );

    // An untrusted forwarded scheme is ignored.
    let passed = app_with(HttpsRedirect::new())
        .handle(get_with_headers(&[("x-forwarded-proto", "https")]))
        .await;
    assert_eq!(passed.status(), StatusCode::PERMANENT_REDIRECT);
}

#[tokio::test]
async fn proxy_headers_ignore_untrusted_forwarded_host() {
    use tork::middleware::{ProxyHeaders, TrustedHost};

    let app = Arc::new(
        App::new()
            .middleware(ProxyHeaders::new())
            .middleware(TrustedHost::new(["real.example.com"]))
            .include_router(Router::new().route(Route::new(Method::GET, "/", ok_handler())))
            .build()
            .unwrap(),
    );

    // The direct Host is untrusted, but X-Forwarded-Host carries the real one.
    let response = app
        .handle(get_with_headers(&[
            ("host", "proxy.internal"),
            ("x-forwarded-host", "real.example.com"),
        ]))
        .await;
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn proxy_headers_rewrite_host_for_trusted_proxies() {
    use tork::middleware::{ProxyHeaders, TrustedHost};

    let client = TestClient::serve(
        App::new()
            .middleware(ProxyHeaders::new().trust_loopback())
            .middleware(TrustedHost::new(["real.example.com"]))
            .include_router(Router::new().route(Route::new(Method::GET, "/", ok_handler()))),
    )
    .bind_random_port()
    .await
    .unwrap();

    let response = client
        .get("/")
        .unsafe_header("host", "proxy.internal")
        .unsafe_header("x-forwarded-host", "real.example.com")
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);

    client.shutdown().await.unwrap();
}

#[tokio::test]
async fn cors_answers_preflight() {
    use tork::middleware::Cors;

    let app = app_with(
        Cors::new()
            .allow_origin("https://app.example.com")
            .allow_methods(["GET", "POST"])
            .allow_headers(["Authorization", "Content-Type"]),
    );
    let preflight = http::Request::builder()
        .method(Method::OPTIONS)
        .uri("/")
        .header("origin", "https://app.example.com")
        .header("access-control-request-method", "POST")
        .body(box_body(Full::new(Bytes::new())))
        .unwrap();

    let response = app.handle(preflight).await;
    assert_eq!(response.status(), StatusCode::NO_CONTENT);
    assert_eq!(
        response
            .headers()
            .get("access-control-allow-origin")
            .unwrap(),
        "https://app.example.com"
    );
    assert_eq!(
        response
            .headers()
            .get("access-control-allow-methods")
            .unwrap(),
        "GET, POST"
    );
}

#[tokio::test]
async fn cors_annotates_actual_request() {
    use tork::middleware::Cors;

    let app = app_with(
        Cors::new()
            .allow_origin("*")
            .expose_headers(["X-Request-Id"]),
    );
    let response = app
        .handle(get_with_headers(&[("origin", "https://anywhere.test")]))
        .await;

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(
        response
            .headers()
            .get("access-control-allow-origin")
            .unwrap(),
        "*"
    );
    assert_eq!(
        response
            .headers()
            .get("access-control-expose-headers")
            .unwrap(),
        "X-Request-Id"
    );
}

fn large_handler() -> HandlerFn {
    Arc::new(
        |_ctx: RequestContext| -> BoxFuture<'static, Result<Response>> {
            Box::pin(async {
                Ok(bytes_response(
                    StatusCode::OK,
                    "text/plain; charset=utf-8",
                    Bytes::from(vec![b'a'; 2000]),
                ))
            })
        },
    )
}

fn app_with_handler<M: Middleware>(mw: M, handler: HandlerFn) -> Arc<tork::AppInner> {
    Arc::new(
        App::new()
            .middleware(mw)
            .include_router(Router::new().route(Route::new(Method::GET, "/", handler)))
            .build()
            .unwrap(),
    )
}

#[tokio::test]
async fn compression_gzips_large_responses() {
    use tork::middleware::Compression;

    let app = app_with_handler(
        Compression::new().gzip().minimum_size(1000),
        large_handler(),
    );
    let response = app
        .handle(get_with_headers(&[("accept-encoding", "gzip")]))
        .await;

    assert_eq!(response.headers().get("content-encoding").unwrap(), "gzip");

    let body = response.into_body().collect().await.unwrap().to_bytes();
    let mut decoder = flate2::read::GzDecoder::new(&body[..]);
    let mut decoded = Vec::new();
    std::io::Read::read_to_end(&mut decoder, &mut decoded).unwrap();
    assert_eq!(decoded, vec![b'a'; 2000]);
}

#[tokio::test]
async fn compression_skips_bodies_over_the_maximum_size() {
    use tork::middleware::Compression;

    // The 2000-byte body exceeds the 1500-byte cap, so it is sent uncompressed
    // (and, advertising a Content-Length, is never buffered for compression).
    let app = app_with_handler(
        Compression::new()
            .gzip()
            .minimum_size(1000)
            .maximum_size(1500),
        large_handler(),
    );
    let response = app
        .handle(get_with_headers(&[("accept-encoding", "gzip")]))
        .await;

    assert!(response.headers().get("content-encoding").is_none());
    let body = response.into_body().collect().await.unwrap().to_bytes();
    assert_eq!(body, Bytes::from(vec![b'a'; 2000]));
}

#[tokio::test]
async fn compression_skips_without_accept_encoding() {
    use tork::middleware::Compression;

    let app = app_with_handler(
        Compression::new().gzip().minimum_size(1000),
        large_handler(),
    );
    // `request()` carries no Accept-Encoding.
    let response = app.handle(request()).await;
    assert!(response.headers().get("content-encoding").is_none());
}

#[test]
fn duplicate_singleton_middleware_is_rejected_at_build() {
    use tork::middleware::Cors;

    let result = App::new()
        .middleware(Cors::new())
        .middleware(Cors::new())
        .build();

    let error = result.err().expect("duplicate Cors should be rejected");
    let message = error.message();
    assert!(
        message.contains("Duplicate middleware detected: Cors"),
        "message: {message}"
    );
    assert!(
        message.contains("can only be registered once per scope"),
        "message: {message}"
    );
}

#[tokio::test]
async fn request_id_propagates_incoming() {
    use tork::middleware::RequestId;
    let req = http::Request::builder()
        .method(Method::GET)
        .uri("/")
        .header("x-request-id", "req-supplied")
        .body(box_body(Full::new(Bytes::new())))
        .unwrap();
    let response = app_with(RequestId::new()).handle(req).await;
    assert_eq!(
        response.headers().get("x-request-id").unwrap(),
        "req-supplied"
    );
}

#[tokio::test]
async fn request_id_supports_custom_header_name() {
    use tork::middleware::RequestId;
    let req = http::Request::builder()
        .method(Method::GET)
        .uri("/")
        .header("x-correlation-id", "corr-1")
        .body(box_body(Full::new(Bytes::new())))
        .unwrap();

    let response = app_with(RequestId::new().header_name("x-correlation-id"))
        .handle(req)
        .await;
    assert_eq!(
        response.headers().get("x-correlation-id").unwrap(),
        "corr-1"
    );
}

#[tokio::test]
async fn trace_middleware_logs_success_and_errors() {
    use tork::middleware::Trace;

    let recorder = LogRecorder::new();
    let client = TestClient::builder(
        App::new()
            .logger(LoggerConfig::new().request_logs(false))
            .middleware(Trace::new())
            .include_router(Router::new().route(Route::new(Method::GET, "/", ok_handler()))),
    )
    .logger(recorder.clone())
    .build()
    .await
    .unwrap();

    assert_eq!(client.get("/").send().await.unwrap().status(), 200);
    assert_eq!(client.get("/missing").send().await.unwrap().status(), 404);

    let records = recorder.records();
    assert!(records.iter().any(|r| r.message.contains("GET / 200")));
    assert!(records
        .iter()
        .any(|r| r.message.contains("GET /missing 404")));
}

#[tokio::test]
async fn trusted_host_accepts_host_with_port() {
    use tork::middleware::TrustedHost;

    let response = app_with(TrustedHost::new(["example.com"]))
        .handle(get_with_headers(&[("host", "example.com:8443")]))
        .await;
    assert_eq!(response.status(), StatusCode::OK);
}

#[tokio::test]
async fn cors_wildcard_with_credentials_fails_closed_and_keeps_other_headers() {
    use tork::middleware::Cors;

    let app = app_with(
        Cors::new()
            .allow_origin("*")
            .allow_credentials(true)
            .allow_headers(["Authorization"])
            .allow_methods(["GET"])
            .max_age(600),
    );
    let preflight = http::Request::builder()
        .method(Method::OPTIONS)
        .uri("/")
        .header("origin", "https://app.example.com")
        .header("access-control-request-method", "GET")
        .body(box_body(Full::new(Bytes::new())))
        .unwrap();

    let response = app.handle(preflight).await;
    assert!(response
        .headers()
        .get("access-control-allow-origin")
        .is_none());
    assert_eq!(
        response
            .headers()
            .get("access-control-allow-credentials")
            .unwrap(),
        "true"
    );
    assert_eq!(
        response.headers().get("access-control-max-age").unwrap(),
        "600"
    );
}

#[tokio::test]
async fn https_redirect_preserves_path_and_query() {
    use tork::middleware::HttpsRedirect;

    let req = http::Request::builder()
        .method(Method::GET)
        .uri("/search?q=tork")
        .header("host", "example.com")
        .body(box_body(Full::new(Bytes::new())))
        .unwrap();

    let response = app_with(HttpsRedirect::new()).handle(req).await;
    assert_eq!(
        response.headers().get("location").unwrap(),
        "https://example.com/search?q=tork"
    );
}

#[tokio::test]
async fn proxy_headers_spoofing_bypasses_trusted_host_without_proxy_headers() {
    use tork::middleware::TrustedHost;

    let app = Arc::new(
        App::new()
            .middleware(TrustedHost::new(["real.example.com"]))
            .include_router(Router::new().route(Route::new(Method::GET, "/", ok_handler())))
            .build()
            .unwrap(),
    );

    let response = app
        .handle(get_with_headers(&[
            ("host", "evil.com"),
            ("x-forwarded-host", "real.example.com"),
        ]))
        .await;
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn https_redirect_spoofing_via_x_forwarded_proto() {
    use tork::middleware::HttpsRedirect;

    let app = app_with(HttpsRedirect::new());

    let response = app
        .clone()
        .handle(get_with_headers(&[
            ("host", "example.com"),
            ("x-forwarded-proto", "https"),
        ]))
        .await;
    assert_eq!(response.status(), StatusCode::PERMANENT_REDIRECT);

    let response = app
        .handle(get_with_headers(&[
            ("host", "example.com"),
            ("x-forwarded-proto", "http"),
        ]))
        .await;
    assert_eq!(response.status(), StatusCode::PERMANENT_REDIRECT);
}

#[tokio::test]
async fn https_redirect_honors_trusted_proxy_scheme() {
    use tork::middleware::{HttpsRedirect, ProxyHeaders};

    let client = TestClient::serve(
        App::new()
            .middleware(ProxyHeaders::new().trust_loopback())
            .middleware(HttpsRedirect::new())
            .include_router(Router::new().route(Route::new(Method::GET, "/", ok_handler()))),
    )
    .bind_random_port()
    .await
    .unwrap();

    let response = client
        .get("/")
        .unsafe_header("host", "example.com")
        .unsafe_header("x-forwarded-proto", "https")
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);

    client.shutdown().await.unwrap();
}

#[tokio::test]
async fn cors_wildcard_with_credentials_is_rejected() {
    use tork::middleware::Cors;

    let app = app_with(Cors::new().allow_origin("*").allow_credentials(true));

    let response = app
        .handle(get_with_headers(&[("origin", "https://evil.example.com")]))
        .await;
    assert!(response
        .headers()
        .get("access-control-allow-origin")
        .is_none());
}

#[tokio::test]
async fn cors_preflight_includes_full_vary_headers() {
    use tork::middleware::Cors;

    let app = app_with(
        Cors::new()
            .allow_origin("https://app.example.com")
            .allow_methods(["GET", "POST"])
            .allow_headers(["Authorization", "Content-Type"]),
    );

    let preflight = http::Request::builder()
        .method(Method::OPTIONS)
        .uri("/")
        .header("origin", "https://app.example.com")
        .header("access-control-request-method", "POST")
        .header("access-control-request-headers", "Authorization")
        .body(box_body(Full::new(Bytes::new())))
        .unwrap();

    let response = app.handle(preflight).await;
    assert_eq!(response.status(), StatusCode::NO_CONTENT);

    let vary = response.headers().get("vary").unwrap().to_str().unwrap();
    assert!(vary.contains("Origin"));
    assert!(vary.contains("Access-Control-Request-Method"));
    assert!(vary.contains("Access-Control-Request-Headers"));
}

#[tokio::test]
async fn cors_actual_request_includes_vary_origin() {
    use tork::middleware::Cors;

    let app = app_with(Cors::new().allow_origin("https://app.example.com"));
    let response = app
        .handle(get_with_headers(&[("origin", "https://app.example.com")]))
        .await;

    let vary = response.headers().get("vary").unwrap().to_str().unwrap();
    assert!(vary.contains("Origin"));
}

#[tokio::test]
async fn trusted_host_accepts_bracketed_ipv6_with_a_port() {
    use tork::middleware::TrustedHost;

    // A bracketed IPv6 host with a port is parsed correctly and matches the
    // allowlisted literal, instead of being mangled and rejected.
    let allowed = app_with(TrustedHost::new(["[::1]", "localhost"]))
        .handle(get_with_headers(&[("host", "[::1]:8080")]))
        .await;
    assert_eq!(allowed.status(), StatusCode::OK);

    // An IPv6 host not on the allowlist is still rejected.
    let rejected = app_with(TrustedHost::new(["[::1]"]))
        .handle(get_with_headers(&[("host", "[2001:db8::1]:443")]))
        .await;
    assert_eq!(rejected.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn panic_recovery_returns_500_by_default() {
    fn panic_handler() -> HandlerFn {
        Arc::new(
            |_ctx: RequestContext| -> BoxFuture<'static, Result<Response>> {
                Box::pin(async {
                    panic!("intentional panic for testing");
                })
            },
        )
    }

    let app = Arc::new(
        App::new()
            .include_router(Router::new().route(Route::new(Method::GET, "/panic", panic_handler())))
            .build()
            .unwrap(),
    );

    let req = http::Request::builder()
        .method(Method::GET)
        .uri("/panic")
        .body(box_body(Full::new(Bytes::new())))
        .unwrap();
    let response = app.handle(req).await;
    assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);

    let body = response.into_body().collect().await.unwrap().to_bytes();
    let body_str = String::from_utf8_lossy(&body);
    assert!(body_str.contains("Internal server error"));
}

#[tokio::test]
async fn panic_recovery_with_test_client_returns_500() {
    #[get("/panic")]
    async fn panic_endpoint() -> tork::Result<serde_json::Value> {
        panic!("intentional panic for testing");
    }

    let app = App::new()
        .include(panic_endpoint)
        .build_test()
        .await
        .unwrap();
    let client = TestClient::new(app).await.unwrap();

    let response = client.get("/panic").send().await.unwrap();
    assert_eq!(response.status(), 500);
}

#[tokio::test]
async fn body_limit_chunked_requests_are_rejected_once_the_stream_crosses_the_limit() {
    use bytes::Bytes;
    use http_body::Frame;
    use tork::middleware::BodyLimit;

    let app = app_with_post(BodyLimit::bytes(100));

    let chunks: Vec<Result<Frame<Bytes>, std::convert::Infallible>> = (0..50)
        .map(|_| Ok(Frame::data(Bytes::from_static(b"xxxxxxxxxx"))))
        .collect();
    let body = StreamBody::new(stream::iter(chunks));

    let req = http::Request::builder()
        .method(Method::POST)
        .uri("/")
        .header("transfer-encoding", "chunked")
        .body(box_body(body))
        .unwrap();

    let response = app.handle(req).await;
    assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);
}

#[tokio::test]
async fn body_limit_allows_chunked_requests_under_limit() {
    use bytes::Bytes;
    use http_body::Frame;
    use tork::middleware::BodyLimit;

    let app = app_with_post(BodyLimit::bytes(1000));

    let chunks: Vec<Result<Frame<Bytes>, std::convert::Infallible>> = (0..5)
        .map(|_| Ok(Frame::data(Bytes::from_static(b"xxxxxxxxxx"))))
        .collect();
    let body = StreamBody::new(stream::iter(chunks));

    let req = http::Request::builder()
        .method(Method::POST)
        .uri("/")
        .header("transfer-encoding", "chunked")
        .body(box_body(body))
        .unwrap();

    let response = app.handle(req).await;
    assert_eq!(response.status(), StatusCode::OK);
}

#[tokio::test]
async fn security_headers_set_baseline_on_responses() {
    use tork::middleware::SecurityHeaders;

    let response = app_with(SecurityHeaders::new()).handle(request()).await;
    assert_eq!(response.status(), StatusCode::OK);
    let headers = response.headers();
    assert_eq!(
        headers.get("strict-transport-security").unwrap(),
        "max-age=31536000; includeSubDomains"
    );
    assert_eq!(headers.get("x-content-type-options").unwrap(), "nosniff");
    assert_eq!(headers.get("x-frame-options").unwrap(), "DENY");
    assert_eq!(headers.get("referrer-policy").unwrap(), "no-referrer");
    // CSP is off by default.
    assert!(headers.get("content-security-policy").is_none());
}

#[tokio::test]
async fn security_headers_respect_builder_and_handler_overrides() {
    use tork::middleware::SecurityHeaders;

    let mw = SecurityHeaders::new()
        .frame_options("SAMEORIGIN")
        .content_security_policy("default-src 'self'")
        .without_hsts();
    let response = app_with(mw).handle(request()).await;
    let headers = response.headers();
    assert_eq!(headers.get("x-frame-options").unwrap(), "SAMEORIGIN");
    assert_eq!(
        headers.get("content-security-policy").unwrap(),
        "default-src 'self'"
    );
    // without_hsts() drops the HSTS header entirely.
    assert!(headers.get("strict-transport-security").is_none());
}

#[tokio::test]
async fn security_headers_rejects_duplicate_registration() {
    use tork::middleware::SecurityHeaders;

    let err = App::new()
        .middleware(SecurityHeaders::new())
        .middleware(SecurityHeaders::new())
        .include_router(Router::new().route(Route::new(Method::GET, "/", ok_handler())))
        .build()
        .err()
        .expect("duplicate SecurityHeaders should be rejected");
    assert!(err.to_string().contains("SecurityHeaders"));
}

#[tokio::test]
async fn compression_sets_vary_even_when_not_compressing() {
    use tork::middleware::Compression;

    // gzip is enabled but the request does not accept it, so the response is not
    // compressed; it must still advertise `Vary: Accept-Encoding` so caches do not
    // hand it to a gzip-expecting client.
    let response = app_with(Compression::new().gzip()).handle(request()).await;

    assert!(response.headers().get("content-encoding").is_none());
    let varies = response.headers().get_all("vary").iter().any(|value| {
        value
            .to_str()
            .unwrap()
            .to_ascii_lowercase()
            .contains("accept-encoding")
    });
    assert!(varies, "expected Vary: Accept-Encoding");
}

#[tokio::test]
async fn error_responses_are_not_cacheable() {
    let app = std::sync::Arc::new(
        App::new()
            .include_router(Router::new().route(Route::new(Method::GET, "/", ok_handler())))
            .build()
            .unwrap(),
    );

    // A request to a missing route yields a 404 error response.
    let req = http::Request::builder()
        .method(Method::GET)
        .uri("/missing")
        .body(box_body(Full::new(Bytes::new())))
        .unwrap();
    let response = app.handle(req).await;

    assert_eq!(response.status(), StatusCode::NOT_FOUND);
    assert_eq!(response.headers().get("cache-control").unwrap(), "no-store");
}