actix_web/http/header/
etag.rs

1use super::{EntityTag, ETAG};
2
3crate::http::header::common_header! {
4    /// `ETag` header, defined in
5    /// [RFC 7232 ยง2.3](https://datatracker.ietf.org/doc/html/rfc7232#section-2.3)
6    ///
7    /// The `ETag` header field in a response provides the current entity-tag
8    /// for the selected representation, as determined at the conclusion of
9    /// handling the request.  An entity-tag is an opaque validator for
10    /// differentiating between multiple representations of the same
11    /// resource, regardless of whether those multiple representations are
12    /// due to resource state changes over time, content negotiation
13    /// resulting in multiple representations being valid at the same time,
14    /// or both.  An entity-tag consists of an opaque quoted string, possibly
15    /// prefixed by a weakness indicator.
16    ///
17    /// # ABNF
18    /// ```plain
19    /// ETag       = entity-tag
20    /// ```
21    ///
22    /// # Example Values
23    /// * `"xyzzy"`
24    /// * `W/"xyzzy"`
25    /// * `""`
26    ///
27    /// # Examples
28    /// ```
29    /// use actix_web::HttpResponse;
30    /// use actix_web::http::header::{ETag, EntityTag};
31    ///
32    /// let mut builder = HttpResponse::Ok();
33    /// builder.insert_header(
34    ///     ETag(EntityTag::new_strong("xyzzy".to_owned()))
35    /// );
36    /// ```
37    ///
38    /// ```
39    /// use actix_web::HttpResponse;
40    /// use actix_web::http::header::{ETag, EntityTag};
41    ///
42    /// let mut builder = HttpResponse::Ok();
43    /// builder.insert_header(
44    ///     ETag(EntityTag::new_weak("xyzzy".to_owned()))
45    /// );
46    /// ```
47    (ETag, ETAG) => [EntityTag]
48
49    test_parse_and_format {
50        // From the RFC
51        crate::http::header::common_header_test!(test1,
52            [b"\"xyzzy\""],
53            Some(ETag(EntityTag::new_strong("xyzzy".to_owned()))));
54        crate::http::header::common_header_test!(test2,
55            [b"W/\"xyzzy\""],
56            Some(ETag(EntityTag::new_weak("xyzzy".to_owned()))));
57        crate::http::header::common_header_test!(test3,
58            [b"\"\""],
59            Some(ETag(EntityTag::new_strong("".to_owned()))));
60        // Own tests
61        crate::http::header::common_header_test!(test4,
62            [b"\"foobar\""],
63            Some(ETag(EntityTag::new_strong("foobar".to_owned()))));
64        crate::http::header::common_header_test!(test5,
65            [b"\"\""],
66            Some(ETag(EntityTag::new_strong("".to_owned()))));
67        crate::http::header::common_header_test!(test6,
68            [b"W/\"weak-etag\""],
69            Some(ETag(EntityTag::new_weak("weak-etag".to_owned()))));
70        crate::http::header::common_header_test!(test7,
71            [b"W/\"\x65\x62\""],
72            Some(ETag(EntityTag::new_weak("\u{0065}\u{0062}".to_owned()))));
73        crate::http::header::common_header_test!(test8,
74            [b"W/\"\""],
75            Some(ETag(EntityTag::new_weak("".to_owned()))));
76        crate::http::header::common_header_test!(test9,
77            [b"no-dquotes"],
78            None::<ETag>);
79        crate::http::header::common_header_test!(test10,
80            [b"w/\"the-first-w-is-case-sensitive\""],
81            None::<ETag>);
82        crate::http::header::common_header_test!(test11,
83            [b""],
84            None::<ETag>);
85        crate::http::header::common_header_test!(test12,
86            [b"\"unmatched-dquotes1"],
87            None::<ETag>);
88        crate::http::header::common_header_test!(test13,
89            [b"unmatched-dquotes2\""],
90            None::<ETag>);
91        crate::http::header::common_header_test!(test14,
92            [b"matched-\"dquotes\""],
93            None::<ETag>);
94        crate::http::header::common_header_test!(test15,
95            [b"\""],
96            None::<ETag>);
97    }
98}