headers_ext/common/
pragma.rs

1use ::HeaderValue;
2
3/// The `Pragma` header defined by HTTP/1.0.
4///
5/// > The "Pragma" header field allows backwards compatibility with
6/// > HTTP/1.0 caches, so that clients can specify a "no-cache" request
7/// > that they will understand (as Cache-Control was not defined until
8/// > HTTP/1.1).  When the Cache-Control header field is also present and
9/// > understood in a request, Pragma is ignored.
10/// > In HTTP/1.0, Pragma was defined as an extensible field for
11/// > implementation-specified directives for recipients.  This
12/// > specification deprecates such extensions to improve interoperability.
13///
14/// Spec: [https://tools.ietf.org/html/rfc7234#section-5.4][url]
15///
16/// [url]: https://tools.ietf.org/html/rfc7234#section-5.4
17///
18/// # Examples
19///
20/// ```
21/// # extern crate headers_ext as headers;
22/// use headers::Pragma;
23///
24/// let pragma = Pragma::no_cache();
25/// ```
26#[derive(Clone, Debug, PartialEq, Header)]
27pub struct Pragma(HeaderValue);
28
29impl Pragma {
30    /// Construct the literal `no-cache` Pragma header.
31    pub fn no_cache() -> Pragma {
32        Pragma(HeaderValue::from_static("no-cache"))
33    }
34
35    /// Return whether this pragma is `no-cache`.
36    pub fn is_no_cache(&self) -> bool {
37        self.0 == "no-cache"
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::Pragma;
44    use super::super::test_decode;
45
46    #[test]
47    fn no_cache_is_no_cache() {
48        assert!(Pragma::no_cache().is_no_cache());
49    }
50
51    #[test]
52    fn etc_is_not_no_cache() {
53        let ext = test_decode::<Pragma>(&["dexter"]).unwrap();
54        assert!(!ext.is_no_cache());
55    }
56}