Skip to main content

rama_http_headers/common/
allow.rs

1use rama_http_types::Method;
2
3derive_non_empty_flat_csv_header! {
4    #[header(name = ALLOW, sep = Comma)]
5    #[derive(Clone, Debug, PartialEq)]
6    /// `Allow` header, defined in [RFC7231](https://datatracker.ietf.org/doc/html/rfc7231#section-7.4.1)
7    ///
8    /// The `Allow` header field lists the set of methods advertised as
9    /// supported by the target resource.  The purpose of this field is
10    /// strictly to inform the recipient of valid request methods associated
11    /// with the resource.
12    ///
13    /// # ABNF
14    ///
15    /// ```text
16    /// Allow = #method
17    /// ```
18    ///
19    /// # Example values
20    /// * `GET, HEAD, PUT`
21    /// * `OPTIONS, GET, PUT, POST, DELETE, HEAD, TRACE, CONNECT, PATCH, fOObAr`
22    /// * ``
23    ///
24    /// # Examples
25    ///
26    /// ```
27    /// use rama_utils::collections::non_empty_smallvec;
28    /// use rama_http_types::Method;
29    /// use rama_http_headers::Allow;
30    ///
31    /// let allow_methods = Allow(
32    ///     non_empty_smallvec![Method::GET, Method::PUT],
33    /// );
34    /// ```
35    pub struct Allow(pub NonEmptySmallVec<7, Method>);
36}
37
38#[cfg(test)]
39mod tests {
40    use super::super::{test_decode, test_encode};
41    use super::*;
42    use rama_utils::collections::non_empty_smallvec;
43
44    #[test]
45    fn decode_single() {
46        let Allow(allowed) = test_decode(&["GET, PUT"]).unwrap();
47
48        assert_eq!(allowed.len(), 2);
49        assert_eq!(allowed[0], Method::GET);
50        assert_eq!(allowed[1], Method::PUT);
51    }
52
53    #[test]
54    fn decode_multi() {
55        let Allow(allowed) = test_decode(&["GET, PUT", "POST"]).unwrap();
56
57        assert_eq!(allowed.len(), 3);
58        assert_eq!(allowed[0], Method::GET);
59        assert_eq!(allowed[1], Method::PUT);
60        assert_eq!(allowed[2], Method::POST);
61    }
62
63    #[test]
64    fn decode_single_empty_value() {
65        assert!(test_decode::<Allow>(&[""]).is_none());
66    }
67
68    #[test]
69    fn decode_single_no_header_values() {
70        assert!(test_decode::<Allow>(&[]).is_none());
71    }
72
73    #[test]
74    fn encode() {
75        let allow = Allow(non_empty_smallvec![Method::GET, Method::PUT]);
76
77        let headers = test_encode(allow);
78        assert_eq!(headers["allow"], "GET, PUT");
79    }
80}