1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01:00
actix-web/tests/test_server.rs

1029 lines
33 KiB
Rust
Raw Normal View History

2018-05-29 19:31:37 +02:00
use std::io::{Read, Write};
2019-03-30 00:28:19 +01:00
use std::sync::mpsc;
use std::thread;
2019-03-02 07:51:32 +01:00
use actix_http::http::header::{
2019-03-26 23:14:32 +01:00
ContentEncoding, ACCEPT_ENCODING, CONTENT_ENCODING, CONTENT_LENGTH,
TRANSFER_ENCODING,
2019-03-02 07:51:32 +01:00
};
2019-03-26 23:14:32 +01:00
use actix_http::{h1, Error, HttpService, Response};
2019-03-02 07:51:32 +01:00
use actix_http_test::TestServer;
2019-03-26 23:14:32 +01:00
use brotli2::write::{BrotliDecoder, BrotliEncoder};
2019-03-02 07:51:32 +01:00
use bytes::Bytes;
2018-02-19 22:18:18 +01:00
use flate2::read::GzDecoder;
2019-03-26 23:14:32 +01:00
use flate2::write::{GzEncoder, ZlibDecoder, ZlibEncoder};
use flate2::Compression;
2019-03-28 22:27:07 +01:00
use futures::stream::once;
2019-03-02 07:51:32 +01:00
use rand::{distributions::Alphanumeric, Rng};
use actix_web::{http, test, web, App, HttpResponse, HttpServer};
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "brotli", feature = "flate2-zlib", feature = "flate2-rust"))]
use actix_web::middleware::encoding;
2019-03-30 00:28:19 +01:00
#[cfg(any(feature = "brotli", feature = "flate2-zlib", feature = "flate2-rust"))]
use actix_web::middleware::encoding::BodyEncoding;
2018-02-19 22:18:18 +01:00
2018-04-14 01:02:01 +02:00
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 \
Hello World Hello World Hello World Hello World Hello World";
2018-01-11 06:47:30 +01:00
#[test]
fn test_body() {
2019-03-02 07:51:32 +01:00
let mut srv = TestServer::new(|| {
h1::H1Service::new(
App::new()
.service(web::resource("/").route(web::to(|| Response::Ok().body(STR)))),
2019-03-02 07:51:32 +01:00
)
});
2018-02-19 22:18:18 +01:00
2019-04-02 22:35:01 +02:00
let mut response = srv.block_on(srv.get("/").send()).unwrap();
2018-02-19 22:18:18 +01:00
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(response.body()).unwrap();
2018-01-11 07:42:26 +01:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
2018-01-11 07:42:26 +01:00
#[test]
fn test_body_gzip() {
2019-03-02 07:51:32 +01:00
let mut srv = TestServer::new(|| {
h1::H1Service::new(
App::new()
.wrap(encoding::Compress::new(ContentEncoding::Gzip))
.service(web::resource("/").route(web::to(|| Response::Ok().body(STR)))),
2019-03-02 07:51:32 +01:00
)
2018-04-14 01:02:01 +02:00
});
2018-02-19 22:18:18 +01:00
2019-04-02 22:35:01 +02:00
let mut response = srv.block_on(srv.get("/").no_decompress().send()).unwrap();
2018-02-19 22:18:18 +01:00
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(response.body()).unwrap();
2018-02-19 22:18:18 +01:00
// decode
let mut e = GzDecoder::new(&bytes[..]);
let mut dec = Vec::new();
e.read_to_end(&mut dec).unwrap();
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
2018-01-11 06:47:30 +01:00
}
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
#[test]
fn test_body_encoding_override() {
let mut srv = TestServer::new(|| {
h1::H1Service::new(
App::new()
.wrap(encoding::Compress::new(ContentEncoding::Gzip))
.service(web::resource("/").route(web::to(|| {
use actix_web::middleware::encoding::BodyEncoding;
Response::Ok().encoding(ContentEncoding::Deflate).body(STR)
2019-03-30 00:29:11 +01:00
})))
.service(web::resource("/raw").route(web::to(|| {
use actix_web::middleware::encoding::BodyEncoding;
let body = actix_web::dev::Body::Bytes(STR.into());
2019-03-30 02:51:07 +01:00
let mut response =
Response::with_body(actix_web::http::StatusCode::OK, body);
2019-03-30 00:29:11 +01:00
response.encoding(ContentEncoding::Deflate);
response
}))),
)
});
2019-03-30 00:29:11 +01:00
// Builder
2019-04-02 22:35:01 +02:00
let mut response = srv.block_on(srv.get("/").no_decompress().send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(response.body()).unwrap();
// decode
let mut e = ZlibDecoder::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()));
2019-03-30 00:29:11 +01:00
// Raw Response
2019-03-30 02:51:07 +01:00
let mut response = srv
.block_on(
srv.request(actix_web::http::Method::GET, srv.url("/raw"))
.no_decompress()
.send(),
)
.unwrap();
2019-03-30 00:29:11 +01:00
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(response.body()).unwrap();
2019-03-30 00:29:11 +01:00
// decode
let mut e = ZlibDecoder::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()));
}
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
#[test]
fn test_body_gzip_large() {
2018-03-05 02:54:58 +01:00
let data = STR.repeat(10);
2019-03-02 07:51:32 +01:00
let srv_data = data.clone();
2019-03-02 07:51:32 +01:00
let mut srv = TestServer::new(move || {
2018-04-14 01:02:01 +02:00
let data = srv_data.clone();
2019-03-02 07:51:32 +01:00
h1::H1Service::new(
App::new()
.wrap(encoding::Compress::new(ContentEncoding::Gzip))
.service(
web::resource("/")
.route(web::to(move || Response::Ok().body(data.clone()))),
),
2019-03-02 07:51:32 +01:00
)
2018-04-14 01:02:01 +02:00
});
2019-04-02 22:35:01 +02:00
let mut response = srv.block_on(srv.get("/").no_decompress().send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(response.body()).unwrap();
// decode
let mut e = GzDecoder::new(&bytes[..]);
let mut dec = Vec::new();
e.read_to_end(&mut dec).unwrap();
assert_eq!(Bytes::from(dec), Bytes::from(data));
}
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
#[test]
fn test_body_gzip_large_random() {
2018-04-29 18:09:08 +02:00
let data = rand::thread_rng()
2018-05-29 19:31:37 +02:00
.sample_iter(&Alphanumeric)
2018-04-29 18:09:08 +02:00
.take(70_000)
.collect::<String>();
2019-03-02 07:51:32 +01:00
let srv_data = data.clone();
2019-03-02 07:51:32 +01:00
let mut srv = TestServer::new(move || {
2018-04-14 01:02:01 +02:00
let data = srv_data.clone();
2019-03-02 07:51:32 +01:00
h1::H1Service::new(
App::new()
.wrap(encoding::Compress::new(ContentEncoding::Gzip))
.service(
web::resource("/")
.route(web::to(move || Response::Ok().body(data.clone()))),
),
2019-03-02 07:51:32 +01:00
)
2018-04-14 01:02:01 +02:00
});
2019-04-02 22:35:01 +02:00
let mut response = srv.block_on(srv.get("/").no_decompress().send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(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));
}
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
2018-01-12 00:26:46 +01:00
#[test]
2018-02-24 05:29:35 +01:00
fn test_body_chunked_implicit() {
2019-03-02 07:51:32 +01:00
let mut srv = TestServer::new(move || {
h1::H1Service::new(
App::new()
.wrap(encoding::Compress::new(ContentEncoding::Gzip))
.service(web::resource("/").route(web::get().to(move || {
Response::Ok().streaming(once(Ok::<_, Error>(Bytes::from_static(
STR.as_ref(),
))))
}))),
2019-03-02 07:51:32 +01:00
)
2018-04-14 01:02:01 +02:00
});
2018-01-12 00:26:46 +01:00
2019-04-02 22:35:01 +02:00
let mut response = srv.block_on(srv.get("/").no_decompress().send()).unwrap();
2018-02-19 22:18:18 +01:00
assert!(response.status().is_success());
2019-03-02 07:51:32 +01:00
assert_eq!(
response.headers().get(TRANSFER_ENCODING).unwrap(),
&b"chunked"[..]
);
2018-02-19 22:18:18 +01:00
// read response
let bytes = srv.block_on(response.body()).unwrap();
2018-02-19 22:18:18 +01:00
// decode
let mut e = GzDecoder::new(&bytes[..]);
let mut dec = Vec::new();
e.read_to_end(&mut dec).unwrap();
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
2018-01-12 00:26:46 +01:00
}
2018-01-12 01:22:27 +01:00
#[test]
2019-03-28 22:27:07 +01:00
#[cfg(feature = "brotli")]
2018-01-12 01:22:27 +01:00
fn test_body_br_streaming() {
2019-03-02 07:51:32 +01:00
let mut srv = TestServer::new(move || {
h1::H1Service::new(
App::new()
.wrap(encoding::Compress::new(ContentEncoding::Br))
.service(web::resource("/").route(web::to(move || {
Response::Ok().streaming(once(Ok::<_, Error>(Bytes::from_static(
STR.as_ref(),
))))
}))),
2019-03-02 07:51:32 +01:00
)
2018-04-14 01:02:01 +02:00
});
2018-01-12 01:22:27 +01:00
2019-03-26 20:31:51 +01:00
let mut response = srv
2019-03-27 04:45:00 +01:00
.block_on(
2019-04-02 22:35:01 +02:00
srv.get("/")
2019-03-27 04:45:00 +01:00
.header(ACCEPT_ENCODING, "br")
.no_decompress()
.send(),
)
2019-03-26 20:31:51 +01:00
.unwrap();
2018-02-19 22:18:18 +01:00
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(response.body()).unwrap();
2018-01-12 01:22:27 +01:00
2018-02-19 22:18:18 +01:00
// decode br
2018-01-12 01:22:27 +01:00
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()));
}
2018-01-21 01:12:38 +01:00
#[test]
fn test_head_binary() {
2019-03-02 07:51:32 +01:00
let mut srv = TestServer::new(move || {
h1::H1Service::new(App::new().service(web::resource("/").route(
web::head().to(move || Response::Ok().content_length(100).body(STR)),
)))
2018-04-14 01:02:01 +02:00
});
2018-01-21 01:12:38 +01:00
2019-04-02 22:35:01 +02:00
let mut response = srv.block_on(srv.head("/").send()).unwrap();
2018-02-19 22:18:18 +01:00
assert!(response.status().is_success());
{
2019-03-02 07:51:32 +01:00
let len = response.headers().get(CONTENT_LENGTH).unwrap();
2018-02-19 22:18:18 +01:00
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
}
// read response
let bytes = srv.block_on(response.body()).unwrap();
2018-07-06 04:11:36 +02:00
assert!(bytes.is_empty());
2018-01-21 01:12:38 +01:00
}
#[test]
2019-03-02 07:51:32 +01:00
fn test_no_chunking() {
let mut srv = TestServer::new(move || {
h1::H1Service::new(App::new().service(web::resource("/").route(web::to(
move || {
2019-03-02 07:51:32 +01:00
Response::Ok()
.no_chunking()
.content_length(STR.len() as u64)
.streaming(once(Ok::<_, Error>(Bytes::from_static(STR.as_ref()))))
},
))))
2018-04-14 01:02:01 +02:00
});
2018-01-12 01:22:27 +01:00
2019-04-02 22:35:01 +02:00
let mut response = srv.block_on(srv.get("/").send()).unwrap();
2018-02-19 22:18:18 +01:00
assert!(response.status().is_success());
2019-03-02 07:51:32 +01:00
assert!(!response.headers().contains_key(TRANSFER_ENCODING));
2018-02-19 22:18:18 +01:00
// read response
let bytes = srv.block_on(response.body()).unwrap();
2018-01-12 01:22:27 +01:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
2018-01-11 06:47:30 +01:00
#[test]
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
2018-01-11 06:47:30 +01:00
fn test_body_deflate() {
2019-03-02 07:51:32 +01:00
let mut srv = TestServer::new(move || {
h1::H1Service::new(
App::new()
.wrap(encoding::Compress::new(ContentEncoding::Deflate))
.service(
web::resource("/").route(web::to(move || Response::Ok().body(STR))),
),
2019-03-02 07:51:32 +01:00
)
2018-04-14 01:02:01 +02:00
});
2018-01-11 07:42:26 +01:00
2018-02-19 22:18:18 +01:00
// client request
2019-04-02 22:35:01 +02:00
let mut response = srv.block_on(srv.get("/").no_decompress().send()).unwrap();
2018-02-19 22:18:18 +01:00
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(response.body()).unwrap();
2018-02-19 22:18:18 +01:00
let mut e = ZlibDecoder::new(Vec::new());
2018-01-11 07:42:26 +01:00
e.write_all(bytes.as_ref()).unwrap();
let dec = e.finish().unwrap();
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
2018-01-11 06:47:30 +01:00
}
#[test]
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "brotli"))]
2018-01-11 06:47:30 +01:00
fn test_body_brotli() {
2019-03-02 07:51:32 +01:00
let mut srv = TestServer::new(move || {
h1::H1Service::new(
App::new()
.wrap(encoding::Compress::new(ContentEncoding::Br))
.service(
web::resource("/").route(web::to(move || Response::Ok().body(STR))),
),
2019-03-02 07:51:32 +01:00
)
2018-04-14 01:02:01 +02:00
});
2018-01-11 07:42:26 +01:00
2018-02-19 22:18:18 +01:00
// client request
2019-03-26 20:31:51 +01:00
let mut response = srv
2019-03-27 04:45:00 +01:00
.block_on(
2019-04-02 22:35:01 +02:00
srv.get("/")
2019-03-27 04:45:00 +01:00
.header(ACCEPT_ENCODING, "br")
.no_decompress()
.send(),
)
2019-03-26 20:31:51 +01:00
.unwrap();
2018-02-19 22:18:18 +01:00
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(response.body()).unwrap();
2018-02-19 22:18:18 +01:00
// decode brotli
2018-01-11 07:42:26 +01:00
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()));
2018-01-11 06:47:30 +01:00
}
#[test]
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
fn test_encoding() {
let mut srv = TestServer::new(move || {
HttpService::new(
App::new().enable_encoding().service(
web::resource("/")
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
),
)
});
// client request
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(STR.as_ref()).unwrap();
let enc = e.finish().unwrap();
let request = srv
2019-04-02 22:35:01 +02:00
.post("/")
.header(CONTENT_ENCODING, "gzip")
.send_body(enc.clone());
let mut response = srv.block_on(request).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
2019-03-26 23:14:32 +01:00
#[test]
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
2019-03-26 23:14:32 +01:00
fn test_gzip_encoding() {
let mut srv = TestServer::new(move || {
HttpService::new(
App::new().chain(encoding::Decompress::new()).service(
2019-03-26 23:14:32 +01:00
web::resource("/")
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
),
)
});
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
// client request
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(STR.as_ref()).unwrap();
let enc = e.finish().unwrap();
let request = srv
2019-04-02 22:35:01 +02:00
.post("/")
2019-03-26 23:14:32 +01:00
.header(CONTENT_ENCODING, "gzip")
.send_body(enc.clone());
let mut response = srv.block_on(request).unwrap();
assert!(response.status().is_success());
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
// read response
let bytes = srv.block_on(response.body()).unwrap();
2019-03-26 23:14:32 +01:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
#[test]
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
2019-03-26 23:14:32 +01:00
fn test_gzip_encoding_large() {
let data = STR.repeat(10);
let mut srv = TestServer::new(move || {
h1::H1Service::new(
App::new().chain(encoding::Decompress::new()).service(
2019-03-26 23:14:32 +01:00
web::resource("/")
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
),
)
});
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
// 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
2019-04-02 22:35:01 +02:00
.post("/")
2019-03-26 23:14:32 +01:00
.header(CONTENT_ENCODING, "gzip")
.send_body(enc.clone());
let mut response = srv.block_on(request).unwrap();
assert!(response.status().is_success());
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
// read response
let bytes = srv.block_on(response.body()).unwrap();
2019-03-26 23:14:32 +01:00
assert_eq!(bytes, Bytes::from(data));
}
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
#[test]
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
2019-03-26 23:14:32 +01:00
fn test_reading_gzip_encoding_large_random() {
let data = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(60_000)
.collect::<String>();
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
let mut srv = TestServer::new(move || {
HttpService::new(
App::new().chain(encoding::Decompress::new()).service(
2019-03-26 23:14:32 +01:00
web::resource("/")
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
),
)
});
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
// 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
2019-04-02 22:35:01 +02:00
.post("/")
2019-03-26 23:14:32 +01:00
.header(CONTENT_ENCODING, "gzip")
.send_body(enc.clone());
let mut response = srv.block_on(request).unwrap();
assert!(response.status().is_success());
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
// read response
let bytes = srv.block_on(response.body()).unwrap();
2019-03-26 23:14:32 +01:00
assert_eq!(bytes.len(), data.len());
assert_eq!(bytes, Bytes::from(data));
}
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
#[test]
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
2019-03-26 23:14:32 +01:00
fn test_reading_deflate_encoding() {
let mut srv = TestServer::new(move || {
h1::H1Service::new(
App::new().chain(encoding::Decompress::new()).service(
2019-03-26 23:14:32 +01:00
web::resource("/")
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
),
)
});
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
e.write_all(STR.as_ref()).unwrap();
let enc = e.finish().unwrap();
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
// client request
let request = srv
2019-04-02 22:35:01 +02:00
.post("/")
2019-03-26 23:14:32 +01:00
.header(CONTENT_ENCODING, "deflate")
.send_body(enc.clone());
let mut response = srv.block_on(request).unwrap();
assert!(response.status().is_success());
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
// read response
let bytes = srv.block_on(response.body()).unwrap();
2019-03-26 23:14:32 +01:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
#[test]
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
2019-03-26 23:14:32 +01:00
fn test_reading_deflate_encoding_large() {
let data = STR.repeat(10);
let mut srv = TestServer::new(move || {
h1::H1Service::new(
App::new().chain(encoding::Decompress::new()).service(
2019-03-26 23:14:32 +01:00
web::resource("/")
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
),
)
});
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
// client request
let request = srv
2019-04-02 22:35:01 +02:00
.post("/")
2019-03-26 23:14:32 +01:00
.header(CONTENT_ENCODING, "deflate")
.send_body(enc.clone());
let mut response = srv.block_on(request).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(response.body()).unwrap();
2019-03-26 23:14:32 +01:00
assert_eq!(bytes, Bytes::from(data));
}
#[test]
2019-03-28 22:27:07 +01:00
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
2019-03-26 23:14:32 +01:00
fn test_reading_deflate_encoding_large_random() {
let data = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(160_000)
.collect::<String>();
2019-03-02 07:51:32 +01:00
2019-03-26 23:14:32 +01:00
let mut srv = TestServer::new(move || {
h1::H1Service::new(
App::new().chain(encoding::Decompress::new()).service(
2019-03-26 23:14:32 +01:00
web::resource("/")
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
),
)
});
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
// client request
let request = srv
2019-04-02 22:35:01 +02:00
.post("/")
2019-03-26 23:14:32 +01:00
.header(CONTENT_ENCODING, "deflate")
.send_body(enc.clone());
let mut response = srv.block_on(request).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(response.body()).unwrap();
2019-03-26 23:14:32 +01:00
assert_eq!(bytes.len(), data.len());
assert_eq!(bytes, Bytes::from(data));
}
#[test]
2019-03-28 22:27:07 +01:00
#[cfg(feature = "brotli")]
2019-03-26 23:14:32 +01:00
fn test_brotli_encoding() {
let mut srv = TestServer::new(move || {
h1::H1Service::new(
App::new().chain(encoding::Decompress::new()).service(
2019-03-26 23:14:32 +01:00
web::resource("/")
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
),
)
});
let mut e = BrotliEncoder::new(Vec::new(), 5);
e.write_all(STR.as_ref()).unwrap();
let enc = e.finish().unwrap();
// client request
let request = srv
2019-04-02 22:35:01 +02:00
.post("/")
2019-03-26 23:14:32 +01:00
.header(CONTENT_ENCODING, "br")
.send_body(enc.clone());
let mut response = srv.block_on(request).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(response.body()).unwrap();
2019-03-26 23:14:32 +01:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
#[cfg(feature = "brotli")]
#[test]
fn test_brotli_encoding_large() {
let data = STR.repeat(10);
let mut srv = TestServer::new(move || {
h1::H1Service::new(
App::new().chain(encoding::Decompress::new()).service(
2019-03-26 23:14:32 +01:00
web::resource("/")
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
),
)
});
let mut e = BrotliEncoder::new(Vec::new(), 5);
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
// client request
let request = srv
2019-04-02 22:35:01 +02:00
.post("/")
2019-03-26 23:14:32 +01:00
.header(CONTENT_ENCODING, "br")
.send_body(enc.clone());
let mut response = srv.block_on(request).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(response.body()).unwrap();
2019-03-26 23:14:32 +01:00
assert_eq!(bytes, Bytes::from(data));
}
2019-03-02 07:51:32 +01:00
// #[cfg(all(feature = "brotli", feature = "ssl"))]
// #[test]
// fn test_brotli_encoding_large_ssl() {
// use actix::{Actor, System};
// use openssl::ssl::{
// SslAcceptor, SslConnector, SslFiletype, SslMethod, SslVerifyMode,
// };
// // load ssl keys
// let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
// builder
// .set_private_key_file("tests/key.pem", SslFiletype::PEM)
// .unwrap();
// builder
// .set_certificate_chain_file("tests/cert.pem")
// .unwrap();
// let data = STR.repeat(10);
// let srv = test::TestServer::build().ssl(builder).start(|app| {
// app.handler(|req: &HttpRequest| {
// req.body()
// .and_then(|bytes: Bytes| {
// Ok(HttpResponse::Ok()
// .content_encoding(http::ContentEncoding::Identity)
// .body(bytes))
// })
// .responder()
// })
// });
// let mut rt = System::new("test");
// // client connector
// let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
// builder.set_verify(SslVerifyMode::NONE);
// let conn = client::ClientConnector::with_connector(builder.build()).start();
// // body
// let mut e = BrotliEncoder::new(Vec::new(), 5);
// e.write_all(data.as_ref()).unwrap();
// let enc = e.finish().unwrap();
// // client request
// let request = client::ClientRequest::build()
// .uri(srv.url("/"))
// .method(http::Method::POST)
// .header(http::header::CONTENT_ENCODING, "br")
// .with_connector(conn)
// .body(enc)
// .unwrap();
// let response = rt.block_on(request.send()).unwrap();
// assert!(response.status().is_success());
// // read response
// let bytes = rt.block_on(response.body()).unwrap();
// assert_eq!(bytes, Bytes::from(data));
// }
2019-03-30 00:28:19 +01:00
#[cfg(all(
feature = "rust-tls",
feature = "ssl",
any(feature = "flate2-zlib", feature = "flate2-rust")
))]
#[test]
fn test_reading_deflate_encoding_large_random_ssl() {
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
use rustls::internal::pemfile::{certs, rsa_private_keys};
use rustls::{NoClientAuth, ServerConfig};
use std::fs::File;
use std::io::BufReader;
2019-03-02 07:51:32 +01:00
2019-03-30 00:28:19 +01:00
let addr = TestServer::unused_addr();
let (tx, rx) = mpsc::channel();
2019-03-02 07:51:32 +01:00
2019-03-30 00:28:19 +01:00
let data = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(160_000)
.collect::<String>();
2019-03-02 07:51:32 +01:00
2019-03-30 00:28:19 +01:00
thread::spawn(move || {
let sys = actix_rt::System::new("test");
// load ssl keys
let mut config = ServerConfig::new(NoClientAuth::new());
let cert_file = &mut BufReader::new(File::open("tests/cert.pem").unwrap());
let key_file = &mut BufReader::new(File::open("tests/key.pem").unwrap());
let cert_chain = certs(cert_file).unwrap();
let mut keys = rsa_private_keys(key_file).unwrap();
config.set_single_cert(cert_chain, keys.remove(0)).unwrap();
let srv = HttpServer::new(|| {
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
Ok::<_, Error>(
HttpResponse::Ok()
.encoding(http::ContentEncoding::Identity)
.body(bytes),
)
})))
})
.bind_rustls(addr, config)
.unwrap()
.start();
let _ = tx.send((srv, actix_rt::System::current()));
let _ = sys.run();
});
let (srv, _sys) = rx.recv().unwrap();
let client = test::run_on(|| {
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
builder.set_verify(SslVerifyMode::NONE);
let _ = builder.set_alpn_protos(b"\x02h2\x08http/1.1").unwrap();
awc::Client::build()
.connector(
awc::Connector::new()
.timeout(std::time::Duration::from_millis(500))
.ssl(builder.build())
2019-04-06 09:16:04 +02:00
.finish(),
2019-03-30 00:28:19 +01:00
)
.finish()
});
2019-03-02 07:51:32 +01:00
2019-03-30 00:28:19 +01:00
// encode data
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
2019-03-02 07:51:32 +01:00
2019-03-30 00:28:19 +01:00
// client request
let _req = client
.post(format!("https://{}/", addr))
.header(http::header::CONTENT_ENCODING, "deflate")
.send_body(enc);
2019-03-02 07:51:32 +01:00
2019-03-30 00:28:19 +01:00
// TODO: fix
// let response = test::block_on(req).unwrap();
// assert!(response.status().is_success());
// read response
// let bytes = test::block_on(response.body()).unwrap();
// assert_eq!(bytes.len(), data.len());
// assert_eq!(bytes, Bytes::from(data));
// stop
let _ = srv.stop(false);
}
2019-03-02 07:51:32 +01:00
// #[cfg(all(feature = "tls", feature = "ssl"))]
// #[test]
// fn test_reading_deflate_encoding_large_random_tls() {
// use native_tls::{Identity, TlsAcceptor};
// use openssl::ssl::{
// SslAcceptor, SslConnector, SslFiletype, SslMethod, SslVerifyMode,
// };
// use std::fs::File;
// use std::sync::mpsc;
// use actix::{Actor, System};
// let (tx, rx) = mpsc::channel();
// // load ssl keys
// let mut file = File::open("tests/identity.pfx").unwrap();
// let mut identity = vec![];
// file.read_to_end(&mut identity).unwrap();
// let identity = Identity::from_pkcs12(&identity, "1").unwrap();
// let acceptor = TlsAcceptor::new(identity).unwrap();
// // load ssl keys
// let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
// builder
// .set_private_key_file("tests/key.pem", SslFiletype::PEM)
// .unwrap();
// builder
// .set_certificate_chain_file("tests/cert.pem")
// .unwrap();
// let data = rand::thread_rng()
// .sample_iter(&Alphanumeric)
// .take(160_000)
// .collect::<String>();
// let addr = test::TestServer::unused_addr();
// thread::spawn(move || {
// System::run(move || {
// server::new(|| {
// App::new().handler("/", |req: &HttpRequest| {
// req.body()
// .and_then(|bytes: Bytes| {
// Ok(HttpResponse::Ok()
// .content_encoding(http::ContentEncoding::Identity)
// .body(bytes))
// })
// .responder()
// })
// })
// .bind_tls(addr, acceptor)
// .unwrap()
// .start();
// let _ = tx.send(System::current());
// });
// });
// let sys = rx.recv().unwrap();
// let mut rt = System::new("test");
// // client connector
// let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
// builder.set_verify(SslVerifyMode::NONE);
// let conn = client::ClientConnector::with_connector(builder.build()).start();
// // encode data
// let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
// e.write_all(data.as_ref()).unwrap();
// let enc = e.finish().unwrap();
// // client request
// let request = client::ClientRequest::build()
// .uri(format!("https://{}/", addr))
// .method(http::Method::POST)
// .header(http::header::CONTENT_ENCODING, "deflate")
// .with_connector(conn)
// .body(enc)
// .unwrap();
// let response = rt.block_on(request.send()).unwrap();
// assert!(response.status().is_success());
// // read response
// let bytes = rt.block_on(response.body()).unwrap();
// assert_eq!(bytes.len(), data.len());
// assert_eq!(bytes, Bytes::from(data));
// let _ = sys.stop();
// }
// #[test]
// fn test_server_cookies() {
// use actix_web::http;
// let mut srv = test::TestServer::with_factory(|| {
// App::new().resource("/", |r| {
// r.f(|_| {
// HttpResponse::Ok()
// .cookie(
// http::CookieBuilder::new("first", "first_value")
// .http_only(true)
// .finish(),
// )
// .cookie(http::Cookie::new("second", "first_value"))
// .cookie(http::Cookie::new("second", "second_value"))
// .finish()
// })
// })
// });
// let first_cookie = http::CookieBuilder::new("first", "first_value")
// .http_only(true)
// .finish();
// let second_cookie = http::Cookie::new("second", "second_value");
2019-04-02 22:35:01 +02:00
// let request = srv.get("/").finish().unwrap();
2019-03-02 07:51:32 +01:00
// let response = srv.execute(request.send()).unwrap();
// assert!(response.status().is_success());
// let cookies = response.cookies().expect("To have cookies");
// assert_eq!(cookies.len(), 2);
// if cookies[0] == first_cookie {
// assert_eq!(cookies[1], second_cookie);
// } else {
// assert_eq!(cookies[0], second_cookie);
// assert_eq!(cookies[1], first_cookie);
// }
// let first_cookie = first_cookie.to_string();
// let second_cookie = second_cookie.to_string();
// //Check that we have exactly two instances of raw cookie headers
// let cookies = response
// .headers()
// .get_all(http::header::SET_COOKIE)
// .iter()
// .map(|header| header.to_str().expect("To str").to_string())
// .collect::<Vec<_>>();
// assert_eq!(cookies.len(), 2);
// if cookies[0] == first_cookie {
// assert_eq!(cookies[1], second_cookie);
// } else {
// assert_eq!(cookies[0], second_cookie);
// assert_eq!(cookies[1], first_cookie);
// }
// }
// #[test]
// fn test_slow_request() {
// use actix::System;
// use std::net;
// use std::sync::mpsc;
// let (tx, rx) = mpsc::channel();
// let addr = test::TestServer::unused_addr();
// thread::spawn(move || {
// System::run(move || {
// let srv = server::new(|| {
// vec![App::new().resource("/", |r| {
// r.method(http::Method::GET).f(|_| HttpResponse::Ok())
// })]
// });
// let srv = srv.bind(addr).unwrap();
// srv.client_timeout(200).start();
// let _ = tx.send(System::current());
// });
// });
// let sys = rx.recv().unwrap();
// thread::sleep(time::Duration::from_millis(200));
// let mut stream = net::TcpStream::connect(addr).unwrap();
// let mut data = String::new();
// let _ = stream.read_to_string(&mut data);
// assert!(data.starts_with("HTTP/1.1 408 Request Timeout"));
// let mut stream = net::TcpStream::connect(addr).unwrap();
// let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\n");
// let mut data = String::new();
// let _ = stream.read_to_string(&mut data);
// assert!(data.starts_with("HTTP/1.1 408 Request Timeout"));
// sys.stop();
// }
// #[test]
// #[cfg(feature = "ssl")]
// fn test_ssl_handshake_timeout() {
// use actix::System;
// use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
// use std::net;
// use std::sync::mpsc;
// let (tx, rx) = mpsc::channel();
// let addr = test::TestServer::unused_addr();
// // load ssl keys
// let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
// builder
// .set_private_key_file("tests/key.pem", SslFiletype::PEM)
// .unwrap();
// builder
// .set_certificate_chain_file("tests/cert.pem")
// .unwrap();
// thread::spawn(move || {
// System::run(move || {
// let srv = server::new(|| {
// App::new().resource("/", |r| {
// r.method(http::Method::GET).f(|_| HttpResponse::Ok())
// })
// });
// srv.bind_ssl(addr, builder)
// .unwrap()
// .workers(1)
// .client_timeout(200)
// .start();
// let _ = tx.send(System::current());
// });
// });
// let sys = rx.recv().unwrap();
// let mut stream = net::TcpStream::connect(addr).unwrap();
// let mut data = String::new();
// let _ = stream.read_to_string(&mut data);
// assert!(data.is_empty());
// let _ = sys.stop();
// }