camber 0.4.2

Opinionated async Rust for IO-bound services on top of Tokio
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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
//! Where a refusal is mapped, relative to the middleware around it.
//!
//! A refusal produced after a child router was selected is that child's
//! terminal: it maps at the terminal and unwinds through the frames already
//! entered outside it. A Host value that selects no child has no child chain to
//! unwind through, so none runs. The same rule governs a middleware frame's own
//! failure: it maps where it is produced, and only the frames already entered
//! outside it see the mapped response.

use crate::http as wire;
use crate::http::PathSpec;
use crate::rejection_support::{
    self as observed, COLLAPSED_STATUS, Journal, MALFORMED_JSON, Ticket, Trail,
    UNREPRESENTABLE_HEADER, mark, take,
};
use crate::runtime_support as common;

use camber::http::{
    HostRouter, Next, Rejection, RejectionContext, RejectionKind, RejectionProtocol, Request,
    Response, Router, validate,
};
use camber::{Resource, RuntimeError, runtime};
use std::net::SocketAddr;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

/// Register one middleware frame that records its entry and its unwind.
fn record_frame(router: &mut Router, trail: &Trail, enter: &'static str, exit: &'static str) {
    let trail = Arc::clone(trail);
    router.use_middleware(move |req: &Request, next: Next| {
        let trail = Arc::clone(&trail);
        mark(&trail, enter);
        let inner = next.call(req);
        async move {
            let response = inner.await;
            mark(&trail, exit);
            response
        }
    });
}

/// The host-routed fixture every ordering row shares.
///
/// The child registers `GET /items/:id` only, so a wrong or unsupported method
/// on that path is a terminal of the child's own chain rather than a miss.
fn ordering_hosts(trail: &Trail) -> HostRouter {
    let mut child = Router::new();
    record_frame(&mut child, trail, "outer:enter", "outer:exit");
    record_frame(&mut child, trail, "inner:enter", "inner:exit");
    child.get("/items/:id", |_req: &Request| async {
        Response::text(200, "item")
    });

    let mapper_trail = Arc::clone(trail);
    let child =
        child.rejection_mapper(move |rejection: &Rejection, _context: &RejectionContext| {
            mark(&mapper_trail, "child:mapper");
            Response::text(rejection.status(), rejection.message())
        });

    let host_trail = Arc::clone(trail);
    let mut hosts = HostRouter::new().rejection_mapper(
        move |rejection: &Rejection, _context: &RejectionContext| {
            mark(&host_trail, "host:mapper");
            Response::text(rejection.status(), rejection.message())
        },
    );
    hosts.add("app.test", child);
    hosts
}

/// What one ordering row sends, and the markers it must produce in order.
struct OrderingRow {
    label: &'static str,
    method: &'static str,
    host: &'static str,
    path: PathSpec,
    status: u16,
    trail: &'static [&'static str],
}

/// The markers a terminal inside the selected child's chain produces.
const CHILD_TERMINAL: &[&str] = &[
    "outer:enter",
    "inner:enter",
    "child:mapper",
    "inner:exit",
    "outer:exit",
];

/// The markers a Host that selects no child produces.
const HOST_TERMINAL: &[&str] = &["host:mapper"];

/// The target a row asks for when it means to reach no route at all.
const ORDERING_MISS: &str = "/nothing/here";

/// The routed path a row asks for when it means to reach the registered route.
const SELECTED_PATH: &str = "/items/7";

const ORDERING_ROWS: [OrderingRow; 6] = [
    OrderingRow {
        label: "malformed host",
        method: "GET",
        host: "bad host",
        path: PathSpec::Exact(ORDERING_MISS),
        status: 400,
        trail: HOST_TERMINAL,
    },
    OrderingRow {
        label: "unmatched host",
        method: "GET",
        host: "nowhere.test",
        path: PathSpec::Exact(ORDERING_MISS),
        status: 404,
        trail: HOST_TERMINAL,
    },
    OrderingRow {
        label: "URI depth",
        method: "GET",
        host: "app.test",
        path: PathSpec::Deep,
        status: 414,
        trail: CHILD_TERMINAL,
    },
    OrderingRow {
        label: "unmatched path",
        method: "GET",
        host: "app.test",
        path: PathSpec::Exact(ORDERING_MISS),
        status: 404,
        trail: CHILD_TERMINAL,
    },
    OrderingRow {
        label: "wrong method",
        method: "DELETE",
        host: "app.test",
        path: PathSpec::Exact(SELECTED_PATH),
        status: 405,
        trail: CHILD_TERMINAL,
    },
    OrderingRow {
        label: "unsupported method",
        method: "PROPFIND",
        host: "app.test",
        path: PathSpec::Exact(SELECTED_PATH),
        status: 405,
        trail: CHILD_TERMINAL,
    },
];

