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
//! Experimental typed headers.

pub use crate::{
    cache_control::{CacheControl, CacheDirective},
    content_length::ContentLength,
    forwarded::Forwarded,
    strict_transport_security::StrictTransportSecurity,
    x_forwarded_prefix::{XForwardedPrefix, X_FORWARDED_PREFIX},
};

#[cfg(test)]
mod header_test_helpers {
    use std::fmt;

    use actix_http::header::Header;
    use actix_web::{test, HttpRequest};

    fn req_from_raw_headers<H: Header, I: IntoIterator<Item = V>, V: AsRef<[u8]>>(
        header_lines: I,
    ) -> HttpRequest {
        header_lines
            .into_iter()
            .fold(test::TestRequest::default(), |req, item| {
                req.append_header((H::name(), item.as_ref().to_vec()))
            })
            .to_http_request()
    }

    #[track_caller]
    pub(crate) fn assert_parse_eq<
        H: Header + fmt::Debug + PartialEq,
        I: IntoIterator<Item = V>,
        V: AsRef<[u8]>,
    >(
        headers: I,
        expect: H,
    ) {
        let req = req_from_raw_headers::<H, _, _>(headers);
        assert_eq!(H::parse(&req).unwrap(), expect);
    }

    #[track_caller]
    pub(crate) fn assert_parse_fail<
        H: Header + fmt::Debug,
        I: IntoIterator<Item = V>,
        V: AsRef<[u8]>,
    >(
        headers: I,
    ) {
        let req = req_from_raw_headers::<H, _, _>(headers);
        H::parse(&req).unwrap_err();
    }
}

#[cfg(test)]
pub(crate) use header_test_helpers::{assert_parse_eq, assert_parse_fail};