From 767c94e3e974ee0fd00a394d373b8e9e5e34214e Mon Sep 17 00:00:00 2001 From: Tpt Date: Fri, 9 Apr 2021 07:14:56 +0200 Subject: [PATCH] Fixes Accept wildcard It is "*/*" according to https://tools.ietf.org/html/rfc7231#section-5.3.2 --- src/content/accept.rs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/content/accept.rs b/src/content/accept.rs index 927e8dc5..910abcd8 100644 --- a/src/content/accept.rs +++ b/src/content/accept.rs @@ -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; } @@ -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(), } } @@ -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![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(()) + } }