Skip to content

Commit d2f0cea

Browse files
committed
tests(hyper): Add tests to increase coverage
1 parent 7f5e038 commit d2f0cea

File tree

6 files changed

+48
-56
lines changed

6 files changed

+48
-56
lines changed

src/header/common/connection.rs

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
1-
use header::{Header, HeaderFormat};
21
use std::fmt::{self, Display};
32
use std::str::FromStr;
4-
use header::parsing::{from_comma_delimited, fmt_comma_delimited};
53
use unicase::UniCase;
64

75
pub use self::ConnectionOption::{KeepAlive, Close, ConnectionHeader};
86

9-
/// `Connection` header, defined in [RFC7230](https://tools.ietf.org/html/rfc7230#section-6.1)
10-
///
11-
/// The `Connection` header field allows the sender to indicate desired
12-
/// control options for the current connection. In order to avoid
13-
/// confusing downstream recipients, a proxy or gateway MUST remove or
14-
/// replace any received connection options before forwarding the
15-
/// message.
16-
///
17-
/// # ABNF
18-
/// ```plain
19-
/// Connection = 1#connection-option
20-
/// connection-option = token
21-
/// ```
22-
///
23-
/// # Example values
24-
/// * `close`
25-
/// * `upgrade`
26-
/// * `keep-alive`
27-
#[derive(Clone, PartialEq, Debug)]
28-
pub struct Connection(pub Vec<ConnectionOption>);
29-
30-
deref!(Connection => Vec<ConnectionOption>);
31-
327
/// Values that can be in the `Connection` header.
338
#[derive(Clone, PartialEq, Debug)]
349
pub enum ConnectionOption {
@@ -50,38 +25,49 @@ pub enum ConnectionOption {
5025
impl FromStr for ConnectionOption {
5126
type Err = ();
5227
fn from_str(s: &str) -> Result<ConnectionOption, ()> {
53-
Ok(match s {
54-
"keep-alive" => KeepAlive,
55-
"close" => Close,
56-
s => ConnectionHeader(UniCase(s.to_string())),
57-
})
28+
match s {
29+
"keep-alive" => Ok(KeepAlive),
30+
"close" => Ok(Close),
31+
s => Ok(ConnectionHeader(UniCase(s.to_string())))
32+
}
5833
}
5934
}
6035

6136
impl Display for ConnectionOption {
62-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63-
f.write_str(match *self {
37+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
38+
write!(fmt, "{}", match *self {
6439
KeepAlive => "keep-alive",
6540
Close => "close",
66-
ConnectionHeader(UniCase(ref s)) => s,
41+
ConnectionHeader(UniCase(ref s)) => s.as_ref()
6742
})
6843
}
6944
}
7045

71-
impl Header for Connection {
72-
fn header_name() -> &'static str {
73-
"Connection"
74-
}
75-
76-
fn parse_header(raw: &[Vec<u8>]) -> Option<Connection> {
77-
from_comma_delimited(raw).map(|vec| Connection(vec))
78-
}
79-
}
46+
header! {
47+
#[doc="`Connection` header, defined in"]
48+
#[doc="[RFC7230](http://tools.ietf.org/html/rfc7230#section-6.1)"]
49+
#[doc=""]
50+
#[doc="The `Connection` header field allows the sender to indicate desired"]
51+
#[doc="control options for the current connection. In order to avoid"]
52+
#[doc="confusing downstream recipients, a proxy or gateway MUST remove or"]
53+
#[doc="replace any received connection options before forwarding the"]
54+
#[doc="message."]
55+
#[doc=""]
56+
#[doc="# ABNF"]
57+
#[doc="```plain"]
58+
#[doc="Connection = 1#connection-option"]
59+
#[doc="connection-option = token"]
60+
#[doc=""]
61+
#[doc="# Example values"]
62+
#[doc="* `close`"]
63+
#[doc="* `keep-alive`"]
64+
#[doc="* `upgrade`"]
65+
(Connection, "Connection") => (ConnectionOption)+
8066

81-
impl HeaderFormat for Connection {
82-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
83-
let Connection(ref parts) = *self;
84-
fmt_comma_delimited(f, &parts[..])
67+
test_connection {
68+
test_header!(test1, vec![b"close"]);
69+
test_header!(test2, vec![b"keep-alive"]);
70+
test_header!(test3, vec![b"upgrade"]);
8571
}
8672
}
8773

src/header/common/if_range.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ impl Display for IfRange {
6565
}
6666

6767
#[cfg(test)]
68-
mod test_range {
68+
mod test_if_range {
69+
use std::str;
6970
use header::*;
7071
use super::IfRange as HeaderField;
7172
test_header!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
7273
test_header!(test2, vec![b"\"xyzzy\""]);
74+
test_header!(test3, vec![b"this-is-invalid"], None::<IfRange>);
7375
}

src/header/common/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ macro_rules! header {
256256
if raw.len() == 1 {
257257
if raw[0] == b"*" {
258258
return Some($id::Any)
259-
} else if raw[0] == b"" {
260-
return None
261259
}
262260
}
263261
$crate::header::parsing::from_comma_delimited(raw).map(|vec| $id::Items(vec))

src/header/shared/entity.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@ use std::fmt::{self, Display};
66
// 2. in the range %x23 to %x7E, or
77
// 3. in the range %x80 to %xFF
88
fn check_slice_validity(slice: &str) -> bool {
9-
for c in slice.bytes() {
10-
match c {
11-
b'\x21' | b'\x23' ... b'\x7e' | b'\x80' ... b'\xff' => (),
12-
_ => { return false; }
13-
}
14-
}
15-
true
9+
slice.bytes().all(|c|
10+
c == b'\x21' || (c >= b'\x23' && c <= b'\x7e') | (c >= b'\x80' && c <= b'\xff'))
1611
}
1712

1813
/// An entity tag, defined in [RFC7232](https://tools.ietf.org/html/rfc7232#section-2.3)

src/header/shared/httpdate.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,9 @@ mod tests {
8383
fn test_asctime() {
8484
assert_eq!("Sun Nov 7 08:48:37 1994".parse(), Ok(NOV_07));
8585
}
86+
87+
#[test]
88+
fn test_no_date() {
89+
assert_eq!("this-is-no-date".parse(), Err::<HttpDate, ()>(()));
90+
}
8691
}

src/method.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ impl fmt::Display for Method {
129129
mod tests {
130130
use std::collections::HashMap;
131131
use std::str::FromStr;
132+
use error::HttpError;
132133
use super::Method;
133134
use super::Method::{Get, Post, Put, Extension};
134135

@@ -150,6 +151,11 @@ mod tests {
150151
assert_eq!(Get, FromStr::from_str("GET").unwrap());
151152
assert_eq!(Extension("MOVE".to_string()),
152153
FromStr::from_str("MOVE").unwrap());
154+
let x: Result<Method, _> = FromStr::from_str("");
155+
if let Err(HttpError::HttpMethodError) = x {
156+
} else {
157+
panic!("An empty method is invalid!")
158+
}
153159
}
154160

155161
#[test]

0 commit comments

Comments
 (0)