1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 01:52:57 +01:00

conditional test compilation [range, charset] (#1483)

* conditionally compile range and charset tests

* remove deprecated try macros

Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
This commit is contained in:
Rob Ede 2020-05-03 14:33:29 +01:00 committed by GitHub
parent f37cb6dd0b
commit b521e9b221
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 159 additions and 175 deletions

View File

@ -6,6 +6,8 @@ use fxhash::FxHashMap;
#[derive(Default)] #[derive(Default)]
/// A type map of request extensions. /// A type map of request extensions.
pub struct Extensions { pub struct Extensions {
/// Use FxHasher with a std HashMap with for faster
/// lookups on the small `TypeId` (u64 equivalent) keys.
map: FxHashMap<TypeId, Box<dyn Any>>, map: FxHashMap<TypeId, Box<dyn Any>>,
} }

View File

@ -183,13 +183,13 @@ impl fmt::Display for Range {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
Range::Bytes(ref ranges) => { Range::Bytes(ref ranges) => {
try!(write!(f, "bytes=")); write!(f, "bytes=")?;
for (i, range) in ranges.iter().enumerate() { for (i, range) in ranges.iter().enumerate() {
if i != 0 { if i != 0 {
try!(f.write_str(",")); f.write_str(",")?;
} }
try!(Display::fmt(range, f)); Display::fmt(range, f)?;
} }
Ok(()) Ok(())
} }
@ -214,9 +214,9 @@ impl FromStr for Range {
} }
Ok(Range::Bytes(ranges)) Ok(Range::Bytes(ranges))
} }
(Some(unit), Some(range_str)) if unit != "" && range_str != "" => Ok( (Some(unit), Some(range_str)) if unit != "" && range_str != "" => {
Range::Unregistered(unit.to_owned(), range_str.to_owned()), Ok(Range::Unregistered(unit.to_owned(), range_str.to_owned()))
), }
_ => Err(::Error::Header), _ => Err(::Error::Header),
} }
} }
@ -229,7 +229,8 @@ impl FromStr for ByteRangeSpec {
let mut parts = s.splitn(2, '-'); let mut parts = s.splitn(2, '-');
match (parts.next(), parts.next()) { match (parts.next(), parts.next()) {
(Some(""), Some(end)) => end.parse() (Some(""), Some(end)) => end
.parse()
.or(Err(::Error::Header)) .or(Err(::Error::Header))
.map(ByteRangeSpec::Last), .map(ByteRangeSpec::Last),
(Some(start), Some("")) => start (Some(start), Some("")) => start
@ -272,6 +273,10 @@ impl Header for Range {
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test] #[test]
fn test_parse_bytes_range_valid() { fn test_parse_bytes_range_valid() {
let r: Range = Header::parse_header(&"bytes=1-100".into()).unwrap(); let r: Range = Header::parse_header(&"bytes=1-100".into()).unwrap();
@ -361,10 +366,7 @@ fn test_fmt() {
assert_eq!(&headers.to_string(), "Range: bytes=\r\n"); assert_eq!(&headers.to_string(), "Range: bytes=\r\n");
headers.clear(); headers.clear();
headers.set(Range::Unregistered( headers.set(Range::Unregistered("custom".to_owned(), "1-xxx".to_owned()));
"custom".to_owned(),
"1-xxx".to_owned(),
));
assert_eq!(&headers.to_string(), "Range: custom=1-xxx\r\n"); assert_eq!(&headers.to_string(), "Range: custom=1-xxx\r\n");
} }
@ -383,18 +385,9 @@ fn test_byte_range_spec_to_satisfiable_range() {
Some((1, 2)), Some((1, 2)),
ByteRangeSpec::FromTo(1, 5).to_satisfiable_range(3) ByteRangeSpec::FromTo(1, 5).to_satisfiable_range(3)
); );
assert_eq!( assert_eq!(None, ByteRangeSpec::FromTo(3, 3).to_satisfiable_range(3));
None, assert_eq!(None, ByteRangeSpec::FromTo(2, 1).to_satisfiable_range(3));
ByteRangeSpec::FromTo(3, 3).to_satisfiable_range(3) assert_eq!(None, ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(0));
);
assert_eq!(
None,
ByteRangeSpec::FromTo(2, 1).to_satisfiable_range(3)
);
assert_eq!(
None,
ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(0)
);
assert_eq!( assert_eq!(
Some((0, 2)), Some((0, 2)),
@ -404,31 +397,14 @@ fn test_byte_range_spec_to_satisfiable_range() {
Some((2, 2)), Some((2, 2)),
ByteRangeSpec::AllFrom(2).to_satisfiable_range(3) ByteRangeSpec::AllFrom(2).to_satisfiable_range(3)
); );
assert_eq!( assert_eq!(None, ByteRangeSpec::AllFrom(3).to_satisfiable_range(3));
None, assert_eq!(None, ByteRangeSpec::AllFrom(5).to_satisfiable_range(3));
ByteRangeSpec::AllFrom(3).to_satisfiable_range(3) assert_eq!(None, ByteRangeSpec::AllFrom(0).to_satisfiable_range(0));
);
assert_eq!(
None,
ByteRangeSpec::AllFrom(5).to_satisfiable_range(3)
);
assert_eq!(
None,
ByteRangeSpec::AllFrom(0).to_satisfiable_range(0)
);
assert_eq!( assert_eq!(Some((1, 2)), ByteRangeSpec::Last(2).to_satisfiable_range(3));
Some((1, 2)), assert_eq!(Some((2, 2)), ByteRangeSpec::Last(1).to_satisfiable_range(3));
ByteRangeSpec::Last(2).to_satisfiable_range(3) assert_eq!(Some((0, 2)), ByteRangeSpec::Last(5).to_satisfiable_range(3));
);
assert_eq!(
Some((2, 2)),
ByteRangeSpec::Last(1).to_satisfiable_range(3)
);
assert_eq!(
Some((0, 2)),
ByteRangeSpec::Last(5).to_satisfiable_range(3)
);
assert_eq!(None, ByteRangeSpec::Last(0).to_satisfiable_range(3)); assert_eq!(None, ByteRangeSpec::Last(0).to_satisfiable_range(3));
assert_eq!(None, ByteRangeSpec::Last(2).to_satisfiable_range(0)); assert_eq!(None, ByteRangeSpec::Last(2).to_satisfiable_range(0));
} }
}

View File

@ -137,6 +137,10 @@ impl FromStr for Charset {
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test] #[test]
fn test_parse() { fn test_parse() {
assert_eq!(Us_Ascii, "us-ascii".parse().unwrap()); assert_eq!(Us_Ascii, "us-ascii".parse().unwrap());
@ -151,3 +155,4 @@ fn test_display() {
assert_eq!("US-ASCII", format!("{}", Us_Ascii)); assert_eq!("US-ASCII", format!("{}", Us_Ascii));
assert_eq!("ABCD", format!("{}", Ext("ABCD".to_owned()))); assert_eq!("ABCD", format!("{}", Ext("ABCD".to_owned())));
} }
}

View File

@ -123,6 +123,7 @@ impl AppService {
} }
} }
/// Application connection config
#[derive(Clone)] #[derive(Clone)]
pub struct AppConfig(Rc<AppConfigInner>); pub struct AppConfig(Rc<AppConfigInner>);