actix_web/http/header/if_match.rs
1use super::{common_header, EntityTag, IF_MATCH};
2
3common_header! {
4 /// `If-Match` header, defined
5 /// in [RFC 7232 ยง3.1](https://datatracker.ietf.org/doc/html/rfc7232#section-3.1)
6 ///
7 /// The `If-Match` header field makes the request method conditional on
8 /// the recipient origin server either having at least one current
9 /// representation of the target resource, when the field-value is "*",
10 /// or having a current representation of the target resource that has an
11 /// entity-tag matching a member of the list of entity-tags provided in
12 /// the field-value.
13 ///
14 /// An origin server MUST use the strong comparison function when
15 /// comparing entity-tags for `If-Match`, since the client
16 /// intends this precondition to prevent the method from being applied if
17 /// there have been any changes to the representation data.
18 ///
19 /// # Note
20 /// This is a request header used for conditional requests (typically to avoid lost updates).
21 /// Servers should not send `If-Match` in responses; use [`ETag`](super::ETag) to describe the
22 /// current representation instead.
23 ///
24 /// # ABNF
25 /// ```plain
26 /// If-Match = "*" / 1#entity-tag
27 /// ```
28 ///
29 /// # Example Values
30 /// * `"xyzzy"`
31 /// * "xyzzy", "r2d2xxxx", "c3piozzzz"
32 ///
33 /// # Examples
34 /// ```
35 /// use actix_web::{http::header::IfMatch, test};
36 ///
37 /// let req = test::TestRequest::default()
38 /// .insert_header(IfMatch::Any)
39 /// .to_http_request();
40 /// # let _ = req;
41 /// ```
42 ///
43 /// ```
44 /// use actix_web::{http::header::{EntityTag, IfMatch}, test};
45 ///
46 /// let req = test::TestRequest::default()
47 /// .insert_header(IfMatch::Items(vec![
48 /// EntityTag::new(false, "xyzzy".to_owned()),
49 /// EntityTag::new(false, "foobar".to_owned()),
50 /// EntityTag::new(false, "bazquux".to_owned()),
51 /// ]))
52 /// .to_http_request();
53 /// # let _ = req;
54 /// ```
55 (IfMatch, IF_MATCH) => {Any / (EntityTag)+}
56
57 test_parse_and_format {
58 crate::http::header::common_header_test!(
59 test1,
60 [b"\"xyzzy\""],
61 Some(HeaderField::Items(
62 vec![EntityTag::new_strong("xyzzy".to_owned())])));
63
64 crate::http::header::common_header_test!(
65 test2,
66 [b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""],
67 Some(HeaderField::Items(
68 vec![EntityTag::new_strong("xyzzy".to_owned()),
69 EntityTag::new_strong("r2d2xxxx".to_owned()),
70 EntityTag::new_strong("c3piozzzz".to_owned())])));
71 crate::http::header::common_header_test!(test3, [b"*"], Some(IfMatch::Any));
72 }
73}