1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-24 00:50:36 +00:00

add tests with large random bodies for gzip

This commit is contained in:
François Mockers 2018-03-06 14:26:48 +01:00
parent 7eb310f8ce
commit 779e773185

View file

@ -7,6 +7,7 @@ extern crate http;
extern crate bytes; extern crate bytes;
extern crate flate2; extern crate flate2;
extern crate brotli2; extern crate brotli2;
extern crate rand;
use std::{net, thread, time}; use std::{net, thread, time};
use std::io::{Read, Write}; use std::io::{Read, Write};
@ -23,6 +24,7 @@ use bytes::{Bytes, BytesMut};
use http::{header, Request}; use http::{header, Request};
use tokio_core::net::TcpStream; use tokio_core::net::TcpStream;
use tokio_core::reactor::Core; use tokio_core::reactor::Core;
use rand::Rng;
use actix::System; use actix::System;
use actix_web::*; use actix_web::*;
@ -235,6 +237,37 @@ fn test_body_gzip_large() {
assert_eq!(Bytes::from(dec), Bytes::from(data)); 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::<String>();
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] #[test]
fn test_body_chunked_implicit() { fn test_body_chunked_implicit() {
let mut srv = test::TestServer::new( let mut srv = test::TestServer::new(
@ -491,6 +524,39 @@ fn test_gzip_encoding_large() {
assert_eq!(bytes, Bytes::from(data)); assert_eq!(bytes, Bytes::from(data));
} }
#[test]
fn test_gzip_encoding_large_random() {
let data = rand::thread_rng()
.gen_ascii_chars()
.take(6000)
.collect::<String>();
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] #[test]
fn test_deflate_encoding() { fn test_deflate_encoding() {
let mut srv = test::TestServer::new(|app| app.handler(|req: HttpRequest| { let mut srv = test::TestServer::new(|app| app.handler(|req: HttpRequest| {