1 use super::{EntityTag, IF_NONE_MATCH};
2 
3 crate::http::header::common_header! {
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 actix_web::HttpResponse;
36     /// use actix_web::http::header::IfNoneMatch;
37     ///
38     /// let mut builder = HttpResponse::Ok();
39     /// builder.insert_header(IfNoneMatch::Any);
40     /// ```
41     ///
42     /// ```
43     /// use actix_web::HttpResponse;
44     /// use actix_web::http::header::{IfNoneMatch, EntityTag};
45     ///
46     /// let mut builder = HttpResponse::Ok();
47     /// builder.insert_header(
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         crate::http::header::common_header_test!(test1, vec![b"\"xyzzy\""]);
59         crate::http::header::common_header_test!(test2, vec![b"W/\"xyzzy\""]);
60         crate::http::header::common_header_test!(test3, vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""]);
61         crate::http::header::common_header_test!(test4, vec![b"W/\"xyzzy\", W/\"r2d2xxxx\", W/\"c3piozzzz\""]);
62         crate::http::header::common_header_test!(test5, vec![b"*"]);
63     }
64 }
65 
66 #[cfg(test)]
67 mod tests {
68     use super::IfNoneMatch;
69     use crate::http::header::{EntityTag, Header, IF_NONE_MATCH};
70     use actix_http::test::TestRequest;
71 
72     #[test]
test_if_none_match()73     fn test_if_none_match() {
74         let mut if_none_match: Result<IfNoneMatch, _>;
75 
76         let req = TestRequest::default()
77             .insert_header((IF_NONE_MATCH, "*"))
78             .finish();
79         if_none_match = Header::parse(&req);
80         assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Any));
81 
82         let req = TestRequest::default()
83             .insert_header((IF_NONE_MATCH, &b"\"foobar\", W/\"weak-etag\""[..]))
84             .finish();
85 
86         if_none_match = Header::parse(&req);
87         let mut entities: Vec<EntityTag> = Vec::new();
88         let foobar_etag = EntityTag::new(false, "foobar".to_owned());
89         let weak_etag = EntityTag::new(true, "weak-etag".to_owned());
90         entities.push(foobar_etag);
91         entities.push(weak_etag);
92         assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Items(entities)));
93     }
94 }
95