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:
Andrey "MOU" Larionov 2022-10-13 00:56:57 +02:00
parent 99e2788ee7
commit b69f8d67c3
No known key found for this signature in database
GPG key ID: 5FF293FC94C01D6A
3 changed files with 115 additions and 2 deletions

View file

@ -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,