mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-07-27 16:51:01 +00:00
Added test to verify response encoding
Alongside request encoding (compression) support, it is helpful to verify that the server respect `Accept-Encoding` headers and apply the corresponding compression to responses.
This commit is contained in:
@ -2,7 +2,8 @@ use actix_http::header::TryIntoHeaderPair;
|
||||
use bytes::Bytes;
|
||||
use flate2::write::{GzEncoder, ZlibEncoder};
|
||||
use flate2::Compression;
|
||||
use std::io::Write;
|
||||
use std::io::{Read, Write};
|
||||
use flate2::read::{GzDecoder, ZlibDecoder};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Encoder {
|
||||
@ -41,6 +42,34 @@ impl Encoder {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decode(self: &Encoder, bytes: impl Into<Bytes>) -> impl Into<Bytes> {
|
||||
let mut buffer = Vec::new();
|
||||
let input = bytes.into();
|
||||
match self {
|
||||
Self::Gzip => {
|
||||
GzDecoder::new(input.as_ref())
|
||||
.read_to_end(&mut buffer)
|
||||
.expect("Invalid gzip stream");
|
||||
},
|
||||
Self::Deflate => {
|
||||
ZlibDecoder::new(input.as_ref())
|
||||
.read_to_end(&mut buffer)
|
||||
.expect("Invalid zlib stream");
|
||||
},
|
||||
Self::Plain => {
|
||||
buffer
|
||||
.write(input.as_ref())
|
||||
.expect("Unexpected memory copying issue");
|
||||
},
|
||||
Self::Brotli => {
|
||||
brotli::Decompressor::new(input.as_ref(), 4096 )
|
||||
.read_to_end(&mut buffer)
|
||||
.expect("Invalid brotli stream");
|
||||
},
|
||||
};
|
||||
buffer
|
||||
}
|
||||
|
||||
pub fn header(self: &Encoder) -> Option<impl TryIntoHeaderPair> {
|
||||
match self {
|
||||
Self::Plain => None,
|
||||
|
Reference in New Issue
Block a user