guard-core-rs 4.3.3

Framework-agnostic security engine for the Guard ecosystem.
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
//! The required-headers and authentication pipeline stage for tower stacks.
//!
//! One `tower::Layer` that answers the reference engine's
//! `required_headers` check
//! (`guard_core/core/checks/implementations/required_headers.py`) and
//! `authentication` check
//! (`guard_core/core/checks/implementations/authentication.py`) in the
//! pipeline's order, over the decision core
//! [`guard_core_engine::headers_auth`]:
//!
//! ```text
//! route resolves to no rules (or no resolver):   pass through
//! first failing required-header rule:            400 "Missing required header: {name}"
//!                                                400 "Header '{n}' does not match the required value"
//! any authentication failure:                    401 "Authentication required"
//! everything else:                               pass through
//! ```
//!
//! ## Reference contract, point by point
//!
//! - **Route-scoped rules, global verifier fallback**: the rules live on
//!   the reference `RouteConfig`; the stage learns them through the
//!   [`RouteGuards`] resolver (`path -> Option<RouteGuard>`, the tower
//!   counterpart of `request.state.route_config`). The global
//!   `config.auth_verifier` is the stage-level fallback each route's
//!   `auth_verifier` / `api_key_verifier` overrides.
//! - **Order**: required headers run before authentication, and the stage
//!   sits where the reference pipeline runs them: after the
//!   request-size/content stage, before the IP-visible stages.
//! - **Bodies**: the required-header blocks answer the reference's dynamic
//!   default messages; every authentication failure answers the fixed
//!   `401 "Authentication required"` (the reasons are observability-only
//!   in the reference too; the [`GuardViolation`] on each block carries
//!   the reference `violation_type`).
//! - **Not mirrored**: the reference logs via `log_activity`, emits
//!   `EVENT_DECORATOR_VIOLATION` events, honors `passive_mode`, consults
//!   `custom_error_responses` for body overrides, and on success stores
//!   `request.state.auth_principal` (the verifier returns a principal;
//!   this port's verifier is a predicate and the stage discards the
//!   outcome). None of those config surfaces exist in the Rust family yet.
//!
//! # Example
//!
//! ```
//! use std::sync::Arc;
//!
//! use guard_core_engine::headers_auth::{
//!     HeaderAuthRules, RequiredHeader, REQUIRED_SENTINEL,
//! };
//! use guard_core_rs::headers_auth::{HeadersAuthStage, RouteGuard};
//!
//! let stage = HeadersAuthStage::new(
//!     None,
//!     Arc::new(|path| {
//!         (path == "/admin").then(|| Arc::new(RouteGuard {
//!             rules: HeaderAuthRules {
//!                 required_headers: vec![RequiredHeader {
//!                     name: "X-Request-ID".to_owned(),
//!                     expected: REQUIRED_SENTINEL.to_owned(),
//!                 }],
//!                 authorization_header_required: Some("bearer".to_owned()),
//!                 ..HeaderAuthRules::default()
//!             },
//!             verifier: None,
//!             api_key_verifier: None,
//!         }))
//!     }),
//! );
//!
//! let passing = stage
//!     .decide("/admin", &[("x-request-id", "abc"), ("authorization", "Bearer tok")])
//!     .map(|(_, block)| block);
//! assert!(passing.is_none());
//!
//! let (_, block) = stage
//!     .decide("/admin", &[("authorization", "Bearer tok")])
//!     .expect("missing header");
//! assert_eq!(block.status, 400);
//! assert_eq!(block.body, "Missing required header: X-Request-ID");
//!
//! let (_, block) = stage
//!     .decide("/admin", &[("x-request-id", "abc")])
//!     .expect("auth failure");
//! assert_eq!(block.status, 401);
//! assert_eq!(block.body, "Authentication required");
//! ```

use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use ::tower::Layer;
use http::{Request, Response, StatusCode};

pub use guard_core_engine::headers_auth::{
    AuthVerifier, GuardBlock, GuardViolation, HeaderAuthRules, REQUIRED_SENTINEL, RequiredHeader,
    RouteVerifiers, decide as decide_guard, extract_credential,
};

/// The fixed answer body of every authentication failure
/// (`AuthenticationCheck`'s default message).
pub const AUTHENTICATION_REQUIRED_BODY: &str = "Authentication required";

