http-handle 0.0.5

A fast and lightweight Rust library for handling HTTP requests and responses.
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
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (c) 2023 - 2026 HTTP Handle

//! HTTP/3 production profile primitives.
//!
//! This module defines ALPN routing and fallback policy helpers so deployments
//! can enforce deterministic protocol behavior when HTTP/3 is enabled.

/// Effective protocol route selected after ALPN negotiation.
/// # Examples
///
/// ```rust
/// use http_handle::http3_profile::ProtocolRoute;
/// assert_eq!(ProtocolRoute::Http2.to_string(), "h2");
/// ```
///
/// # Panics
///
/// This type does not panic.
#[cfg(feature = "http3-profile")]
#[cfg_attr(docsrs, doc(cfg(feature = "http3-profile")))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ProtocolRoute {
    /// Use HTTP/3 over QUIC.
    Http3,
    /// Fallback to HTTP/2.
    Http2,
    /// Fallback to HTTP/1.1.
    Http11,
}

/// Runtime QUIC tuning preset.
/// # Examples
///
/// ```rust
/// use http_handle::http3_profile::QuicTuningPreset;
/// assert!(matches!(QuicTuningPreset::Balanced, QuicTuningPreset::Balanced));
/// ```
///
/// # Panics
///
/// This type does not panic.
#[cfg(feature = "http3-profile")]
#[cfg_attr(docsrs, doc(cfg(feature = "http3-profile")))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum QuicTuningPreset {
    /// Lower resource use and conservative timeouts.
    Conservative,
    /// Balanced defaults for general production use.
    Balanced,
    /// Throughput-biased tuning for high-capacity edge deployments.
    Aggressive,
}

/// Derived QUIC runtime tuning values.
/// # Examples
///
/// ```rust
/// use http_handle::http3_profile::QuicTuning;
/// let t = QuicTuning { idle_timeout_ms: 1, keep_alive_interval_ms: 1, max_bidi_streams: 1, datagram_receive_buffer_bytes: 1 };
/// assert_eq!(t.max_bidi_streams, 1);
/// ```
///
/// # Panics
///
/// This type does not panic.
#[cfg(feature = "http3-profile")]
#[cfg_attr(docsrs, doc(cfg(feature = "http3-profile")))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct QuicTuning {
    /// QUIC idle timeout in milliseconds.
    pub idle_timeout_ms: u64,
    /// Keep-alive probe interval in milliseconds.
    pub keep_alive_interval_ms: u64,
    /// Max concurrent bidirectional streams.
    pub max_bidi_streams: u64,
    /// Datagram receive buffer target in bytes.
    pub datagram_receive_buffer_bytes: usize,
}

/// Reason describing how route selection was resolved.
/// # Examples
///
/// ```rust
/// use http_handle::http3_profile::RouteReason;
/// assert_eq!(RouteReason::Negotiated.to_string(), "negotiated");
/// ```
///
/// # Panics
///
/// This type does not panic.
#[cfg(feature = "http3-profile")]
#[cfg_attr(docsrs, doc(cfg(feature = "http3-profile")))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RouteReason {
    /// Standard negotiated protocol route.
    Negotiated,
    /// HTTP/3 profile disabled.
    H3Disabled,
    /// ALPN missing during negotiation.
    AlpnMissing,
    /// ALPN provided but not recognized.
    AlpnUnsupported,
    /// H3 handshake failed and fallback was applied.
    H3HandshakeFailedFallback,
    /// H3 handshake failed and fallback is disabled.
    H3HandshakeFailedNoFallback,
}

/// Decision output for ALPN+fallback route resolution.
/// # Examples
///
/// ```rust
/// use http_handle::http3_profile::{ProtocolRoute, RouteDecision, RouteReason};
/// let d = RouteDecision { selected: ProtocolRoute::Http11, reason: RouteReason::AlpnMissing, negotiated_alpn: None, fallback_chain: vec![ProtocolRoute::Http11] };
/// assert_eq!(d.selected, ProtocolRoute::Http11);
/// ```
///
/// # Panics
///
/// This type does not panic.
#[cfg(feature = "http3-profile")]
#[cfg_attr(docsrs, doc(cfg(feature = "http3-profile")))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RouteDecision {
    /// Final selected route.
    pub selected: ProtocolRoute,
    /// Resolution reason.
    pub reason: RouteReason,
    /// Raw negotiated ALPN token if present.
    pub negotiated_alpn: Option<String>,
    /// Ordered chain considered for fallback.
    pub fallback_chain: Vec<ProtocolRoute>,
}

