requiem_http/header/common/
if_none_match.rs

1use crate::header::{EntityTag, IF_NONE_MATCH};
2
3header! {
4    /// `If-None-Match` header, defined in
5    /// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.2)
6    ///
7    /// The `If-None-Match` header field makes the request method conditional
8    /// on a recipient cache or origin server either not having any current
9    /// representation of the target resource, when the field-value is "*",
10    /// or having a selected representation with an entity-tag that does not
11    /// match any of those listed in the field-value.
12    ///
13    /// A recipient MUST use the weak comparison function when comparing
14    /// entity-tags for If-None-Match (Section 2.3.2), since weak entity-tags
15    /// can be used for cache validation even if there have been changes to
16    /// the representation data.
17    ///
18    /// # ABNF
19    ///
20    /// ```text
21    /// If-None-Match = "*" / 1#entity-tag
22    /// ```
23    ///
24    /// # Example values
25    ///
26    /// * `"xyzzy"`
27    /// * `W/"xyzzy"`
28    /// * `"xyzzy", "r2d2xxxx", "c3piozzzz"`
29    /// * `W/"xyzzy", W/"r2d2xxxx", W/"c3piozzzz"`
30    /// * `*`
31    ///
32    /// # Examples
33    ///
34    /// ```rust
35    /// use requiem_http::Response;
36    /// use requiem_http::http::header::IfNoneMatch;
37    ///
38    /// let mut builder = Response::Ok();
39    /// builder.set(IfNoneMatch::Any);
40    /// ```
41    ///
42    /// ```rust
43    /// use requiem_http::Response;
44    /// use requiem_http::http::header::{IfNoneMatch, EntityTag};
45    ///
46    /// let mut builder = Response::Ok();
47    /// builder.set(
48    ///     IfNoneMatch::Items(vec![
49    ///         EntityTag::new(false, "xyzzy".to_owned()),
50    ///         EntityTag::new(false, "foobar".to_owned()),
51    ///         EntityTag::new(false, "bazquux".to_owned()),
52    ///     ])
53    /// );
54    /// ```
55    (IfNoneMatch, IF_NONE_MATCH) => {Any / (EntityTag)+}
56
57    test_if_none_match {
58        test_header!(test1, vec![b"\"xyzzy\""]);
59        test_header!(test2, vec![b"W/\"xyzzy\""]);
60        test_header!(test3, vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""]);
61        test_header!(test4, vec![b"W/\"xyzzy\", W/\"r2d2xxxx\", W/\"c3piozzzz\""]);
62        test_header!(test5, vec![b"*"]);
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::IfNoneMatch;
69    use crate::header::{EntityTag, Header, IF_NONE_MATCH};
70    use crate::test::TestRequest;
71
72    #[test]
73    fn test_if_none_match() {
74        let mut if_none_match: Result<IfNoneMatch, _>;
75
76        let req = TestRequest::with_header(IF_NONE_MATCH, "*").finish();
77        if_none_match = Header::parse(&req);
78        assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Any));
79
80        let req =
81            TestRequest::with_header(IF_NONE_MATCH, &b"\"foobar\", W/\"weak-etag\""[..])
82                .finish();
83
84        if_none_match = Header::parse(&req);
85        let mut entities: Vec<EntityTag> = Vec::new();
86        let foobar_etag = EntityTag::new(false, "foobar".to_owned());
87        let weak_etag = EntityTag::new(true, "weak-etag".to_owned());
88        entities.push(foobar_etag);
89        entities.push(weak_etag);
90        assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Items(entities)));
91    }
92}