1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +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,163 +273,138 @@ impl Header for Range {
} }
} }
#[test] #[cfg(test)]
fn test_parse_bytes_range_valid() { mod tests {
let r: Range = Header::parse_header(&"bytes=1-100".into()).unwrap(); use super::*;
let r2: Range = Header::parse_header(&"bytes=1-100,-".into()).unwrap();
let r3 = Range::bytes(1, 100);
assert_eq!(r, r2);
assert_eq!(r2, r3);
let r: Range = Header::parse_header(&"bytes=1-100,200-".into()).unwrap(); #[test]
let r2: Range = fn test_parse_bytes_range_valid() {
Header::parse_header(&"bytes= 1-100 , 101-xxx, 200- ".into()).unwrap(); let r: Range = Header::parse_header(&"bytes=1-100".into()).unwrap();
let r3 = Range::Bytes(vec![ let r2: Range = Header::parse_header(&"bytes=1-100,-".into()).unwrap();
ByteRangeSpec::FromTo(1, 100), let r3 = Range::bytes(1, 100);
ByteRangeSpec::AllFrom(200), assert_eq!(r, r2);
]); assert_eq!(r2, r3);
assert_eq!(r, r2);
assert_eq!(r2, r3);
let r: Range = Header::parse_header(&"bytes=1-100,-100".into()).unwrap(); let r: Range = Header::parse_header(&"bytes=1-100,200-".into()).unwrap();
let r2: Range = Header::parse_header(&"bytes=1-100, ,,-100".into()).unwrap(); let r2: Range =
let r3 = Range::Bytes(vec![ Header::parse_header(&"bytes= 1-100 , 101-xxx, 200- ".into()).unwrap();
ByteRangeSpec::FromTo(1, 100), let r3 = Range::Bytes(vec![
ByteRangeSpec::Last(100), ByteRangeSpec::FromTo(1, 100),
]); ByteRangeSpec::AllFrom(200),
assert_eq!(r, r2); ]);
assert_eq!(r2, r3); assert_eq!(r, r2);
assert_eq!(r2, r3);
let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap(); let r: Range = Header::parse_header(&"bytes=1-100,-100".into()).unwrap();
let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned()); let r2: Range = Header::parse_header(&"bytes=1-100, ,,-100".into()).unwrap();
assert_eq!(r, r2); let r3 = Range::Bytes(vec![
} ByteRangeSpec::FromTo(1, 100),
ByteRangeSpec::Last(100),
#[test] ]);
fn test_parse_unregistered_range_valid() { assert_eq!(r, r2);
let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap(); assert_eq!(r2, r3);
let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
assert_eq!(r, r2); let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap();
let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
let r: Range = Header::parse_header(&"custom=abcd".into()).unwrap(); assert_eq!(r, r2);
let r2 = Range::Unregistered("custom".to_owned(), "abcd".to_owned()); }
assert_eq!(r, r2);
#[test]
let r: Range = Header::parse_header(&"custom=xxx-yyy".into()).unwrap(); fn test_parse_unregistered_range_valid() {
let r2 = Range::Unregistered("custom".to_owned(), "xxx-yyy".to_owned()); let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap();
assert_eq!(r, r2); let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
} assert_eq!(r, r2);
#[test] let r: Range = Header::parse_header(&"custom=abcd".into()).unwrap();
fn test_parse_invalid() { let r2 = Range::Unregistered("custom".to_owned(), "abcd".to_owned());
let r: ::Result<Range> = Header::parse_header(&"bytes=1-a,-".into()); assert_eq!(r, r2);
assert_eq!(r.ok(), None);
let r: Range = Header::parse_header(&"custom=xxx-yyy".into()).unwrap();
let r: ::Result<Range> = Header::parse_header(&"bytes=1-2-3".into()); let r2 = Range::Unregistered("custom".to_owned(), "xxx-yyy".to_owned());
assert_eq!(r.ok(), None); assert_eq!(r, r2);
}
let r: ::Result<Range> = Header::parse_header(&"abc".into());
assert_eq!(r.ok(), None); #[test]
fn test_parse_invalid() {
let r: ::Result<Range> = Header::parse_header(&"bytes=1-100=".into()); let r: ::Result<Range> = Header::parse_header(&"bytes=1-a,-".into());
assert_eq!(r.ok(), None); assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"bytes=".into()); let r: ::Result<Range> = Header::parse_header(&"bytes=1-2-3".into());
assert_eq!(r.ok(), None); assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"custom=".into()); let r: ::Result<Range> = Header::parse_header(&"abc".into());
assert_eq!(r.ok(), None); assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"=1-100".into()); let r: ::Result<Range> = Header::parse_header(&"bytes=1-100=".into());
assert_eq!(r.ok(), None); assert_eq!(r.ok(), None);
}
let r: ::Result<Range> = Header::parse_header(&"bytes=".into());
#[test] assert_eq!(r.ok(), None);
fn test_fmt() {
use header::Headers; let r: ::Result<Range> = Header::parse_header(&"custom=".into());
assert_eq!(r.ok(), None);
let mut headers = Headers::new();
let r: ::Result<Range> = Header::parse_header(&"=1-100".into());
headers.set(Range::Bytes(vec![ assert_eq!(r.ok(), None);
ByteRangeSpec::FromTo(0, 1000), }
ByteRangeSpec::AllFrom(2000),
])); #[test]
assert_eq!(&headers.to_string(), "Range: bytes=0-1000,2000-\r\n"); fn test_fmt() {
use header::Headers;
headers.clear();
headers.set(Range::Bytes(vec![])); let mut headers = Headers::new();
assert_eq!(&headers.to_string(), "Range: bytes=\r\n"); headers.set(Range::Bytes(vec![
ByteRangeSpec::FromTo(0, 1000),
headers.clear(); ByteRangeSpec::AllFrom(2000),
headers.set(Range::Unregistered( ]));
"custom".to_owned(), assert_eq!(&headers.to_string(), "Range: bytes=0-1000,2000-\r\n");
"1-xxx".to_owned(),
)); headers.clear();
headers.set(Range::Bytes(vec![]));
assert_eq!(&headers.to_string(), "Range: custom=1-xxx\r\n");
} assert_eq!(&headers.to_string(), "Range: bytes=\r\n");
#[test] headers.clear();
fn test_byte_range_spec_to_satisfiable_range() { headers.set(Range::Unregistered("custom".to_owned(), "1-xxx".to_owned()));
assert_eq!(
Some((0, 0)), assert_eq!(&headers.to_string(), "Range: custom=1-xxx\r\n");
ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(3) }
);
assert_eq!( #[test]
Some((1, 2)), fn test_byte_range_spec_to_satisfiable_range() {
ByteRangeSpec::FromTo(1, 2).to_satisfiable_range(3) assert_eq!(
); Some((0, 0)),
assert_eq!( ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(3)
Some((1, 2)), );
ByteRangeSpec::FromTo(1, 5).to_satisfiable_range(3) assert_eq!(
); Some((1, 2)),
assert_eq!( ByteRangeSpec::FromTo(1, 2).to_satisfiable_range(3)
None, );
ByteRangeSpec::FromTo(3, 3).to_satisfiable_range(3) assert_eq!(
); Some((1, 2)),
assert_eq!( ByteRangeSpec::FromTo(1, 5).to_satisfiable_range(3)
None, );
ByteRangeSpec::FromTo(2, 1).to_satisfiable_range(3) assert_eq!(None, ByteRangeSpec::FromTo(3, 3).to_satisfiable_range(3));
); assert_eq!(None, ByteRangeSpec::FromTo(2, 1).to_satisfiable_range(3));
assert_eq!( assert_eq!(None, ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(0));
None,
ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(0) assert_eq!(
); Some((0, 2)),
ByteRangeSpec::AllFrom(0).to_satisfiable_range(3)
assert_eq!( );
Some((0, 2)), assert_eq!(
ByteRangeSpec::AllFrom(0).to_satisfiable_range(3) Some((2, 2)),
); ByteRangeSpec::AllFrom(2).to_satisfiable_range(3)
assert_eq!( );
Some((2, 2)), assert_eq!(None, ByteRangeSpec::AllFrom(3).to_satisfiable_range(3));
ByteRangeSpec::AllFrom(2).to_satisfiable_range(3) assert_eq!(None, ByteRangeSpec::AllFrom(5).to_satisfiable_range(3));
); assert_eq!(None, ByteRangeSpec::AllFrom(0).to_satisfiable_range(0));
assert_eq!(
None, assert_eq!(Some((1, 2)), ByteRangeSpec::Last(2).to_satisfiable_range(3));
ByteRangeSpec::AllFrom(3).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!( assert_eq!(None, ByteRangeSpec::Last(0).to_satisfiable_range(3));
None, assert_eq!(None, ByteRangeSpec::Last(2).to_satisfiable_range(0));
ByteRangeSpec::AllFrom(5).to_satisfiable_range(3) }
);
assert_eq!(
None,
ByteRangeSpec::AllFrom(0).to_satisfiable_range(0)
);
assert_eq!(
Some((1, 2)),
ByteRangeSpec::Last(2).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(2).to_satisfiable_range(0));
} }