/// Production-focused HTTP/3 configuration profile.
/// # Examples
///
/// ```rust
/// use http_handle::http3_profile::Http3ProductionProfile;
/// let p = Http3ProductionProfile::default();
/// assert!(p.enabled);
/// ```
///
/// # Panics
///
/// This type does not panic.
#[cfg(feature = "http3-profile")]
#[cfg_attr(docsrs, doc(cfg(feature = "http3-profile")))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Http3ProductionProfile {
    /// Whether HTTP/3 is enabled.
    pub enabled: bool,
    /// Ordered ALPN preference, e.g. `["h3", "h2", "http/1.1"]`.
    pub alpn_order: Vec<String>,
    /// QUIC idle timeout in milliseconds.
    pub quic_idle_timeout_ms: u64,
    /// QUIC tuning preset.
    pub quic_preset: QuicTuningPreset,
    /// Accept draft h3 tokens (for example `h3-29`) as h3 route.
    pub allow_h3_draft: bool,
    /// Whether failed HTTP/3 handshakes should fallback to H2/H1.
    pub fallback_on_h3_error: bool,
}

#[cfg(feature = "http3-profile")]
#[cfg_attr(docsrs, doc(cfg(feature = "http3-profile")))]
impl Default for Http3ProductionProfile {
    fn default() -> Self {
        Self {
            enabled: true,
            alpn_order: vec![
                "h3".to_string(),
                "h2".to_string(),
                "http/1.1".to_string(),
            ],
            quic_idle_timeout_ms: 30_000,
            quic_preset: QuicTuningPreset::Balanced,
            allow_h3_draft: true,
            fallback_on_h3_error: true,
        }
    }
}

#[cfg(feature = "http3-profile")]
#[cfg_attr(docsrs, doc(cfg(feature = "http3-profile")))]
impl Http3ProductionProfile {
    /// Returns a strict production baseline with h3-first ALPN ordering.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_handle::http3_profile::Http3ProductionProfile;
    /// let p = Http3ProductionProfile::production_baseline();
    /// assert!(p.enabled);
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn production_baseline() -> Self {
        Self::default()
    }

    /// Returns effective QUIC tuning values from preset and profile.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_handle::http3_profile::Http3ProductionProfile;
    /// let p = Http3ProductionProfile::default();
    /// let t = p.quic_tuning();
    /// assert!(t.idle_timeout_ms > 0);
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn quic_tuning(&self) -> QuicTuning {
        match self.quic_preset {
            QuicTuningPreset::Conservative => QuicTuning {
                idle_timeout_ms: self.quic_idle_timeout_ms.max(45_000),
                keep_alive_interval_ms: 15_000,
                max_bidi_streams: 64,
                datagram_receive_buffer_bytes: 512 * 1024,
            },
            QuicTuningPreset::Balanced => QuicTuning {
                idle_timeout_ms: self.quic_idle_timeout_ms.max(30_000),
                keep_alive_interval_ms: 10_000,
                max_bidi_streams: 128,
                datagram_receive_buffer_bytes: 1024 * 1024,
            },
            QuicTuningPreset::Aggressive => QuicTuning {
                idle_timeout_ms: self.quic_idle_timeout_ms.max(20_000),
                keep_alive_interval_ms: 8_000,
                max_bidi_streams: 256,
                datagram_receive_buffer_bytes: 2 * 1024 * 1024,
            },
        }
    }

    /// Derives the serving route from negotiated ALPN protocol bytes.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_handle::http3_profile::{Http3ProductionProfile, ProtocolRoute};
    /// let p = Http3ProductionProfile::default();
    /// assert_eq!(p.route_for_alpn(Some(b"h3")), ProtocolRoute::Http3);
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn route_for_alpn(
        &self,
        negotiated_alpn: Option<&[u8]>,
    ) -> ProtocolRoute {
        if !self.enabled {
            return ProtocolRoute::Http11;
        }
        match negotiated_alpn {
            Some(b"h3") => ProtocolRoute::Http3,
            Some(b"h2") => ProtocolRoute::Http2,
            Some(b"http/1.1") => ProtocolRoute::Http11,
            Some(raw)
                if self.allow_h3_draft
                    && std::str::from_utf8(raw)
                        .map(|v| v.starts_with("h3-"))
                        .unwrap_or(false) =>
            {
                ProtocolRoute::Http3
            }
            _ => ProtocolRoute::Http11,
        }
    }

