Skip to main content

hyper_sync/header/common/
etag.rs

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