/// One route's rules plus its verifier overrides: the reference
/// `RouteConfig.auth_verifier` / `RouteConfig.api_key_verifier` pair, which
/// override the stage-level `auth_verifier` fallback.
#[derive(Clone, Default)]
pub struct RouteGuard {
    /// The route's header and authentication rules.
    pub rules: HeaderAuthRules,
    /// `RouteConfig.auth_verifier` (the `auth_required` path).
    pub verifier: Option<AuthVerifier>,
    /// `RouteConfig.api_key_verifier` (the `api_key_required` path).
    pub api_key_verifier: Option<AuthVerifier>,
}

/// How the stage learns a path's guard rules.
///
/// The reference reads them from `request.state.route_config`; a tower
/// stack resolves them from the request path. `None` (no entry) means the
/// route carries no rules and passes, the reference's no-decorator shape.
pub type RouteGuards = Arc<dyn Fn(&str) -> Option<Arc<RouteGuard>> + Send + Sync>;

/// The stage's answer: status plus the (dynamic) reference default message
/// body.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StageAnswer {
    /// `400` for the required-header shapes, `401` for authentication.
    pub status: StatusCode,
    /// The reference default message body.
    pub body: String,
}

/// The required-headers and authentication stage over one route resolver.
///
/// Build it once at startup with [`HeadersAuthStage::new`] and install it
/// with [`HeadersAuthStageLayer`]. The stage is cheaply clonable; the
/// resolver and the global verifier are shared.
#[derive(Clone)]
pub struct HeadersAuthStage {
    global_verifier: Option<AuthVerifier>,
    routes: RouteGuards,
}

impl fmt::Debug for HeadersAuthStage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("HeadersAuthStage")
            .field("global_verifier", &self.global_verifier.is_some())
            .finish_non_exhaustive()
    }
}

impl HeadersAuthStage {
    /// Build the stage over `routes` with the global `auth_verifier`
    /// fallback (`SecurityConfig.auth_verifier`; `None` when unset).
    #[must_use]
    pub fn new(global_verifier: Option<AuthVerifier>, routes: RouteGuards) -> Self {
        Self {
            global_verifier,
            routes,
        }
    }

    /// One pass of the stage: `path` selects the route guard and `headers`
    /// carries the request headers (looked up case-insensitively). `None`
    /// passes; `Some` carries the status, the reference default message
    /// body, and the violation type.
    #[must_use]
    pub fn decide(
        &self,
        path: &str,
        headers: &[(&str, &str)],
    ) -> Option<(GuardViolation, StageAnswer)> {
        let guard = (self.routes)(path)?;
        let verifiers = RouteVerifiers {
            auth: guard
                .verifier
                .clone()
                .or_else(|| self.global_verifier.clone()),
            api_key: guard
                .api_key_verifier
                .clone()
                .or_else(|| self.global_verifier.clone()),
        };
        let lookup = |name: &str| {
            headers
                .iter()
                .find(|(key, _)| key.eq_ignore_ascii_case(name))
                .map(|(_, value)| (*value).to_owned())
        };
        decide_guard(&guard.rules, &verifiers, lookup).map(|block| {
            (
                block.violation,
                StageAnswer {
                    status: StatusCode::from_u16(block.status).expect("reference statuses"),
                    body: block.body,
                },
            )
        })
    }
}

/// The `tower::Layer` carrying [`HeadersAuthStage`].
///
/// Wrap any `Service<http::Request<B>>` whose responses are
/// `http::Response<ResBody>` with `ResBody: From<String>` (Axum's body and
/// `http_body_util::Full<Bytes>` qualify) and the stage answers the
/// reference's block shapes itself.
#[derive(Clone)]
pub struct HeadersAuthStageLayer {
    stage: HeadersAuthStage,
}

impl HeadersAuthStageLayer {
    /// Carry `stage` into every service this layer wraps.
    #[must_use]
    pub const fn new(stage: HeadersAuthStage) -> Self {
        Self { stage }
    }
}

impl fmt::Debug for HeadersAuthStageLayer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("HeadersAuthStageLayer")
            .field("stage", &self.stage)
            .finish()
    }
}

impl<S> Layer<S> for HeadersAuthStageLayer {
    type Service = HeadersAuthStageService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        HeadersAuthStageService {
            inner,
            stage: self.stage.clone(),
        }
    }
}

/// The stage as a `tower::Service` around the inner service it wrapped.
#[derive(Clone)]
pub struct HeadersAuthStageService<S> {
    inner: S,
    stage: HeadersAuthStage,
}

