armature-core 0.8.4

High-performance async HTTP framework core - routing, handlers, middleware
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
//! Interned, `Bytes`-backed HTTP header storage.
//!
//! Most requests have fewer than 12 headers, so they are stored inline on the
//! stack. Names are interned to a [`HeaderId`] — an enum variant for the ~33
//! well-known fields, a lowercased [`ByteStr`] for everything else — so a lookup
//! for a well-known name is an integer comparison rather than a
//! case-insensitive string compare. A custom name (`x-request-id` and friends)
//! still costs an ASCII-insensitive compare per stored header, but no
//! allocation: by-name lookups resolve the needle borrowed rather than
//! materializing a `HeaderId::Other` per call. Values are [`Bytes`], so once
//! Plan 4 wires the serve path through `armature-h1` they become slices of the
//! connection read buffer rather than copies.
//!
//! ## Case normalization
//!
//! Field names are case-insensitive (RFC 9110 §5.1), and interning settles the
//! question once: `iter()`, `keys()`, and `to_hash_map()` report lowercase names
//! regardless of how they were inserted. Lookups remain case-insensitive.
//!
//! ## Non-UTF-8 values
//!
//! A header value is bytes on the wire, not text. [`HeaderMap::get`] promises a
//! `&str` and therefore returns `None` for a value that is not valid UTF-8;
//! [`HeaderMap::get_bytes`] returns it. Handing back lossy text from `get` would
//! be worse than returning nothing, because the caller would go on to trust it.
//!
//! ## Performance
//!
//! | Operation | HashMap | HeaderMap |
//! |-----------|---------|-----------|
//! | Insert (first 12) | Heap alloc | Stack only |
//! | Lookup | O(1) hash of the name | O(n) compares, no alloc |
//! | Well-known name | `String` alloc | enum variant, no alloc |
//! | Value clone | `String` copy | refcount bump |

use armature_h1::{ByteStr, HeaderId, header as header_id};
use bytes::Bytes;
use smallvec::SmallVec;
use std::collections::HashMap;
use std::fmt;

/// Number of headers to store inline (on stack).
///
/// Most HTTP requests have 5–10 headers.
pub const INLINE_HEADERS: usize = 12;

/// A by-name lookup needle, resolved without allocating.
///
/// `header_id::intern` returns `HeaderId::Other(ByteStr::from(lowercased))` for
/// any name outside the well-known table — a `String` plus a `Bytes` per call.
/// Interning the needle would therefore charge an allocation to every lookup of
/// exactly the custom names applications reach for most (`x-request-id`,
/// `x-tenant-id`). A borrowed needle compares against the stored name instead,
/// while a well-known name still collapses to a discriminant compare.
enum Needle<'a> {
    Known(HeaderId),
    Custom(&'a str),
}

impl<'a> Needle<'a> {
    #[inline]
    fn new(name: &'a str) -> Self {
        match HeaderId::from_bytes(name.as_bytes()) {
            Some(id) => Needle::Known(id),
            None => Needle::Custom(name),
        }
    }

    /// Whether a stored header's name is this needle.
    ///
    /// `HeaderId::as_str` is always the canonical lowercase form, so the
    /// custom arm only has to be insensitive on the needle's side.
    #[inline]
    fn matches(&self, id: &HeaderId) -> bool {
        match self {
            Needle::Known(known) => known == id,
            Needle::Custom(name) => id.as_str().eq_ignore_ascii_case(name),
        }
    }
}

/// A header field: an interned name and a `Bytes` value.
#[derive(Clone, PartialEq, Eq)]
pub struct Header {
    /// The interned field name.
    pub id: HeaderId,
    /// The field value, exactly as it arrived.
    pub value: Bytes,
}

impl Header {
    /// Create a header, interning the name.
    #[inline]
    pub fn new(name: impl AsRef<str>, value: impl HeaderValueInput) -> Self {
        Self {
            id: header_id::intern(name.as_ref()),
            value: value.into_value(),
        }
    }

    /// The field name, lowercased.
    #[inline]
    pub fn name(&self) -> &str {
        self.id.as_str()
    }

