Skip to content

Commit

Permalink
Merge pull request #57 from fermyon/multi-header-test
Browse files Browse the repository at this point in the history
Test that best encoding still works on multi-header case
  • Loading branch information
rylev authored May 17, 2024
2 parents 813e715 + e60ba4e commit 882a18d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
29 changes: 11 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const FALLBACK_FAVICON_ICO: &[u8] = include_bytes!("../spin-favicon.ico");
const BUFFER_SIZE: usize = 64 * 1024;
const DEFLATE_LEVEL: flate2::Compression = flate2::Compression::fast();

#[derive(PartialEq)]
#[derive(PartialEq, Debug)]
struct ContentEncoding {
// We limit expressed encodings to ones that we support
encoding: SupportedEncoding,
Expand Down Expand Up @@ -179,25 +179,18 @@ impl SupportedEncoding {
.iter()
.filter(|(k, _)| HeaderName::from_bytes(k.as_bytes()).ok() == Some(ACCEPT_ENCODING))
.flat_map(|(_, v)| {
str::from_utf8(v).ok().map(|v| {
v.split(',')
.map(|v| ContentEncoding::from_str(v).ok())
.filter(|v| match v {
Some(y) => match y.encoding {
// Filter out "None" values to ensure some compression is
// preferred. This is mostly to be defensive to types we don't
// understand as we only parse encodings we support.
// It's probably subpar if somebody actually _doesn't_ want
// compression but supports it anyway.
SupportedEncoding::None => false,
_ => true,
},
None => false,
})
.flatten()
str::from_utf8(v).ok().into_iter().flat_map(|v| {
v.split(',').filter_map(|v| {
let e = ContentEncoding::from_str(v).ok()?;
// Filter out "None" values to ensure some compression is
// preferred. This is mostly to be defensive to types we don't
// understand as we only parse encodings we support.
// It's probably subpar if somebody actually _doesn't_ want
// compression but supports it anyway.
(e.encoding != SupportedEncoding::None).then_some(e)
})
})
})
.flatten()
.collect();

accepted_encodings.sort_by(|a, b| b.partial_cmp(a).unwrap_or(Ordering::Equal));
Expand Down
25 changes: 25 additions & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,28 @@ fn prefers_brotoli_encoding() {
vec![String::from("br").into_bytes()]
);
}

#[spin_test]
fn prefers_brotoli_encoding_multi_header() {
let headers = http::types::Headers::new();
headers
.append(
&String::from("accept-encoding"),
&String::from("deflate,gzip").into_bytes(),
)
.unwrap();
headers
.append(
&String::from("accept-encoding"),
&String::from("br").into_bytes(),
)
.unwrap();
let request = http::types::OutgoingRequest::new(headers);
request.set_path_with_query(Some("/README.md")).unwrap();
let response = spin_test_sdk::perform_request(request);
assert_eq!(response.status(), 200);
assert_eq!(
response.headers().get(&"content-encoding".into()),
vec![String::from("br").into_bytes()]
);
}

0 comments on commit 882a18d

Please sign in to comment.