/// A table with no rows would drive no request and still report success.
///
/// Stated as a compile-time claim, which is the only honest place for it: the
/// length is a literal, so a runtime check could never have failed.
const _: () = assert!(!ORDERING_ROWS.is_empty());

#[test]
fn routing_stage_rejections_follow_declared_middleware_order() {
    common::test_runtime()
        .run(|| {
            let trail = Trail::default();
            let addr = common::spawn_host_server(ordering_hosts(&trail));
            take(&trail);

            let deep = wire::overdeep_path();
            for row in &ORDERING_ROWS {
                let path = row.path.resolve(&deep);
                let response = wire::send_to_host(addr, row.method, path, row.host);
                let label = row.label;

                assert_eq!(response.status, row.status, "{label}: wire status");
                assert_eq!(
                    take(&trail).as_ref(),
                    row.trail,
                    "{label}: mapping and middleware order"
                );
            }

            let accepted = wire::send_to_host(addr, "GET", SELECTED_PATH, "app.test");
            assert_eq!(accepted.status, 200);
            assert_eq!(
                take(&trail).as_ref(),
                ["outer:enter", "inner:enter", "inner:exit", "outer:exit"].as_slice(),
                "an accepted request runs the same chain with no mapper in it"
            );

            runtime::request_shutdown();
        })
        .expect("the fixture runtime ran to completion");
}

// ── Buffered producers around the middleware chain ─────────────────

/// A JSON body the validated route accepts.
const VALID_JSON: &str = "{\"id\":\"ticket-1\"}";

/// The declared client-safe message a middleware refusal carries.
const DECLARED_MESSAGE: &str = "tenant header is required";

/// The private cause a faulted middleware frame reports to operators only.
const PRIVATE_MIDDLEWARE_CAUSE: &str = "policy store unreachable";

/// The body the counting terminal answers with when it is reached.
const HANDLED_BODY: &str = "handled";

/// Send one request under the representation every JSON case here declares.
///
/// The send itself, its bound, and the sentence a send that never completed
/// fails with are `wire::send`'s. What is stated here is the one thing these
/// cases vary: the body is JSON and it says so, in one place rather than at
/// every call.
fn post_json(addr: SocketAddr, path: &str, body: &str) -> wire::HttpResponse {
    wire::send(
        addr,
        "POST",
        path,
        &[("Content-Type", "application/json")],
        body.as_bytes(),
    )
}

/// Register the middleware frame that fails with the error it is given.
fn failing_frame(router: &mut Router, error: fn() -> RuntimeError) {
    router.use_middleware(move |_req: &Request, _next: Next| {
        let refused = error();
        async move { Err::<Response, RuntimeError>(refused) }
    });
}

