Skip to main content

actix_web/http/header/
if_none_match.rs

1use super::{EntityTag, IF_NONE_MATCH};
2
3crate::http::header::common_header! {
4    /// `If-None-Match` header, defined
5    /// in [RFC 7232 ยง3.2](https://datatracker.ietf.org/doc/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    /// # Note
19    /// This is a request header used for cache validation (and conditional requests). Servers
20    /// should not send `If-None-Match` in responses; use [`ETag`](super::ETag) to describe the
21    /// current representation instead.
22    ///
23    /// # ABNF
24    /// ```plain
25    /// If-None-Match = "*" / 1#entity-tag
26    /// ```
27    ///
28    /// # Example Values
29    /// * `"xyzzy"`
30    /// * `W/"xyzzy"`
31    /// * `"xyzzy", "r2d2xxxx", "c3piozzzz"`
32    /// * `W/"xyzzy", W/"r2d2xxxx", W/"c3piozzzz"`
33    /// * `*`
34    ///
35    /// # Examples
36    /// ```
37    /// use actix_web::{http::header::IfNoneMatch, test};
38    ///
39    /// let req = test::TestRequest::default()
40    ///     .insert_header(IfNoneMatch::Any)
41    ///     .to_http_request();
42    /// # let _ = req;
43    /// ```
44    ///
45    /// ```
46    /// use actix_web::{http::header::{EntityTag, IfNoneMatch}, test};
47    ///
48    /// let req = test::TestRequest::default()
49    ///     .insert_header(IfNoneMatch::Items(vec![
50    ///         EntityTag::new(false, "xyzzy".to_owned()),
51    ///         EntityTag::new(false, "foobar".to_owned()),
52    ///         EntityTag::new(false, "bazquux".to_owned()),
53    ///     ]))
54    ///     .to_http_request();
55    /// # let _ = req;
56    /// ```
57    (IfNoneMatch, IF_NONE_MATCH) => {Any / (EntityTag)+}
58
59    test_parse_and_format {
60        crate::http::header::common_header_test!(test1, [b"\"xyzzy\""]);
61        crate::http::header::common_header_test!(test2, [b"W/\"xyzzy\""]);
62        crate::http::header::common_header_test!(test3, [b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""]);
63        crate::http::header::common_header_test!(test4, [b"W/\"xyzzy\", W/\"r2d2xxxx\", W/\"c3piozzzz\""]);
64        crate::http::header::common_header_test!(test5, [b"*"]);
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use actix_http::test::TestRequest;
71
72    use super::IfNoneMatch;
73    use crate::http::header::{EntityTag, Header, IF_NONE_MATCH};
74
75    #[test]
76    fn test_if_none_match() {
77        let req = TestRequest::default()
78            .insert_header((IF_NONE_MATCH, "*"))
79            .finish();
80
81        let mut if_none_match = IfNoneMatch::parse(&req);
82        assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Any));
83
84        let req = TestRequest::default()
85            .insert_header((IF_NONE_MATCH, &b"\"foobar\", W/\"weak-etag\""[..]))
86            .finish();
87
88        if_none_match = Header::parse(&req);
89        let mut entities: Vec<EntityTag> = Vec::new();
90        let foobar_etag = EntityTag::new_strong("foobar".to_owned());
91        let weak_etag = EntityTag::new_weak("weak-etag".to_owned());
92        entities.push(foobar_etag);
93        entities.push(weak_etag);
94        assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Items(entities)));
95    }
96}