Skip to main content

rama_http_headers/common/
access_control_allow_methods.rs

1use rama_http_types::Method;
2
3derive_values_or_any_header! {
4    #[header(name = ACCESS_CONTROL_ALLOW_METHODS, sep = Comma)]
5    #[derive(Clone, Debug, PartialEq)]
6    /// `Access-Control-Allow-Methods` header, as defined on
7    /// [mdn](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Access-Control-Allow-Methods).
8    ///
9    /// The `Access-Control-Allow-Methods` header indicates, as part of the
10    /// response to a preflight request, which methods can be used during the
11    /// actual request.
12    ///
13    /// # ABNF
14    ///
15    /// ```text
16    /// Access-Control-Allow-Methods: "Access-Control-Allow-Methods" ":" #Method | *
17    /// ```
18    ///
19    /// # Example values
20    /// * `PUT, DELETE, XMODIFY`
21    /// * `*`
22    ///
23    /// # Examples
24    ///
25    /// ```
26    /// use rama_utils::collections::non_empty_vec;
27    /// use rama_http_types::Method;
28    /// use rama_http_headers::AccessControlAllowMethods;
29    ///
30    /// let allow_methods = AccessControlAllowMethods::new_values(
31    ///     non_empty_vec![Method::GET, Method::PUT],
32    /// );
33    ///
34    /// let allow_any_methods = AccessControlAllowMethods::new_any();
35    /// ```
36    pub struct AccessControlAllowMethods(pub ValuesOrAny<Method>);
37}
38
39#[cfg(test)]
40mod tests {
41    use super::super::{test_decode, test_encode};
42    use super::*;
43    use rama_utils::collections::non_empty_vec;
44
45    #[test]
46    fn decode_single() {
47        let allowed_methods = test_decode::<AccessControlAllowMethods>(&["GET, PUT"])
48            .unwrap()
49            .into_values()
50            .unwrap();
51
52        assert_eq!(allowed_methods.len(), 2);
53        assert_eq!(allowed_methods[0], Method::GET);
54        assert_eq!(allowed_methods[1], Method::PUT);
55    }
56
57    #[test]
58    fn decode_any() {
59        assert!(
60            test_decode::<AccessControlAllowMethods>(&["*"])
61                .unwrap()
62                .is_any()
63        );
64    }
65
66    #[test]
67    fn decode_any_with_trailer_value() {
68        let allowed_methods = test_decode::<AccessControlAllowMethods>(&["*, GET"])
69            .unwrap()
70            .into_values()
71            .unwrap();
72
73        assert_eq!(allowed_methods.len(), 2);
74        assert_eq!(allowed_methods[0], "*".parse::<Method>().unwrap());
75        assert_eq!(allowed_methods[1], Method::GET);
76    }
77
78    #[test]
79    fn decode_any_with_trailer_header() {
80        let allowed_methods = test_decode::<AccessControlAllowMethods>(&["*", "GET"])
81            .unwrap()
82            .into_values()
83            .unwrap();
84
85        assert_eq!(allowed_methods.len(), 2);
86        assert_eq!(allowed_methods[0], "*".parse::<Method>().unwrap());
87        assert_eq!(allowed_methods[1], Method::GET);
88    }
89
90    #[test]
91    fn decode_multi() {
92        let allowed_methods = test_decode::<AccessControlAllowMethods>(&["GET, PUT", "POST"])
93            .unwrap()
94            .into_values()
95            .unwrap();
96
97        assert_eq!(allowed_methods.len(), 3);
98        assert_eq!(allowed_methods[0], Method::GET);
99        assert_eq!(allowed_methods[1], Method::PUT);
100        assert_eq!(allowed_methods[2], Method::POST);
101    }
102
103    #[test]
104    fn decode_single_empty_value() {
105        assert!(test_decode::<AccessControlAllowMethods>(&[""]).is_none());
106    }
107
108    #[test]
109    fn decode_single_no_header_values() {
110        assert!(test_decode::<AccessControlAllowMethods>(&[]).is_none());
111    }
112
113    #[test]
114    fn encode_methods() {
115        let allow = AccessControlAllowMethods::new_values(non_empty_vec![Method::GET, Method::PUT]);
116
117        let headers = test_encode(allow);
118        assert_eq!(headers["access-control-allow-methods"], "GET, PUT");
119    }
120
121    #[test]
122    fn encodeany() {
123        let allow = AccessControlAllowMethods::new_any();
124
125        let headers = test_encode(allow);
126        assert_eq!(headers["access-control-allow-methods"], "*");
127    }
128}