#[test]
fn validation_and_invalid_head_map_once_around_the_chain() {
    common::test_runtime()
        .run(|| {
            let journal = Journal::default();
            let handled = Arc::new(AtomicUsize::new(0));

            let mut router = Router::new();
            router.use_middleware(validate::json::<Ticket>());
            router.post(
                "/validated",
                observed::counting_handler(&handled, HANDLED_BODY),
            );
            router.post("/unsendable", observed::unrepresentable_handler(&handled));
            let router = router.rejection_mapper(observed::recording_mapper(&journal, "router"));
            let addr = common::spawn_server(router);
            observed::drain(&journal);

            let refused = post_json(addr, "/validated", MALFORMED_JSON);
            assert_eq!(refused.status, 400, "validation failure: wire status");
            assert_eq!(refused.text().as_ref(), "malformed request body");
            let validation = observed::only(&journal, "validation failure");
            assert_eq!(validation.kind, RejectionKind::MalformedBody);
            assert_eq!(validation.status, 400);
            assert_eq!(
                handled.load(Ordering::SeqCst),
                0,
                "a validation failure never reaches the application handler"
            );

            let unsendable = post_json(addr, "/unsendable", VALID_JSON);
            assert_eq!(unsendable.status, 500, "invalid response head: wire status");
            assert_eq!(unsendable.text().as_ref(), observed::REDACTED_BODY);
            let invalid = observed::only(&journal, "invalid response head");
            assert_eq!(invalid.kind, RejectionKind::InvalidHeader);
            assert_eq!(invalid.status, 500);
            assert_eq!(
                handled.load(Ordering::SeqCst),
                1,
                "a response Camber cannot represent is produced by the handler that ran"
            );

            runtime::request_shutdown();
        })
        .expect("the fixture runtime ran to completion");
}

/// What one middleware classification row produces, and the category it keeps.
///
/// `error` is the failure this row's frame raises, and `None` only for the row
/// whose failure the terminal itself produces. The frame reads it back by the
/// row's own `path`, which is also the path the row's route is registered under
/// and the path the row sends to: one string, so a lookup cannot find a
/// different row than the request reached.
///
/// `handler_calls` is what makes the frame's position load-bearing. Every row
/// whose frame refuses claims the refusal happened before the terminal, and
/// only the counter says whether it did.
///
/// `private` is the cause this row's producer holds and the peer must never be
/// shown. Only one producer here holds one, and a leak search handed nothing to
/// look for passes over every answer there is — so a row that holds nothing says
/// so, and the guard below the loop states how many rows are of each kind.
struct MiddlewareRow {
    label: &'static str,
    path: &'static str,
    error: Option<fn() -> RuntimeError>,
    kind: RejectionKind,
    status: u16,
    message: &'static str,
    handler_calls: usize,
    private: Option<&'static str>,
}

const MIDDLEWARE_ROWS: [MiddlewareRow; 4] = [
    MiddlewareRow {
        label: "middleware declared refusal",
        path: "/declared",
        error: Some(|| RuntimeError::BadRequest(DECLARED_MESSAGE.into())),
        kind: RejectionKind::Middleware,
        status: 400,
        message: DECLARED_MESSAGE,
        handler_calls: 0,
        private: None,
    },
    MiddlewareRow {
        label: "middleware draining service",
        path: "/draining",
        error: Some(|| RuntimeError::ScopeClosed),
        kind: RejectionKind::InternalService,
        status: 503,
        message: observed::UNAVAILABLE_BODY,
        handler_calls: 0,
        private: None,
    },
    MiddlewareRow {
        label: "middleware fault",
        path: "/faulted",
        error: Some(|| RuntimeError::Http(PRIVATE_MIDDLEWARE_CAUSE.into())),
        kind: RejectionKind::Middleware,
        status: 500,
        message: observed::REDACTED_BODY,
        handler_calls: 0,
        private: Some(PRIVATE_MIDDLEWARE_CAUSE),
    },
    MiddlewareRow {
        label: "invalid response head",
        path: "/unsendable",
        error: None,
        kind: RejectionKind::InvalidHeader,
        status: 500,
        message: observed::REDACTED_BODY,
        handler_calls: 1,
        private: None,
    },
];

/// The same claim the ordering table owes, for the table below it.
const _: () = assert!(!MIDDLEWARE_ROWS.is_empty());

/// The failure the frame raises for the row that declares this path.
///
/// A path no row declares gets no frame. No request the table drives asks for
/// one, and a probe of the fixture server that did would find no route either.
/// The fall-through cannot reach a declared row: a route is registered under
/// the same `path` its row is found by and the same one its request is sent to,
/// so a request that reached a row's route reached that row here.
fn declared_failure(path: &str) -> Option<RuntimeError> {
    MIDDLEWARE_ROWS
        .iter()
        .find(|row| row.path == path)
        .and_then(|row| row.error)
        .map(|error| error())
}