impl<S, B, ResBody> ::tower::Service<Request<B>> for HeadersAuthStageService<S>
where
    S: ::tower::Service<Request<B>, Response = Response<ResBody>>,
    S::Future: Send + 'static,
    ResBody: From<String> + From<&'static str> + Send + 'static,
{
    type Response = S::Response;
    type Error = S::Error;
    type Future = Pin<Box<dyn Future<Output = Result<S::Response, S::Error>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), S::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, request: Request<B>) -> Self::Future {
        let path = request.uri().path().to_owned();
        let map = request.headers().clone();
        let pairs: Vec<(String, String)> = map
            .iter()
            .filter_map(|(name, value)| {
                value
                    .to_str()
                    .ok()
                    .map(|value| (name.as_str().to_owned(), value.to_owned()))
            })
            .collect();
        let borrowed: Vec<(&str, &str)> = pairs
            .iter()
            .map(|(name, value)| (name.as_str(), value.as_str()))
            .collect();
        if let Some((_, answer)) = self.stage.decide(&path, &borrowed) {
            let response = render(answer);
            return Box::pin(async move { Ok(response) });
        }
        let future = self.inner.call(request);
        Box::pin(future)
    }
}

/// Render the stage's answer into the wrapped service's response body type.
fn render<ResBody: From<String>>(answer: StageAnswer) -> Response<ResBody> {
    let mut response = Response::new(ResBody::from(answer.body));
    *response.status_mut() = answer.status;
    response
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::convert::Infallible;
    use std::sync::atomic::{AtomicUsize, Ordering};

    use ::tower::{Service, ServiceBuilder};

    const HEADER_ONLY: HeaderAuthRules = HeaderAuthRules {
        required_headers: Vec::new(),
        auth_required: None,
        api_key_required: false,
        authorization_header_required: None,
        api_key_header: None,
    };

    fn admin_guard(verifier: Option<AuthVerifier>) -> RouteGuards {
        Arc::new(move |path: &str| {
            (path == "/admin").then(|| {
                Arc::new(RouteGuard {
                    rules: HeaderAuthRules {
                        required_headers: vec![RequiredHeader {
                            name: "X-Request-ID".to_owned(),
                            expected: REQUIRED_SENTINEL.to_owned(),
                        }],
                        auth_required: Some("bearer".to_owned()),
                        ..HEADER_ONLY
                    },
                    verifier: verifier.clone(),
                    api_key_verifier: None,
                })
            })
        })
    }

    fn presence_guard() -> RouteGuards {
        Arc::new(|path: &str| {
            (path == "/admin").then(|| {
                Arc::new(RouteGuard {
                    rules: HeaderAuthRules {
                        required_headers: vec![RequiredHeader {
                            name: "X-Request-ID".to_owned(),
                            expected: REQUIRED_SENTINEL.to_owned(),
                        }],
                        authorization_header_required: Some("bearer".to_owned()),
                        ..HEADER_ONLY
                    },
                    verifier: None,
                    api_key_verifier: None,
                })
            })
        })
    }

    fn stage_with(routes: RouteGuards) -> HeadersAuthStage {
        HeadersAuthStage::new(None, routes)
    }

    #[test]
    fn unconfigured_paths_pass_everything() {
        let stage = stage_with(admin_guard(None));
        assert!(stage.decide("/public", &[]).is_none());
        assert!(
            stage
                .decide("/public", &[("authorization", "junk")])
                .is_none()
        );
    }

    #[test]
    fn required_header_blocks_use_the_dynamic_reference_bodies() {
        let stage = stage_with(admin_guard(None));

        let (violation, answer) = stage.decide("/admin", &[]).expect("missing header");
        assert_eq!(violation, GuardViolation::MissingHeader);
        assert_eq!(answer.status, StatusCode::BAD_REQUEST);
        assert_eq!(answer.body, "Missing required header: X-Request-ID");

        let (violation, answer) = stage
            .decide(
                "/admin",
                &[("x-request-id", ""), ("authorization", "Bearer t")],
            )
            .expect("empty header");
        assert_eq!(violation, GuardViolation::MissingHeader);
        assert_eq!(answer.body, "Missing required header: X-Request-ID");

        let (violation, answer) = stage
            .decide(
                "/admin",
                &[("X-Request-ID", "abc"), ("authorization", "junk")],
            )
            .expect("auth failure");
        assert_eq!(violation, GuardViolation::RequireAuth);
        assert_eq!(answer.status, StatusCode::UNAUTHORIZED);
        assert_eq!(answer.body, AUTHENTICATION_REQUIRED_BODY);
    }

    #[test]
    fn presence_scheme_short_circuits_the_verifier_paths() {
        // A route guard with only a bearer presence rule and no verifier
        // passes a well-formed header without consulting any verifier...
        let stage = stage_with(presence_guard());
        assert!(
            stage
                .decide(
                    "/admin",
                    &[
                        ("x-request-id", "abc"),
                        ("authorization", "Bearer anything")
                    ]
                )
                .is_none()
        );
        // ...while a malformed one fails with the presence violation.
        let (violation, answer) = stage
            .decide(
                "/admin",
                &[("x-request-id", "abc"), ("authorization", "junk")],
            )
            .expect("presence failure");
        assert_eq!(violation, GuardViolation::AuthorizationHeader);
        assert_eq!(answer.status, StatusCode::UNAUTHORIZED);
        let _ = stage_with(admin_guard(None));
    }

    #[test]
    fn a_route_verifier_overrides_the_global_fallback() {
        // Route verifier accepts only "good"; the global fallback accepts
        // everything. A "good" credential passes, proving the route
        // verifier won (the global one accepts either way, so also check
        // the rejection side below).
        let stage = HeadersAuthStage::new(
            Some(Arc::new(|_| true)),
            admin_guard(Some(Arc::new(|credential| credential == "good"))),
        );
        assert!(
            stage
                .decide(
                    "/admin",
                    &[("x-request-id", "a"), ("authorization", "Bearer good")]
                )
                .is_none()
        );
        let (_, answer) = stage
            .decide(
                "/admin",
                &[("x-request-id", "a"), ("authorization", "Bearer other")],
            )
            .expect("route verifier rejected");
        assert_eq!(answer.status, StatusCode::UNAUTHORIZED);

        // Without a route verifier the global fallback decides.
        let stage = HeadersAuthStage::new(
            Some(Arc::new(|credential| credential == "global")),
            admin_guard(None),
        );
        assert!(
            stage
                .decide(
                    "/admin",
                    &[("x-request-id", "a"), ("authorization", "Bearer global")]
                )
                .is_none()
        );
        assert!(
            stage
                .decide(
                    "/admin",
                    &[("x-request-id", "a"), ("authorization", "Bearer nope")]
                )
                .is_some()
        );
    }

    #[test]
    fn an_api_key_route_reads_its_header_and_verifier() {
        let stage = HeadersAuthStage::new(
            None,
            Arc::new(|path: &str| {
                (path == "/ingest").then(|| {
                    Arc::new(RouteGuard {
                        rules: HeaderAuthRules {
                            api_key_required: true,
                            api_key_header: Some("X-Key".to_owned()),
                            ..HEADER_ONLY
                        },
                        verifier: None,
                        api_key_verifier: Some(Arc::new(|credential| credential == "k-1")),
                    })
                })
            }),
        );
        assert!(stage.decide("/ingest", &[("X-Key", "k-1")]).is_none());
        let (violation, answer) = stage.decide("/ingest", &[]).expect("missing key");
        assert_eq!(violation, GuardViolation::RequireAuth);
        assert_eq!(answer.status, StatusCode::UNAUTHORIZED);
        let (_, answer) = stage
            .decide("/ingest", &[("X-Key", "nope")])
            .expect("rejected");
        assert_eq!(answer.body, AUTHENTICATION_REQUIRED_BODY);
    }

    /// The future the plumbing tests drive: the stub futures are always
    /// immediately ready, so a noop-waker spin never spins.
    fn block_on<F: Future>(future: F) -> F::Output {
        let mut future = std::pin::pin!(future);
        let waker = std::task::Waker::noop();
        let mut cx = Context::from_waker(waker);
        loop {
            match future.as_mut().poll(&mut cx) {
                Poll::Ready(output) => return output,
                Poll::Pending => std::hint::spin_loop(),
            }
        }
    }

    /// The inner service the plumbing tests wrap: counts its calls and
    /// answers `200 "inner"`.
    #[derive(Clone)]
    struct Inner {
        calls: Arc<AtomicUsize>,
    }

    impl Inner {
        fn new() -> Self {
            Self {
                calls: Arc::new(AtomicUsize::new(0)),
            }
        }

        fn call_count(&self) -> usize {
            self.calls.load(Ordering::Relaxed)
        }
    }

    impl ::tower::Service<Request<&'static str>> for Inner {
        type Response = Response<String>;
        type Error = Infallible;
        type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Infallible>> + Send>>;

        fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
            Poll::Ready(Ok(()))
        }

        fn call(&mut self, request: Request<&'static str>) -> Self::Future {
            self.calls.fetch_add(1, Ordering::Relaxed);
            drop(request);
            Box::pin(async move { Ok(Response::new("inner".to_owned())) })
        }
    }

    fn request_to(path: &str, headers: &[(&str, &str)]) -> Request<&'static str> {
        let mut builder = Request::builder().uri(path);
        for (name, value) in headers {
            builder = builder.header(*name, *value);
        }
        builder.body("body").expect("request")
    }

    #[test]
    fn layer_passes_through_and_the_inner_service_answers() {
        // The presence guard passes a well-formed request without any
        // verifier configured.
        let layer = HeadersAuthStageLayer::new(stage_with(presence_guard()));
        let inner = Inner::new();
        let mut service = ServiceBuilder::new().layer(layer).service(inner.clone());

        let response = block_on(service.call(request_to(
            "/admin",
            &[("x-request-id", "abc"), ("authorization", "Bearer tok")],
        )))
        .expect("ready");
        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(response.body(), "inner");
        assert_eq!(inner.call_count(), 1);
    }

    #[test]
    fn layer_answers_the_400_without_hitting_the_inner_service() {
        let layer = HeadersAuthStageLayer::new(stage_with(presence_guard()));
        let inner = Inner::new();
        let mut service = ServiceBuilder::new().layer(layer).service(inner.clone());

        let response =
            block_on(service.call(request_to("/admin", &[("authorization", "Bearer tok")])))
                .expect("ready");
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
        assert_eq!(response.body(), "Missing required header: X-Request-ID");
        assert_eq!(inner.call_count(), 0, "the blocked request never hits it");
    }

    #[test]
    fn layer_answers_the_401_for_an_auth_failure() {
        let layer = HeadersAuthStageLayer::new(stage_with(admin_guard(None)));
        let inner = Inner::new();
        let mut service = ServiceBuilder::new().layer(layer).service(inner.clone());

        let response = block_on(service.call(request_to("/admin", &[("x-request-id", "abc")])))
            .expect("ready");
        assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
        assert_eq!(response.body(), AUTHENTICATION_REQUIRED_BODY);
        assert_eq!(inner.call_count(), 0);
    }

    #[test]
    fn layer_clone_shares_the_resolver() {
        let stage = stage_with(admin_guard(None));
        let mut first = HeadersAuthStageService {
            inner: Inner::new(),
            stage: stage.clone(),
        };
        let mut second = HeadersAuthStageService {
            inner: Inner::new(),
            stage,
        };

        let response =
            block_on(first.call(request_to("/admin", &[("x-request-id", "abc")]))).expect("ready");
        assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
        let response =
            block_on(second.call(request_to("/admin", &[("x-request-id", "abc")]))).expect("ready");
        assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
    }

    #[test]
    fn route_verifiers_debug_renders() {
        let verifiers = RouteVerifiers {
            auth: None,
            api_key: None,
        };
        assert!(format!("{verifiers:?}").contains("RouteVerifiers"));
    }

    #[test]
    fn stage_and_layer_debug_render_their_names() {
        let stage = stage_with(admin_guard(None));
        assert!(format!("{stage:?}").contains("HeadersAuthStage"));
        let layer = HeadersAuthStageLayer::new(stage);
        assert!(format!("{layer:?}").contains("HeadersAuthStageLayer"));
    }

    #[test]
    fn block_on_drives_a_pending_once_future() {
        struct PendingOnce {
            polled: std::sync::Arc<std::sync::atomic::AtomicBool>,
        }
        impl Future for PendingOnce {
            type Output = ();
            fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
                if !self.polled.swap(true, std::sync::atomic::Ordering::Relaxed) {
                    cx.waker().wake_by_ref();
                    return Poll::Pending;
                }
                Poll::Ready(())
            }
        }
        let polled = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
        block_on(PendingOnce {
            polled: std::sync::Arc::clone(&polled),
        });
    }

    #[test]
    fn the_layer_service_polls_ready() {
        let layer = HeadersAuthStageLayer::new(stage_with(admin_guard(None)));
        let mut service = ::tower::ServiceBuilder::new()
            .layer(layer)
            .service(Inner::new());
        let waker = std::task::Waker::noop();
        let mut cx = Context::from_waker(waker);
        let polled = ::tower::Service::<Request<&'static str>>::poll_ready(&mut service, &mut cx);
        assert!(polled.is_ready());
        assert_eq!(polled.map(|result| result.is_ok()), Poll::Ready(true));
    }
}