headers/common/
if_match.rs1use super::ETag;
2use util::EntityTagRange;
3use HeaderValue;
4
5#[derive(Clone, Debug, PartialEq)]
40pub struct IfMatch(EntityTagRange);
41
42derive_header! {
43 IfMatch(_),
44 name: IF_MATCH
45}
46
47impl IfMatch {
48 pub fn any() -> IfMatch {
50 IfMatch(EntityTagRange::Any)
51 }
52
53 pub fn is_any(&self) -> bool {
55 match self.0 {
56 EntityTagRange::Any => true,
57 EntityTagRange::Tags(..) => false,
58 }
59 }
60
61 pub fn precondition_passes(&self, etag: &ETag) -> bool {
63 self.0.matches_strong(&etag.0)
64 }
65}
66
67impl From<ETag> for IfMatch {
68 fn from(etag: ETag) -> IfMatch {
69 IfMatch(EntityTagRange::Tags(HeaderValue::from(etag.0).into()))
70 }
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn is_any() {
79 assert!(IfMatch::any().is_any());
80 assert!(!IfMatch::from(ETag::from_static("\"yolo\"")).is_any());
81 }
82
83 #[test]
84 fn precondition_fails() {
85 let if_match = IfMatch::from(ETag::from_static("\"foo\""));
86
87 let bar = ETag::from_static("\"bar\"");
88 let weak_foo = ETag::from_static("W/\"foo\"");
89
90 assert!(!if_match.precondition_passes(&bar));
91 assert!(!if_match.precondition_passes(&weak_foo));
92 }
93
94 #[test]
95 fn precondition_passes() {
96 let foo = ETag::from_static("\"foo\"");
97
98 let if_match = IfMatch::from(foo.clone());
99
100 assert!(if_match.precondition_passes(&foo));
101 }
102
103 #[test]
104 fn precondition_any() {
105 let foo = ETag::from_static("\"foo\"");
106
107 let if_match = IfMatch::any();
108
109 assert!(if_match.precondition_passes(&foo));
110 }
111}