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(msg: &T) -> Result { 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 { 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::().unwrap(); assert_eq!(h.0, date()); } }