mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-23 18:45:06 +08:00
11b986a81d
Refactored tests code to allow to specify compression (content-encoding) algorithm. Added tests to verify what actix actually handle different content encodings properly.
51 lines
1.8 KiB
Rust
51 lines
1.8 KiB
Rust
use std::io::Write;
|
|
use actix_http::header::TryIntoHeaderPair;
|
|
use bytes::Bytes;
|
|
use flate2::write::{GzEncoder, ZlibEncoder};
|
|
use flate2::Compression;
|
|
|
|
#[derive(Clone,Copy)]
|
|
pub enum Encoder {
|
|
Plain,
|
|
Gzip,
|
|
Deflate,
|
|
Brotli,
|
|
}
|
|
|
|
impl Encoder {
|
|
pub fn encode(self: &Encoder, body: impl Into<Bytes>) -> impl Into<Bytes> {
|
|
match self {
|
|
Self::Gzip => {
|
|
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
|
|
encoder.write_all(&body.into()).expect("Failed to encode request body");
|
|
encoder.finish().expect("Failed to encode request body")
|
|
}
|
|
Self::Deflate => {
|
|
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
|
|
encoder.write_all(&body.into()).expect("Failed to encode request body");
|
|
encoder.finish().unwrap()
|
|
}
|
|
Self::Plain => Vec::from(body.into()),
|
|
Self::Brotli => {
|
|
let mut encoder = brotli::CompressorWriter::new(Vec::new(), 32 * 1024, 3, 22);
|
|
encoder.write_all(&body.into()).expect("Failed to encode request body");
|
|
encoder.flush().expect("Failed to encode request body");
|
|
encoder.into_inner()
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn header(self: &Encoder) -> Option<impl TryIntoHeaderPair> {
|
|
match self {
|
|
Self::Plain => None,
|
|
Self::Gzip => Some(("Content-Encoding", "gzip")),
|
|
Self::Deflate => Some(("Content-Encoding", "deflate")),
|
|
Self::Brotli => Some(("Content-Encoding", "br")),
|
|
}
|
|
}
|
|
|
|
pub fn iterator() -> impl Iterator<Item = Self> {
|
|
[Self::Plain, Self::Gzip, Self::Deflate, Self::Brotli].iter().copied()
|
|
}
|
|
}
|