headers/common/
pragma.rs

1use http::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/// use headers::Pragma;
22///
23/// let pragma = Pragma::no_cache();
24/// ```
25#[derive(Clone, Debug, PartialEq)]
26pub struct Pragma(HeaderValue);
27
28derive_header! {
29    Pragma(_),
30    name: PRAGMA
31}
32
33impl Pragma {
34    /// Construct the literal `no-cache` Pragma header.
35    pub fn no_cache() -> Pragma {
36        Pragma(HeaderValue::from_static("no-cache"))
37    }
38
39    /// Return whether this pragma is `no-cache`.
40    pub fn is_no_cache(&self) -> bool {
41        self.0 == "no-cache"
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::super::test_decode;
48    use super::Pragma;
49
50    #[test]
51    fn no_cache_is_no_cache() {
52        assert!(Pragma::no_cache().is_no_cache());
53    }
54
55    #[test]
56    fn etc_is_not_no_cache() {
57        let ext = test_decode::<Pragma>(&["dexter"]).unwrap();
58        assert!(!ext.is_no_cache());
59    }
60}