Skip to content

Commit 882a18d

Browse files
authored
Merge pull request #57 from fermyon/multi-header-test
Test that best encoding still works on multi-header case
2 parents 813e715 + e60ba4e commit 882a18d

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/lib.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const FALLBACK_FAVICON_ICO: &[u8] = include_bytes!("../spin-favicon.ico");
5353
const BUFFER_SIZE: usize = 64 * 1024;
5454
const DEFLATE_LEVEL: flate2::Compression = flate2::Compression::fast();
5555

56-
#[derive(PartialEq)]
56+
#[derive(PartialEq, Debug)]
5757
struct ContentEncoding {
5858
// We limit expressed encodings to ones that we support
5959
encoding: SupportedEncoding,
@@ -179,25 +179,18 @@ impl SupportedEncoding {
179179
.iter()
180180
.filter(|(k, _)| HeaderName::from_bytes(k.as_bytes()).ok() == Some(ACCEPT_ENCODING))
181181
.flat_map(|(_, v)| {
182-
str::from_utf8(v).ok().map(|v| {
183-
v.split(',')
184-
.map(|v| ContentEncoding::from_str(v).ok())
185-
.filter(|v| match v {
186-
Some(y) => match y.encoding {
187-
// Filter out "None" values to ensure some compression is
188-
// preferred. This is mostly to be defensive to types we don't
189-
// understand as we only parse encodings we support.
190-
// It's probably subpar if somebody actually _doesn't_ want
191-
// compression but supports it anyway.
192-
SupportedEncoding::None => false,
193-
_ => true,
194-
},
195-
None => false,
196-
})
197-
.flatten()
182+
str::from_utf8(v).ok().into_iter().flat_map(|v| {
183+
v.split(',').filter_map(|v| {
184+
let e = ContentEncoding::from_str(v).ok()?;
185+
// Filter out "None" values to ensure some compression is
186+
// preferred. This is mostly to be defensive to types we don't
187+
// understand as we only parse encodings we support.
188+
// It's probably subpar if somebody actually _doesn't_ want
189+
// compression but supports it anyway.
190+
(e.encoding != SupportedEncoding::None).then_some(e)
191+
})
198192
})
199193
})
200-
.flatten()
201194
.collect();
202195

203196
accepted_encodings.sort_by(|a, b| b.partial_cmp(a).unwrap_or(Ordering::Equal));

tests/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,28 @@ fn prefers_brotoli_encoding() {
104104
vec![String::from("br").into_bytes()]
105105
);
106106
}
107+
108+
#[spin_test]
109+
fn prefers_brotoli_encoding_multi_header() {
110+
let headers = http::types::Headers::new();
111+
headers
112+
.append(
113+
&String::from("accept-encoding"),
114+
&String::from("deflate,gzip").into_bytes(),
115+
)
116+
.unwrap();
117+
headers
118+
.append(
119+
&String::from("accept-encoding"),
120+
&String::from("br").into_bytes(),
121+
)
122+
.unwrap();
123+
let request = http::types::OutgoingRequest::new(headers);
124+
request.set_path_with_query(Some("/README.md")).unwrap();
125+
let response = spin_test_sdk::perform_request(request);
126+
assert_eq!(response.status(), 200);
127+
assert_eq!(
128+
response.headers().get(&"content-encoding".into()),
129+
vec![String::from("br").into_bytes()]
130+
);
131+
}

0 commit comments

Comments
 (0)