hyperx/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 /// # extern crate http;
36 /// use hyperx::header::{IfNoneMatch, TypedHeaders};
37 ///
38 /// let mut headers = http::HeaderMap::new();
39 /// headers.encode(&IfNoneMatch::Any);
40 /// ```
41 ///
42 /// ```
43 /// # extern crate http;
44 /// use hyperx::header::{IfNoneMatch, EntityTag, TypedHeaders};
45 ///
46 /// let mut headers = http::HeaderMap::new();
47 /// headers.encode(
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 header::{Header, Raw};
70 use header::EntityTag;
71
72 #[test]
73 fn test_if_none_match() {
74 let mut if_none_match: ::Result<IfNoneMatch>;
75
76 let r: Raw = b"*".as_ref().into();
77 if_none_match = Header::parse_header(&r);
78 assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Any));
79
80 let r: Raw = b"\"foobar\", W/\"weak-etag\"".as_ref().into();
81 if_none_match = Header::parse_header(&r);
82 let mut entities: Vec<EntityTag> = Vec::new();
83 let foobar_etag = EntityTag::new(false, "foobar".to_owned());
84 let weak_etag = EntityTag::new(true, "weak-etag".to_owned());
85 entities.push(foobar_etag);
86 entities.push(weak_etag);
87 assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Items(entities)));
88 }
89}
90
91bench_header!(bench, IfNoneMatch, { vec![b"W/\"nonemptytag\"".to_vec()] });
92
93standard_header!(IfNoneMatch, IF_NONE_MATCH);