    /// The value as UTF-8, or `None` if it is not valid UTF-8.
    #[inline]
    pub fn value_str(&self) -> Option<&str> {
        std::str::from_utf8(&self.value).ok()
    }
}

impl fmt::Debug for Header {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.value_str() {
            Some(v) => write!(f, "{}: {}", self.name(), v),
            None => write!(f, "{}: <{} non-utf8 bytes>", self.name(), self.value.len()),
        }
    }
}

/// Anything that can become a header value.
///
/// This exists so every existing `insert(name, value)` call site keeps compiling
/// across `&str`, `String`, and `Bytes` alike. A plain `impl Into<Bytes>` bound
/// would not: `Bytes: From<&'static str>` but not `From<&'a str>`, so every
/// borrowed-`&str` call site would break.
pub trait HeaderValueInput {
    /// Convert into the stored representation.
    fn into_value(self) -> Bytes;
}

impl HeaderValueInput for Bytes {
    #[inline]
    fn into_value(self) -> Bytes {
        self
    }
}

impl HeaderValueInput for &str {
    #[inline]
    fn into_value(self) -> Bytes {
        Bytes::copy_from_slice(self.as_bytes())
    }
}

impl HeaderValueInput for &String {
    #[inline]
    fn into_value(self) -> Bytes {
        Bytes::copy_from_slice(self.as_bytes())
    }
}

impl HeaderValueInput for String {
    #[inline]
    fn into_value(self) -> Bytes {
        Bytes::from(self.into_bytes())
    }
}

impl HeaderValueInput for &[u8] {
    #[inline]
    fn into_value(self) -> Bytes {
        Bytes::copy_from_slice(self)
    }
}

impl HeaderValueInput for Vec<u8> {
    #[inline]
    fn into_value(self) -> Bytes {
        Bytes::from(self)
    }
}

impl HeaderValueInput for ByteStr {
    #[inline]
    fn into_value(self) -> Bytes {
        self.into_bytes()
    }
}

impl HeaderValueInput for std::borrow::Cow<'_, str> {
    #[inline]
    fn into_value(self) -> Bytes {
        match self {
            std::borrow::Cow::Borrowed(s) => Bytes::copy_from_slice(s.as_bytes()),
            std::borrow::Cow::Owned(s) => Bytes::from(s.into_bytes()),
        }
    }
}

/// A compact header map using `SmallVec` for inline storage.
///
/// # Example
///
/// ```rust
/// use armature_core::headers::HeaderMap;
///
/// let mut headers = HeaderMap::new();
/// headers.insert("Content-Type", "application/json");
/// headers.insert("Accept", "text/html");
///
/// // Lookup is case-insensitive; the value comes back as a `&str`.
/// assert_eq!(headers.get("content-type"), Some("application/json"));
/// assert!(headers.is_inline()); // Still on stack
/// ```
#[derive(Clone, Default)]
pub struct HeaderMap {
    inner: SmallVec<[Header; INLINE_HEADERS]>,
}

impl HeaderMap {
    /// Create a new empty header map.
    #[inline]
    pub const fn new() -> Self {
        Self {
            inner: SmallVec::new_const(),
        }
    }