    /// Selects a route from offered client ALPN tokens and server preference.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_handle::http3_profile::{Http3ProductionProfile, ProtocolRoute};
    /// let p = Http3ProductionProfile::default();
    /// let offered = vec![b"h2".to_vec()];
    /// assert_eq!(p.route_for_client_alpns(&offered), ProtocolRoute::Http2);
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn route_for_client_alpns(
        &self,
        client_offered_alpns: &[Vec<u8>],
    ) -> ProtocolRoute {
        if !self.enabled {
            return ProtocolRoute::Http11;
        }
        let offered = client_offered_alpns
            .iter()
            .map(|v| self.route_for_alpn(Some(v)).to_string())
            .collect::<Vec<_>>();
        for preferred in self.fallback_chain() {
            if offered.iter().any(|v| v == &preferred.to_string()) {
                return preferred;
            }
        }
        ProtocolRoute::Http11
    }

    /// Returns ordered protocol fallback chain.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_handle::http3_profile::Http3ProductionProfile;
    /// let p = Http3ProductionProfile::default();
    /// assert!(!p.fallback_chain().is_empty());
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn fallback_chain(&self) -> Vec<ProtocolRoute> {
        let mut chain = Vec::new();
        for protocol in &self.alpn_order {
            let route = match protocol.as_str() {
                "h3" => ProtocolRoute::Http3,
                "h2" => ProtocolRoute::Http2,
                "http/1.1" => ProtocolRoute::Http11,
                _ => continue,
            };
            if !chain.contains(&route) {
                chain.push(route);
            }
        }
        if chain.is_empty() {
            chain.push(ProtocolRoute::Http11);
        }
        chain
    }

    /// Resolves final route with explicit fallback decision tree and reason.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_handle::http3_profile::{Http3ProductionProfile, RouteReason};
    /// let p = Http3ProductionProfile::default();
    /// let d = p.resolve_route(Some(b"h3"), false);
    /// assert!(matches!(d.reason, RouteReason::H3HandshakeFailedFallback));
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn resolve_route(
        &self,
        negotiated_alpn: Option<&[u8]>,
        h3_handshake_ok: bool,
    ) -> RouteDecision {
        let chain = self.fallback_chain();
        let negotiated = negotiated_alpn
            .map(|v| String::from_utf8_lossy(v).to_string());
        let mut selected = self.route_for_alpn(negotiated_alpn);
        let mut reason = if !self.enabled {
            RouteReason::H3Disabled
        } else {
            match negotiated_alpn {
                None => RouteReason::AlpnMissing,
                Some(b"h3") | Some(b"h2") | Some(b"http/1.1") => {
                    RouteReason::Negotiated
                }
                Some(raw)
                    if self.allow_h3_draft
                        && std::str::from_utf8(raw)
                            .map(|v| v.starts_with("h3-"))
                            .unwrap_or(false) =>
                {
                    RouteReason::Negotiated
                }
                Some(_) => RouteReason::AlpnUnsupported,
            }
        };

        if selected == ProtocolRoute::Http3 && !h3_handshake_ok {
            if self.fallback_on_h3_error {
                selected = chain
                    .iter()
                    .copied()
                    .find(|r| *r != ProtocolRoute::Http3)
                    .unwrap_or(ProtocolRoute::Http11);
                reason = RouteReason::H3HandshakeFailedFallback;
            } else {
                selected = ProtocolRoute::Http11;
                reason = RouteReason::H3HandshakeFailedNoFallback;
            }
        }

        RouteDecision {
            selected,
            reason,
            negotiated_alpn: negotiated,
            fallback_chain: chain,
        }
    }

    /// Serializes a compact fallback telemetry line for logs.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_handle::http3_profile::{Http3ProductionProfile, RouteDecision, RouteReason, ProtocolRoute};
    /// let p = Http3ProductionProfile::default();
    /// let d = RouteDecision { selected: ProtocolRoute::Http2, reason: RouteReason::Negotiated, negotiated_alpn: Some("h2".into()), fallback_chain: vec![ProtocolRoute::Http3, ProtocolRoute::Http2] };
    /// let line = p.telemetry_line(&d);
    /// assert!(line.contains("http3.route"));
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn telemetry_line(&self, decision: &RouteDecision) -> String {
        format!(
            "http3.route={} reason={} negotiated={} chain={}",
            decision.selected,
            decision.reason,
            decision.negotiated_alpn.as_deref().unwrap_or("none"),
            decision
                .fallback_chain
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>()
                .join(">")
        )
    }
}

