1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-25 08:22:43 +01:00
actix-extras/src/header/common/if_modified_since.rs

53 lines
1.4 KiB
Rust
Raw Normal View History

2018-03-06 04:28:42 +01:00
use http::header;
use header::{Header, HttpDate, IntoHeaderValue};
use error::ParseError;
use httpmessage::HttpMessage;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct IfModifiedSince(pub HttpDate);
impl Header for IfModifiedSince {
fn name() -> header::HeaderName {
header::IF_MODIFIED_SINCE
}
fn parse<T: HttpMessage>(msg: &T) -> Result<Self, ParseError> {
let val = msg.headers().get(Self::name())
.ok_or(ParseError::Header)?.to_str().map_err(|_| ParseError::Header)?;
Ok(IfModifiedSince(val.parse()?))
}
}
impl IntoHeaderValue for IfModifiedSince {
type Error = header::InvalidHeaderValueBytes;
fn try_into(self) -> Result<header::HeaderValue, Self::Error> {
self.0.try_into()
}
}
#[cfg(test)]
mod tests {
use time::Tm;
use test::TestRequest;
use httpmessage::HttpMessage;
use super::HttpDate;
use super::IfModifiedSince;
fn date() -> HttpDate {
Tm {
tm_nsec: 0, tm_sec: 37, tm_min: 48, tm_hour: 8,
tm_mday: 7, tm_mon: 10, tm_year: 94,
tm_wday: 0, tm_isdst: 0, tm_yday: 0, tm_utcoff: 0}.into()
}
#[test]
fn test_if_mod_since() {
let req = TestRequest::with_hdr(IfModifiedSince(date())).finish();
let h = req.get::<IfModifiedSince>().unwrap();
assert_eq!(h.0, date());
}
}