From 779e773185c0b81f43467ad26d4fb698176ff5bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Tue, 6 Mar 2018 14:26:48 +0100 Subject: [PATCH] add tests with large random bodies for gzip --- tests/test_server.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/test_server.rs b/tests/test_server.rs index 92a876b5c..cafbea740 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -7,6 +7,7 @@ extern crate http; extern crate bytes; extern crate flate2; extern crate brotli2; +extern crate rand; use std::{net, thread, time}; use std::io::{Read, Write}; @@ -23,6 +24,7 @@ use bytes::{Bytes, BytesMut}; use http::{header, Request}; use tokio_core::net::TcpStream; use tokio_core::reactor::Core; +use rand::Rng; use actix::System; use actix_web::*; @@ -235,6 +237,37 @@ fn test_body_gzip_large() { assert_eq!(Bytes::from(dec), Bytes::from(data)); } +#[test] +fn test_body_gzip_large_random() { + let data = rand::thread_rng() + .gen_ascii_chars() + .take(70000) + .collect::(); + let srv_data = Arc::new(data.clone()); + + let mut srv = test::TestServer::new( + move |app| { + let data = srv_data.clone(); + app.handler( + move |_| httpcodes::HTTPOk.build() + .content_encoding(headers::ContentEncoding::Gzip) + .body(data.as_ref()))}); + + let request = srv.get().disable_decompress().finish().unwrap(); + let response = srv.execute(request.send()).unwrap(); + assert!(response.status().is_success()); + + // read response + let bytes = srv.execute(response.body()).unwrap(); + + // decode + let mut e = GzDecoder::new(&bytes[..]); + let mut dec = Vec::new(); + e.read_to_end(&mut dec).unwrap(); + assert_eq!(dec.len(), data.len()); + assert_eq!(Bytes::from(dec), Bytes::from(data)); +} + #[test] fn test_body_chunked_implicit() { let mut srv = test::TestServer::new( @@ -491,6 +524,39 @@ fn test_gzip_encoding_large() { assert_eq!(bytes, Bytes::from(data)); } +#[test] +fn test_gzip_encoding_large_random() { + let data = rand::thread_rng() + .gen_ascii_chars() + .take(6000) + .collect::(); + + let mut srv = test::TestServer::new(|app| app.handler(|req: HttpRequest| { + req.body() + .and_then(|bytes: Bytes| { + Ok(httpcodes::HTTPOk + .build() + .content_encoding(headers::ContentEncoding::Identity) + .body(bytes)) + }).responder()} + )); + + // client request + let mut e = GzEncoder::new(Vec::new(), Compression::default()); + e.write_all(data.as_ref()).unwrap(); + let enc = e.finish().unwrap(); + + let request = srv.post() + .header(header::CONTENT_ENCODING, "gzip") + .body(enc.clone()).unwrap(); + let response = srv.execute(request.send()).unwrap(); + assert!(response.status().is_success()); + + // read response + let bytes = srv.execute(response.body()).unwrap(); + assert_eq!(bytes, Bytes::from(data)); +} + #[test] fn test_deflate_encoding() { let mut srv = test::TestServer::new(|app| app.handler(|req: HttpRequest| {