/// Serve every classification row from one router.
///
/// One frame in front of every row's terminal, raising the failure the requested
/// path declares. The rows already differ by path, so a listener per row proved
/// nothing the path does not already separate — and four servers for four
/// adjacent edge cases is what the testing strategy asks a suite not to start.
fn classification_addr(journal: &Journal, handled: &Arc<AtomicUsize>) -> SocketAddr {
    let mut router = Router::new();
    router.use_middleware(|req: &Request, next: Next| {
        let refused = declared_failure(req.path());
        // Resolved before the future, because a frame that refuses must not
        // enter the rest of the chain at all: the counter under every refusing
        // row is what says the refusal preceded the terminal.
        let inner = refused.is_none().then(|| next.call(req));
        async move {
            match refused {
                Some(error) => Err(error),
                None => Ok(inner
                    .expect("a path with no declared failure entered the rest of the chain")
                    .await),
            }
        }
    });
    for row in &MIDDLEWARE_ROWS {
        router.post(row.path, observed::unrepresentable_handler(handled));
    }
    common::spawn_server(router.rejection_mapper(observed::collapsing_mapper(
        journal,
        "router",
        COLLAPSED_STATUS,
    )))
}

#[test]
fn classification_table_keeps_middleware_kinds_distinct() {
    common::test_runtime()
        .run(|| {
            let journal = Journal::default();
            let handled = Arc::new(AtomicUsize::new(0));
            let addr = classification_addr(&journal, &handled);
            observed::drain(&journal);

            for row in &MIDDLEWARE_ROWS {
                let response = post_json(addr, row.path, VALID_JSON);
                let label = row.label;

                observed::assert_collapsed(
                    &journal,
                    &response,
                    label,
                    &observed::Collapsed {
                        kind: row.kind,
                        status: row.status,
                        message: row.message,
                    },
                );
                if let Some(private) = row.private {
                    observed::assert_no_private_text(&response, &[private], label);
                }
                // Taken rather than read, so the count a row asserts is the
                // change its own request made to the one shared terminal.
                assert_eq!(
                    handled.swap(0, Ordering::SeqCst),
                    row.handler_calls,
                    "{label}: whether the refusal preceded the terminal"
                );
            }

            // The leak search above is conditional, so the condition owes an
            // account of itself: a table where no row held a private cause would
            // search for nothing and report success, and one where every row
            // claimed the same cause would be searching three answers that never
            // carried it.
            let holding = MIDDLEWARE_ROWS
                .iter()
                .filter(|row| row.private.is_some())
                .count();
            assert_eq!(
                holding, 1,
                "one producer here holds a private cause, and its row searched for it"
            );
            assert!(
                holding < MIDDLEWARE_ROWS.len(),
                "the rows whose producers hold nothing private search for nothing"
            );

            runtime::request_shutdown();
        })
        .expect("the fixture runtime ran to completion");
}

// ── Ordinary unwind order ──────────────────────────────────────────

/// The path every ordering row below is driven through.
const UNWIND_PATH: &str = "/ordered";

/// What the inner middleware frame of one unwind row does.
#[derive(Clone, Copy)]
enum InnerFrame {
    /// Enter, call the rest of the chain, and record the unwind.
    Pass,
    /// Enter and fail, so this frame is where the refusal is produced.
    Fail,
    /// Enter and answer deliberately, without calling the rest of the chain.
    Answer,
    /// Validate the body before the rest of the chain runs.
    Validate,
}

/// What the route one unwind row reaches answers with.
#[derive(Clone, Copy)]
enum Route {
    /// A handler that answers normally.
    Handled,
    /// A handler that fails.
    Faulted,
    /// A handler whose response head Hyper cannot carry.
    Unsendable,
}

/// A mapper that records only that it ran, in the trail's own order.
///
/// Two fixtures place policy in the trail this way and answer with the safe
/// defaults; the marking itself is the harness's.
fn marking_mapper(
    trail: &Trail,
) -> impl Fn(&Rejection, &RejectionContext) -> Result<Response, RuntimeError> + Send + Sync + 'static
{
    observed::marking(
        trail,
        "mapper",
        |rejection: &Rejection, _context: &RejectionContext| {
            Response::text(rejection.status(), rejection.message())
        },
    )
}