View File

@ -137,17 +137,22 @@ impl FromStr for Charset {
} }
} }
#[test] #[cfg(test)]
fn test_parse() { mod tests {
assert_eq!(Us_Ascii, "us-ascii".parse().unwrap()); use super::*;
assert_eq!(Us_Ascii, "US-Ascii".parse().unwrap());
assert_eq!(Us_Ascii, "US-ASCII".parse().unwrap());
assert_eq!(Shift_Jis, "Shift-JIS".parse().unwrap());
assert_eq!(Ext("ABCD".to_owned()), "abcd".parse().unwrap());
}
#[test] #[test]
fn test_display() { fn test_parse() {
assert_eq!("US-ASCII", format!("{}", Us_Ascii)); assert_eq!(Us_Ascii, "us-ascii".parse().unwrap());
assert_eq!("ABCD", format!("{}", Ext("ABCD".to_owned()))); assert_eq!(Us_Ascii, "US-Ascii".parse().unwrap());
assert_eq!(Us_Ascii, "US-ASCII".parse().unwrap());
assert_eq!(Shift_Jis, "Shift-JIS".parse().unwrap());
assert_eq!(Ext("ABCD".to_owned()), "abcd".parse().unwrap());
}
#[test]
fn test_display() {
assert_eq!("US-ASCII", format!("{}", Us_Ascii));
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>);