use std::collections::BTreeSet;
#[cfg(feature = "gzip")]
use tez::BodyExt as _;
use tez::{Client, header};
#[cfg(feature = "gzip")]
use tez::{StatusCode, Timeouts};
#[path = "harness/httpmock.rs"]
mod httpmock;
const DECODED: &[u8] = b"Tez compression fixture.\n";
const GZIP: &[u8] = &[
31, 139, 8, 0, 0, 0, 0, 0, 0, 19, 11, 73, 173, 82, 72, 206, 207, 45, 40, 74, 45, 46, 206, 204,
207, 83, 72, 203, 172, 40, 41, 45, 74, 213, 227, 2, 0, 58, 171, 44, 106, 25, 0, 0, 0,
];
const DEFLATE: &[u8] = &[
120, 156, 11, 73, 173, 82, 72, 206, 207, 45, 40, 74, 45, 46, 206, 204, 207, 83, 72, 203, 172,
40, 41, 45, 74, 213, 227, 2, 0, 126, 45, 9, 101,
];
const BROTLI: &[u8] = &[
11, 12, 128, 84, 101, 122, 32, 99, 111, 109, 112, 114, 101, 115, 115, 105, 111, 110, 32, 102,
105, 120, 116, 117, 114, 101, 46, 10, 3,
];
const ZSTD: &[u8] = &[
40, 181, 47, 253, 32, 25, 201, 0, 0, 84, 101, 122, 32, 99, 111, 109, 112, 114, 101, 115, 115,
105, 111, 110, 32, 102, 105, 120, 116, 117, 114, 101, 46, 10,
];
#[derive(Clone, Copy)]
struct Codec {
feature: &'static str,
token: &'static str,
fixture: &'static [u8],
enabled: bool,
}
const CODECS: [Codec; 4] = [
Codec {
feature: "gzip",
token: "gzip",
fixture: GZIP,
enabled: cfg!(feature = "gzip"),
},
Codec {
feature: "brotli",
token: "br",
fixture: BROTLI,
enabled: cfg!(feature = "brotli"),
},
Codec {
feature: "deflate",
token: "deflate",
fixture: DEFLATE,
enabled: cfg!(feature = "deflate"),
},
Codec {
feature: "zstd",
token: "zstd",
fixture: ZSTD,
enabled: cfg!(feature = "zstd"),
},
];
#[tokio::test]
async fn enabled_features_are_advertised_and_decoded() {
let server = httpmock::Server::start(|request| {
let codec = codec_for_path(request.path_and_query());
httpmock::MockResponse::ok(codec.fixture)
.with_header(header::CONTENT_ENCODING.as_str(), codec.token)
})
.await;
let client = Client::new().unwrap();
for codec in CODECS {
let response = client
.get(format!("{}/{}", server.base_url(), codec.feature))
.send()
.await
.unwrap();
if codec.enabled {
assert_eq!(
response.headers().get(header::CONTENT_ENCODING),
None,
"{} Content-Encoding",
codec.feature
);
assert_eq!(
response.headers().get(header::CONTENT_LENGTH),
None,
"{} Content-Length",
codec.feature
);
assert_eq!(response.content_length(), None, "{}", codec.feature);
assert_eq!(
response.bytes().await.unwrap(),
DECODED,
"{}",
codec.feature
);
} else {
assert_eq!(
response.headers()[header::CONTENT_ENCODING],
codec.token,
"{} Content-Encoding",
codec.feature
);
assert_eq!(
response.content_length(),
Some(codec.fixture.len() as u64),
"{}",
codec.feature
);
assert_eq!(
response.bytes().await.unwrap(),
codec.fixture,
"{}",
codec.feature
);
}
}
let expected = CODECS
.iter()
.filter(|codec| codec.enabled)
.map(|codec| codec.token)
.collect::<BTreeSet<_>>();
for request in server.recorded_requests() {
assert_eq!(
encoding_tokens(request.header(header::ACCEPT_ENCODING.as_str())),
expected
);
}
server.shutdown().await;
}
#[tokio::test]
async fn disable_compression_preserves_encoded_responses() {
let server = httpmock::Server::start(|request| {
let codec = codec_for_path(request.path_and_query());
httpmock::MockResponse::ok(codec.fixture)
.with_header(header::CONTENT_ENCODING.as_str(), codec.token)
})
.await;
let client = Client::builder().disable_compression().build().unwrap();
for codec in CODECS {
let response = client
.get(format!("{}/{}", server.base_url(), codec.feature))
.send()
.await
.unwrap();
assert_eq!(
response.headers()[header::CONTENT_ENCODING],
codec.token,
"{} Content-Encoding",
codec.feature
);
assert_eq!(
response.headers()[header::CONTENT_LENGTH],
codec.fixture.len().to_string(),
"{} Content-Length",
codec.feature
);
assert_eq!(
response.content_length(),
Some(codec.fixture.len() as u64),
"{}",
codec.feature
);
assert_eq!(
response.bytes().await.unwrap(),
codec.fixture,
"{}",
codec.feature
);
}
let response = client
.get(format!("{}/gzip", server.base_url()))
.header(header::ACCEPT_ENCODING, "gzip")
.send()
.await
.unwrap();
assert_eq!(response.headers()[header::CONTENT_ENCODING], "gzip");
assert_eq!(response.bytes().await.unwrap(), GZIP);
let requests = server.recorded_requests();
for request in &requests[..CODECS.len()] {
assert_eq!(request.header(header::ACCEPT_ENCODING.as_str()), None);
}
assert_eq!(
requests[CODECS.len()].header(header::ACCEPT_ENCODING.as_str()),
Some("gzip")
);
let debug = format!("{client:?}");
assert!(debug.contains("decompresses_responses: false"));
assert!(!debug.contains("Accept-Encoding"));
server.shutdown().await;
}
#[tokio::test]
async fn range_requests_do_not_implicitly_advertise_compression() {
let server = httpmock::Server::start(|_| httpmock::MockResponse::ok("partial")).await;
Client::new()
.unwrap()
.get(server.base_url())
.header(header::RANGE, "bytes=0-6")
.send()
.await
.unwrap()
.discard()
.await
.unwrap();
let request = &server.recorded_requests()[0];
assert_eq!(request.header(header::ACCEPT_ENCODING.as_str()), None);
server.shutdown().await;
}
#[cfg(feature = "gzip")]
#[tokio::test]
async fn range_responses_are_not_transparently_decoded() {
let server = httpmock::Server::start(|_| {
httpmock::MockResponse::with_status(StatusCode::PARTIAL_CONTENT, GZIP)
.with_header(header::CONTENT_ENCODING.as_str(), "gzip")
})
.await;
let response = Client::new()
.unwrap()
.get(server.base_url())
.header(header::RANGE, "bytes=0-6")
.header(header::ACCEPT_ENCODING, "gzip")
.send()
.await
.unwrap();
assert_eq!(response.headers()[header::CONTENT_ENCODING], "gzip");
assert_eq!(response.bytes().await.unwrap(), GZIP);
server.shutdown().await;
}
#[cfg(feature = "gzip")]
#[tokio::test]
async fn bodyless_responses_are_not_transparently_decoded() {
let server = httpmock::Server::start(|request| {
let status = match request.path_and_query() {
"/not-modified" => StatusCode::NOT_MODIFIED,
"/reset-content" => StatusCode::RESET_CONTENT,
_ => StatusCode::NO_CONTENT,
};
httpmock::MockResponse::with_status(status, "")
.with_header(header::CONTENT_ENCODING.as_str(), "gzip")
})
.await;
let client = Client::new().unwrap();
for path in ["no-content", "reset-content", "not-modified"] {
let response = client
.get(format!("{}/{path}", server.base_url()))
.send()
.await
.unwrap();
assert_eq!(response.headers()[header::CONTENT_ENCODING], "gzip");
assert!(response.bytes().await.unwrap().is_empty());
}
let response = client
.head(format!("{}/head", server.base_url()))
.send()
.await
.unwrap();
assert_eq!(response.headers()[header::CONTENT_ENCODING], "gzip");
assert!(response.bytes().await.unwrap().is_empty());
server.shutdown().await;
}
#[cfg(feature = "gzip")]
#[tokio::test]
async fn compressed_body_timeouts_remain_timeouts() {
let server = httpmock::Server::start(|_| {
httpmock::MockResponse::ok(GZIP)
.with_header(header::CONTENT_ENCODING.as_str(), "gzip")
.with_body_delay(std::time::Duration::from_millis(100))
})
.await;
let client = Client::builder()
.timeouts(
Timeouts::default()
.total(std::time::Duration::from_secs(1))
.read(std::time::Duration::from_millis(20)),
)
.build()
.unwrap();
let error = client
.get(server.base_url())
.send()
.await
.unwrap()
.bytes()
.await
.unwrap_err();
assert!(error.is_timeout());
server.shutdown().await;
}
#[cfg(feature = "gzip")]
#[tokio::test]
async fn explicit_accept_encoding_is_decoded_when_compression_is_enabled() {
let server = httpmock::Server::start(|_| {
httpmock::MockResponse::ok(GZIP).with_header(header::CONTENT_ENCODING.as_str(), "gzip")
})
.await;
let response = Client::new()
.unwrap()
.get(server.base_url())
.header(header::ACCEPT_ENCODING, "gzip")
.send()
.await
.unwrap();
assert_eq!(response.headers().get(header::CONTENT_ENCODING), None);
assert_eq!(response.headers().get(header::CONTENT_LENGTH), None);
assert_eq!(response.content_length(), None);
assert_eq!(response.bytes().await.unwrap(), DECODED);
let request = server
.recorded_requests()
.into_iter()
.next()
.expect("the request must be recorded");
assert_eq!(
request.header(header::ACCEPT_ENCODING.as_str()),
Some("gzip")
);
server.shutdown().await;
}
#[cfg(feature = "gzip")]
#[tokio::test]
async fn repeated_content_encoding_fields_are_decoded_in_wire_order() {
let server = httpmock::Server::start(|_| {
httpmock::MockResponse::ok(GZIP)
.with_header(header::CONTENT_ENCODING.as_str(), "identity")
.with_header(header::CONTENT_ENCODING.as_str(), "gzip")
})
.await;
let response = Client::new()
.unwrap()
.get(server.base_url())
.send()
.await
.unwrap();
assert_eq!(response.headers().get(header::CONTENT_ENCODING), None);
assert_eq!(response.bytes().await.unwrap(), DECODED);
server.shutdown().await;
}
#[cfg(feature = "gzip")]
#[tokio::test]
async fn decoded_responses_preserve_http_trailers_after_the_data() {
let server = httpmock::Server::start(|_| {
httpmock::MockResponse::ok(GZIP)
.with_header(header::CONTENT_ENCODING.as_str(), "gzip")
.with_trailer("x-checksum", "complete")
})
.await;
let mut body = Client::new()
.unwrap()
.get(server.base_url())
.header(header::TE, "trailers")
.send()
.await
.unwrap()
.into_body();
let mut decoded = Vec::new();
let mut checksum = None;
while let Some(frame) = body.frame().await {
let frame = frame.unwrap();
match frame.into_data() {
Ok(data) => decoded.extend_from_slice(&data),
Err(frame) => {
checksum = frame
.into_trailers()
.ok()
.and_then(|mut trailers| trailers.remove("x-checksum"));
}
}
}
assert_eq!(decoded, DECODED);
assert_eq!(checksum.unwrap(), "complete");
server.shutdown().await;
}
fn codec_for_path(path: &str) -> Codec {
CODECS
.iter()
.copied()
.find(|codec| path == format!("/{}", codec.feature))
.expect("the request path must select a codec fixture")
}
fn encoding_tokens(value: Option<&str>) -> BTreeSet<&str> {
value
.into_iter()
.flat_map(|value| value.split(','))
.map(str::trim)
.filter(|token| !token.is_empty())
.collect()
}