mirror of
https://github.com/actix/actix-web.git
synced 2024-12-30 03:50:42 +00:00
properly enable encoding tests
This commit is contained in:
parent
f7d9b45e64
commit
43f14224b1
2 changed files with 56 additions and 23 deletions
|
@ -289,19 +289,17 @@ impl PayloadEncoder {
|
|||
PayloadEncoder(ContentEncoder::Identity(TransferEncoding::eof(bytes)))
|
||||
}
|
||||
|
||||
pub fn new(buf: SharedBytes, req: &HttpMessage, resp: &mut HttpResponse)
|
||||
-> PayloadEncoder
|
||||
{
|
||||
pub fn new(buf: SharedBytes, req: &HttpMessage, resp: &mut HttpResponse) -> PayloadEncoder {
|
||||
let version = resp.version().unwrap_or_else(|| req.version);
|
||||
let mut body = resp.replace_body(Body::Empty);
|
||||
let has_body = match body {
|
||||
Body::Empty => false,
|
||||
Body::Binary(ref bin) => bin.len() >= 1024,
|
||||
Body::Binary(ref bin) => bin.len() >= 512,
|
||||
_ => true,
|
||||
};
|
||||
|
||||
// Enable content encoding only if response does not contain Content-Encoding header
|
||||
let mut encoding = if has_body && !resp.headers().contains_key(CONTENT_ENCODING) {
|
||||
let mut encoding = if has_body {
|
||||
let encoding = match *resp.content_encoding() {
|
||||
ContentEncoding::Auto => {
|
||||
// negotiate content-encoding
|
||||
|
@ -326,10 +324,6 @@ impl PayloadEncoder {
|
|||
ContentEncoding::Identity
|
||||
};
|
||||
|
||||
// in general case it is very expensive to get compressed payload length,
|
||||
// just switch to chunked encoding
|
||||
let compression = encoding != ContentEncoding::Identity;
|
||||
|
||||
let transfer = match body {
|
||||
Body::Empty => {
|
||||
if resp.chunked() {
|
||||
|
@ -339,9 +333,9 @@ impl PayloadEncoder {
|
|||
TransferEncoding::eof(buf)
|
||||
},
|
||||
Body::Binary(ref mut bytes) => {
|
||||
if compression {
|
||||
let buf = SharedBytes::default();
|
||||
let transfer = TransferEncoding::eof(buf.clone());
|
||||
if encoding.is_compression() {
|
||||
let tmp = SharedBytes::default();
|
||||
let transfer = TransferEncoding::eof(tmp.clone());
|
||||
let mut enc = match encoding {
|
||||
ContentEncoding::Deflate => ContentEncoder::Deflate(
|
||||
DeflateEncoder::new(transfer, Compression::default())),
|
||||
|
@ -356,7 +350,7 @@ impl PayloadEncoder {
|
|||
let _ = enc.write(bytes.as_ref());
|
||||
let _ = enc.write_eof();
|
||||
|
||||
*bytes = Binary::from(buf.get_mut().take());
|
||||
*bytes = Binary::from(tmp.get_mut().take());
|
||||
encoding = ContentEncoding::Identity;
|
||||
}
|
||||
resp.headers_mut().remove(CONTENT_LENGTH);
|
||||
|
|
|
@ -14,8 +14,8 @@ use std::io::Write;
|
|||
use std::sync::{Arc, mpsc};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use flate2::Compression;
|
||||
use flate2::write::{GzEncoder, DeflateEncoder};
|
||||
use brotli2::write::BrotliEncoder;
|
||||
use flate2::write::{GzEncoder, DeflateEncoder, DeflateDecoder};
|
||||
use brotli2::write::{BrotliEncoder, BrotliDecoder};
|
||||
use futures::Future;
|
||||
use h2::client;
|
||||
use bytes::{Bytes, BytesMut, BufMut};
|
||||
|
@ -29,6 +29,22 @@ use actix::System;
|
|||
|
||||
const STR: &str =
|
||||
"Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
|
@ -76,7 +92,22 @@ fn test_body() {
|
|||
|app| app.handler(|_| httpcodes::HTTPOk.build().body(STR)));
|
||||
let mut res = reqwest::get(&srv.url("/")).unwrap();
|
||||
assert!(res.status().is_success());
|
||||
let mut bytes = BytesMut::with_capacity(1024).writer();
|
||||
let mut bytes = BytesMut::with_capacity(2048).writer();
|
||||
let _ = res.copy_to(&mut bytes);
|
||||
let bytes = bytes.into_inner();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_body_gzip() {
|
||||
let srv = test::TestServer::new(
|
||||
|app| app.handler(
|
||||
|_| httpcodes::HTTPOk.build()
|
||||
.content_encoding(headers::ContentEncoding::Gzip)
|
||||
.body(STR)));
|
||||
let mut res = reqwest::get(&srv.url("/")).unwrap();
|
||||
assert!(res.status().is_success());
|
||||
let mut bytes = BytesMut::with_capacity(2048).writer();
|
||||
let _ = res.copy_to(&mut bytes);
|
||||
let bytes = bytes.into_inner();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
|
@ -92,10 +123,14 @@ fn test_body_deflate() {
|
|||
.body(STR)));
|
||||
let mut res = reqwest::get(&srv.url("/")).unwrap();
|
||||
assert!(res.status().is_success());
|
||||
let mut bytes = BytesMut::with_capacity(1024).writer();
|
||||
let mut bytes = BytesMut::with_capacity(2048).writer();
|
||||
let _ = res.copy_to(&mut bytes);
|
||||
let bytes = bytes.into_inner();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
|
||||
let mut e = DeflateDecoder::new(Vec::new());
|
||||
e.write_all(bytes.as_ref()).unwrap();
|
||||
let dec = e.finish().unwrap();
|
||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -108,10 +143,14 @@ fn test_body_brotli() {
|
|||
.body(STR)));
|
||||
let mut res = reqwest::get(&srv.url("/")).unwrap();
|
||||
assert!(res.status().is_success());
|
||||
let mut bytes = BytesMut::with_capacity(1024).writer();
|
||||
let mut bytes = BytesMut::with_capacity(2048).writer();
|
||||
let _ = res.copy_to(&mut bytes);
|
||||
let bytes = bytes.into_inner();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
|
||||
let mut e = BrotliDecoder::new(Vec::with_capacity(2048));
|
||||
e.write_all(bytes.as_ref()).unwrap();
|
||||
let dec = e.finish().unwrap();
|
||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -163,7 +202,7 @@ fn test_gzip_encoding() {
|
|||
let mut res = client.post(&srv.url("/"))
|
||||
.header(ContentEncoding(vec![Encoding::Gzip]))
|
||||
.body(enc.clone()).send().unwrap();
|
||||
let mut bytes = BytesMut::with_capacity(1024).writer();
|
||||
let mut bytes = BytesMut::with_capacity(2048).writer();
|
||||
let _ = res.copy_to(&mut bytes);
|
||||
let bytes = bytes.into_inner();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
|
@ -189,7 +228,7 @@ fn test_deflate_encoding() {
|
|||
let mut res = client.post(&srv.url("/"))
|
||||
.header(ContentEncoding(vec![Encoding::Deflate]))
|
||||
.body(enc.clone()).send().unwrap();
|
||||
let mut bytes = BytesMut::with_capacity(1024).writer();
|
||||
let mut bytes = BytesMut::with_capacity(2048).writer();
|
||||
let _ = res.copy_to(&mut bytes);
|
||||
let bytes = bytes.into_inner();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
|
@ -215,7 +254,7 @@ fn test_brotli_encoding() {
|
|||
let mut res = client.post(&srv.url("/"))
|
||||
.header(ContentEncoding(vec![Encoding::Brotli]))
|
||||
.body(enc.clone()).send().unwrap();
|
||||
let mut bytes = BytesMut::with_capacity(1024).writer();
|
||||
let mut bytes = BytesMut::with_capacity(2048).writer();
|
||||
let _ = res.copy_to(&mut bytes);
|
||||
let bytes = bytes.into_inner();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
|
|
Loading…
Reference in a new issue