actix_prost/
http_compatibility.rs

1use std::str::FromStr;
2
3pub fn to_actix_status(status: http::StatusCode) -> actix_web::http::StatusCode {
4    actix_web::http::StatusCode::from_u16(status.as_u16()).expect("status codes are always valid")
5}
6
7pub fn from_actix_header(
8    header: (
9        actix_http::header::HeaderName,
10        actix_http::header::HeaderValue,
11    ),
12) -> (http::HeaderName, http::HeaderValue) {
13    (
14        http::HeaderName::from_str(header.0.as_str()).expect("was a valid header name"),
15        http::HeaderValue::from_bytes(header.1.as_bytes()).expect("was a valid header value"),
16    )
17}
18#[cfg(test)]
19mod tests {
20    use super::*;
21    use actix_http::header::{HeaderName, HeaderValue};
22    use std::str::FromStr;
23
24    #[test]
25    fn test_header_names() {
26        let a_1 = "a".to_string();
27        let a_8192 = "a".repeat(8192);
28        let a_8193 = "a".repeat(8193);
29        let a_16384 = "a".repeat(16384);
30        let a_16385 = "a".repeat(16385);
31
32        let test_cases = vec![
33            // Empty
34            "",
35            // Single characters
36            "a",
37            "A",
38            "-",
39            // Common headers
40            "content-type",
41            "x-custom-header",
42            "CONTENT-TYPE",
43            "X-CUSTOM-HEADER",
44            // Various lengths
45            &a_1,
46            &a_8192,
47            &a_8193,
48            &a_16384,
49            &a_16385,
50            // Special characters
51            "header\0name",
52            "header\tname",
53            "header name",
54            "headerñame",
55            "header\r\name",
56            "header\n\name",
57            "header:name",
58            // Mixed case
59            "Content-Type",
60            "X-Mixed-Case",
61        ];
62
63        for input in test_cases {
64            if let Ok(name) = HeaderName::from_str(input) {
65                let dummy_value = HeaderValue::from_static("");
66                let _ = from_actix_header((name, dummy_value));
67            }
68        }
69    }
70
71    #[test]
72    fn test_header_values() {
73        let valid_name = HeaderName::from_static("x-test");
74
75        let nul = '\u{0}'.to_string();
76        let us = '\u{1f}'.to_string();
77        let del = '\u{7f}'.to_string();
78        let a_8192 = "a".repeat(8192);
79        let a_16384 = "a".repeat(16384);
80
81        let test_cases = vec![
82            // Empty
83            "",
84            // ASCII range
85            &nul, // NUL
86            "\t", // TAB
87            &us,  // US
88            " ",  // space
89            "~",  // tilde
90            &del, // DEL
91            // ASCII printable
92            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
93            "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
94            // Control characters
95            "\n",
96            "\r",
97            "\x0B", // VT
98            "\x0C", // FF
99            // UTF-8 sequences
100            "Hello™",
101            "Hello🌍",
102            "Привет",
103            // Mixed content
104            "Hello\nWorld",
105            "Hello\0World",
106            "Hello\tWorld",
107            // Long values
108            &a_8192,
109            &a_16384,
110        ];
111
112        for input in test_cases {
113            if let Ok(value) = HeaderValue::from_str(input) {
114                let _ = from_actix_header((valid_name.clone(), value));
115            }
116        }
117    }
118}