/// Register the inner frame one unwind row declares.
fn register_inner(router: &mut Router, trail: &Trail, inner: InnerFrame) {
    match inner {
        InnerFrame::Pass => record_frame(router, trail, "inner:enter", "inner:exit"),
        InnerFrame::Fail => {
            let trail = Arc::clone(trail);
            router.use_middleware(move |_req: &Request, _next: Next| {
                mark(&trail, "inner:enter");
                async {
                    Err::<Response, RuntimeError>(RuntimeError::Http(
                        PRIVATE_MIDDLEWARE_CAUSE.into(),
                    ))
                }
            });
        }
        InnerFrame::Answer => {
            let trail = Arc::clone(trail);
            router.use_middleware(move |_req: &Request, _next: Next| {
                mark(&trail, "inner:enter");
                async { Response::text(503, "intentional middleware response") }
            });
        }
        InnerFrame::Validate => router.use_middleware(validate::json::<Ticket>()),
    }
}

/// Register the route one unwind row reaches.
fn register_route(router: &mut Router, route: Route, handled: &Arc<AtomicUsize>) {
    match route {
        Route::Handled => router.post(
            UNWIND_PATH,
            observed::counting_handler(handled, HANDLED_BODY),
        ),
        Route::Faulted => {
            let handled = Arc::clone(handled);
            router.post(UNWIND_PATH, move |_req: &Request| {
                handled.fetch_add(1, Ordering::SeqCst);
                std::future::ready(Err::<Response, RuntimeError>(RuntimeError::Http(
                    PRIVATE_MIDDLEWARE_CAUSE.into(),
                )))
            });
        }
        Route::Unsendable => router.post(UNWIND_PATH, observed::unrepresentable_handler(handled)),
    }
}

/// What one unwind row runs, and the order its participants must run in.
struct UnwindRow {
    label: &'static str,
    inner: InnerFrame,
    route: Route,
    body: &'static str,
    status: u16,
    handler_calls: usize,
    trail: &'static [&'static str],
}

const UNWIND_ROWS: [UnwindRow; 5] = [
    UnwindRow {
        label: "validation failure",
        inner: InnerFrame::Validate,
        route: Route::Handled,
        body: MALFORMED_JSON,
        status: 400,
        handler_calls: 0,
        trail: &["outer:enter", "mapper", "outer:exit"],
    },
    UnwindRow {
        label: "inner middleware error",
        inner: InnerFrame::Fail,
        route: Route::Handled,
        body: VALID_JSON,
        status: 500,
        handler_calls: 0,
        trail: &["outer:enter", "inner:enter", "mapper", "outer:exit"],
    },
    UnwindRow {
        label: "handler error",
        inner: InnerFrame::Pass,
        route: Route::Faulted,
        body: VALID_JSON,
        status: 500,
        handler_calls: 1,
        trail: &[
            "outer:enter",
            "inner:enter",
            "mapper",
            "inner:exit",
            "outer:exit",
        ],
    },
    UnwindRow {
        label: "intentional middleware response",
        inner: InnerFrame::Answer,
        route: Route::Handled,
        body: VALID_JSON,
        status: 503,
        handler_calls: 0,
        trail: &["outer:enter", "inner:enter", "outer:exit"],
    },
    UnwindRow {
        label: "post-unwind invalid response",
        inner: InnerFrame::Pass,
        route: Route::Unsendable,
        body: VALID_JSON,
        status: 500,
        handler_calls: 1,
        trail: &[
            "outer:enter",
            "inner:enter",
            "inner:exit",
            "outer:exit",
            "mapper",
        ],
    },
];

/// The same claim the two tables above owe, for the unwind table.
const _: () = assert!(!UNWIND_ROWS.is_empty());

