mirror of https://github.com/fafhrd91/actix-web
AcceptEncoding.{ranked,negotiate}() prefers brotli > zstd > gzip
This commit is contained in:
parent
e5ed406af6
commit
f82f05c6c9
|
@ -223,7 +223,11 @@ impl AcceptEncoding {
|
||||||
// use stable sort so items with equal q-factor retain listed order
|
// use stable sort so items with equal q-factor retain listed order
|
||||||
types.sort_by(|a, b| {
|
types.sort_by(|a, b| {
|
||||||
// sort by q-factor descending
|
// sort by q-factor descending
|
||||||
|
if b.quality == a.quality {
|
||||||
|
encoding_rank(b).cmp(&encoding_rank(a))
|
||||||
|
} else {
|
||||||
b.quality.cmp(&a.quality)
|
b.quality.cmp(&a.quality)
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
types.into_iter()
|
types.into_iter()
|
||||||
|
@ -393,11 +397,11 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
test.negotiate([Encoding::gzip(), Encoding::brotli(), Encoding::identity()].iter()),
|
test.negotiate([Encoding::gzip(), Encoding::brotli(), Encoding::identity()].iter()),
|
||||||
Some(Encoding::gzip())
|
Some(Encoding::brotli())
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
test.negotiate([Encoding::brotli(), Encoding::gzip(), Encoding::identity()].iter()),
|
test.negotiate([Encoding::brotli(), Encoding::gzip(), Encoding::identity()].iter()),
|
||||||
Some(Encoding::gzip())
|
Some(Encoding::brotli())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -414,6 +418,9 @@ mod tests {
|
||||||
|
|
||||||
let test = accept_encoding!("br", "gzip", "*");
|
let test = accept_encoding!("br", "gzip", "*");
|
||||||
assert_eq!(test.ranked(), vec![enc("br"), enc("gzip"), enc("*")]);
|
assert_eq!(test.ranked(), vec![enc("br"), enc("gzip"), enc("*")]);
|
||||||
|
|
||||||
|
let test = accept_encoding!("gzip", "br", "*");
|
||||||
|
assert_eq!(test.ranked(), vec![enc("br"), enc("gzip"), enc("*")]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -96,7 +96,7 @@ async fn negotiate_encoding_gzip() {
|
||||||
|
|
||||||
let req = srv
|
let req = srv
|
||||||
.post("/static")
|
.post("/static")
|
||||||
.insert_header((header::ACCEPT_ENCODING, "gzip,br,zstd"))
|
.insert_header((header::ACCEPT_ENCODING, "gzip"))
|
||||||
.send();
|
.send();
|
||||||
|
|
||||||
let mut res = req.await.unwrap();
|
let mut res = req.await.unwrap();
|
||||||
|
@ -109,7 +109,7 @@ async fn negotiate_encoding_gzip() {
|
||||||
let mut res = srv
|
let mut res = srv
|
||||||
.post("/static")
|
.post("/static")
|
||||||
.no_decompress()
|
.no_decompress()
|
||||||
.insert_header((header::ACCEPT_ENCODING, "gzip,br,zstd"))
|
.insert_header((header::ACCEPT_ENCODING, "gzip"))
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -154,7 +154,7 @@ async fn negotiate_encoding_zstd() {
|
||||||
|
|
||||||
let req = srv
|
let req = srv
|
||||||
.post("/static")
|
.post("/static")
|
||||||
.insert_header((header::ACCEPT_ENCODING, "zstd,gzip,br"))
|
.insert_header((header::ACCEPT_ENCODING, "zstd,gzip"))
|
||||||
.send();
|
.send();
|
||||||
|
|
||||||
let mut res = req.await.unwrap();
|
let mut res = req.await.unwrap();
|
||||||
|
@ -167,7 +167,7 @@ async fn negotiate_encoding_zstd() {
|
||||||
let mut res = srv
|
let mut res = srv
|
||||||
.post("/static")
|
.post("/static")
|
||||||
.no_decompress()
|
.no_decompress()
|
||||||
.insert_header((header::ACCEPT_ENCODING, "zstd,gzip,br"))
|
.insert_header((header::ACCEPT_ENCODING, "zstd,gzip"))
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
Loading…
Reference in New Issue