From 43f14224b1b188cb30dc97833a25f8a95b3db8ca Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 10 Jan 2018 22:42:26 -0800 Subject: [PATCH] properly enable encoding tests --- src/encoding.rs | 20 ++++++--------- tests/test_server.rs | 59 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 23 deletions(-) diff --git a/src/encoding.rs b/src/encoding.rs index 2a054536b..90b084141 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -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); diff --git a/tests/test_server.rs b/tests/test_server.rs index b2aa76427..e8d58d751 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -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()));