/// Serve one unwind row behind the frames it declares.
fn unwind_addr(trail: &Trail, row: &UnwindRow, handled: &Arc<AtomicUsize>) -> SocketAddr {
    let mut router = Router::new();
    record_frame(&mut router, trail, "outer:enter", "outer:exit");
    register_inner(&mut router, trail, row.inner);
    register_route(&mut router, row.route, handled);
    common::spawn_server(router.rejection_mapper(marking_mapper(trail)))
}

/// A resource that is always well, so `/health` exists to be routed to.
struct AlwaysHealthy;

impl Resource for AlwaysHealthy {
    fn name(&self) -> &str {
        "fixture"
    }

    fn health_check(&self) -> Result<(), RuntimeError> {
        Ok(())
    }

    fn shutdown(&self) -> Result<(), RuntimeError> {
        Ok(())
    }
}

/// Serve `/health` behind one recording frame, with the bypass flag set.
fn internal_route_addr(trail: &Trail, skip: bool) -> SocketAddr {
    let mut router = Router::new();
    record_frame(&mut router, trail, "outer:enter", "outer:exit");
    common::spawn_server(
        router
            .skip_middleware_for_internal(skip)
            .rejection_mapper(marking_mapper(trail)),
    )
}

/// Serve `/health` behind a frame that refuses it, without the bypass.
///
/// Policy both takes its place in the trail and records what it was handed. The
/// internal-route case needs both: the trail states where policy ran relative to
/// the chain, and the observation states what policy was given. Asserting only
/// the first leaves the category free to be anything.
fn refused_internal_route_addr(trail: &Trail, journal: &Journal) -> SocketAddr {
    let mut router = Router::new();
    failing_frame(&mut router, || {
        RuntimeError::BadRequest(DECLARED_MESSAGE.into())
    });
    common::spawn_server(router.skip_middleware_for_internal(false).rejection_mapper(
        observed::marking(
            trail,
            "mapper",
            observed::recording_mapper(journal, "router"),
        ),
    ))
}

/// Assert the middleware an internal route runs through follows the flag.
fn assert_internal_route_ordering(trail: &Trail, journal: &Journal) {
    let wrapped = wire::request_with_host(
        internal_route_addr(trail, false),
        "GET",
        "/health",
        "localhost",
    )
    .expect("the wrapped internal route answered");
    assert_eq!(wrapped.status, 200, "wrapped internal route: wire status");
    assert_eq!(
        take(trail).as_ref(),
        ["outer:enter", "outer:exit"].as_slice(),
        "an internal route runs the chain when the bypass is off"
    );

    let bypassed = wire::request_with_host(
        internal_route_addr(trail, true),
        "GET",
        "/health",
        "localhost",
    )
    .expect("the bypassed internal route answered");
    assert_eq!(bypassed.status, 200, "bypassed internal route: wire status");
    assert!(
        take(trail).is_empty(),
        "an internal route runs no middleware when the bypass is on"
    );

    // Drained after the server is up rather than before it. A drain that ran
    // first would leave whatever the readiness probe reached policy with in the
    // journal, and the `only` below would read it as this request's own. Every
    // other site here drains in this order.
    let refused_addr = refused_internal_route_addr(trail, journal);
    observed::drain(journal);
    let refused = wire::request_with_host(refused_addr, "GET", "/health", "localhost")
        .expect("the refused internal route answered");
    assert_eq!(refused.status, 400, "refused internal route: wire status");
    assert_eq!(refused.text().as_ref(), DECLARED_MESSAGE);
    assert_eq!(
        take(trail).as_ref(),
        ["mapper"].as_slice(),
        "an internal route refusal still reaches the selected policy"
    );

    let seen = observed::only(journal, "refused internal route");
    assert_eq!(
        seen.kind,
        RejectionKind::Middleware,
        "refused internal route: the refusing frame's own category"
    );
    assert_eq!(seen.status, 400, "refused internal route: default status");
    assert_eq!(
        seen.message.as_ref(),
        DECLARED_MESSAGE,
        "refused internal route: safe message"
    );
    assert_eq!(
        seen.route.as_deref(),
        Some("/health"),
        "an internal route names the fixed identity it dispatches under"
    );
    assert_eq!(
        seen.protocol,
        Some(RejectionProtocol::OrdinaryHttp),
        "an internal route establishes its dispatch class"
    );
}

