Skip to main content

rama_http_headers/common/
pragma.rs

1use rama_http_types::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 rama_http_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    #[must_use]
36    pub fn no_cache() -> Self {
37        Self(HeaderValue::from_static("no-cache"))
38    }
39
40    /// Return whether this pragma is `no-cache`.
41    pub fn is_no_cache(&self) -> bool {
42        self.0 == "no-cache"
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::super::test_decode;
49    use super::Pragma;
50
51    #[test]
52    fn no_cache_is_no_cache() {
53        assert!(Pragma::no_cache().is_no_cache());
54    }
55
56    #[test]
57    fn etc_is_not_no_cache() {
58        let ext = test_decode::<Pragma>(&["dexter"]).unwrap();
59        assert!(!ext.is_no_cache());
60    }
61}