Skip to content

Commit c4ac582

Browse files
committed
Enable the Accept tests
1 parent 374f756 commit c4ac582

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

src/common/accept.rs

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use http::HttpTryFrom;
22

33
use mime::{self, Mime};
44

5-
use util::QualityValue;
5+
use {util::QualityValue, Header};
66

77
fn qitem(mime: Mime) -> QualityValue<Mime> {
88
QualityValue::new(mime, Default::default())
@@ -84,10 +84,10 @@ fn qitem(mime: Mime) -> QualityValue<Mime> {
8484
/// ])
8585
/// );
8686
/// ```
87-
#[derive(Debug)]
87+
#[derive(Debug, PartialEq, Eq)]
8888
pub struct Accept(pub Vec<QualityValue<Mime>>);
8989

90-
impl ::Header for Accept {
90+
impl Header for Accept {
9191
fn name() -> &'static ::HeaderName {
9292
&::http::header::ACCEPT
9393
}
@@ -141,3 +141,76 @@ impl Accept {
141141
Accept(vec![qitem(mime::IMAGE_STAR)])
142142
}
143143
}
144+
145+
#[cfg(test)]
146+
mod tests {
147+
use super::*;
148+
149+
use {
150+
http::HeaderValue,
151+
mime::{TEXT_HTML, TEXT_PLAIN, TEXT_PLAIN_UTF_8},
152+
};
153+
154+
use util::Quality;
155+
156+
macro_rules! test_header {
157+
($name: ident, $input: expr, $expected: expr) => {
158+
#[test]
159+
fn $name() {
160+
assert_eq!(
161+
Accept::decode(
162+
&mut $input
163+
.into_iter()
164+
.map(|s| HeaderValue::from_bytes(s).unwrap())
165+
.collect::<Vec<_>>()
166+
.iter()
167+
)
168+
.ok(),
169+
$expected,
170+
);
171+
}
172+
};
173+
}
174+
175+
// Tests from the RFC
176+
test_header!(
177+
test1,
178+
vec![b"audio/*; q=0.2, audio/basic"],
179+
Some(Accept(vec![
180+
QualityValue::new("audio/*".parse().unwrap(), Quality::from(200)),
181+
qitem("audio/basic".parse().unwrap()),
182+
]))
183+
);
184+
test_header!(
185+
test2,
186+
vec![b"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"],
187+
Some(Accept(vec![
188+
QualityValue::new(TEXT_PLAIN, Quality::from(500)),
189+
qitem(TEXT_HTML),
190+
QualityValue::new("text/x-dvi".parse().unwrap(), Quality::from(800)),
191+
qitem("text/x-c".parse().unwrap()),
192+
]))
193+
);
194+
// Custom tests
195+
test_header!(
196+
test3,
197+
vec![b"text/plain; charset=utf-8"],
198+
Some(Accept(vec![qitem(TEXT_PLAIN_UTF_8),]))
199+
);
200+
test_header!(
201+
test4,
202+
vec![b"text/plain; charset=utf-8; q=0.5"],
203+
Some(Accept(vec![QualityValue::new(
204+
TEXT_PLAIN_UTF_8,
205+
Quality::from(500)
206+
),]))
207+
);
208+
209+
#[test]
210+
#[ignore]
211+
fn test_fuzzing1() {
212+
let raw = HeaderValue::from_static("chunk#;e");
213+
let header = Accept::decode(&mut Some(&raw).into_iter());
214+
assert!(header.is_ok());
215+
}
216+
}

src/util/quality_value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl Default for Quality {
3232

3333
/// Represents an item with a quality value as defined in
3434
/// [RFC7231](https://tools.ietf.org/html/rfc7231#section-5.3.1).
35-
#[derive(Clone, PartialEq, Debug)]
35+
#[derive(Clone, PartialEq, Eq, Debug)]
3636
pub struct QualityValue<T> {
3737
/// The actual contents of the field.
3838
pub value: T,

0 commit comments

Comments
 (0)