#[test]
fn ordinary_rejection_unwinds_only_entered_middleware_frames() {
    common::test_runtime()
        .resource(AlwaysHealthy)
        .run(|| {
            let trail = Trail::default();
            let journal = Journal::default();

            for row in &UNWIND_ROWS {
                let handled = Arc::new(AtomicUsize::new(0));
                let addr = unwind_addr(&trail, row, &handled);
                take(&trail);

                let response = post_json(addr, UNWIND_PATH, row.body);
                let label = row.label;

                assert_eq!(response.status, row.status, "{label}: wire status");
                assert_eq!(
                    take(&trail).as_ref(),
                    row.trail,
                    "{label}: mapping and unwind order"
                );
                assert_eq!(
                    handled.load(Ordering::SeqCst),
                    row.handler_calls,
                    "{label}: handler entry"
                );
            }

            assert_internal_route_ordering(&trail, &journal);

            runtime::request_shutdown();
        })
        .expect("the fixture runtime ran to completion");
}

/// What one profiling failure row configures and must record around mapping.
#[cfg(feature = "profiling")]
struct InternalFailureRow {
    label: &'static str,
    skip_middleware: bool,
    trail: &'static [&'static str],
}

#[cfg(feature = "profiling")]
const INTERNAL_FAILURE_ROWS: [InternalFailureRow; 2] = [
    InternalFailureRow {
        label: "wrapped internal execution failure",
        skip_middleware: false,
        trail: &["outer:enter", "mapper", "outer:exit"],
    },
    InternalFailureRow {
        label: "bypassed internal execution failure",
        skip_middleware: true,
        trail: &["mapper"],
    },
];

/// The same claim the three tables above owe, for the profiling table.
#[cfg(feature = "profiling")]
const _: () = assert!(!INTERNAL_FAILURE_ROWS.is_empty());

/// Serve the profiling route behind one frame with the declared bypass mode.
///
/// Policy takes its place in the trail and records what it was handed, while
/// collapsing every category onto one wire status: what the journal carries is
/// then the producer's own classification rather than something a reader could
/// have re-derived from the response.
#[cfg(feature = "profiling")]
fn profiling_failure_addr(trail: &Trail, journal: &Journal, skip_middleware: bool) -> SocketAddr {
    let mut router = Router::new();
    record_frame(&mut router, trail, "outer:enter", "outer:exit");
    common::spawn_server(
        router
            .skip_middleware_for_internal(skip_middleware)
            .rejection_mapper(observed::marking(
                trail,
                "mapper",
                observed::collapsing_mapper(journal, "router", COLLAPSED_STATUS),
            )),
    )
}

/// Assert the safe typed projection of one failed internal-route execution.
#[cfg(feature = "profiling")]
fn assert_internal_failure(row: &InternalFailureRow, seen: &observed::Observed) {
    let label = row.label;
    observed::assert_classification(
        seen,
        &observed::Collapsed {
            kind: RejectionKind::InternalService,
            status: 500,
            message: observed::REDACTED_BODY,
        },
        label,
    );
    assert_eq!(
        seen.route.as_deref(),
        Some("/debug/pprof/cpu"),
        "{label}: route"
    );
    assert_eq!(
        seen.protocol,
        Some(RejectionProtocol::OrdinaryHttp),
        "{label}: protocol"
    );
}

#[cfg(feature = "profiling")]
#[test]
fn internal_route_execution_failure_keeps_internal_service_classification() {
    let profiler = pprof::ProfilerGuardBuilder::default()
        .frequency(1000)
        .build()
        .expect("the fixture acquired the process profiler");

    let result = common::test_runtime().with_profiling().run(|| {
        let trail = Trail::default();
        let journal = Journal::default();
        for row in &INTERNAL_FAILURE_ROWS {
            let addr = profiling_failure_addr(&trail, &journal, row.skip_middleware);
            take(&trail);
            observed::drain(&journal);

            let response =
                wire::send_to_host(addr, "GET", "/debug/pprof/cpu?seconds=0", "localhost");
            assert_eq!(
                response.status, COLLAPSED_STATUS,
                "{}: wire status",
                row.label
            );
            assert_eq!(response.text().as_ref(), observed::REDACTED_BODY);
            assert_eq!(
                take(&trail).as_ref(),
                row.trail,
                "{}: middleware order",
                row.label
            );
            assert_internal_failure(row, &observed::only(&journal, row.label));
            observed::assert_no_private_text(&response, &["profiler"], row.label);
        }
        runtime::request_shutdown();
    });
    drop(profiler);
    result.expect("the fixture runtime ran to completion");
}

