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