Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Accept wildcard #355

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 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 @@ -177,8 +177,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 @@ -420,4 +420,31 @@ mod test {
assert_eq!(accept.negotiate(&[mime::XML])?, mime::XML);
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(())
}
}