mirror of https://github.com/fafhrd91/actix-web
use browser-realistic encoding tests
This commit is contained in:
parent
7e67dbdd69
commit
c4ca352ebd
|
@ -168,15 +168,17 @@ impl AcceptEncoding {
|
||||||
let mut max_item = None;
|
let mut max_item = None;
|
||||||
let mut max_pref = Quality::ZERO;
|
let mut max_pref = Quality::ZERO;
|
||||||
let mut max_rank = 0;
|
let mut max_rank = 0;
|
||||||
|
|
||||||
// uses manual max lookup loop since we want the first occurrence in the case of same
|
// uses manual max lookup loop since we want the first occurrence in the case of same
|
||||||
// preference but `Iterator::max_by_key` would give us the last occurrence
|
// preference but `Iterator::max_by_key` would give us the last occurrence
|
||||||
|
|
||||||
for pref in &self.0 {
|
for pref in &self.0 {
|
||||||
// only change if strictly greater
|
// only change if strictly greater
|
||||||
// equal items, even while unsorted, still have higher preference if they appear first
|
// equal items, even while unsorted, still have higher preference if they appear first
|
||||||
let rank = encoding_rank(&pref);
|
|
||||||
|
|
||||||
if pref.quality > max_pref || (pref.quality == max_pref && max_rank < rank) {
|
let rank = encoding_rank(&pref.item);
|
||||||
|
|
||||||
|
if (pref.quality, rank) > (max_pref, max_rank) {
|
||||||
max_pref = pref.quality;
|
max_pref = pref.quality;
|
||||||
max_item = Some(pref.item.clone());
|
max_item = Some(pref.item.clone());
|
||||||
max_rank = rank;
|
max_rank = rank;
|
||||||
|
@ -222,23 +224,20 @@ 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 then server ranking descending
|
||||||
if b.quality == a.quality {
|
|
||||||
encoding_rank(b).cmp(&encoding_rank(a))
|
b.quality
|
||||||
} else {
|
.cmp(&a.quality)
|
||||||
b.quality.cmp(&a.quality)
|
.then(encoding_rank(&b.item).cmp(&encoding_rank(&a.item)))
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
types.into_iter()
|
types.into_iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encoding_rank(q: &QualityItem<Preference<Encoding>>) -> u8 {
|
/// Returns server-defined encoding ranking.
|
||||||
if q.quality == Quality::ZERO {
|
fn encoding_rank(enc: &Preference<Encoding>) -> u8 {
|
||||||
return 0;
|
match enc {
|
||||||
}
|
|
||||||
match q.item {
|
|
||||||
Preference::Specific(Encoding::Known(ContentEncoding::Brotli)) => 2,
|
Preference::Specific(Encoding::Known(ContentEncoding::Brotli)) => 2,
|
||||||
Preference::Specific(Encoding::Known(ContentEncoding::Zstd)) => 1,
|
Preference::Specific(Encoding::Known(ContentEncoding::Zstd)) => 1,
|
||||||
_ => 0,
|
_ => 0,
|
||||||
|
|
|
@ -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"))
|
.insert_header((header::ACCEPT_ENCODING, "gzip, br;q=0.8, zstd;q=0.5"))
|
||||||
.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"))
|
.insert_header((header::ACCEPT_ENCODING, "gzip, br;q=0.8, zstd;q=0.5"))
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -123,9 +123,11 @@ async fn negotiate_encoding_gzip() {
|
||||||
async fn negotiate_encoding_br() {
|
async fn negotiate_encoding_br() {
|
||||||
let srv = test_server!();
|
let srv = test_server!();
|
||||||
|
|
||||||
|
// check that brotli content-encoding header is returned
|
||||||
|
|
||||||
let req = srv
|
let req = srv
|
||||||
.post("/static")
|
.post("/static")
|
||||||
.insert_header((header::ACCEPT_ENCODING, "br,zstd,gzip"))
|
.insert_header((header::ACCEPT_ENCODING, "br, zstd, gzip"))
|
||||||
.send();
|
.send();
|
||||||
|
|
||||||
let mut res = req.await.unwrap();
|
let mut res = req.await.unwrap();
|
||||||
|
@ -135,10 +137,26 @@ async fn negotiate_encoding_br() {
|
||||||
let bytes = res.body().await.unwrap();
|
let bytes = res.body().await.unwrap();
|
||||||
assert_eq!(bytes, Bytes::from_static(LOREM));
|
assert_eq!(bytes, Bytes::from_static(LOREM));
|
||||||
|
|
||||||
|
// check that brotli is preferred even when later in (q-less) list
|
||||||
|
|
||||||
|
let req = srv
|
||||||
|
.post("/static")
|
||||||
|
.insert_header((header::ACCEPT_ENCODING, "gzip, zstd, br"))
|
||||||
|
.send();
|
||||||
|
|
||||||
|
let mut res = req.await.unwrap();
|
||||||
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
|
assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "br");
|
||||||
|
|
||||||
|
let bytes = res.body().await.unwrap();
|
||||||
|
assert_eq!(bytes, Bytes::from_static(LOREM));
|
||||||
|
|
||||||
|
// check that returned content is actually brotli encoded
|
||||||
|
|
||||||
let mut res = srv
|
let mut res = srv
|
||||||
.post("/static")
|
.post("/static")
|
||||||
.no_decompress()
|
.no_decompress()
|
||||||
.insert_header((header::ACCEPT_ENCODING, "br,zstd,gzip"))
|
.insert_header((header::ACCEPT_ENCODING, "br, zstd, gzip"))
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -154,7 +172,7 @@ async fn negotiate_encoding_zstd() {
|
||||||
|
|
||||||
let req = srv
|
let req = srv
|
||||||
.post("/static")
|
.post("/static")
|
||||||
.insert_header((header::ACCEPT_ENCODING, "zstd,gzip"))
|
.insert_header((header::ACCEPT_ENCODING, "zstd, gzip, br;q=0.8"))
|
||||||
.send();
|
.send();
|
||||||
|
|
||||||
let mut res = req.await.unwrap();
|
let mut res = req.await.unwrap();
|
||||||
|
@ -167,7 +185,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"))
|
.insert_header((header::ACCEPT_ENCODING, "zstd, gzip, br;q=0.8"))
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -207,7 +225,7 @@ async fn gzip_no_decompress() {
|
||||||
// don't decompress response body
|
// don't decompress response body
|
||||||
.no_decompress()
|
.no_decompress()
|
||||||
// signal that we want a compressed body
|
// signal that we want a compressed body
|
||||||
.insert_header((header::ACCEPT_ENCODING, "gzip,br,zstd"))
|
.insert_header((header::ACCEPT_ENCODING, "gzip, br;q=0.8, zstd;q=0.5"))
|
||||||
.send();
|
.send();
|
||||||
|
|
||||||
let mut res = req.await.unwrap();
|
let mut res = req.await.unwrap();
|
||||||
|
|
Loading…
Reference in New Issue