Skip to content

Commit

Permalink
Add zstd serve_file test
Browse files Browse the repository at this point in the history
  • Loading branch information
randomairborne committed Jan 4, 2024
1 parent 43b0ca0 commit 0a2a730
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tower-http/src/services/fs/serve_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ where
mod tests {
use crate::services::ServeFile;
use crate::test_helpers::Body;
use async_compression::tokio::bufread::ZstdDecoder;
use brotli::BrotliDecompress;
use flate2::bufread::DeflateDecoder;
use flate2::bufread::GzDecoder;
Expand All @@ -153,6 +154,7 @@ mod tests {
use mime::Mime;
use std::io::Read;
use std::str::FromStr;
use tokio::io::AsyncReadExt;
use tower::ServiceExt;

#[tokio::test]
Expand Down Expand Up @@ -356,6 +358,25 @@ mod tests {
assert!(decompressed.starts_with("\"This is a test file!\""));
}

#[tokio::test]
async fn precompressed_zstd() {
let svc = ServeFile::new("../test-files/precompressed.txt").precompressed_zstd();
let request = Request::builder()
.header("Accept-Encoding", "zstd,br")
.body(Body::empty())
.unwrap();
let res = svc.oneshot(request).await.unwrap();

assert_eq!(res.headers()["content-type"], "text/plain");
assert_eq!(res.headers()["content-encoding"], "zstd");

let body = res.into_body().collect().await.unwrap().to_bytes();
let mut decoder = ZstdDecoder::new(&body[..]);
let mut decompressed = String::new();
decoder.read_to_string(&mut decompressed).await.unwrap();
assert!(decompressed.starts_with("\"This is a test file!\""));
}

#[tokio::test]
async fn multi_precompressed() {
let svc = ServeFile::new("../test-files/precompressed.txt")
Expand Down

0 comments on commit 0a2a730

Please sign in to comment.