mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-03 20:07:09 +02: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:
parent
99e2788ee7
commit
b69f8d67c3
3 changed files with 115 additions and 2 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue