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