#[cfg(feature = "http3-profile")]
#[cfg_attr(docsrs, doc(cfg(feature = "http3-profile")))]
impl std::fmt::Display for ProtocolRoute {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            ProtocolRoute::Http3 => "h3",
            ProtocolRoute::Http2 => "h2",
            ProtocolRoute::Http11 => "http/1.1",
        };
        write!(f, "{s}")
    }
}

#[cfg(feature = "http3-profile")]
#[cfg_attr(docsrs, doc(cfg(feature = "http3-profile")))]
impl std::fmt::Display for RouteReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            RouteReason::Negotiated => "negotiated",
            RouteReason::H3Disabled => "h3_disabled",
            RouteReason::AlpnMissing => "alpn_missing",
            RouteReason::AlpnUnsupported => "alpn_unsupported",
            RouteReason::H3HandshakeFailedFallback => {
                "h3_handshake_failed_fallback"
            }
            RouteReason::H3HandshakeFailedNoFallback => {
                "h3_handshake_failed_no_fallback"
            }
        };
        write!(f, "{s}")
    }
}

#[cfg(all(test, feature = "http3-profile"))]
mod tests {
    use super::*;

    #[test]
    fn production_baseline_prefers_h3() {
        let p = Http3ProductionProfile::production_baseline();
        assert!(p.enabled);
        assert_eq!(p.alpn_order[0], "h3");
        assert!(p.fallback_on_h3_error);
    }

    #[test]
    fn route_for_alpn_handles_known_protocols() {
        let p = Http3ProductionProfile::default();
        assert_eq!(p.route_for_alpn(Some(b"h3")), ProtocolRoute::Http3);
        assert_eq!(p.route_for_alpn(Some(b"h2")), ProtocolRoute::Http2);
        assert_eq!(
            p.route_for_alpn(Some(b"http/1.1")),
            ProtocolRoute::Http11
        );
        assert_eq!(
            p.route_for_alpn(Some(b"h3-29")),
            ProtocolRoute::Http3
        );
        assert_eq!(p.route_for_alpn(None), ProtocolRoute::Http11);
    }

    #[test]
    fn fallback_chain_is_unique_and_ordered() {
        let p = Http3ProductionProfile {
            alpn_order: vec![
                "h3".into(),
                "h2".into(),
                "h2".into(),
                "http/1.1".into(),
            ],
            ..Http3ProductionProfile::default()
        };
        assert_eq!(
            p.fallback_chain(),
            vec![
                ProtocolRoute::Http3,
                ProtocolRoute::Http2,
                ProtocolRoute::Http11
            ]
        );
    }

    #[test]
    fn route_for_client_alpns_respects_server_order() {
        let p = Http3ProductionProfile {
            alpn_order: vec![
                "h2".into(),
                "h3".into(),
                "http/1.1".into(),
            ],
            ..Http3ProductionProfile::default()
        };
        let client = vec![b"h3".to_vec(), b"h2".to_vec()];
        assert_eq!(
            p.route_for_client_alpns(&client),
            ProtocolRoute::Http2
        );
    }

    #[test]
    fn resolve_route_falls_back_on_h3_handshake_failure() {
        let p = Http3ProductionProfile::default();
        let decision = p.resolve_route(Some(b"h3"), false);
        assert_eq!(decision.selected, ProtocolRoute::Http2);
        assert_eq!(
            decision.reason,
            RouteReason::H3HandshakeFailedFallback
        );
    }

    #[test]
    fn resolve_route_handles_no_fallback_mode() {
        let p = Http3ProductionProfile {
            fallback_on_h3_error: false,
            ..Http3ProductionProfile::default()
        };
        let decision = p.resolve_route(Some(b"h3"), false);
        assert_eq!(decision.selected, ProtocolRoute::Http11);
        assert_eq!(
            decision.reason,
            RouteReason::H3HandshakeFailedNoFallback
        );
    }

    #[test]
    fn quic_preset_changes_tuning_envelope() {
        let conservative = Http3ProductionProfile {
            quic_preset: QuicTuningPreset::Conservative,
            ..Http3ProductionProfile::default()
        }
        .quic_tuning();
        let aggressive = Http3ProductionProfile {
            quic_preset: QuicTuningPreset::Aggressive,
            ..Http3ProductionProfile::default()
        }
        .quic_tuning();
        assert!(
            aggressive.max_bidi_streams > conservative.max_bidi_streams
        );
        assert!(
            aggressive.datagram_receive_buffer_bytes
                > conservative.datagram_receive_buffer_bytes
        );
    }

