Skip to main content

faucet_stream/auth/
custom.rs

1//! Custom header-based authentication.
2
3use reqwest::header::HeaderMap;
4
5pub fn apply(headers: &mut HeaderMap, custom: &HeaderMap) {
6    headers.extend(custom.clone());
7}
8
9#[cfg(test)]
10mod tests {
11    use super::*;
12    use reqwest::header::{HeaderName, HeaderValue};
13
14    #[test]
15    fn apply_merges_custom_headers() {
16        let mut headers = HeaderMap::new();
17        headers.insert("existing", HeaderValue::from_static("keep"));
18
19        let mut custom = HeaderMap::new();
20        custom.insert(
21            HeaderName::from_static("x-custom"),
22            HeaderValue::from_static("val1"),
23        );
24        custom.insert(
25            HeaderName::from_static("x-another"),
26            HeaderValue::from_static("val2"),
27        );
28
29        apply(&mut headers, &custom);
30
31        assert_eq!(headers.get("existing").unwrap(), "keep");
32        assert_eq!(headers.get("x-custom").unwrap(), "val1");
33        assert_eq!(headers.get("x-another").unwrap(), "val2");
34    }
35
36    #[test]
37    fn apply_overwrites_conflicting_header() {
38        let mut headers = HeaderMap::new();
39        headers.insert("x-key", HeaderValue::from_static("old"));
40
41        let mut custom = HeaderMap::new();
42        custom.insert(
43            HeaderName::from_static("x-key"),
44            HeaderValue::from_static("new"),
45        );
46
47        apply(&mut headers, &custom);
48        assert_eq!(headers.get("x-key").unwrap(), "new");
49    }
50
51    #[test]
52    fn apply_empty_custom_is_noop() {
53        let mut headers = HeaderMap::new();
54        headers.insert("existing", HeaderValue::from_static("value"));
55
56        apply(&mut headers, &HeaderMap::new());
57        assert_eq!(headers.len(), 1);
58        assert_eq!(headers.get("existing").unwrap(), "value");
59    }
60}