aboutsummaryrefslogtreecommitdiff
path: root/src/response.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/response.rs')
-rw-r--r--src/response.rs55
1 files changed, 30 insertions, 25 deletions
diff --git a/src/response.rs b/src/response.rs
index ceb0f61..c8ff23a 100644
--- a/src/response.rs
+++ b/src/response.rs
@@ -10,6 +10,9 @@ use hyper::http::response::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;
}
pub fn redirect(location: &str, code: StatusCode) -> Builder {
@@ -18,31 +21,39 @@ pub fn redirect(location: &str, code: StatusCode) -> Builder {
impl SputnikBuilder for Builder {
fn content_type(mut self, mime: mime::Mime) -> Self {
- if let Some(headers) = self.headers_mut() {
- headers.insert(header::CONTENT_TYPE, mime.to_string().try_into().unwrap());
- }
+ 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
+}
+
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);
-
- /// Appends a Set-Cookie header to delete a cookie.
- fn delete_cookie(&mut self, name: &str);
}
impl SputnikHeaders for HeaderMap {
- fn set_cookie(&mut self, cookie: Cookie) {
- self.append(header::SET_COOKIE, cookie.encoded().to_string().try_into().unwrap());
+ fn content_type(&mut self, mime: mime::Mime) {
+ self.insert(header::CONTENT_TYPE, mime.to_string().try_into().unwrap());
}
- fn delete_cookie(&mut self, name: &str) {
- let mut cookie = Cookie::new(name, "");
- cookie.set_max_age(Duration::seconds(0));
- cookie.set_expires(OffsetDateTime::now_utc() - Duration::days(365));
- self.set_cookie(cookie);
+ fn set_cookie(&mut self, cookie: Cookie) {
+ self.append(header::SET_COOKIE, cookie.encoded().to_string().try_into().unwrap());
}
}
@@ -71,16 +82,10 @@ mod tests {
#[test]
fn test_content_type() {
- let resp = Builder::new()
- .content_type(mime::TEXT_PLAIN)
- .content_type(mime::TEXT_HTML)
- .body(hyper::Body::empty()).unwrap();
- assert_eq!(resp.headers().len(), 1);
- assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/html");
-
- // doesn't panic after invalid header
- Builder::new()
- .header("foo", "invalid\r\n")
- .content_type(mime::TEXT_HTML);
+ 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");
}
-} \ No newline at end of file
+}