    #[test]
    fn telemetry_line_contains_decision_fields() {
        let p = Http3ProductionProfile::default();
        let decision = p.resolve_route(Some(b"h2"), true);
        let line = p.telemetry_line(&decision);
        assert!(line.contains("http3.route=h2"));
        assert!(line.contains("reason=negotiated"));
        assert!(line.contains("chain=h3>h2>http/1.1"));
    }

    #[test]
    fn quic_tuning_balanced_preset_is_reachable() {
        let tuning = Http3ProductionProfile::default().quic_tuning();
        assert_eq!(tuning.max_bidi_streams, 128);
        assert_eq!(tuning.datagram_receive_buffer_bytes, 1024 * 1024);
        assert_eq!(tuning.keep_alive_interval_ms, 10_000);
    }

    #[test]
    fn route_for_alpn_returns_http11_when_disabled() {
        let p = Http3ProductionProfile {
            enabled: false,
            ..Http3ProductionProfile::default()
        };
        assert_eq!(
            p.route_for_alpn(Some(b"h3")),
            ProtocolRoute::Http11
        );
    }

    #[test]
    fn route_for_client_alpns_returns_http11_when_disabled() {
        let p = Http3ProductionProfile {
            enabled: false,
            ..Http3ProductionProfile::default()
        };
        let offered = vec![b"h3".to_vec()];
        assert_eq!(
            p.route_for_client_alpns(&offered),
            ProtocolRoute::Http11
        );
    }

    #[test]
    fn route_for_client_alpns_falls_back_when_no_match() {
        let p = Http3ProductionProfile {
            alpn_order: vec!["h3".into(), "h2".into()],
            ..Http3ProductionProfile::default()
        };
        let offered = vec![b"http/1.1".to_vec()];
        assert_eq!(
            p.route_for_client_alpns(&offered),
            ProtocolRoute::Http11
        );
    }

    #[test]
    fn fallback_chain_skips_unknown_protocols() {
        let p = Http3ProductionProfile {
            alpn_order: vec!["gopher".into(), "h2".into()],
            ..Http3ProductionProfile::default()
        };
        assert_eq!(p.fallback_chain(), vec![ProtocolRoute::Http2]);
    }

    #[test]
    fn fallback_chain_defaults_to_http11_when_empty() {
        let p = Http3ProductionProfile {
            alpn_order: vec!["gopher".into(), "ftp".into()],
            ..Http3ProductionProfile::default()
        };
        assert_eq!(p.fallback_chain(), vec![ProtocolRoute::Http11]);
    }

    #[test]
    fn resolve_route_reports_h3_disabled() {
        let p = Http3ProductionProfile {
            enabled: false,
            ..Http3ProductionProfile::default()
        };
        let decision = p.resolve_route(Some(b"h3"), true);
        assert_eq!(decision.reason, RouteReason::H3Disabled);
    }

    #[test]
    fn resolve_route_accepts_h3_draft_as_negotiated() {
        let p = Http3ProductionProfile::default();
        let decision = p.resolve_route(Some(b"h3-29"), true);
        assert_eq!(decision.selected, ProtocolRoute::Http3);
        assert_eq!(decision.reason, RouteReason::Negotiated);
    }

    #[test]
    fn resolve_route_marks_unknown_alpn_unsupported() {
        let p = Http3ProductionProfile::default();
        let decision = p.resolve_route(Some(b"spdy/3"), true);
        assert_eq!(decision.reason, RouteReason::AlpnUnsupported);
    }

    #[test]
    fn resolve_route_reports_alpn_missing_when_no_negotiation() {
        let p = Http3ProductionProfile::default();
        let decision = p.resolve_route(None, true);
        assert_eq!(decision.reason, RouteReason::AlpnMissing);
    }

    #[test]
    fn route_reason_display_covers_all_variants() {
        assert_eq!(RouteReason::Negotiated.to_string(), "negotiated");
        assert_eq!(RouteReason::H3Disabled.to_string(), "h3_disabled");
        assert_eq!(
            RouteReason::AlpnMissing.to_string(),
            "alpn_missing"
        );
        assert_eq!(
            RouteReason::AlpnUnsupported.to_string(),
            "alpn_unsupported"
        );
        assert_eq!(
            RouteReason::H3HandshakeFailedFallback.to_string(),
            "h3_handshake_failed_fallback"
        );
        assert_eq!(
            RouteReason::H3HandshakeFailedNoFallback.to_string(),
            "h3_handshake_failed_no_fallback"
        );
    }
}