mirror of
https://github.com/fafhrd91/actix-web
synced 2025-01-18 13:51:50 +01:00
refactor test server impl
This commit is contained in:
parent
fa07415721
commit
db1d6b7963
18
Cargo.toml
18
Cargo.toml
@ -43,10 +43,7 @@ members = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["compress", "client", "fail"]
|
default = ["compress", "fail"]
|
||||||
|
|
||||||
# http client
|
|
||||||
client = ["awc"]
|
|
||||||
|
|
||||||
# content-encoding support
|
# content-encoding support
|
||||||
compress = ["actix-http/compress", "awc/compress"]
|
compress = ["actix-http/compress", "awc/compress"]
|
||||||
@ -57,10 +54,10 @@ secure-cookies = ["actix-http/secure-cookies"]
|
|||||||
fail = ["actix-http/fail"]
|
fail = ["actix-http/fail"]
|
||||||
|
|
||||||
# openssl
|
# openssl
|
||||||
openssl = ["actix-tls/openssl", "awc/openssl"]
|
openssl = ["actix-tls/openssl", "awc/openssl", "open-ssl"]
|
||||||
|
|
||||||
# rustls
|
# rustls
|
||||||
rustls = ["actix-tls/rustls", "awc/rustls"]
|
rustls = ["actix-tls/rustls", "awc/rustls", "rust-tls"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-codec = "0.2.0"
|
actix-codec = "0.2.0"
|
||||||
@ -70,12 +67,13 @@ actix-router = "0.2.0"
|
|||||||
actix-rt = "1.0.0"
|
actix-rt = "1.0.0"
|
||||||
actix-server = "1.0.0"
|
actix-server = "1.0.0"
|
||||||
actix-testing = "1.0.0"
|
actix-testing = "1.0.0"
|
||||||
|
actix-macros = "0.1.0"
|
||||||
actix-threadpool = "0.3.0"
|
actix-threadpool = "0.3.0"
|
||||||
actix-tls = "1.0.0"
|
actix-tls = "1.0.0"
|
||||||
|
|
||||||
actix-web-codegen = "0.2.0-alpha.2"
|
actix-web-codegen = "0.2.0-alpha.2"
|
||||||
actix-http = "1.0.0"
|
actix-http = "1.0.0"
|
||||||
awc = { version = "1.0.0", default-features = false, optional = true }
|
awc = { version = "1.0.0", default-features = false }
|
||||||
|
|
||||||
bytes = "0.5.2"
|
bytes = "0.5.2"
|
||||||
derive_more = "0.99.2"
|
derive_more = "0.99.2"
|
||||||
@ -92,18 +90,16 @@ serde_json = "1.0"
|
|||||||
serde_urlencoded = "0.6.1"
|
serde_urlencoded = "0.6.1"
|
||||||
time = "0.1.42"
|
time = "0.1.42"
|
||||||
url = "2.1"
|
url = "2.1"
|
||||||
|
open-ssl = { version="0.10", package = "openssl", optional = true }
|
||||||
|
rust-tls = { version = "0.16.0", package = "rustls", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
# actix = "0.8.3"
|
# actix = "0.8.3"
|
||||||
actix-connect = "1.0.0"
|
|
||||||
actix-http-test = "1.0.0-alpha.3"
|
|
||||||
rand = "0.7"
|
rand = "0.7"
|
||||||
env_logger = "0.6"
|
env_logger = "0.6"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
brotli = "3.3.0"
|
brotli = "3.3.0"
|
||||||
flate2 = "1.0.13"
|
flate2 = "1.0.13"
|
||||||
open-ssl = { version="0.10", package = "openssl" }
|
|
||||||
rust_tls = { version = "0.16.0", package = "rustls" }
|
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
lto = true
|
||||||
|
@ -100,10 +100,6 @@ pub mod test;
|
|||||||
mod types;
|
mod types;
|
||||||
pub mod web;
|
pub mod web;
|
||||||
|
|
||||||
#[allow(unused_imports)]
|
|
||||||
#[macro_use]
|
|
||||||
extern crate actix_web_codegen;
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub use actix_web_codegen::*;
|
pub use actix_web_codegen::*;
|
||||||
|
|
||||||
@ -163,7 +159,6 @@ pub mod dev {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
|
||||||
pub mod client {
|
pub mod client {
|
||||||
//! An HTTP Client
|
//! An HTTP Client
|
||||||
//!
|
//!
|
||||||
|
409
src/test.rs
409
src/test.rs
@ -1,16 +1,24 @@
|
|||||||
//! Various helpers for Actix applications to use during testing.
|
//! Various helpers for Actix applications to use during testing.
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::sync::mpsc;
|
||||||
|
use std::{fmt, net, thread, time};
|
||||||
|
|
||||||
|
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
||||||
use actix_http::http::header::{ContentType, Header, HeaderName, IntoHeaderValue};
|
use actix_http::http::header::{ContentType, Header, HeaderName, IntoHeaderValue};
|
||||||
use actix_http::http::{Error as HttpError, Method, StatusCode, Uri, Version};
|
use actix_http::http::{Error as HttpError, Method, StatusCode, Uri, Version};
|
||||||
use actix_http::test::TestRequest as HttpTestRequest;
|
use actix_http::test::TestRequest as HttpTestRequest;
|
||||||
use actix_http::{cookie::Cookie, Extensions, Request};
|
use actix_http::{cookie::Cookie, ws, Extensions, HttpService, Request};
|
||||||
use actix_router::{Path, ResourceDef, Url};
|
use actix_router::{Path, ResourceDef, Url};
|
||||||
|
use actix_rt::System;
|
||||||
|
use actix_server::Server;
|
||||||
use actix_service::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
use actix_service::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
||||||
|
use awc::error::PayloadError;
|
||||||
|
use awc::{Client, ClientRequest, ClientResponse, Connector};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures::future::ok;
|
use futures::future::ok;
|
||||||
use futures::stream::{Stream, StreamExt};
|
use futures::stream::{Stream, StreamExt};
|
||||||
|
use net2::TcpBuilder;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
@ -500,6 +508,405 @@ impl TestRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Start test server with default configuration
|
||||||
|
///
|
||||||
|
/// Test server is very simple server that simplify process of writing
|
||||||
|
/// integration tests cases for actix web applications.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use actix_web::{web, test, App, HttpResponse, Error};
|
||||||
|
///
|
||||||
|
/// async fn my_handler() -> Result<HttpResponse, Error> {
|
||||||
|
/// Ok(HttpResponse::Ok().into())
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[actix_rt::test]
|
||||||
|
/// async fn test_example() {
|
||||||
|
/// let mut srv = test::start(
|
||||||
|
/// || App::new().service(
|
||||||
|
/// web::resource("/").to(my_handler))
|
||||||
|
/// );
|
||||||
|
///
|
||||||
|
/// let req = srv.get("/");
|
||||||
|
/// let response = req.send().await.unwrap();
|
||||||
|
/// assert!(response.status().is_success());
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub fn start<F, I, S, B>(factory: F) -> TestServer
|
||||||
|
where
|
||||||
|
F: Fn() -> I + Send + Clone + 'static,
|
||||||
|
I: IntoServiceFactory<S>,
|
||||||
|
S: ServiceFactory<Config = (), Request = Request> + 'static,
|
||||||
|
S::Error: Into<Error> + 'static,
|
||||||
|
S::InitError: fmt::Debug,
|
||||||
|
S::Response: Into<HttpResponse<B>> + 'static,
|
||||||
|
<S::Service as Service>::Future: 'static,
|
||||||
|
B: MessageBody + 'static,
|
||||||
|
{
|
||||||
|
start_with(TestServerConfig::default(), factory)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start test server with custom configuration
|
||||||
|
///
|
||||||
|
/// Test server could be configured in different ways, for details check
|
||||||
|
/// `TestServerConfig` docs.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use actix_web::{web, test, App, HttpResponse, Error};
|
||||||
|
///
|
||||||
|
/// async fn my_handler() -> Result<HttpResponse, Error> {
|
||||||
|
/// Ok(HttpResponse::Ok().into())
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[actix_rt::test]
|
||||||
|
/// async fn test_example() {
|
||||||
|
/// let mut srv = test::start_with(test::config().h1(), ||
|
||||||
|
/// App::new().service(web::resource("/").to(my_handler))
|
||||||
|
/// );
|
||||||
|
///
|
||||||
|
/// let req = srv.get("/");
|
||||||
|
/// let response = req.send().await.unwrap();
|
||||||
|
/// assert!(response.status().is_success());
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub fn start_with<F, I, S, B>(cfg: TestServerConfig, factory: F) -> TestServer
|
||||||
|
where
|
||||||
|
F: Fn() -> I + Send + Clone + 'static,
|
||||||
|
I: IntoServiceFactory<S>,
|
||||||
|
S: ServiceFactory<Config = (), Request = Request> + 'static,
|
||||||
|
S::Error: Into<Error> + 'static,
|
||||||
|
S::InitError: fmt::Debug,
|
||||||
|
S::Response: Into<HttpResponse<B>> + 'static,
|
||||||
|
<S::Service as Service>::Future: 'static,
|
||||||
|
B: MessageBody + 'static,
|
||||||
|
{
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
|
let ssl = match cfg.stream {
|
||||||
|
StreamType::Tcp => false,
|
||||||
|
#[cfg(feature = "openssl")]
|
||||||
|
StreamType::Openssl(_) => true,
|
||||||
|
#[cfg(feature = "rustls")]
|
||||||
|
StreamType::Rustls(_) => true,
|
||||||
|
};
|
||||||
|
|
||||||
|
// run server in separate thread
|
||||||
|
thread::spawn(move || {
|
||||||
|
let sys = System::new("actix-test-server");
|
||||||
|
let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||||
|
let local_addr = tcp.local_addr().unwrap();
|
||||||
|
let factory = factory.clone();
|
||||||
|
let cfg = cfg.clone();
|
||||||
|
let ctimeout = cfg.client_timeout;
|
||||||
|
let builder = Server::build().workers(1).disable_signals();
|
||||||
|
|
||||||
|
match cfg.stream {
|
||||||
|
StreamType::Tcp => match cfg.tp {
|
||||||
|
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
||||||
|
HttpService::build()
|
||||||
|
.client_timeout(ctimeout)
|
||||||
|
.h1(factory())
|
||||||
|
.tcp()
|
||||||
|
}),
|
||||||
|
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
||||||
|
HttpService::build()
|
||||||
|
.client_timeout(ctimeout)
|
||||||
|
.h2(factory())
|
||||||
|
.tcp()
|
||||||
|
}),
|
||||||
|
HttpVer::Both => builder.listen("test", tcp, move || {
|
||||||
|
HttpService::build()
|
||||||
|
.client_timeout(ctimeout)
|
||||||
|
.finish(factory())
|
||||||
|
.tcp()
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
#[cfg(feature = "openssl")]
|
||||||
|
StreamType::Openssl(acceptor) => match cfg.tp {
|
||||||
|
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
||||||
|
HttpService::build()
|
||||||
|
.client_timeout(ctimeout)
|
||||||
|
.h1(factory())
|
||||||
|
.openssl(acceptor.clone())
|
||||||
|
}),
|
||||||
|
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
||||||
|
HttpService::build()
|
||||||
|
.client_timeout(ctimeout)
|
||||||
|
.h2(factory())
|
||||||
|
.openssl(acceptor.clone())
|
||||||
|
}),
|
||||||
|
HttpVer::Both => builder.listen("test", tcp, move || {
|
||||||
|
HttpService::build()
|
||||||
|
.client_timeout(ctimeout)
|
||||||
|
.finish(factory())
|
||||||
|
.openssl(acceptor.clone())
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
#[cfg(feature = "rustls")]
|
||||||
|
StreamType::Rustls(config) => match cfg.tp {
|
||||||
|
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
||||||
|
HttpService::build()
|
||||||
|
.client_timeout(ctimeout)
|
||||||
|
.h1(factory())
|
||||||
|
.rustls(config.clone())
|
||||||
|
}),
|
||||||
|
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
||||||
|
HttpService::build()
|
||||||
|
.client_timeout(ctimeout)
|
||||||
|
.h2(factory())
|
||||||
|
.rustls(config.clone())
|
||||||
|
}),
|
||||||
|
HttpVer::Both => builder.listen("test", tcp, move || {
|
||||||
|
HttpService::build()
|
||||||
|
.client_timeout(ctimeout)
|
||||||
|
.finish(factory())
|
||||||
|
.rustls(config.clone())
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.unwrap()
|
||||||
|
.start();
|
||||||
|
|
||||||
|
tx.send((System::current(), local_addr)).unwrap();
|
||||||
|
sys.run()
|
||||||
|
});
|
||||||
|
|
||||||
|
let (system, addr) = rx.recv().unwrap();
|
||||||
|
|
||||||
|
let client = {
|
||||||
|
let connector = {
|
||||||
|
#[cfg(feature = "openssl")]
|
||||||
|
{
|
||||||
|
use open_ssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
|
||||||
|
|
||||||
|
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
|
||||||
|
builder.set_verify(SslVerifyMode::NONE);
|
||||||
|
let _ = builder
|
||||||
|
.set_alpn_protos(b"\x02h2\x08http/1.1")
|
||||||
|
.map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
|
||||||
|
Connector::new()
|
||||||
|
.conn_lifetime(time::Duration::from_secs(0))
|
||||||
|
.timeout(time::Duration::from_millis(3000))
|
||||||
|
.ssl(builder.build())
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
#[cfg(not(feature = "openssl"))]
|
||||||
|
{
|
||||||
|
Connector::new()
|
||||||
|
.conn_lifetime(time::Duration::from_secs(0))
|
||||||
|
.timeout(time::Duration::from_millis(3000))
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Client::build().connector(connector).finish()
|
||||||
|
};
|
||||||
|
|
||||||
|
TestServer {
|
||||||
|
ssl,
|
||||||
|
addr,
|
||||||
|
client,
|
||||||
|
system,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct TestServerConfig {
|
||||||
|
tp: HttpVer,
|
||||||
|
stream: StreamType,
|
||||||
|
client_timeout: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
enum HttpVer {
|
||||||
|
Http1,
|
||||||
|
Http2,
|
||||||
|
Both,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
enum StreamType {
|
||||||
|
Tcp,
|
||||||
|
#[cfg(feature = "openssl")]
|
||||||
|
Openssl(open_ssl::ssl::SslAcceptor),
|
||||||
|
#[cfg(feature = "rustls")]
|
||||||
|
Rustls(rust_tls::ServerConfig),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for TestServerConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
TestServerConfig::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create default test server config
|
||||||
|
pub fn config() -> TestServerConfig {
|
||||||
|
TestServerConfig::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TestServerConfig {
|
||||||
|
/// Create default server configuration
|
||||||
|
pub(crate) fn new() -> TestServerConfig {
|
||||||
|
TestServerConfig {
|
||||||
|
tp: HttpVer::Both,
|
||||||
|
stream: StreamType::Tcp,
|
||||||
|
client_timeout: 5000,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start http/1.1 server only
|
||||||
|
pub fn h1(mut self) -> Self {
|
||||||
|
self.tp = HttpVer::Http1;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start http/2 server only
|
||||||
|
pub fn h2(mut self) -> Self {
|
||||||
|
self.tp = HttpVer::Http2;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start openssl server
|
||||||
|
#[cfg(feature = "openssl")]
|
||||||
|
pub fn openssl(mut self, acceptor: open_ssl::ssl::SslAcceptor) -> Self {
|
||||||
|
self.stream = StreamType::Openssl(acceptor);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start rustls server
|
||||||
|
#[cfg(feature = "rustls")]
|
||||||
|
pub fn rustls(mut self, config: rust_tls::ServerConfig) -> Self {
|
||||||
|
self.stream = StreamType::Rustls(config);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set server client timeout in milliseconds for first request.
|
||||||
|
pub fn client_timeout(mut self, val: u64) -> Self {
|
||||||
|
self.client_timeout = val;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get first available unused address
|
||||||
|
pub fn unused_addr() -> net::SocketAddr {
|
||||||
|
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
||||||
|
let socket = TcpBuilder::new_v4().unwrap();
|
||||||
|
socket.bind(&addr).unwrap();
|
||||||
|
socket.reuse_address(true).unwrap();
|
||||||
|
let tcp = socket.to_tcp_listener().unwrap();
|
||||||
|
tcp.local_addr().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test server controller
|
||||||
|
pub struct TestServer {
|
||||||
|
addr: net::SocketAddr,
|
||||||
|
client: awc::Client,
|
||||||
|
system: actix_rt::System,
|
||||||
|
ssl: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TestServer {
|
||||||
|
/// Construct test server url
|
||||||
|
pub fn addr(&self) -> net::SocketAddr {
|
||||||
|
self.addr
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Construct test server url
|
||||||
|
pub fn url(&self, uri: &str) -> String {
|
||||||
|
let scheme = if self.ssl { "https" } else { "http" };
|
||||||
|
|
||||||
|
if uri.starts_with('/') {
|
||||||
|
format!("{}://localhost:{}{}", scheme, self.addr.port(), uri)
|
||||||
|
} else {
|
||||||
|
format!("{}://localhost:{}/{}", scheme, self.addr.port(), uri)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create `GET` request
|
||||||
|
pub fn get<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
||||||
|
self.client.get(self.url(path.as_ref()).as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create `POST` request
|
||||||
|
pub fn post<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
||||||
|
self.client.post(self.url(path.as_ref()).as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create `HEAD` request
|
||||||
|
pub fn head<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
||||||
|
self.client.head(self.url(path.as_ref()).as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create `PUT` request
|
||||||
|
pub fn put<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
||||||
|
self.client.put(self.url(path.as_ref()).as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create `PATCH` request
|
||||||
|
pub fn patch<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
||||||
|
self.client.patch(self.url(path.as_ref()).as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create `DELETE` request
|
||||||
|
pub fn delete<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
||||||
|
self.client.delete(self.url(path.as_ref()).as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create `OPTIONS` request
|
||||||
|
pub fn options<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
||||||
|
self.client.options(self.url(path.as_ref()).as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Connect to test http server
|
||||||
|
pub fn request<S: AsRef<str>>(&self, method: Method, path: S) -> ClientRequest {
|
||||||
|
self.client.request(method, path.as_ref())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn load_body<S>(
|
||||||
|
&mut self,
|
||||||
|
mut response: ClientResponse<S>,
|
||||||
|
) -> Result<Bytes, PayloadError>
|
||||||
|
where
|
||||||
|
S: Stream<Item = Result<Bytes, PayloadError>> + Unpin + 'static,
|
||||||
|
{
|
||||||
|
response.body().limit(10_485_760).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Connect to websocket server at a given path
|
||||||
|
pub async fn ws_at(
|
||||||
|
&mut self,
|
||||||
|
path: &str,
|
||||||
|
) -> Result<Framed<impl AsyncRead + AsyncWrite, ws::Codec>, awc::error::WsClientError>
|
||||||
|
{
|
||||||
|
let url = self.url(path);
|
||||||
|
let connect = self.client.ws(url).connect();
|
||||||
|
connect.await.map(|(_, framed)| framed)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Connect to a websocket server
|
||||||
|
pub async fn ws(
|
||||||
|
&mut self,
|
||||||
|
) -> Result<Framed<impl AsyncRead + AsyncWrite, ws::Codec>, awc::error::WsClientError>
|
||||||
|
{
|
||||||
|
self.ws_at("/").await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stop http server
|
||||||
|
fn stop(&mut self) {
|
||||||
|
self.system.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for TestServer {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use actix_http::httpmessage::HttpMessage;
|
use actix_http::httpmessage::HttpMessage;
|
||||||
|
@ -4,9 +4,7 @@ use actix_http::http::header::{
|
|||||||
ContentEncoding, ACCEPT_ENCODING, CONTENT_ENCODING, CONTENT_LENGTH,
|
ContentEncoding, ACCEPT_ENCODING, CONTENT_ENCODING, CONTENT_LENGTH,
|
||||||
TRANSFER_ENCODING,
|
TRANSFER_ENCODING,
|
||||||
};
|
};
|
||||||
use actix_http::{Error, HttpService, Response};
|
use brotli::{CompressorWriter, DecompressorWriter};
|
||||||
use actix_http_test::TestServer;
|
|
||||||
use brotli::DecompressorWriter;
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use flate2::read::GzDecoder;
|
use flate2::read::GzDecoder;
|
||||||
use flate2::write::{GzEncoder, ZlibDecoder, ZlibEncoder};
|
use flate2::write::{GzEncoder, ZlibDecoder, ZlibEncoder};
|
||||||
@ -15,7 +13,7 @@ use futures::{future::ok, stream::once};
|
|||||||
use rand::{distributions::Alphanumeric, Rng};
|
use rand::{distributions::Alphanumeric, Rng};
|
||||||
|
|
||||||
use actix_web::middleware::{BodyEncoding, Compress};
|
use actix_web::middleware::{BodyEncoding, Compress};
|
||||||
use actix_web::{dev, http, web, App, HttpResponse, HttpServer};
|
use actix_web::{dev, http, test, web, App, Error, HttpResponse};
|
||||||
|
|
||||||
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
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 \
|
||||||
@ -41,11 +39,9 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_body() {
|
async fn test_body() {
|
||||||
let srv = TestServer::start(|| {
|
let srv = test::start(|| {
|
||||||
HttpService::build()
|
App::new()
|
||||||
.h1(App::new()
|
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
||||||
.service(web::resource("/").route(web::to(|| Response::Ok().body(STR)))))
|
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut response = srv.get("/").send().await.unwrap();
|
let mut response = srv.get("/").send().await.unwrap();
|
||||||
@ -58,12 +54,10 @@ async fn test_body() {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_body_gzip() {
|
async fn test_body_gzip() {
|
||||||
let srv = TestServer::start(|| {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new()
|
||||||
.h1(App::new()
|
|
||||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||||
.service(web::resource("/").route(web::to(|| Response::Ok().body(STR)))))
|
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut response = srv
|
let mut response = srv
|
||||||
@ -87,14 +81,12 @@ async fn test_body_gzip() {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_body_gzip2() {
|
async fn test_body_gzip2() {
|
||||||
let srv = TestServer::start(|| {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new()
|
||||||
.h1(App::new()
|
|
||||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||||
.service(web::resource("/").route(web::to(|| {
|
.service(web::resource("/").route(web::to(|| {
|
||||||
Response::Ok().body(STR).into_body::<dev::Body>()
|
HttpResponse::Ok().body(STR).into_body::<dev::Body>()
|
||||||
}))))
|
})))
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut response = srv
|
let mut response = srv
|
||||||
@ -118,23 +110,23 @@ async fn test_body_gzip2() {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_body_encoding_override() {
|
async fn test_body_encoding_override() {
|
||||||
let srv = TestServer::start(|| {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new()
|
||||||
.h1(App::new()
|
|
||||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||||
.service(web::resource("/").route(web::to(|| {
|
.service(web::resource("/").route(web::to(|| {
|
||||||
Response::Ok().encoding(ContentEncoding::Deflate).body(STR)
|
HttpResponse::Ok()
|
||||||
|
.encoding(ContentEncoding::Deflate)
|
||||||
|
.body(STR)
|
||||||
})))
|
})))
|
||||||
.service(web::resource("/raw").route(web::to(|| {
|
.service(web::resource("/raw").route(web::to(|| {
|
||||||
let body = actix_web::dev::Body::Bytes(STR.into());
|
let body = actix_web::dev::Body::Bytes(STR.into());
|
||||||
let mut response =
|
let mut response =
|
||||||
Response::with_body(actix_web::http::StatusCode::OK, body);
|
HttpResponse::with_body(actix_web::http::StatusCode::OK, body);
|
||||||
|
|
||||||
response.encoding(ContentEncoding::Deflate);
|
response.encoding(ContentEncoding::Deflate);
|
||||||
|
|
||||||
response
|
response
|
||||||
}))))
|
})))
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Builder
|
// Builder
|
||||||
@ -181,16 +173,14 @@ async fn test_body_gzip_large() {
|
|||||||
let data = STR.repeat(10);
|
let data = STR.repeat(10);
|
||||||
let srv_data = data.clone();
|
let srv_data = data.clone();
|
||||||
|
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), move || {
|
||||||
let data = srv_data.clone();
|
let data = srv_data.clone();
|
||||||
HttpService::build()
|
App::new()
|
||||||
.h1(App::new()
|
|
||||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||||
.service(
|
.service(
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.route(web::to(move || Response::Ok().body(data.clone()))),
|
.route(web::to(move || HttpResponse::Ok().body(data.clone()))),
|
||||||
))
|
)
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut response = srv
|
let mut response = srv
|
||||||
@ -220,16 +210,14 @@ async fn test_body_gzip_large_random() {
|
|||||||
.collect::<String>();
|
.collect::<String>();
|
||||||
let srv_data = data.clone();
|
let srv_data = data.clone();
|
||||||
|
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), move || {
|
||||||
let data = srv_data.clone();
|
let data = srv_data.clone();
|
||||||
HttpService::build()
|
App::new()
|
||||||
.h1(App::new()
|
|
||||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||||
.service(
|
.service(
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.route(web::to(move || Response::Ok().body(data.clone()))),
|
.route(web::to(move || HttpResponse::Ok().body(data.clone()))),
|
||||||
))
|
)
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut response = srv
|
let mut response = srv
|
||||||
@ -254,16 +242,13 @@ async fn test_body_gzip_large_random() {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_body_chunked_implicit() {
|
async fn test_body_chunked_implicit() {
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new()
|
||||||
.h1(App::new()
|
|
||||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||||
.service(web::resource("/").route(web::get().to(move || {
|
.service(web::resource("/").route(web::get().to(move || {
|
||||||
Response::Ok().streaming(once(ok::<_, Error>(Bytes::from_static(
|
HttpResponse::Ok()
|
||||||
STR.as_ref(),
|
.streaming(once(ok::<_, Error>(Bytes::from_static(STR.as_ref()))))
|
||||||
))))
|
})))
|
||||||
}))))
|
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut response = srv
|
let mut response = srv
|
||||||
@ -291,16 +276,13 @@ async fn test_body_chunked_implicit() {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_body_br_streaming() {
|
async fn test_body_br_streaming() {
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new().wrap(Compress::new(ContentEncoding::Br)).service(
|
||||||
.h1(App::new().wrap(Compress::new(ContentEncoding::Br)).service(
|
|
||||||
web::resource("/").route(web::to(move || {
|
web::resource("/").route(web::to(move || {
|
||||||
Response::Ok().streaming(once(ok::<_, Error>(Bytes::from_static(
|
HttpResponse::Ok()
|
||||||
STR.as_ref(),
|
.streaming(once(ok::<_, Error>(Bytes::from_static(STR.as_ref()))))
|
||||||
))))
|
|
||||||
})),
|
})),
|
||||||
))
|
)
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut response = srv
|
let mut response = srv
|
||||||
@ -324,12 +306,10 @@ async fn test_body_br_streaming() {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_head_binary() {
|
async fn test_head_binary() {
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new().service(web::resource("/").route(
|
||||||
.h1(App::new().service(web::resource("/").route(
|
web::head().to(move || HttpResponse::Ok().content_length(100).body(STR)),
|
||||||
web::head().to(move || Response::Ok().content_length(100).body(STR)),
|
))
|
||||||
)))
|
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut response = srv.head("/").send().await.unwrap();
|
let mut response = srv.head("/").send().await.unwrap();
|
||||||
@ -347,19 +327,13 @@ async fn test_head_binary() {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_no_chunking() {
|
async fn test_no_chunking() {
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
|
||||||
.h1(
|
|
||||||
App::new().service(web::resource("/").route(web::to(move || {
|
App::new().service(web::resource("/").route(web::to(move || {
|
||||||
Response::Ok()
|
HttpResponse::Ok()
|
||||||
.no_chunking()
|
.no_chunking()
|
||||||
.content_length(STR.len() as u64)
|
.content_length(STR.len() as u64)
|
||||||
.streaming(once(ok::<_, Error>(Bytes::from_static(
|
.streaming(once(ok::<_, Error>(Bytes::from_static(STR.as_ref()))))
|
||||||
STR.as_ref(),
|
})))
|
||||||
))))
|
|
||||||
}))),
|
|
||||||
)
|
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut response = srv.get("/").send().await.unwrap();
|
let mut response = srv.get("/").send().await.unwrap();
|
||||||
@ -373,14 +347,12 @@ async fn test_no_chunking() {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_body_deflate() {
|
async fn test_body_deflate() {
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new()
|
||||||
.h1(App::new()
|
|
||||||
.wrap(Compress::new(ContentEncoding::Deflate))
|
.wrap(Compress::new(ContentEncoding::Deflate))
|
||||||
.service(
|
.service(
|
||||||
web::resource("/").route(web::to(move || Response::Ok().body(STR))),
|
web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))),
|
||||||
))
|
)
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// client request
|
// client request
|
||||||
@ -404,12 +376,10 @@ async fn test_body_deflate() {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_body_brotli() {
|
async fn test_body_brotli() {
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new().wrap(Compress::new(ContentEncoding::Br)).service(
|
||||||
.h1(App::new().wrap(Compress::new(ContentEncoding::Br)).service(
|
web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))),
|
||||||
web::resource("/").route(web::to(move || Response::Ok().body(STR))),
|
)
|
||||||
))
|
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// client request
|
// client request
|
||||||
@ -434,13 +404,11 @@ async fn test_body_brotli() {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_encoding() {
|
async fn test_encoding() {
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new().wrap(Compress::default()).service(
|
||||||
.h1(App::new().wrap(Compress::default()).service(
|
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
|
.route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||||
))
|
)
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// client request
|
// client request
|
||||||
@ -462,13 +430,11 @@ async fn test_encoding() {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_gzip_encoding() {
|
async fn test_gzip_encoding() {
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new().service(
|
||||||
.h1(App::new().service(
|
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
|
.route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||||
))
|
)
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// client request
|
// client request
|
||||||
@ -491,13 +457,11 @@ async fn test_gzip_encoding() {
|
|||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_gzip_encoding_large() {
|
async fn test_gzip_encoding_large() {
|
||||||
let data = STR.repeat(10);
|
let data = STR.repeat(10);
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new().service(
|
||||||
.h1(App::new().service(
|
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
|
.route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||||
))
|
)
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// client request
|
// client request
|
||||||
@ -524,13 +488,11 @@ async fn test_reading_gzip_encoding_large_random() {
|
|||||||
.take(60_000)
|
.take(60_000)
|
||||||
.collect::<String>();
|
.collect::<String>();
|
||||||
|
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new().service(
|
||||||
.h1(App::new().service(
|
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
|
.route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||||
))
|
)
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// client request
|
// client request
|
||||||
@ -553,13 +515,11 @@ async fn test_reading_gzip_encoding_large_random() {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_reading_deflate_encoding() {
|
async fn test_reading_deflate_encoding() {
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new().service(
|
||||||
.h1(App::new().service(
|
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
|
.route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||||
))
|
)
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||||
@ -582,13 +542,11 @@ async fn test_reading_deflate_encoding() {
|
|||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_reading_deflate_encoding_large() {
|
async fn test_reading_deflate_encoding_large() {
|
||||||
let data = STR.repeat(10);
|
let data = STR.repeat(10);
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new().service(
|
||||||
.h1(App::new().service(
|
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
|
.route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||||
))
|
)
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||||
@ -615,13 +573,11 @@ async fn test_reading_deflate_encoding_large_random() {
|
|||||||
.take(160_000)
|
.take(160_000)
|
||||||
.collect::<String>();
|
.collect::<String>();
|
||||||
|
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new().service(
|
||||||
.h1(App::new().service(
|
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
|
.route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||||
))
|
)
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||||
@ -643,20 +599,17 @@ async fn test_reading_deflate_encoding_large_random() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
#[cfg(feature = "brotli")]
|
|
||||||
async fn test_brotli_encoding() {
|
async fn test_brotli_encoding() {
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new().service(
|
||||||
.h1(App::new().service(
|
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
|
.route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||||
))
|
)
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut e = BrotliEncoder::new(Vec::new(), 5);
|
let mut e = CompressorWriter::new(Vec::new(), 0, 3, 0);
|
||||||
e.write_all(STR.as_ref()).unwrap();
|
e.write_all(STR.as_ref()).unwrap();
|
||||||
let enc = e.finish().unwrap();
|
let enc = e.into_inner();
|
||||||
|
|
||||||
// client request
|
// client request
|
||||||
let request = srv
|
let request = srv
|
||||||
@ -671,22 +624,19 @@ async fn test_brotli_encoding() {
|
|||||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "brotli")]
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_brotli_encoding_large() {
|
async fn test_brotli_encoding_large() {
|
||||||
let data = STR.repeat(10);
|
let data = STR.repeat(10);
|
||||||
let srv = TestServer::start(move || {
|
let srv = test::start_with(test::config().h1(), || {
|
||||||
HttpService::build()
|
App::new().service(
|
||||||
.h1(App::new().service(
|
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
|
.route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||||
))
|
)
|
||||||
.tcp()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut e = BrotliEncoder::new(Vec::new(), 5);
|
let mut e = CompressorWriter::new(Vec::new(), 0, 3, 0);
|
||||||
e.write_all(data.as_ref()).unwrap();
|
e.write_all(data.as_ref()).unwrap();
|
||||||
let enc = e.finish().unwrap();
|
let enc = e.into_inner();
|
||||||
|
|
||||||
// client request
|
// client request
|
||||||
let request = srv
|
let request = srv
|
||||||
@ -701,83 +651,60 @@ async fn test_brotli_encoding_large() {
|
|||||||
assert_eq!(bytes, Bytes::from(data));
|
assert_eq!(bytes, Bytes::from(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[cfg(feature = "ssl")]
|
#[cfg(feature = "openssl")]
|
||||||
// #[actix_rt::test]
|
#[actix_rt::test]
|
||||||
// async fn test_brotli_encoding_large_ssl() {
|
async fn test_brotli_encoding_large_openssl() {
|
||||||
// use actix::{Actor, System};
|
// load ssl keys
|
||||||
// use openssl::ssl::{
|
use open_ssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
||||||
// SslAcceptor, SslConnector, SslFiletype, SslMethod, SslVerifyMode,
|
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||||
// };
|
builder
|
||||||
// // load ssl keys
|
.set_private_key_file("tests/key.pem", SslFiletype::PEM)
|
||||||
// let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
.unwrap();
|
||||||
// builder
|
builder
|
||||||
// .set_private_key_file("tests/key.pem", SslFiletype::PEM)
|
.set_certificate_chain_file("tests/cert.pem")
|
||||||
// .unwrap();
|
.unwrap();
|
||||||
// builder
|
|
||||||
// .set_certificate_chain_file("tests/cert.pem")
|
|
||||||
// .unwrap();
|
|
||||||
|
|
||||||
// let data = STR.repeat(10);
|
let data = STR.repeat(10);
|
||||||
// let srv = test::TestServer::build().ssl(builder).start(|app| {
|
let srv = test::start_with(test::config().openssl(builder.build()), move || {
|
||||||
// app.handler(|req: &HttpRequest| {
|
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
|
||||||
// req.body()
|
HttpResponse::Ok()
|
||||||
// .and_then(|bytes: Bytes| {
|
.encoding(http::ContentEncoding::Identity)
|
||||||
// Ok(HttpResponse::Ok()
|
.body(bytes)
|
||||||
// .content_encoding(http::ContentEncoding::Identity)
|
})))
|
||||||
// .body(bytes))
|
});
|
||||||
// })
|
|
||||||
// .responder()
|
|
||||||
// })
|
|
||||||
// });
|
|
||||||
// let mut rt = System::new("test");
|
|
||||||
|
|
||||||
// // client connector
|
// body
|
||||||
// let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
|
let mut e = CompressorWriter::new(Vec::new(), 0, 3, 0);
|
||||||
// builder.set_verify(SslVerifyMode::NONE);
|
e.write_all(data.as_ref()).unwrap();
|
||||||
// let conn = client::ClientConnector::with_connector(builder.build()).start();
|
let enc = e.into_inner();
|
||||||
|
|
||||||
// // body
|
// client request
|
||||||
// let mut e = BrotliEncoder::new(Vec::new(), 5);
|
let mut response = srv
|
||||||
// e.write_all(data.as_ref()).unwrap();
|
.post("/")
|
||||||
// let enc = e.finish().unwrap();
|
.header(http::header::CONTENT_ENCODING, "br")
|
||||||
|
.send_body(enc)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
// // client request
|
// read response
|
||||||
// let request = client::ClientRequest::build()
|
let bytes = response.body().await.unwrap();
|
||||||
// .uri(srv.url("/"))
|
assert_eq!(bytes, Bytes::from(data));
|
||||||
// .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));
|
|
||||||
// }
|
|
||||||
|
|
||||||
#[cfg(all(feature = "rustls", feature = "openssl"))]
|
#[cfg(all(feature = "rustls", feature = "openssl"))]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_reading_deflate_encoding_large_random_ssl() {
|
async fn test_reading_deflate_encoding_large_random_rustls() {
|
||||||
use open_ssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
|
|
||||||
use rust_tls::internal::pemfile::{certs, pkcs8_private_keys};
|
use rust_tls::internal::pemfile::{certs, pkcs8_private_keys};
|
||||||
use rust_tls::{NoClientAuth, ServerConfig};
|
use rust_tls::{NoClientAuth, ServerConfig};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
use std::sync::mpsc;
|
|
||||||
|
|
||||||
let addr = TestServer::unused_addr();
|
|
||||||
let (tx, rx) = mpsc::channel();
|
|
||||||
|
|
||||||
let data = rand::thread_rng()
|
let data = rand::thread_rng()
|
||||||
.sample_iter(&Alphanumeric)
|
.sample_iter(&Alphanumeric)
|
||||||
.take(160_000)
|
.take(160_000)
|
||||||
.collect::<String>();
|
.collect::<String>();
|
||||||
|
|
||||||
std::thread::spawn(move || {
|
|
||||||
let sys = actix_rt::System::new("test");
|
|
||||||
|
|
||||||
// load ssl keys
|
// load ssl keys
|
||||||
let mut config = ServerConfig::new(NoClientAuth::new());
|
let mut config = ServerConfig::new(NoClientAuth::new());
|
||||||
let cert_file = &mut BufReader::new(File::open("tests/cert.pem").unwrap());
|
let cert_file = &mut BufReader::new(File::open("tests/cert.pem").unwrap());
|
||||||
@ -786,39 +713,13 @@ async fn test_reading_deflate_encoding_large_random_ssl() {
|
|||||||
let mut keys = pkcs8_private_keys(key_file).unwrap();
|
let mut keys = pkcs8_private_keys(key_file).unwrap();
|
||||||
config.set_single_cert(cert_chain, keys.remove(0)).unwrap();
|
config.set_single_cert(cert_chain, keys.remove(0)).unwrap();
|
||||||
|
|
||||||
let srv = HttpServer::new(|| {
|
let srv = test::start_with(test::config().rustls(config), || {
|
||||||
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
|
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
|
||||||
async move {
|
|
||||||
Ok::<_, Error>(
|
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.encoding(http::ContentEncoding::Identity)
|
.encoding(http::ContentEncoding::Identity)
|
||||||
.body(bytes),
|
.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 = {
|
|
||||||
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())
|
|
||||||
.finish(),
|
|
||||||
)
|
|
||||||
.finish()
|
|
||||||
};
|
|
||||||
|
|
||||||
// encode data
|
// encode data
|
||||||
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||||
@ -826,8 +727,8 @@ async fn test_reading_deflate_encoding_large_random_ssl() {
|
|||||||
let enc = e.finish().unwrap();
|
let enc = e.finish().unwrap();
|
||||||
|
|
||||||
// client request
|
// client request
|
||||||
let req = client
|
let req = srv
|
||||||
.post(format!("https://localhost:{}/", addr.port()))
|
.post("/")
|
||||||
.header(http::header::CONTENT_ENCODING, "deflate")
|
.header(http::header::CONTENT_ENCODING, "deflate")
|
||||||
.send_body(enc);
|
.send_body(enc);
|
||||||
|
|
||||||
@ -838,14 +739,11 @@ async fn test_reading_deflate_encoding_large_random_ssl() {
|
|||||||
let bytes = response.body().await.unwrap();
|
let bytes = response.body().await.unwrap();
|
||||||
assert_eq!(bytes.len(), data.len());
|
assert_eq!(bytes.len(), data.len());
|
||||||
assert_eq!(bytes, Bytes::from(data));
|
assert_eq!(bytes, Bytes::from(data));
|
||||||
|
|
||||||
// stop
|
|
||||||
let _ = srv.stop(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[cfg(all(feature = "tls", feature = "ssl"))]
|
// #[cfg(all(feature = "tls", feature = "ssl"))]
|
||||||
// #[test]
|
// #[test]
|
||||||
// fn test_reading_deflate_encoding_large_random_tls() {
|
// fn test_reading_deflate_encoding_large_random_nativetls() {
|
||||||
// use native_tls::{Identity, TlsAcceptor};
|
// use native_tls::{Identity, TlsAcceptor};
|
||||||
// use openssl::ssl::{
|
// use openssl::ssl::{
|
||||||
// SslAcceptor, SslConnector, SslFiletype, SslMethod, SslVerifyMode,
|
// SslAcceptor, SslConnector, SslFiletype, SslMethod, SslVerifyMode,
|
||||||
@ -986,55 +884,31 @@ async fn test_reading_deflate_encoding_large_random_ssl() {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// #[test]
|
#[actix_rt::test]
|
||||||
// fn test_slow_request() {
|
async fn test_slow_request() {
|
||||||
// use actix::System;
|
use std::net;
|
||||||
|
|
||||||
|
let srv = test::start_with(test::config().client_timeout(200), || {
|
||||||
|
App::new().service(web::resource("/").route(web::to(|| HttpResponse::Ok())))
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut stream = net::TcpStream::connect(srv.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(srv.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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[cfg(feature = "openssl")]
|
||||||
|
// #[actix_rt::test]
|
||||||
|
// async fn test_ssl_handshake_timeout() {
|
||||||
|
// use open_ssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
||||||
// use std::net;
|
// 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
|
// // load ssl keys
|
||||||
// let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
// let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||||
@ -1045,28 +919,12 @@ async fn test_reading_deflate_encoding_large_random_ssl() {
|
|||||||
// .set_certificate_chain_file("tests/cert.pem")
|
// .set_certificate_chain_file("tests/cert.pem")
|
||||||
// .unwrap();
|
// .unwrap();
|
||||||
|
|
||||||
// thread::spawn(move || {
|
// let srv = test::start_with(test::config().openssl(builder.build()), || {
|
||||||
// System::run(move || {
|
// App::new().service(web::resource("/").route(web::to(|| HttpResponse::Ok())))
|
||||||
// let srv = server::new(|| {
|
|
||||||
// App::new().resource("/", |r| {
|
|
||||||
// r.method(http::Method::GET).f(|_| HttpResponse::Ok())
|
|
||||||
// })
|
|
||||||
// });
|
// });
|
||||||
|
|
||||||
// srv.bind_ssl(addr, builder)
|
// let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
||||||
// .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 mut data = String::new();
|
||||||
// let _ = stream.read_to_string(&mut data);
|
// let _ = stream.read_to_string(&mut data);
|
||||||
// assert!(data.is_empty());
|
// assert!(data.is_empty());
|
||||||
|
|
||||||
// let _ = sys.stop();
|
|
||||||
// }
|
// }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user