aboutsummaryrefslogtreecommitdiff
path: root/src/response.rs
blob: 2d5d46910bd87748d44ed87073304e897232db1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! Provides convenience traits and functions to build HTTP responses.

use std::convert::TryInto;

use cookie::Cookie;
use hyper::{HeaderMap, StatusCode, header, http};
use time::{Duration, OffsetDateTime};
use hyper::http::response::Builder;

/// Adds convenience methods to [`Builder`].
pub trait SputnikBuilder {
    /// Sets the Content-Type.
    fn content_type(self, mime: mime::Mime) -> Builder;

    /// Appends the Set-Cookie header.
    fn set_cookie(self, cookie: Cookie) -> Builder;
}

/// Creates a new builder with a given Location header and status code.
pub fn redirect(location: &str, code: StatusCode) -> Builder {
    Builder::new().status(code).header(header::LOCATION, location)
}

impl SputnikBuilder for Builder {
    fn content_type(mut self, mime: mime::Mime) -> Self {
        self.headers_mut().map(|h| h.content_type(mime));
        self
    }

    fn set_cookie(mut self, cookie: Cookie) -> Builder {
        self.headers_mut().map(|h| h.set_cookie(cookie));
        self
    }
}

/// Constructs an expired cookie to delete a cookie.
pub fn delete_cookie(name: &str) -> Cookie {
    let mut cookie = Cookie::new(name, "");
    cookie.set_max_age(Duration::seconds(0));
    cookie.set_expires(OffsetDateTime::now_utc() - Duration::days(365));
    cookie
}

/// Adds convenience methods to [`HeaderMap`].
pub trait SputnikHeaders {
    /// Sets the Content-Type.
    fn content_type(&mut self, mime: mime::Mime);

    /// Appends a Set-Cookie header.
    fn set_cookie(&mut self, cookie: Cookie);
}

impl SputnikHeaders for HeaderMap {
    fn content_type(&mut self, mime: mime::Mime) {
        self.insert(header::CONTENT_TYPE, mime.to_string().try_into().unwrap());
    }

    fn set_cookie(&mut self, cookie: Cookie) {
        self.append(header::SET_COOKIE, cookie.encoded().to_string().try_into().unwrap());
    }
}

/// Adds a convenience method to consume a [`Builder`] with an empty body.
pub trait EmptyBuilder<B> {
    /// Consume the builder with an empty body.
    fn empty(self) -> http::Result<http::response::Response<B>>;
}

impl EmptyBuilder<hyper::Body> for Builder {
    fn empty(self) -> http::Result<http::response::Response<hyper::Body>> {
        self.body(hyper::Body::empty())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_set_cookie() {
        let mut map = HeaderMap::new();
        map.set_cookie(Cookie::new("some", "cookie"));
        map.set_cookie(Cookie::new("some", "cookie"));
        assert_eq!(map.len(), 2);
    }

    #[test]
    fn test_content_type() {
        let mut map = HeaderMap::new();
        map.content_type(mime::TEXT_PLAIN);
        map.content_type(mime::TEXT_HTML);
        assert_eq!(map.len(), 1);
        assert_eq!(map.get(header::CONTENT_TYPE).unwrap(), "text/html");
    }
}