    /// Create with pre-allocated capacity.
    ///
    /// If capacity <= `INLINE_HEADERS`, no heap allocation occurs.
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            inner: SmallVec::with_capacity(capacity),
        }
    }

    /// Check if storage is inline (no heap allocation).
    #[inline]
    pub fn is_inline(&self) -> bool {
        !self.inner.spilled()
    }

    /// Get the number of headers.
    #[inline]
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Check if empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    /// The value of `name` as UTF-8, case-insensitively.
    ///
    /// Returns `None` for a value that is not valid UTF-8; use
    /// [`HeaderMap::get_bytes`] for those.
    #[inline]
    pub fn get(&self, name: &str) -> Option<&str> {
        self.get_bytes(name)
            .and_then(|v| std::str::from_utf8(v).ok())
    }

    /// The raw value of `name`, case-insensitively.
    #[inline]
    pub fn get_bytes(&self, name: &str) -> Option<&Bytes> {
        let needle = Needle::new(name);
        self.inner
            .iter()
            .find(|h| needle.matches(&h.id))
            .map(|h| &h.value)
    }

    /// The raw value for an already-interned name.
    ///
    /// The hot-path accessor: no interning, and for a well-known name the
    /// comparison is on the enum discriminant.
    #[inline]
    pub fn get_id(&self, id: &HeaderId) -> Option<&Bytes> {
        self.inner.iter().find(|h| &h.id == id).map(|h| &h.value)
    }

    /// The value of `name` as UTF-8, case-insensitively.
    ///
    /// Identical to [`HeaderMap::get`]; kept because call sites use both names.
    #[inline]
    pub fn get_ignore_case(&self, name: &str) -> Option<&str> {
        self.get(name)
    }

    /// Check if header exists (case-insensitive).
    #[inline]
    pub fn contains(&self, name: &str) -> bool {
        self.get_bytes(name).is_some()
    }

    /// Check if header exists (case-insensitive).
    ///
    /// HashMap-compatible alias for [`contains`](Self::contains).
    #[inline]
    pub fn contains_key(&self, name: &str) -> bool {
        self.contains(name)
    }

    /// Insert a header, replacing any existing header with the same name.
    ///
    /// Returns the old value if one was replaced.
    #[inline]
    pub fn insert(&mut self, name: impl AsRef<str>, value: impl HeaderValueInput) -> Option<Bytes> {
        let id = header_id::intern(name.as_ref());
        let value = value.into_value();
        if let Some(existing) = self.inner.iter_mut().find(|h| h.id == id) {
            return Some(std::mem::replace(&mut existing.value, value));
        }
        self.inner.push(Header { id, value });
        None
    }

    /// Append a header, allowing duplicates.
    ///
    /// Unlike `insert`, this does not replace. Use it for fields that may repeat,
    /// such as `Set-Cookie`.
    #[inline]
    pub fn append(&mut self, name: impl AsRef<str>, value: impl HeaderValueInput) {
        self.inner.push(Header {
            id: header_id::intern(name.as_ref()),
            value: value.into_value(),
        });
    }

    /// Remove a header by name (case-insensitive), returning its value.
    #[inline]
    pub fn remove(&mut self, name: &str) -> Option<Bytes> {
        let needle = Needle::new(name);
        let pos = self.inner.iter().position(|h| needle.matches(&h.id))?;
        Some(self.inner.remove(pos).value)
    }

    /// Remove every header with the given name, returning how many were removed.
    #[inline]
    pub fn remove_all(&mut self, name: &str) -> usize {
        let needle = Needle::new(name);
        let before = self.inner.len();
        self.inner.retain(|h| !needle.matches(&h.id));
        before - self.inner.len()
    }

    /// Iterate over headers as `(name, value)`, skipping non-UTF-8 values.
    #[inline]
    pub fn iter(&self) -> impl Iterator<Item = (&str, &str)> {
        self.inner
            .iter()
            .filter_map(|h| h.value_str().map(|v| (h.name(), v)))
    }

    /// Iterate over every header, including those with non-UTF-8 values.
    #[inline]
    pub fn iter_raw(&self) -> impl Iterator<Item = (&HeaderId, &Bytes)> {
        self.inner.iter().map(|h| (&h.id, &h.value))
    }

    /// Iterate over header names, lowercased.
    #[inline]
    pub fn names(&self) -> impl Iterator<Item = &str> {
        self.inner.iter().map(|h| h.name())
    }

    /// Iterate over header names.
    ///
    /// HashMap-compatible alias for [`names`](Self::names).
    #[inline]
    pub fn keys(&self) -> impl Iterator<Item = &str> {
        self.names()
    }

    /// Iterate over header values, skipping non-UTF-8 ones.
    #[inline]
    pub fn values(&self) -> impl Iterator<Item = &str> {
        self.inner.iter().filter_map(|h| h.value_str())
    }

    /// Every value for a header name, for multi-value fields.
    #[inline]
    pub fn get_all(&self, name: &str) -> Vec<&str> {
        let needle = Needle::new(name);
        self.inner
            .iter()
            .filter(|h| needle.matches(&h.id))
            .filter_map(|h| h.value_str())
            .collect()
    }

    /// Clear all headers.
    #[inline]
    pub fn clear(&mut self) {
        self.inner.clear();
    }

    /// Extend with headers from an iterator.
    #[inline]
    pub fn extend<I, K, V>(&mut self, iter: I)
    where
        I: IntoIterator<Item = (K, V)>,
        K: AsRef<str>,
        V: HeaderValueInput,
    {
        for (k, v) in iter {
            self.insert(k, v);
        }
    }

    /// Convert to a `HashMap`, for compatibility.
    ///
    /// Names come out lowercased and non-UTF-8 values are dropped. This
    /// allocates a `String` per name and value — reach for it only when a
    /// `HashMap` is genuinely required.
    #[inline]
    pub fn to_hash_map(&self) -> HashMap<String, String> {
        self.iter()
            .map(|(k, v)| (k.to_owned(), v.to_owned()))
            .collect()
    }

    /// Create from a `HashMap`.
    #[inline]
    pub fn from_hash_map(map: HashMap<String, String>) -> Self {
        let mut headers = Self::with_capacity(map.len());
        for (k, v) in map {
            headers.insert(k, v);
        }
        headers
    }

    // ========================================================================
    // Common Header Accessors
    // ========================================================================

    /// Get the `Content-Type` header.
    #[inline]
    pub fn content_type(&self) -> Option<&str> {
        self.str_of(&HeaderId::ContentType)
    }

    /// Get the `Content-Length` header as a `usize`.
    #[inline]
    pub fn content_length(&self) -> Option<usize> {
        self.str_of(&HeaderId::ContentLength)?.parse().ok()
    }

    /// Get the `Accept` header.
    #[inline]
    pub fn accept(&self) -> Option<&str> {
        self.str_of(&HeaderId::Accept)
    }

    /// Get the `Authorization` header.
    #[inline]
    pub fn authorization(&self) -> Option<&str> {
        self.str_of(&HeaderId::Authorization)
    }

    /// Get the `User-Agent` header.
    #[inline]
    pub fn user_agent(&self) -> Option<&str> {
        self.str_of(&HeaderId::UserAgent)
    }

    /// Get the `Host` header.
    #[inline]
    pub fn host(&self) -> Option<&str> {
        self.str_of(&HeaderId::Host)
    }

    /// Get the `Cookie` header.
    #[inline]
    pub fn cookie(&self) -> Option<&str> {
        self.str_of(&HeaderId::Cookie)
    }

    /// Check for a keep-alive connection.
    #[inline]
    pub fn is_keep_alive(&self) -> bool {
        self.str_of(&HeaderId::Connection)
            .map(|v| v.eq_ignore_ascii_case("keep-alive"))
            .unwrap_or(true) // HTTP/1.1 default is keep-alive
    }

    /// Check for chunked transfer encoding.
    #[inline]
    pub fn is_chunked(&self) -> bool {
        self.str_of(&HeaderId::TransferEncoding)
            .map(|v| v.contains("chunked"))
            .unwrap_or(false)
    }

    /// Set the `Content-Type` header.
    #[inline]
    pub fn set_content_type(&mut self, value: impl HeaderValueInput) {
        self.insert("content-type", value);
    }

    /// Set the `Content-Length` header.
    #[inline]
    pub fn set_content_length(&mut self, len: usize) {
        self.insert("content-length", len.to_string());
    }

    /// The UTF-8 value for an already-interned name.
    #[inline]
    fn str_of(&self, id: &HeaderId) -> Option<&str> {
        self.get_id(id).and_then(|v| std::str::from_utf8(v).ok())
    }
}

