Skip to content

Commit

Permalink
fix: accept wildcard
Browse files Browse the repository at this point in the history
based on http-rs#355 by @Tpt
  • Loading branch information
OneOfOne committed Apr 7, 2024
2 parents b5bd4e3 + 767c94e commit 3ee4281
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
16 changes: 11 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ rustdoc-args = ["--cfg", "feature=\"docs\""]
default = ["fs", "cookie-secure", "serde"]
docs = ["unstable"]
unstable = []
hyperium_http = ["http"]
hyperium_http = ["dep:http"]
async_std = ["fs"]
cookies = ["cookie"]
cookies = ["dep:cookie"]
cookie-secure = ["cookies", "cookie/secure"]
fs = ["async-std"]
serde = ["serde_qs", "dep:serde", "serde_json", "serde_urlencoded", "url/serde"]
fs = ["dep:async-std"]
serde = [
"dep:serde_qs",
"dep:serde",
"dep:serde_json",
"dep:serde_urlencoded",
"url/serde",
]

[dependencies]
fastrand = "2.0"
Expand All @@ -60,7 +66,7 @@ serde = { version = "1.0", features = [
"derive",
], optional = true, package = "serde" }
serde_urlencoded = { version = "0.7", optional = true }
serde_qs = { version = "0.12", optional = true }
serde_qs = { version = "0.13", optional = true }


[dev-dependencies]
Expand Down
30 changes: 27 additions & 3 deletions src/content/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Accept {
// Handle empty strings, and wildcard directives.
if part.is_empty() {
continue;
} else if part == "*" {
} else if part == "*/*" {
wildcard = true;
continue;
}
Expand Down Expand Up @@ -175,8 +175,8 @@ impl Header for Accept {

if self.wildcard {
match output.len() {
0 => write!(output, "*").unwrap(),
_ => write!(output, ", *").unwrap(),
0 => write!(output, "*/*").unwrap(),
_ => write!(output, ", */*").unwrap(),
}
}

Expand Down Expand Up @@ -433,4 +433,28 @@ mod test {
assert!(content_type.is_ok(), "server is expected to return HTML content");
Ok(())
}

#[test]
fn parse() -> crate::Result<()> {
let mut headers = Headers::new();
headers.insert("Accept", "application/json; q=0.8,*/*")?;
let accept = Accept::from_headers(headers)?.unwrap();

assert!(accept.wildcard());
assert_eq!(
accept.into_iter().collect::<Vec<_>>(),
vec![MediaTypeProposal::new(mime::JSON, Some(0.8))?]
);
Ok(())
}

#[test]
fn serialize() -> crate::Result<()> {
let mut accept = Accept::new();
accept.push(MediaTypeProposal::new(mime::JSON, Some(0.8))?);
accept.set_wildcard(true);

assert_eq!(accept.header_value().as_str(), "application/json;q=0.800, */*");
Ok(())
}
}
1 change: 0 additions & 1 deletion src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,6 @@ impl Clone for Response {
}

impl AsyncRead for Response {
#[allow(rustdoc::missing_doc_code_examples)]
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
Pin::new(&mut self.body).poll_read(cx, buf)
}
Expand Down

0 comments on commit 3ee4281

Please sign in to comment.