Skip to content

Commit 5f09d81

Browse files
committed
Add AcceptEncoding negotiation tests
1 parent 54f898c commit 5f09d81

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

src/content/accept_encoding.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,20 @@ impl AcceptEncoding {
8686
/// # Errors
8787
///
8888
/// If no suitable encoding is found, an error with the status of `406` will be returned.
89-
pub fn negotiate(&mut self, encodings: &[Encoding]) -> crate::Result<ContentEncoding> {
89+
pub fn negotiate(&mut self, available: &[Encoding]) -> crate::Result<ContentEncoding> {
9090
// Start by ordering the encodings.
9191
self.sort();
9292

9393
// Try and find the first encoding that matches.
9494
for encoding in &self.entries {
95-
if encodings.contains(&encoding) {
95+
if available.contains(&encoding) {
9696
return Ok(encoding.into());
9797
}
9898
}
9999

100100
// If no encoding matches and wildcard is set, send whichever encoding we got.
101101
if self.wildcard {
102-
if let Some(encoding) = encodings.iter().next() {
102+
if let Some(encoding) = available.iter().next() {
103103
return Ok(encoding.into());
104104
}
105105
}
@@ -352,15 +352,52 @@ mod test {
352352
accept.push(EncodingProposal::new(Encoding::Gzip, None)?);
353353
accept.push(EncodingProposal::new(Encoding::Brotli, Some(0.8))?);
354354

355-
let mut headers = Response::new(200);
356-
accept.apply(&mut headers);
355+
let mut res = Response::new(200);
356+
accept.apply(&mut res);
357357

358-
let mut accept = AcceptEncoding::from_headers(headers)?.unwrap();
358+
let mut accept = AcceptEncoding::from_headers(res)?.unwrap();
359359
accept.sort();
360360
let mut accept = accept.iter();
361361
assert_eq!(accept.next().unwrap(), Encoding::Brotli);
362362
assert_eq!(accept.next().unwrap(), Encoding::Gzip);
363363
assert_eq!(accept.next().unwrap(), Encoding::Identity);
364364
Ok(())
365365
}
366+
367+
#[test]
368+
fn negotiate() -> crate::Result<()> {
369+
let mut accept = AcceptEncoding::new();
370+
accept.push(EncodingProposal::new(Encoding::Brotli, Some(0.8))?);
371+
accept.push(EncodingProposal::new(Encoding::Gzip, Some(0.4))?);
372+
accept.push(EncodingProposal::new(Encoding::Identity, None)?);
373+
374+
assert_eq!(
375+
accept.negotiate(&[Encoding::Brotli, Encoding::Gzip])?,
376+
Encoding::Brotli,
377+
);
378+
Ok(())
379+
}
380+
381+
#[test]
382+
fn negotiate_not_acceptable() -> crate::Result<()> {
383+
let mut accept = AcceptEncoding::new();
384+
let err = accept.negotiate(&[Encoding::Gzip]).unwrap_err();
385+
assert_eq!(err.status(), 406);
386+
387+
let mut accept = AcceptEncoding::new();
388+
accept.push(EncodingProposal::new(Encoding::Brotli, Some(0.8))?);
389+
let err = accept.negotiate(&[Encoding::Gzip]).unwrap_err();
390+
assert_eq!(err.status(), 406);
391+
Ok(())
392+
}
393+
394+
#[test]
395+
fn negotiate_wildcard() -> crate::Result<()> {
396+
let mut accept = AcceptEncoding::new();
397+
accept.push(EncodingProposal::new(Encoding::Brotli, Some(0.8))?);
398+
accept.set_wildcard(true);
399+
400+
assert_eq!(accept.negotiate(&[Encoding::Gzip])?, Encoding::Gzip);
401+
Ok(())
402+
}
366403
}

0 commit comments

Comments
 (0)