mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 00:21:08 +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:
parent
f37cb6dd0b
commit
b521e9b221
@ -6,6 +6,8 @@ use fxhash::FxHashMap;
|
||||
#[derive(Default)]
|
||||
/// A type map of request 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>>,
|
||||
}
|
||||
|
||||
|
@ -183,13 +183,13 @@ impl fmt::Display for Range {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Range::Bytes(ref ranges) => {
|
||||
try!(write!(f, "bytes="));
|
||||
write!(f, "bytes=")?;
|
||||
|
||||
for (i, range) in ranges.iter().enumerate() {
|
||||
if i != 0 {
|
||||
try!(f.write_str(","));
|
||||
f.write_str(",")?;
|
||||
}
|
||||
try!(Display::fmt(range, f));
|
||||
Display::fmt(range, f)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -214,9 +214,9 @@ impl FromStr for Range {
|
||||
}
|
||||
Ok(Range::Bytes(ranges))
|
||||
}
|
||||
(Some(unit), Some(range_str)) if unit != "" && range_str != "" => Ok(
|
||||
Range::Unregistered(unit.to_owned(), range_str.to_owned()),
|
||||
),
|
||||
(Some(unit), Some(range_str)) if unit != "" && range_str != "" => {
|
||||
Ok(Range::Unregistered(unit.to_owned(), range_str.to_owned()))
|
||||
}
|
||||
_ => Err(::Error::Header),
|
||||
}
|
||||
}
|
||||
@ -229,7 +229,8 @@ impl FromStr for ByteRangeSpec {
|
||||
let mut parts = s.splitn(2, '-');
|
||||
|
||||
match (parts.next(), parts.next()) {
|
||||
(Some(""), Some(end)) => end.parse()
|
||||
(Some(""), Some(end)) => end
|
||||
.parse()
|
||||
.or(Err(::Error::Header))
|
||||
.map(ByteRangeSpec::Last),
|
||||
(Some(start), Some("")) => start
|
||||
@ -272,8 +273,12 @@ impl Header for Range {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_bytes_range_valid() {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parse_bytes_range_valid() {
|
||||
let r: 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);
|
||||
@ -302,10 +307,10 @@ fn test_parse_bytes_range_valid() {
|
||||
let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap();
|
||||
let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
|
||||
assert_eq!(r, r2);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_unregistered_range_valid() {
|
||||
#[test]
|
||||
fn test_parse_unregistered_range_valid() {
|
||||
let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap();
|
||||
let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
|
||||
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 r2 = Range::Unregistered("custom".to_owned(), "xxx-yyy".to_owned());
|
||||
assert_eq!(r, r2);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_invalid() {
|
||||
#[test]
|
||||
fn test_parse_invalid() {
|
||||
let r: ::Result<Range> = Header::parse_header(&"bytes=1-a,-".into());
|
||||
assert_eq!(r.ok(), None);
|
||||
|
||||
@ -341,10 +346,10 @@ fn test_parse_invalid() {
|
||||
|
||||
let r: ::Result<Range> = Header::parse_header(&"=1-100".into());
|
||||
assert_eq!(r.ok(), None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fmt() {
|
||||
#[test]
|
||||
fn test_fmt() {
|
||||
use header::Headers;
|
||||
|
||||
let mut headers = Headers::new();
|
||||
@ -361,16 +366,13 @@ fn test_fmt() {
|
||||
assert_eq!(&headers.to_string(), "Range: bytes=\r\n");
|
||||
|
||||
headers.clear();
|
||||
headers.set(Range::Unregistered(
|
||||
"custom".to_owned(),
|
||||
"1-xxx".to_owned(),
|
||||
));
|
||||
headers.set(Range::Unregistered("custom".to_owned(), "1-xxx".to_owned()));
|
||||
|
||||
assert_eq!(&headers.to_string(), "Range: custom=1-xxx\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_byte_range_spec_to_satisfiable_range() {
|
||||
#[test]
|
||||
fn test_byte_range_spec_to_satisfiable_range() {
|
||||
assert_eq!(
|
||||
Some((0, 0)),
|
||||
ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(3)
|
||||
@ -383,18 +385,9 @@ fn test_byte_range_spec_to_satisfiable_range() {
|
||||
Some((1, 2)),
|
||||
ByteRangeSpec::FromTo(1, 5).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!(
|
||||
None,
|
||||
ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(0)
|
||||
);
|
||||
assert_eq!(None, ByteRangeSpec::FromTo(3, 3).to_satisfiable_range(3));
|
||||
assert_eq!(None, ByteRangeSpec::FromTo(2, 1).to_satisfiable_range(3));
|
||||
assert_eq!(None, ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(0));
|
||||
|
||||
assert_eq!(
|
||||
Some((0, 2)),
|
||||
@ -404,31 +397,14 @@ fn test_byte_range_spec_to_satisfiable_range() {
|
||||
Some((2, 2)),
|
||||
ByteRangeSpec::AllFrom(2).to_satisfiable_range(3)
|
||||
);
|
||||
assert_eq!(
|
||||
None,
|
||||
ByteRangeSpec::AllFrom(3).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, ByteRangeSpec::AllFrom(3).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!(
|
||||
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!(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));
|
||||
}
|
||||
}
|
||||
|
@ -137,17 +137,22 @@ impl FromStr for Charset {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse() {
|
||||
#[cfg(test)]
|
||||
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!(Shift_Jis, "Shift-JIS".parse().unwrap());
|
||||
assert_eq!(Ext("ABCD".to_owned()), "abcd".parse().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_display() {
|
||||
#[test]
|
||||
fn test_display() {
|
||||
assert_eq!("US-ASCII", format!("{}", Us_Ascii));
|
||||
assert_eq!("ABCD", format!("{}", Ext("ABCD".to_owned())));
|
||||
}
|
||||
}
|
||||
|
@ -123,6 +123,7 @@ impl AppService {
|
||||
}
|
||||
}
|
||||
|
||||
/// Application connection config
|
||||
#[derive(Clone)]
|
||||
pub struct AppConfig(Rc<AppConfigInner>);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user