Skip to content

Commit a27e681

Browse files
committed
test(headers): Add tests for single value headers.
1 parent 6d34448 commit a27e681

16 files changed

+79
-16
lines changed

Diff for: src/header/common/accept_charset.rs

-14
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,3 @@ header! {
2121
test_header!(test1, vec![b"iso-8859-5, unicode-1-1;q=0.8"]);
2222
}
2323
}
24-
25-
26-
#[test]
27-
fn test_parse_header() {
28-
use header::{self, q};
29-
let a: AcceptCharset = header::Header::parse_header(
30-
[b"iso-8859-5, iso-8859-6;q=0.8".to_vec()].as_ref()).unwrap();
31-
let b = AcceptCharset(vec![
32-
QualityItem { item: Charset::Iso_8859_5, quality: q(1.0) },
33-
QualityItem { item: Charset::Iso_8859_6, quality: q(0.8) },
34-
]);
35-
assert_eq!(format!("{}", a), format!("{}", b));
36-
assert_eq!(a, b);
37-
}

Diff for: src/header/common/access_control_max_age.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ header! {
55
#[doc="The `Access-Control-Max-Age` header indicates how long the results of a"]
66
#[doc="preflight request can be cached in a preflight result cache."]
77
(AccessControlMaxAge, "Access-Control-Max-Age") => [u32]
8-
}
8+
9+
test_access_control_max_age {}
10+
}

Diff for: src/header/common/access_control_request_method.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ header! {
77
#[doc="The `Access-Control-Request-Method` header indicates which method will be"]
88
#[doc="used in the actual request as part of the preflight request."]
99
(AccessControlRequestMethod, "Access-Control-Request-Method") => [Method]
10+
11+
test_access_control_request_method {}
1012
}

Diff for: src/header/common/content_length.rs

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ header! {
1616
#[doc="Content-Length = 1*DIGIT"]
1717
#[doc="```"]
1818
(ContentLength, "Content-Length") => [u64]
19+
20+
test_content_length {
21+
// Testcase from RFC
22+
test_header!(test1, vec![b"3495"], Some(HeaderField(3495)));
23+
}
1924
}
2025

2126
bench_header!(bench, ContentLength, { vec![b"42349984".to_vec()] });

Diff for: src/header/common/content_type.rs

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ header! {
1717
#[doc="Content-Type = media-type"]
1818
#[doc="```"]
1919
(ContentType, "Content-Type") => [Mime]
20+
21+
test_content_type {
22+
test_header!(
23+
test1,
24+
// FIXME: Should be b"text/html; charset=ISO-8859-4" but mime crate lowercases
25+
// the whole value so parsing and formatting the value gives a different result
26+
vec![b"text/html; charset=iso-8859-4"],
27+
Some(HeaderField(Mime(TopLevel::Text, SubLevel::Html, vec![(Attr::Charset, Value::Ext("iso-8859-4".to_string()))]))));
28+
}
2029
}
2130

2231
bench_header!(bench, ContentType, { vec![b"application/json; charset=utf-8".to_vec()] });

Diff for: src/header/common/date.rs

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ header! {
1111
#[doc="Date = HTTP-date"]
1212
#[doc="```"]
1313
(Date, "Date") => [HttpDate]
14+
15+
test_date {
16+
test_header!(test1, vec![b"Tue, 15 Nov 1994 08:12:31 GMT"]);
17+
}
1418
}
1519

1620
bench_header!(imf_fixdate, Date, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });

Diff for: src/header/common/etag.rs

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ header! {
1818
#[doc="ETag = entity-tag"]
1919
#[doc="```"]
2020
(ETag, "ETag") => [EntityTag]
21+
22+
test_etag {
23+
test_header!(test1, vec![b"\"xyzzy\""], Some(HeaderField(EntityTag::new(false, "xyzzy".to_string()))));
24+
test_header!(test2, vec![b"W/\"xyzzy\""], Some(HeaderField(EntityTag::new(true, "xyzzy".to_string()))));
25+
test_header!(test3, vec![b"\"\""], Some(HeaderField(EntityTag::new(false, "".to_string()))));
26+
}
2127
}
2228

2329
#[cfg(test)]

Diff for: src/header/common/expires.rs

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ header! {
1515
#[doc="Expires = HTTP-date"]
1616
#[doc="```"]
1717
(Expires, "Expires") => [HttpDate]
18+
19+
test_expires {
20+
// Testcase from RFC
21+
test_header!(test1, vec![b"Thu, 01 Dec 1994 16:00:00 GMT"]);
22+
}
1823
}
1924

