1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-10-03 16:51:58 +00:00

Merge pull request #102 from mockersf/gzip

add tests with large random bodies for gzip
This commit is contained in:
Nikolay Kim 2018-03-06 08:48:06 -08:00 committed by GitHub
commit 9cc6f6b1e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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::<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]
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::<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]
fn test_deflate_encoding() {
let mut srv = test::TestServer::new(|app| app.handler(|req: HttpRequest| {