Skip to content

Commit d1d23e9

Browse files
committed
Finish tests
1 parent 00d243d commit d1d23e9

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/content/accept.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Client header advertising available compression algorithms.
22
3-
use crate::content::MediaTypeProposal;
3+
use crate::content::{ContentType, MediaTypeProposal};
44
use crate::headers::{HeaderName, HeaderValue, Headers, ToHeaderValues, ACCEPT};
55
use crate::utils::sort_by_weight;
66
use crate::{Error, Mime, StatusCode};
@@ -29,10 +29,10 @@ use std::slice;
2929
/// accept.push(MediaTypeProposal::new(mime::PLAIN, None)?);
3030
///
3131
/// let mut res = Response::new(200);
32-
/// let media_type = accept.negotiate(&[mime::XML])?;
33-
/// media_type.apply(&mut res);
32+
/// let content_type = accept.negotiate(&[mime::XML])?;
33+
/// content_type.apply(&mut res);
3434
///
35-
/// assert_eq!(res["Content-Encoding"], "text/html");
35+
/// assert_eq!(res["Content-Type"], "application/xml;charset=utf-8");
3636
/// #
3737
/// # Ok(()) }
3838
/// ```
@@ -111,14 +111,14 @@ impl Accept {
111111
/// # Errors
112112
///
113113
/// If no suitable encoding is found, an error with the status of `406` will be returned.
114-
pub fn negotiate(&mut self, available: &[Mime]) -> crate::Result<Mime> {
114+
pub fn negotiate(&mut self, available: &[Mime]) -> crate::Result<ContentType> {
115115
// Start by ordering the encodings.
116116
self.sort();
117117

118118
// Try and find the first encoding that matches.
119119
for accept in &self.entries {
120120
if available.contains(&accept) {
121-
return Ok(accept.clone().into());
121+
return Ok(accept.media_type.clone().into());
122122
}
123123
}
124124

src/content/content_type.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ use crate::Mime;
2121
/// use http_types::{Response, Mime};
2222
/// use std::str::FromStr;
2323
///
24-
/// let ct = ContentType::new(Mime::from_str("text/*")?);
24+
/// let content_type = ContentType::new(Mime::from_str("text/*")?);
2525
///
2626
/// let mut res = Response::new(200);
27-
/// ct.apply(&mut res);
27+
/// content_type.apply(&mut res);
2828
///
29-
/// let ct = ContentType::from_headers(res)?.unwrap();
30-
/// assert_eq!(ct.value(), format!("{}", Mime::from_str("text/*")?).as_str());
29+
/// let content_type = ContentType::from_headers(res)?.unwrap();
30+
/// assert_eq!(content_type.value(), format!("{}", Mime::from_str("text/*")?).as_str());
3131
/// #
3232
/// # Ok(()) }
3333
/// ```
@@ -91,6 +91,24 @@ impl ContentType {
9191
}
9292
}
9393

94+
impl PartialEq<Mime> for ContentType {
95+
fn eq(&self, other: &Mime) -> bool {
96+
&self.media_type == other
97+
}
98+
}
99+
100+
impl PartialEq<&Mime> for ContentType {
101+
fn eq(&self, other: &&Mime) -> bool {
102+
&&self.media_type == other
103+
}
104+
}
105+
106+
impl From<Mime> for ContentType {
107+
fn from(media_type: Mime) -> Self {
108+
Self { media_type }
109+
}
110+
}
111+
94112
#[cfg(test)]
95113
mod test {
96114
use super::*;

0 commit comments

Comments
 (0)