impl fmt::Debug for HeaderMap {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_map()
            .entries(
                self.inner
                    .iter()
                    .map(|h| (h.name(), h.value_str().unwrap_or("<non-utf8>"))),
            )
            .finish()
    }
}

impl<K, V> FromIterator<(K, V)> for HeaderMap
where
    K: AsRef<str>,
    V: HeaderValueInput,
{
    fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
        let iter = iter.into_iter();
        let (min, max) = iter.size_hint();
        let mut map = HeaderMap::with_capacity(max.unwrap_or(min));
        for (k, v) in iter {
            map.insert(k, v);
        }
        map
    }
}

impl Extend<(String, String)> for HeaderMap {
    fn extend<I: IntoIterator<Item = (String, String)>>(&mut self, iter: I) {
        for (k, v) in iter {
            self.insert(k, v);
        }
    }
}

/// Project a header to its `(name, value)` pair, dropping non-UTF-8 values.
///
/// A free function rather than a closure so [`IntoIterator`] can name its
/// iterator type.
fn utf8_pair(h: &Header) -> Option<(&str, &str)> {
    h.value_str().map(|v| (h.name(), v))
}

/// Project an owned header to an owned pair, dropping non-UTF-8 values.
fn owned_utf8_pair(h: Header) -> Option<(String, String)> {
    let name = h.name().to_owned();
    String::from_utf8(h.value.to_vec())
        .ok()
        .map(|value| (name, value))
}