2025
bench_header!(imf_fixdate, Expires, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });

Diff for: src/header/common/if_modified_since.rs

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ header! {
1515
#[doc="If-Unmodified-Since = HTTP-date"]
1616
#[doc="```"]
1717
(IfModifiedSince, "If-Modified-Since") => [HttpDate]
18+
19+
test_if_modified_since {
20+
// Testcase from RFC
21+
test_header!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
22+
}
1823
}
1924

2025
bench_header!(imf_fixdate, IfModifiedSince, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });

Diff for: src/header/common/if_unmodified_since.rs

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ header! {
1515
#[doc="If-Unmodified-Since = HTTP-date"]
1616
#[doc="```"]
1717
(IfUnmodifiedSince, "If-Unmodified-Since") => [HttpDate]
18+
19+
test_if_unmodified_since {
20+
// Testcase from RFC
21+
test_header!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
22+
}
1823
}
1924

2025
bench_header!(imf_fixdate, IfUnmodifiedSince, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });

Diff for: src/header/common/last_modified.rs

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ header! {
1313
#[doc="Expires = HTTP-date"]
1414
#[doc="```"]
1515
(LastModified, "Last-Modified") => [HttpDate]
16+
17+
test_last_modified {
18+
// Testcase from RFC
19+
test_header!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);}
1620
}
1721

1822
bench_header!(imf_fixdate, LastModified, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });

Diff for: src/header/common/location.rs

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ header! {
1414
// TODO: Use URL
1515
(Location, "Location") => [String]
1616

17+
test_location {
18+
// Testcase from RFC
19+
test_header!(test1, vec![b"/People.html#tim"]);
20+
test_header!(test2, vec![b"http://www.example.net/index.html"]);
21+
}
22+
1723
}
1824

1925
bench_header!(bench, Location, { vec![b"http://foo.com/hello:3000".to_vec()] });

Diff for: src/header/common/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ macro_rules! header {
202202
}
203203
};
204204
// Single value header
205-
($(#[$a:meta])*($id:ident, $n:expr) => [$value:ty]) => {
205+
($(#[$a:meta])*($id:ident, $n:expr) => [$value:ty] $tm:ident{$($tf:item)*}) => {
206206
$(#[$a])*
207207
#[derive(Clone, Debug, PartialEq)]
208208
pub struct $id(pub $value);
@@ -225,6 +225,15 @@ macro_rules! header {
225225
::std::fmt::Display::fmt(&**self, f)
226226
}
227227
}
228+
#[allow(unused_imports)]
229+
mod $tm{
230+
use std::str;
231+
use $crate::header::*;
232+
use $crate::mime::*;
233+
use $crate::method::Method;
234+
use super::$id as HeaderField;
235+
$($tf)*
236+
}
228237
};
229238
// List header, one or more items with "*" option
230239
($(#[$a:meta])*($id:ident, $n:expr) => {Any / ($item:ty)+} $tm:ident{$($tf:item)*}) => {

Diff for: src/header/common/referer.rs

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ header! {
1414
#[doc="```"]
1515
// TODO: Use URL
1616
(Referer, "Referer") => [String]
17+
18+
test_referer {
19+
// Testcase from the RFC
20+
test_header!(test1, vec![b"http://www.example.org/hypertext/Overview.html"]);
21+
}
1722
}
1823

1924
bench_header!(bench, Referer, { vec![b"http://foo.com/hello:3000".to_vec()] });

Diff for: src/header/common/server.rs

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ header! {
1515
#[doc="```"]
1616
// TODO: Maybe parse as defined in the spec?
1717
(Server, "Server") => [String]
18+
19+
test_server {
20+
// Testcase from RFC
21+
test_header!(test1, vec![b"CERN/3.0 libwww/2.17"]);
22+
}
1823
}
1924

2025
bench_header!(bench, Server, { vec![b"Some String".to_vec()] });

Diff for: src/header/common/user_agent.rs

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ header! {
1818
#[doc="```"]
1919
// TODO: Maybe write parsing according to the spec? (Split the String)
2020
(UserAgent, "User-Agent") => [String]
21+
22+
test_user_agent {
23+
// Testcase from RFC
24+
test_header!(test1, vec![b"CERN-LineMode/2.15 libwww/2.17b3"]);
25+
}
2126
}
2227

2328
#[test] fn test_format() {

0 commit comments

Comments
 (0)