rama_http/matcher/
header.rs

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
use crate::{HeaderName, HeaderValue, Request};
use rama_core::{context::Extensions, matcher::Matcher, Context};

#[derive(Debug, Clone)]
/// Matcher based on the [`Request`]'s headers.
///
/// [`Request`]: crate::Request
pub struct HeaderMatcher {
    name: HeaderName,
    kind: HeaderMatcherKind,
}

#[derive(Debug, Clone)]
enum HeaderMatcherKind {
    Exists,
    Is(HeaderValue),
    Contains(HeaderValue),
}

impl HeaderMatcher {
    /// Create a new header matcher to match on the existence of a header.
    pub fn exists(name: HeaderName) -> Self {
        Self {
            name,
            kind: HeaderMatcherKind::Exists,
        }
    }

    /// Create a new header matcher to match on an exact header value match.
    pub fn is(name: HeaderName, value: HeaderValue) -> Self {
        Self {
            name,
            kind: HeaderMatcherKind::Is(value),
        }
    }

    /// Create a new header matcher to match that the header contains the given value.
    pub fn contains(name: HeaderName, value: HeaderValue) -> Self {
        Self {
            name,
            kind: HeaderMatcherKind::Contains(value),
        }
    }
}

impl<State, Body> Matcher<State, Request<Body>> for HeaderMatcher {
    fn matches(
        &self,
        _ext: Option<&mut Extensions>,
        _ctx: &Context<State>,
        req: &Request<Body>,
    ) -> bool {
        let headers = req.headers();
        match self.kind {
            HeaderMatcherKind::Exists => headers.contains_key(&self.name),
            HeaderMatcherKind::Is(ref value) => headers.get(&self.name) == Some(value),
            HeaderMatcherKind::Contains(ref value) => {
                headers.get_all(&self.name).iter().any(|v| v == value)
            }
        }
    }
}

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

    #[test]
    fn test_header_matcher_exists() {
        let matcher = HeaderMatcher::exists("content-type".parse().unwrap());
        let req = Request::builder()
            .header("content-type", "text/plain")
            .body(())
            .unwrap();
        assert!(matcher.matches(None, &Context::default(), &req));
    }

    #[test]
    fn test_header_matcher_exists_no_match() {
        let matcher = HeaderMatcher::exists("content-type".parse().unwrap());
        let req = Request::builder().body(()).unwrap();
        assert!(!matcher.matches(None, &Context::default(), &req));
    }

    #[test]
    fn test_header_matcher_is() {
        let matcher = HeaderMatcher::is(
            "content-type".parse().unwrap(),
            "text/plain".parse().unwrap(),
        );
        let req = Request::builder()
            .header("content-type", "text/plain")
            .body(())
            .unwrap();
        assert!(matcher.matches(None, &Context::default(), &req));
    }

    #[test]
    fn test_header_matcher_is_no_match() {
        let matcher = HeaderMatcher::is(
            "content-type".parse().unwrap(),
            "text/plain".parse().unwrap(),
        );
        let req = Request::builder()
            .header("content-type", "text/html")
            .body(())
            .unwrap();
        assert!(!matcher.matches(None, &Context::default(), &req));
    }

    #[test]
    fn test_header_matcher_contains() {
        let matcher = HeaderMatcher::contains(
            "content-type".parse().unwrap(),
            "text/plain".parse().unwrap(),
        );
        let req = Request::builder()
            .header("content-type", "text/plain")
            .body(())
            .unwrap();
        assert!(matcher.matches(None, &Context::default(), &req));
    }

    #[test]
    fn test_header_matcher_contains_no_match() {
        let matcher = HeaderMatcher::contains(
            "content-type".parse().unwrap(),
            "text/plain".parse().unwrap(),
        );
        let req = Request::builder()
            .header("content-type", "text/html")
            .body(())
            .unwrap();
        assert!(!matcher.matches(None, &Context::default(), &req));
    }

    #[test]
    fn test_header_matcher_contains_multiple() {
        let matcher = HeaderMatcher::contains(
            "content-type".parse().unwrap(),
            "text/plain".parse().unwrap(),
        );
        let req = Request::builder()
            .header("content-type", "text/html")
            .header("content-type", "text/plain")
            .body(())
            .unwrap();
        assert!(matcher.matches(None, &Context::default(), &req));
    }

    #[test]
    fn test_header_matcher_contains_multiple_no_match() {
        let matcher = HeaderMatcher::contains(
            "content-type".parse().unwrap(),
            "text/plain".parse().unwrap(),
        );
        let req = Request::builder()
            .header("content-type", "text/html")
            .header("content-type", "text/xml")
            .body(())
            .unwrap();
        assert!(!matcher.matches(None, &Context::default(), &req));
    }
}