impl<'a> IntoIterator for &'a HeaderMap {
    type Item = (&'a str, &'a str);
    type IntoIter = std::iter::FilterMap<
        std::slice::Iter<'a, Header>,
        fn(&'a Header) -> Option<(&'a str, &'a str)>,
    >;

    fn into_iter(self) -> Self::IntoIter {
        self.inner.iter().filter_map(utf8_pair as _)
    }
}

impl IntoIterator for HeaderMap {
    type Item = (String, String);
    type IntoIter = std::iter::FilterMap<
        smallvec::IntoIter<[Header; INLINE_HEADERS]>,
        fn(Header) -> Option<(String, String)>,
    >;

    fn into_iter(self) -> Self::IntoIter {
        self.inner.into_iter().filter_map(owned_utf8_pair as _)
    }
}

// Allow HashMap-like indexing.
impl std::ops::Index<&str> for HeaderMap {
    type Output = str;

    fn index(&self, name: &str) -> &Self::Output {
        self.get(name).expect("header not found")
    }
}

// ============================================================================
// Conversion from/to HashMap for backwards compatibility
// ============================================================================

impl From<HashMap<String, String>> for HeaderMap {
    fn from(map: HashMap<String, String>) -> Self {
        Self::from_hash_map(map)
    }
}

impl From<HeaderMap> for HashMap<String, String> {
    fn from(map: HeaderMap) -> Self {
        map.to_hash_map()
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new_is_inline() {
        let headers = HeaderMap::new();
        assert!(headers.is_inline());
        assert!(headers.is_empty());
    }

    #[test]
    fn get_returns_str_and_well_known_names_are_interned() {
        let mut h = HeaderMap::new();
        h.insert("Content-Type", "application/json");
        h.insert("X-Tenant-Id", "acme".to_string());

        // Case-insensitive lookup survives the move to HeaderId.
        assert_eq!(h.get("content-type"), Some("application/json"));
        assert_eq!(h.get("CONTENT-TYPE"), Some("application/json"));
        assert_eq!(h.get("x-tenant-id"), Some("acme"));
        assert_eq!(h.get("absent"), None);

        // Well-known names cost no allocation and compare as a discriminant.
        assert_eq!(
            h.get_id(&HeaderId::ContentType).map(|b| &b[..]),
            Some(&b"application/json"[..])
        );
    }

    #[test]
    fn custom_names_stay_case_insensitive_through_the_borrowed_needle() {
        // Names outside the well-known table take the non-allocating compare
        // path, which still has to honour RFC 9110 case-insensitivity in both
        // directions — however the header was inserted, however it is asked for.
        let mut h = HeaderMap::new();
        h.insert("X-Request-ID", "abc123");
        h.append("x-request-id", "def456");

        assert_eq!(h.get("x-request-id"), Some("abc123"));
        assert_eq!(h.get("X-REQUEST-ID"), Some("abc123"));
        assert!(h.contains("X-Request-Id"));
        assert_eq!(h.get_all("X-Request-Id"), vec!["abc123", "def456"]);

        // A custom needle must not match a well-known stored name, or vice versa.
        h.insert("Content-Type", "text/plain");
        assert_eq!(h.get("x-content-type"), None);

        assert_eq!(h.remove_all("X-Request-ID"), 2);
        assert_eq!(h.get("x-request-id"), None);
    }

    #[test]
    fn non_utf8_value_is_invisible_to_get_but_reachable_as_bytes() {
        let mut h = HeaderMap::new();
        h.insert("x-raw", Bytes::from_static(&[0xff, 0x00]));
        // `get` promises a `&str`, and there isn't one. Returning None beats
        // returning lossy text that a caller would go on to trust.
        assert_eq!(h.get("x-raw"), None);
        assert_eq!(h.get_bytes("x-raw").map(|b| b.len()), Some(2));
        // ...and it is still there, so a caller that iterates raw sees it.
        assert_eq!(h.len(), 1);
        assert_eq!(h.iter().count(), 0);
        assert_eq!(h.iter_raw().count(), 1);
    }

    #[test]
    fn test_insert_and_get() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");
        headers.insert("Accept", "text/html");

        assert_eq!(headers.len(), 2);
        assert_eq!(headers.get("Content-Type"), Some("application/json"));
        assert_eq!(headers.get("content-type"), Some("application/json"));
    }

