cogo_http/header/common/
if_none_match.rs

1use crate::header::EntityTag;
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    /// ```plain
20    /// If-None-Match = "*" / 1#entity-tag
21    /// ```
22    ///
23    /// # Example values
24    /// * `"xyzzy"`
25    /// * `W/"xyzzy"`
26    /// * `"xyzzy", "r2d2xxxx", "c3piozzzz"`
27    /// * `W/"xyzzy", W/"r2d2xxxx", W/"c3piozzzz"`
28    /// * `*`
29    ///
30    /// # Examples
31    /// ```
32    /// use cogo_http::header::{Headers, IfNoneMatch};
33    ///
34    /// let mut headers = Headers::new();
35    /// headers.set(IfNoneMatch::Any);
36    /// ```
37    /// ```
38    /// use cogo_http::header::{Headers, IfNoneMatch, EntityTag};
39    ///
40    /// let mut headers = Headers::new();
41    /// headers.set(
42    ///     IfNoneMatch::Items(vec![
43    ///         EntityTag::new(false, "xyzzy".to_owned()),
44    ///         EntityTag::new(false, "foobar".to_owned()),
45    ///         EntityTag::new(false, "bazquux".to_owned()),
46    ///     ])
47    /// );
48    /// ```
49    (IfNoneMatch, "If-None-Match") => {Any / (EntityTag)+}
50
51    test_if_none_match {
52        test_header!(test1, vec![b"\"xyzzy\""]);
53        test_header!(test2, vec![b"W/\"xyzzy\""]);
54        test_header!(test3, vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""]);
55        test_header!(test4, vec![b"W/\"xyzzy\", W/\"r2d2xxxx\", W/\"c3piozzzz\""]);
56        test_header!(test5, vec![b"*"]);
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::IfNoneMatch;
63    use crate::header::Header;
64    use crate::header::EntityTag;
65
66    #[test]
67    fn test_if_none_match() {
68        let mut if_none_match: crate::Result<IfNoneMatch>;
69
70        if_none_match = Header::parse_header([b"*".to_vec()].as_ref());
71        assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Any));
72
73        if_none_match = Header::parse_header([b"\"foobar\", W/\"weak-etag\"".to_vec()].as_ref());
74        let mut entities: Vec<EntityTag> = Vec::new();
75        let foobar_etag = EntityTag::new(false, "foobar".to_owned());
76        let weak_etag = EntityTag::new(true, "weak-etag".to_owned());
77        entities.push(foobar_etag);
78        entities.push(weak_etag);
79        assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Items(entities)));
80    }
81}
82
83bench_header!(bench, IfNoneMatch, { vec![b"W/\"nonemptytag\"".to_vec()] });