/// A mapper that counts its entries and answers with a head Hyper cannot carry.
fn unsendable_mapper(
    calls: &Arc<AtomicUsize>,
) -> impl Fn(&Rejection, &RejectionContext) -> Result<Response, RuntimeError> + Send + Sync + 'static
{
    let calls = Arc::clone(calls);
    move |_rejection: &Rejection, _context: &RejectionContext| {
        calls.fetch_add(1, Ordering::SeqCst);
        Response::text(503, "mapped")
            .map(|response| response.with_header(UNREPRESENTABLE_HEADER, "present"))
    }
}

/// An ordinary response the wire cannot carry reaches policy exactly once.
fn assert_ordinary_invalid_response_maps_once() {
    let journal = Journal::default();
    let handled = Arc::new(AtomicUsize::new(0));

    let mut router = Router::new();
    router.post(UNWIND_PATH, observed::unrepresentable_handler(&handled));
    let addr = common::spawn_server(
        router.rejection_mapper(observed::recording_mapper(&journal, "router")),
    );
    observed::drain(&journal);

    let mapped = post_json(addr, UNWIND_PATH, VALID_JSON);
    assert_eq!(mapped.status, 500, "ordinary invalid head: wire status");
    assert_eq!(
        mapped.text().as_ref(),
        observed::REDACTED_BODY,
        "ordinary invalid head: wire body"
    );
    // `only` is the once: it fails unless exactly one mapper invocation was
    // recorded for this refusal.
    let seen = observed::only(&journal, "ordinary invalid head");
    assert_eq!(seen.kind, RejectionKind::InvalidHeader, "category");
    // The head Camber cannot represent is the handler's own, so the refusal is
    // only about a terminal that ran. A route that never dispatched would
    // produce the same category from nothing at all.
    assert_eq!(
        handled.load(Ordering::SeqCst),
        1,
        "the terminal that produced the unsendable head ran"
    );
}

/// A response policy itself cannot represent uses the fixed fallback instead.
fn assert_invalid_mapper_response_falls_back() {
    let handled = Arc::new(AtomicUsize::new(0));
    let entered = Arc::new(AtomicUsize::new(0));
    let mapper_calls = Arc::new(AtomicUsize::new(0));

    let mut router = Router::new();
    let counted = Arc::clone(&entered);
    router.use_middleware(move |req: &Request, next: Next| {
        counted.fetch_add(1, Ordering::SeqCst);
        next.call(req)
    });
    router.post(UNWIND_PATH, observed::unrepresentable_handler(&handled));
    let addr = common::spawn_server(router.rejection_mapper(unsendable_mapper(&mapper_calls)));

    let fell_back = post_json(addr, UNWIND_PATH, VALID_JSON);
    observed::assert_fixed_fallback(&fell_back, "mapper invalid head");
    assert_eq!(
        mapper_calls.load(Ordering::SeqCst),
        1,
        "the fixed fallback never calls a mapper again"
    );
    assert_eq!(
        entered.load(Ordering::SeqCst),
        1,
        "the fixed fallback does not re-enter middleware"
    );
    assert_eq!(
        handled.load(Ordering::SeqCst),
        1,
        "the fixed fallback does not re-enter the terminal either"
    );
}

#[test]
fn ordinary_invalid_response_maps_once_but_invalid_mapper_response_falls_back() {
    common::test_runtime()
        .run(|| {
            assert_ordinary_invalid_response_maps_once();
            assert_invalid_mapper_response_falls_back();

            runtime::request_shutdown();
        })
        .expect("the fixture runtime ran to completion");
}