    #[test]
    fn test_insert_replaces() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "text/plain");
        let old = headers.insert("Content-Type", "application/json");

        assert_eq!(old.as_deref(), Some(&b"text/plain"[..]));
        assert_eq!(headers.len(), 1);
        assert_eq!(headers.get("Content-Type"), Some("application/json"));
    }

    #[test]
    fn test_append_duplicates() {
        let mut headers = HeaderMap::new();
        headers.append("Set-Cookie", "session=abc");
        headers.append("Set-Cookie", "user=123");

        assert_eq!(headers.len(), 2);
        assert_eq!(
            headers.get_all("set-cookie"),
            vec!["session=abc", "user=123"]
        );
    }

    #[test]
    fn test_remove() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");
        headers.insert("Accept", "text/html");

        let removed = headers.remove("Content-Type");
        assert_eq!(removed.as_deref(), Some(&b"application/json"[..]));
        assert_eq!(headers.len(), 1);
        assert!(!headers.contains("Content-Type"));
    }

    #[test]
    fn test_remove_all() {
        let mut headers = HeaderMap::new();
        headers.append("Set-Cookie", "a=1");
        headers.append("set-cookie", "b=2");
        headers.insert("Accept", "*/*");

        assert_eq!(headers.remove_all("Set-Cookie"), 2);
        assert_eq!(headers.len(), 1);
    }

    #[test]
    fn test_inline_capacity() {
        let mut headers = HeaderMap::new();

        for i in 0..INLINE_HEADERS {
            headers.insert(format!("Header-{i}"), format!("Value-{i}"));
        }
        assert!(headers.is_inline());

        headers.insert("Extra-Header", "Extra-Value");
        assert!(!headers.is_inline());
    }

    #[test]
    fn test_iter() {
        let mut headers = HeaderMap::new();
        headers.insert("A", "1");
        headers.insert("B", "2");

        let pairs: Vec<_> = headers.iter().collect();
        assert_eq!(pairs.len(), 2);
    }

    #[test]
    fn iter_yields_lowercased_names_for_custom_headers() {
        let mut h = HeaderMap::new();
        h.insert("X-A", "1");
        // Interning lowercases custom names once, at insert. A caller comparing
        // `name == "x-a"` must not have to guess which case survived.
        assert_eq!(h.iter().collect::<Vec<_>>(), vec![("x-a", "1")]);
    }

    #[test]
    fn test_common_accessors() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");
        headers.insert("Content-Length", "100");
        headers.insert("Connection", "keep-alive");
        headers.insert("Transfer-Encoding", "chunked");

        assert_eq!(headers.content_type(), Some("application/json"));
        assert_eq!(headers.content_length(), Some(100));
        assert!(headers.is_keep_alive());
        assert!(headers.is_chunked());
    }

    #[test]
    fn test_from_hash_map() {
        let mut map = HashMap::new();
        map.insert("Content-Type".to_string(), "application/json".to_string());
        map.insert("Accept".to_string(), "text/html".to_string());

        let headers = HeaderMap::from_hash_map(map);
        assert_eq!(headers.len(), 2);
        assert!(headers.contains("Content-Type"));
    }

    #[test]
    fn test_to_hash_map_normalizes_names_to_lowercase() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");

        let map = headers.to_hash_map();
        // Names are interned, so the case they were inserted with is gone. This
        // is the documented behavior change in 0.6: lookups stay
        // case-insensitive, but a `HashMap` snapshot reports canonical names.
        assert_eq!(
            map.get("content-type").map(String::as_str),
            Some("application/json")
        );
        assert_eq!(map.get("Content-Type"), None);
    }

    #[test]
    fn test_from_iterator() {
        let headers: HeaderMap = [
            ("Content-Type", "application/json"),
            ("Accept", "text/html"),
        ]
        .into_iter()
        .collect();

        assert_eq!(headers.len(), 2);
    }

    #[test]
    fn test_indexing() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");

        assert_eq!(&headers["Content-Type"], "application/json");
    }

    #[test]
    fn test_contains_key() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");

        assert!(headers.contains_key("Content-Type"));
        assert!(headers.contains_key("content-type"));
        assert!(!headers.contains_key("Accept"));
    }

    #[test]
    fn test_keys() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");
        headers.insert("Accept", "text/html");

        let keys: Vec<_> = headers.keys().collect();
        assert_eq!(keys.len(), 2);
        assert!(keys.contains(&"content-type"));
        assert!(keys.contains(&"accept"));
    }

    #[test]
    fn test_values() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");
        headers.insert("Accept", "text/html");

        let values: Vec<_> = headers.values().collect();
        assert_eq!(values.len(), 2);
        assert!(values.contains(&"application/json"));
        assert!(values.contains(&"text/html"));
    }

    #[test]
    fn test_is_empty() {
        let mut headers = HeaderMap::new();
        assert!(headers.is_empty());
        headers.insert("Content-Type", "application/json");
        assert!(!headers.is_empty());
    }

    #[test]
    fn test_default() {
        let headers = HeaderMap::default();
        assert!(headers.is_empty());
        assert!(headers.is_inline());
    }

    #[test]
    fn test_extend_trait() {
        let mut headers = HeaderMap::new();
        headers.insert("Existing", "1");

        let extra: Vec<(String, String)> = vec![
            ("Content-Type".to_string(), "application/json".to_string()),
            ("Accept".to_string(), "text/html".to_string()),
        ];
        Extend::extend(&mut headers, extra);

        assert_eq!(headers.len(), 3);
        assert_eq!(headers.get("Content-Type"), Some("application/json"));
    }

    #[test]
    fn test_into_iterator_owned() {
        let mut headers = HeaderMap::new();
        headers.insert("A", "1");
        headers.insert("B", "2");

        let collected: Vec<(String, String)> = headers.into_iter().collect();
        assert_eq!(collected.len(), 2);
    }

    #[test]
    fn test_into_iterator_ref() {
        let mut headers = HeaderMap::new();
        headers.insert("A", "1");

        let collected: Vec<(&str, &str)> = (&headers).into_iter().collect();
        assert_eq!(collected, vec![("a", "1")]);
    }

    #[test]
    fn test_hashmap_roundtrip() {
        let mut map = HashMap::new();
        map.insert("Content-Type".to_string(), "application/json".to_string());

        let headers: HeaderMap = map.clone().into();
        assert!(headers.contains_key("content-type"));
        let back: HashMap<String, String> = headers.into();
        // Canonical (lowercase) name on the way out; see
        // `test_to_hash_map_normalizes_names_to_lowercase`.
        assert_eq!(back.get("content-type"), map.get("Content-Type"));
    }

    #[test]
    fn cloning_a_value_does_not_copy_it() {
        let mut headers = HeaderMap::new();
        let big = Bytes::from(vec![b'x'; 4096]);
        headers.insert("x-big", big.clone());
        let copy = headers.clone();
        assert_eq!(
            copy.get_bytes("x-big").map(|b| b.as_ptr()),
            Some(big.as_ptr())
        );
    }
}