1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +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,8 +273,12 @@ impl Header for Range {
} }
} }
#[test] #[cfg(test)]
fn test_parse_bytes_range_valid() { mod tests {
use super::*;
#[test]
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();
let r2: Range = Header::parse_header(&"bytes=1-100,-".into()).unwrap(); let r2: Range = Header::parse_header(&"bytes=1-100,-".into()).unwrap();
let r3 = Range::bytes(1, 100); let r3 = Range::bytes(1, 100);
@ -302,10 +307,10 @@ fn test_parse_bytes_range_valid() {
let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap(); 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 r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
assert_eq!(r, r2); assert_eq!(r, r2);
} }
#[test] #[test]
fn test_parse_unregistered_range_valid() { fn test_parse_unregistered_range_valid() {
let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap(); 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 r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
assert_eq!(r, r2); assert_eq!(r, r2);
@ -317,10 +322,10 @@ fn test_parse_unregistered_range_valid() {
let r: Range = Header::parse_header(&"custom=xxx-yyy".into()).unwrap(); let r: Range = Header::parse_header(&"custom=xxx-yyy".into()).unwrap();
let r2 = Range::Unregistered("custom".to_owned(), "xxx-yyy".to_owned()); let r2 = Range::Unregistered("custom".to_owned(), "xxx-yyy".to_owned());
assert_eq!(r, r2); assert_eq!(r, r2);
} }
#[test] #[test]
fn test_parse_invalid() { fn test_parse_invalid() {
let r: ::Result<Range> = Header::parse_header(&"bytes=1-a,-".into()); let r: ::Result<Range> = Header::parse_header(&"bytes=1-a,-".into());
assert_eq!(r.ok(), None); assert_eq!(r.ok(), None);
@ -341,10 +346,10 @@ fn test_parse_invalid() {
let r: ::Result<Range> = Header::parse_header(&"=1-100".into()); let r: ::Result<Range> = Header::parse_header(&"=1-100".into());
assert_eq!(r.ok(), None); assert_eq!(r.ok(), None);
} }
#[test] #[test]
fn test_fmt() { fn test_fmt() {
use header::Headers; use header::Headers;
let mut headers = Headers::new(); let mut headers = Headers::new();
@ -361,16 +366,13 @@ 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");
} }
#[test] #[test]
fn test_byte_range_spec_to_satisfiable_range() { fn test_byte_range_spec_to_satisfiable_range() {
assert_eq!( assert_eq!(
Some((0, 0)), Some((0, 0)),
ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(3) ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(3)
@ -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,17 +137,22 @@ impl FromStr for Charset {
} }
} }
#[test] #[cfg(test)]
fn test_parse() { mod tests {
use super::*;
#[test]
fn test_parse() {
assert_eq!(Us_Ascii, "us-ascii".parse().unwrap()); assert_eq!(Us_Ascii, "us-ascii".parse().unwrap());
assert_eq!(Us_Ascii, "US-Ascii".parse().unwrap()); assert_eq!(Us_Ascii, "US-Ascii".parse().unwrap());
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!(Shift_Jis, "Shift-JIS".parse().unwrap());
assert_eq!(Ext("ABCD".to_owned()), "abcd".parse().unwrap()); assert_eq!(Ext("ABCD".to_owned()), "abcd".parse().unwrap());
} }
#[test] #[test]
fn test_display() { 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>);