2021-04-02 07:26:59 +00:00
|
|
|
//! Integration testing tools for Actix Web applications.
|
|
|
|
//!
|
|
|
|
//! The main integration testing tool is [`TestServer`]. It spawns a real HTTP server on an
|
|
|
|
//! unused port and provides methods that use a real HTTP client. Therefore, it is much closer to
|
|
|
|
//! real-world cases than using `init_service`, which skips HTTP encoding and decoding.
|
|
|
|
//!
|
|
|
|
//! # Examples
|
|
|
|
//! ```
|
|
|
|
//! use actix_web::{get, web, test, App, HttpResponse, Error, Responder};
|
|
|
|
//!
|
|
|
|
//! #[get("/")]
|
|
|
|
//! async fn my_handler() -> Result<impl Responder, Error> {
|
|
|
|
//! Ok(HttpResponse::Ok())
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! #[actix_rt::test]
|
|
|
|
//! async fn test_example() {
|
|
|
|
//! let srv = actix_test::start(||
|
|
|
|
//! App::new().service(my_handler)
|
|
|
|
//! );
|
|
|
|
//!
|
|
|
|
//! let req = srv.get("/");
|
|
|
|
//! let res = req.send().await.unwrap();
|
|
|
|
//!
|
|
|
|
//! assert!(res.status().is_success());
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
|
2021-12-08 06:09:56 +00:00
|
|
|
#![deny(rust_2018_idioms, nonstandard_style)]
|
|
|
|
#![warn(future_incompatible)]
|
2023-02-26 21:55:25 +00:00
|
|
|
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
|
|
|
|
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
|
|
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
2021-12-08 06:09:56 +00:00
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
#[cfg(feature = "openssl")]
|
|
|
|
extern crate tls_openssl as openssl;
|
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
use std::{fmt, net, thread, time::Duration};
|
2021-04-02 07:26:59 +00:00
|
|
|
|
|
|
|
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
2021-12-15 01:44:51 +00:00
|
|
|
pub use actix_http::{body::to_bytes, test::TestBuffer};
|
2021-12-05 14:37:20 +00:00
|
|
|
use actix_http::{header::HeaderMap, ws, HttpService, Method, Request, Response};
|
2021-12-15 01:44:51 +00:00
|
|
|
pub use actix_http_test::unused_addr;
|
2021-06-17 16:57:58 +00:00
|
|
|
use actix_service::{map_config, IntoServiceFactory, ServiceFactory, ServiceFactoryExt as _};
|
2021-12-15 01:44:51 +00:00
|
|
|
pub use actix_web::test::{
|
2023-07-17 01:38:12 +00:00
|
|
|
call_and_read_body, call_and_read_body_json, call_service, init_service, ok_service, read_body,
|
|
|
|
read_body_json, status_service, TestRequest,
|
2021-12-15 01:44:51 +00:00
|
|
|
};
|
2021-04-02 07:26:59 +00:00
|
|
|
use actix_web::{
|
2021-12-04 19:40:47 +00:00
|
|
|
body::MessageBody,
|
|
|
|
dev::{AppConfig, Server, ServerHandle, Service},
|
2021-11-15 04:03:33 +00:00
|
|
|
rt::{self, System},
|
|
|
|
web, Error,
|
2021-04-02 07:26:59 +00:00
|
|
|
};
|
2024-05-18 18:57:35 +00:00
|
|
|
pub use awc::{error::PayloadError, Client, ClientRequest, ClientResponse, Connector};
|
2021-04-02 07:26:59 +00:00
|
|
|
use futures_core::Stream;
|
2021-11-15 04:03:33 +00:00
|
|
|
use tokio::sync::mpsc;
|
2021-04-02 07:26:59 +00:00
|
|
|
|
|
|
|
/// Start default [`TestServer`].
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// use actix_web::{get, web, test, App, HttpResponse, Error, Responder};
|
|
|
|
///
|
|
|
|
/// #[get("/")]
|
|
|
|
/// async fn my_handler() -> Result<impl Responder, Error> {
|
|
|
|
/// Ok(HttpResponse::Ok())
|
|
|
|
/// }
|
|
|
|
///
|
2021-10-19 16:30:32 +00:00
|
|
|
/// #[actix_web::test]
|
2021-04-02 07:26:59 +00:00
|
|
|
/// async fn test_example() {
|
|
|
|
/// let srv = actix_test::start(||
|
|
|
|
/// App::new().service(my_handler)
|
|
|
|
/// );
|
|
|
|
///
|
|
|
|
/// let req = srv.get("/");
|
|
|
|
/// let res = req.send().await.unwrap();
|
|
|
|
///
|
|
|
|
/// assert!(res.status().is_success());
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn start<F, I, S, B>(factory: F) -> TestServer
|
|
|
|
where
|
|
|
|
F: Fn() -> I + Send + Clone + 'static,
|
|
|
|
I: IntoServiceFactory<S, Request>,
|
|
|
|
S: ServiceFactory<Request, Config = AppConfig> + 'static,
|
|
|
|
S::Error: Into<Error> + 'static,
|
|
|
|
S::InitError: fmt::Debug,
|
2021-04-09 17:07:10 +00:00
|
|
|
S::Response: Into<Response<B>> + 'static,
|
2021-04-02 07:26:59 +00:00
|
|
|
<S::Service as Service<Request>>::Future: 'static,
|
|
|
|
B: MessageBody + 'static,
|
|
|
|
{
|
|
|
|
start_with(TestServerConfig::default(), factory)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start test server with custom configuration
|
|
|
|
///
|
|
|
|
/// Check [`TestServerConfig`] docs for configuration options.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// use actix_web::{get, web, test, App, HttpResponse, Error, Responder};
|
|
|
|
///
|
|
|
|
/// #[get("/")]
|
|
|
|
/// async fn my_handler() -> Result<impl Responder, Error> {
|
|
|
|
/// Ok(HttpResponse::Ok())
|
|
|
|
/// }
|
|
|
|
///
|
2021-10-19 16:30:32 +00:00
|
|
|
/// #[actix_web::test]
|
2021-04-02 07:26:59 +00:00
|
|
|
/// async fn test_example() {
|
|
|
|
/// let srv = actix_test::start_with(actix_test::config().h1(), ||
|
|
|
|
/// App::new().service(my_handler)
|
|
|
|
/// );
|
|
|
|
///
|
|
|
|
/// let req = srv.get("/");
|
|
|
|
/// let res = req.send().await.unwrap();
|
|
|
|
///
|
|
|
|
/// assert!(res.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, Request>,
|
|
|
|
S: ServiceFactory<Request, Config = AppConfig> + 'static,
|
|
|
|
S::Error: Into<Error> + 'static,
|
|
|
|
S::InitError: fmt::Debug,
|
2021-04-09 17:07:10 +00:00
|
|
|
S::Response: Into<Response<B>> + 'static,
|
2021-04-02 07:26:59 +00:00
|
|
|
<S::Service as Service<Request>>::Future: 'static,
|
|
|
|
B: MessageBody + 'static,
|
|
|
|
{
|
2021-11-15 04:03:33 +00:00
|
|
|
// for sending handles and server info back from the spawned thread
|
|
|
|
let (started_tx, started_rx) = std::sync::mpsc::channel();
|
|
|
|
|
|
|
|
// for signaling the shutdown of spawned server and system
|
|
|
|
let (thread_stop_tx, thread_stop_rx) = mpsc::channel(1);
|
2021-04-02 07:26:59 +00:00
|
|
|
|
|
|
|
let tls = match cfg.stream {
|
|
|
|
StreamType::Tcp => false,
|
|
|
|
#[cfg(feature = "openssl")]
|
|
|
|
StreamType::Openssl(_) => true,
|
2023-08-29 00:11:11 +00:00
|
|
|
#[cfg(feature = "rustls-0_20")]
|
|
|
|
StreamType::Rustls020(_) => true,
|
|
|
|
#[cfg(feature = "rustls-0_21")]
|
|
|
|
StreamType::Rustls021(_) => true,
|
2024-02-03 23:55:01 +00:00
|
|
|
#[cfg(feature = "rustls-0_22")]
|
|
|
|
StreamType::Rustls022(_) => true,
|
2024-05-18 19:05:58 +00:00
|
|
|
#[cfg(feature = "rustls-0_23")]
|
|
|
|
StreamType::Rustls023(_) => true,
|
2021-04-02 07:26:59 +00:00
|
|
|
};
|
|
|
|
|
2021-11-15 04:03:33 +00:00
|
|
|
// run server in separate orphaned thread
|
2021-04-02 07:26:59 +00:00
|
|
|
thread::spawn(move || {
|
2021-11-22 01:19:09 +00:00
|
|
|
rt::System::new().block_on(async move {
|
2023-02-26 05:25:36 +00:00
|
|
|
let tcp = net::TcpListener::bind(("127.0.0.1", cfg.port)).unwrap();
|
2021-11-22 01:19:09 +00:00
|
|
|
let local_addr = tcp.local_addr().unwrap();
|
|
|
|
let factory = factory.clone();
|
|
|
|
let srv_cfg = cfg.clone();
|
2022-01-31 17:30:34 +00:00
|
|
|
let timeout = cfg.client_request_timeout;
|
2021-11-22 01:19:09 +00:00
|
|
|
|
2023-07-19 16:48:43 +00:00
|
|
|
let builder = Server::build()
|
|
|
|
.workers(cfg.workers)
|
|
|
|
.disable_signals()
|
|
|
|
.system_exit();
|
2021-11-22 01:19:09 +00:00
|
|
|
|
|
|
|
let srv = match srv_cfg.stream {
|
|
|
|
StreamType::Tcp => match srv_cfg.tp {
|
|
|
|
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
2023-07-17 01:38:12 +00:00
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
2021-11-22 01:19:09 +00:00
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
2022-01-31 17:30:34 +00:00
|
|
|
.client_request_timeout(timeout)
|
2021-11-22 01:19:09 +00:00
|
|
|
.h1(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.tcp()
|
|
|
|
}),
|
|
|
|
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
2023-07-17 01:38:12 +00:00
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
2021-11-22 01:19:09 +00:00
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
2022-01-31 17:30:34 +00:00
|
|
|
.client_request_timeout(timeout)
|
2021-11-22 01:19:09 +00:00
|
|
|
.h2(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.tcp()
|
|
|
|
}),
|
|
|
|
HttpVer::Both => builder.listen("test", tcp, move || {
|
2023-07-17 01:38:12 +00:00
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
2021-11-22 01:19:09 +00:00
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
2022-01-31 17:30:34 +00:00
|
|
|
.client_request_timeout(timeout)
|
2021-11-22 01:19:09 +00:00
|
|
|
.finish(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.tcp()
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
#[cfg(feature = "openssl")]
|
|
|
|
StreamType::Openssl(acceptor) => match cfg.tp {
|
|
|
|
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
2023-07-17 01:38:12 +00:00
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
2021-11-22 01:19:09 +00:00
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
2022-01-31 17:30:34 +00:00
|
|
|
.client_request_timeout(timeout)
|
2021-11-22 01:19:09 +00:00
|
|
|
.h1(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.openssl(acceptor.clone())
|
|
|
|
}),
|
|
|
|
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
2023-07-17 01:38:12 +00:00
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
2021-11-22 01:19:09 +00:00
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
2022-01-31 17:30:34 +00:00
|
|
|
.client_request_timeout(timeout)
|
2021-11-22 01:19:09 +00:00
|
|
|
.h2(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.openssl(acceptor.clone())
|
|
|
|
}),
|
|
|
|
HttpVer::Both => builder.listen("test", tcp, move || {
|
2023-07-17 01:38:12 +00:00
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
2021-11-22 01:19:09 +00:00
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
2022-01-31 17:30:34 +00:00
|
|
|
.client_request_timeout(timeout)
|
2021-11-22 01:19:09 +00:00
|
|
|
.finish(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.openssl(acceptor.clone())
|
|
|
|
}),
|
|
|
|
},
|
2023-08-29 00:11:11 +00:00
|
|
|
#[cfg(feature = "rustls-0_20")]
|
|
|
|
StreamType::Rustls020(config) => match cfg.tp {
|
2021-11-22 01:19:09 +00:00
|
|
|
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
2023-07-17 01:38:12 +00:00
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
2021-11-22 01:19:09 +00:00
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
2022-01-31 17:30:34 +00:00
|
|
|
.client_request_timeout(timeout)
|
2021-11-22 01:19:09 +00:00
|
|
|
.h1(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.rustls(config.clone())
|
|
|
|
}),
|
|
|
|
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
2023-07-17 01:38:12 +00:00
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
2021-11-22 01:19:09 +00:00
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
2022-01-31 17:30:34 +00:00
|
|
|
.client_request_timeout(timeout)
|
2021-11-22 01:19:09 +00:00
|
|
|
.h2(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.rustls(config.clone())
|
|
|
|
}),
|
|
|
|
HttpVer::Both => builder.listen("test", tcp, move || {
|
2023-07-17 01:38:12 +00:00
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
2021-11-22 01:19:09 +00:00
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
2022-01-31 17:30:34 +00:00
|
|
|
.client_request_timeout(timeout)
|
2021-11-22 01:19:09 +00:00
|
|
|
.finish(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.rustls(config.clone())
|
|
|
|
}),
|
|
|
|
},
|
2023-08-29 00:11:11 +00:00
|
|
|
#[cfg(feature = "rustls-0_21")]
|
|
|
|
StreamType::Rustls021(config) => match cfg.tp {
|
|
|
|
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
|
|
|
.client_request_timeout(timeout)
|
|
|
|
.h1(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.rustls_021(config.clone())
|
|
|
|
}),
|
|
|
|
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
|
|
|
.client_request_timeout(timeout)
|
|
|
|
.h2(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.rustls_021(config.clone())
|
|
|
|
}),
|
|
|
|
HttpVer::Both => builder.listen("test", tcp, move || {
|
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
|
|
|
.client_request_timeout(timeout)
|
|
|
|
.finish(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.rustls_021(config.clone())
|
|
|
|
}),
|
|
|
|
},
|
2024-02-03 23:55:01 +00:00
|
|
|
#[cfg(feature = "rustls-0_22")]
|
|
|
|
StreamType::Rustls022(config) => match cfg.tp {
|
|
|
|
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
|
|
|
.client_request_timeout(timeout)
|
|
|
|
.h1(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.rustls_0_22(config.clone())
|
|
|
|
}),
|
|
|
|
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
|
|
|
.client_request_timeout(timeout)
|
|
|
|
.h2(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.rustls_0_22(config.clone())
|
|
|
|
}),
|
|
|
|
HttpVer::Both => builder.listen("test", tcp, move || {
|
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
|
|
|
.client_request_timeout(timeout)
|
|
|
|
.finish(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.rustls_0_22(config.clone())
|
|
|
|
}),
|
|
|
|
},
|
2024-05-18 19:05:58 +00:00
|
|
|
#[cfg(feature = "rustls-0_23")]
|
|
|
|
StreamType::Rustls023(config) => match cfg.tp {
|
|
|
|
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
|
|
|
.client_request_timeout(timeout)
|
|
|
|
.h1(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.rustls_0_23(config.clone())
|
|
|
|
}),
|
|
|
|
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
|
|
|
.client_request_timeout(timeout)
|
|
|
|
.h2(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.rustls_0_23(config.clone())
|
|
|
|
}),
|
|
|
|
HttpVer::Both => builder.listen("test", tcp, move || {
|
|
|
|
let app_cfg =
|
|
|
|
AppConfig::__priv_test_new(false, local_addr.to_string(), local_addr);
|
|
|
|
|
|
|
|
let fac = factory()
|
|
|
|
.into_factory()
|
|
|
|
.map_err(|err| err.into().error_response());
|
|
|
|
|
|
|
|
HttpService::build()
|
|
|
|
.client_request_timeout(timeout)
|
|
|
|
.finish(map_config(fac, move |_| app_cfg.clone()))
|
|
|
|
.rustls_0_23(config.clone())
|
|
|
|
}),
|
|
|
|
},
|
2021-11-22 01:19:09 +00:00
|
|
|
}
|
|
|
|
.expect("test server could not be created");
|
|
|
|
|
|
|
|
let srv = srv.run();
|
|
|
|
started_tx
|
|
|
|
.send((System::current(), srv.handle(), local_addr))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// drive server loop
|
|
|
|
srv.await.unwrap();
|
|
|
|
|
|
|
|
// notify TestServer that server and system have shut down
|
|
|
|
// all thread managed resources should be dropped at this point
|
|
|
|
});
|
|
|
|
|
2023-01-01 20:56:27 +00:00
|
|
|
#[allow(clippy::let_underscore_future)]
|
2021-11-15 04:03:33 +00:00
|
|
|
let _ = thread_stop_tx.send(());
|
2021-04-02 07:26:59 +00:00
|
|
|
});
|
|
|
|
|
2021-11-15 04:03:33 +00:00
|
|
|
let (system, server, addr) = started_rx.recv().unwrap();
|
2021-04-02 07:26:59 +00:00
|
|
|
|
|
|
|
let client = {
|
|
|
|
let connector = {
|
|
|
|
#[cfg(feature = "openssl")]
|
|
|
|
{
|
|
|
|
use openssl::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")
|
2023-08-29 00:11:11 +00:00
|
|
|
.map_err(|err| log::error!("Can not set alpn protocol: {err:?}"));
|
2021-04-02 07:26:59 +00:00
|
|
|
Connector::new()
|
2021-11-15 04:03:33 +00:00
|
|
|
.conn_lifetime(Duration::from_secs(0))
|
|
|
|
.timeout(Duration::from_millis(30000))
|
2021-12-18 16:44:30 +00:00
|
|
|
.openssl(builder.build())
|
2021-04-02 07:26:59 +00:00
|
|
|
}
|
|
|
|
#[cfg(not(feature = "openssl"))]
|
|
|
|
{
|
|
|
|
Connector::new()
|
2021-11-15 04:03:33 +00:00
|
|
|
.conn_lifetime(Duration::from_secs(0))
|
|
|
|
.timeout(Duration::from_millis(30000))
|
2021-04-02 07:26:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Client::builder().connector(connector).finish()
|
|
|
|
};
|
|
|
|
|
|
|
|
TestServer {
|
2021-11-15 04:03:33 +00:00
|
|
|
server,
|
|
|
|
thread_stop_rx,
|
2021-04-02 07:26:59 +00:00
|
|
|
client,
|
|
|
|
system,
|
2021-11-15 04:03:33 +00:00
|
|
|
addr,
|
2021-04-02 07:26:59 +00:00
|
|
|
tls,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum HttpVer {
|
|
|
|
Http1,
|
|
|
|
Http2,
|
|
|
|
Both,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
enum StreamType {
|
|
|
|
Tcp,
|
|
|
|
#[cfg(feature = "openssl")]
|
|
|
|
Openssl(openssl::ssl::SslAcceptor),
|
2023-08-29 00:11:11 +00:00
|
|
|
#[cfg(feature = "rustls-0_20")]
|
|
|
|
Rustls020(tls_rustls_0_20::ServerConfig),
|
|
|
|
#[cfg(feature = "rustls-0_21")]
|
|
|
|
Rustls021(tls_rustls_0_21::ServerConfig),
|
2024-02-03 23:55:01 +00:00
|
|
|
#[cfg(feature = "rustls-0_22")]
|
|
|
|
Rustls022(tls_rustls_0_22::ServerConfig),
|
2024-05-18 19:05:58 +00:00
|
|
|
#[cfg(feature = "rustls-0_23")]
|
|
|
|
Rustls023(tls_rustls_0_23::ServerConfig),
|
2021-04-02 07:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create default test server config.
|
|
|
|
pub fn config() -> TestServerConfig {
|
|
|
|
TestServerConfig::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct TestServerConfig {
|
|
|
|
tp: HttpVer,
|
|
|
|
stream: StreamType,
|
2022-01-31 17:30:34 +00:00
|
|
|
client_request_timeout: Duration,
|
2023-02-26 05:25:36 +00:00
|
|
|
port: u16,
|
2023-07-19 16:48:43 +00:00
|
|
|
workers: usize,
|
2021-04-02 07:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TestServerConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
TestServerConfig::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestServerConfig {
|
2024-02-03 23:55:01 +00:00
|
|
|
/// Constructs default server configuration.
|
2021-04-02 07:26:59 +00:00
|
|
|
pub(crate) fn new() -> TestServerConfig {
|
|
|
|
TestServerConfig {
|
|
|
|
tp: HttpVer::Both,
|
|
|
|
stream: StreamType::Tcp,
|
2022-01-31 17:30:34 +00:00
|
|
|
client_request_timeout: Duration::from_secs(5),
|
2023-02-26 05:25:36 +00:00
|
|
|
port: 0,
|
2023-07-19 16:48:43 +00:00
|
|
|
workers: 1,
|
2021-04-02 07:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 23:55:01 +00:00
|
|
|
/// Accepts HTTP/1.1 only.
|
2021-04-02 07:26:59 +00:00
|
|
|
pub fn h1(mut self) -> Self {
|
|
|
|
self.tp = HttpVer::Http1;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-03 23:55:01 +00:00
|
|
|
/// Accepts HTTP/2 only.
|
2021-04-02 07:26:59 +00:00
|
|
|
pub fn h2(mut self) -> Self {
|
|
|
|
self.tp = HttpVer::Http2;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-03 23:55:01 +00:00
|
|
|
/// Accepts secure connections via OpenSSL.
|
2021-04-02 07:26:59 +00:00
|
|
|
#[cfg(feature = "openssl")]
|
|
|
|
pub fn openssl(mut self, acceptor: openssl::ssl::SslAcceptor) -> Self {
|
|
|
|
self.stream = StreamType::Openssl(acceptor);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-03 23:55:01 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[deprecated(note = "Renamed to `rustls_0_20()`.")]
|
2023-08-29 00:11:11 +00:00
|
|
|
#[cfg(feature = "rustls-0_20")]
|
|
|
|
pub fn rustls(mut self, config: tls_rustls_0_20::ServerConfig) -> Self {
|
|
|
|
self.stream = StreamType::Rustls020(config);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-03 23:55:01 +00:00
|
|
|
/// Accepts secure connections via Rustls v0.20.
|
|
|
|
#[cfg(feature = "rustls-0_20")]
|
|
|
|
pub fn rustls_0_20(mut self, config: tls_rustls_0_20::ServerConfig) -> Self {
|
|
|
|
self.stream = StreamType::Rustls020(config);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[deprecated(note = "Renamed to `rustls_0_21()`.")]
|
2023-08-29 00:11:11 +00:00
|
|
|
#[cfg(feature = "rustls-0_21")]
|
|
|
|
pub fn rustls_021(mut self, config: tls_rustls_0_21::ServerConfig) -> Self {
|
|
|
|
self.stream = StreamType::Rustls021(config);
|
2021-04-02 07:26:59 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-03 23:55:01 +00:00
|
|
|
/// Accepts secure connections via Rustls v0.21.
|
|
|
|
#[cfg(feature = "rustls-0_21")]
|
|
|
|
pub fn rustls_0_21(mut self, config: tls_rustls_0_21::ServerConfig) -> Self {
|
|
|
|
self.stream = StreamType::Rustls021(config);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Accepts secure connections via Rustls v0.22.
|
|
|
|
#[cfg(feature = "rustls-0_22")]
|
|
|
|
pub fn rustls_0_22(mut self, config: tls_rustls_0_22::ServerConfig) -> Self {
|
|
|
|
self.stream = StreamType::Rustls022(config);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-05-18 19:05:58 +00:00
|
|
|
/// Accepts secure connections via Rustls v0.23.
|
|
|
|
#[cfg(feature = "rustls-0_23")]
|
|
|
|
pub fn rustls_0_23(mut self, config: tls_rustls_0_23::ServerConfig) -> Self {
|
|
|
|
self.stream = StreamType::Rustls023(config);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-03 23:55:01 +00:00
|
|
|
/// Sets client timeout for first request.
|
2022-01-31 17:30:34 +00:00
|
|
|
pub fn client_request_timeout(mut self, dur: Duration) -> Self {
|
|
|
|
self.client_request_timeout = dur;
|
2021-04-02 07:26:59 +00:00
|
|
|
self
|
|
|
|
}
|
2023-02-26 05:25:36 +00:00
|
|
|
|
|
|
|
/// Sets test server port.
|
|
|
|
///
|
|
|
|
/// By default, a random free port is determined by the OS.
|
|
|
|
pub fn port(mut self, port: u16) -> Self {
|
|
|
|
self.port = port;
|
|
|
|
self
|
|
|
|
}
|
2023-07-19 16:48:43 +00:00
|
|
|
|
2023-07-19 18:27:20 +00:00
|
|
|
/// Sets number of workers for the test server.
|
2023-07-19 16:48:43 +00:00
|
|
|
///
|
2023-07-19 18:27:20 +00:00
|
|
|
/// By default, the server uses 1 worker
|
2023-07-19 16:48:43 +00:00
|
|
|
pub fn workers(mut self, workers: usize) -> Self {
|
|
|
|
self.workers = workers;
|
|
|
|
self
|
|
|
|
}
|
2021-04-02 07:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A basic HTTP server controller that simplifies the process of writing integration tests for
|
|
|
|
/// Actix Web applications.
|
|
|
|
///
|
|
|
|
/// See [`start`] for usage example.
|
|
|
|
pub struct TestServer {
|
2021-11-15 04:03:33 +00:00
|
|
|
server: ServerHandle,
|
|
|
|
thread_stop_rx: mpsc::Receiver<()>,
|
2021-04-02 07:26:59 +00:00
|
|
|
client: awc::Client,
|
|
|
|
system: rt::System,
|
2021-11-15 04:03:33 +00:00
|
|
|
addr: net::SocketAddr,
|
2021-04-02 07:26:59 +00:00
|
|
|
tls: 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.tls { "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(&self, path: impl AsRef<str>) -> ClientRequest {
|
|
|
|
self.client.get(self.url(path.as_ref()).as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `POST` request.
|
|
|
|
pub fn post(&self, path: impl AsRef<str>) -> ClientRequest {
|
|
|
|
self.client.post(self.url(path.as_ref()).as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `HEAD` request.
|
|
|
|
pub fn head(&self, path: impl AsRef<str>) -> ClientRequest {
|
|
|
|
self.client.head(self.url(path.as_ref()).as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `PUT` request.
|
|
|
|
pub fn put(&self, path: impl AsRef<str>) -> ClientRequest {
|
|
|
|
self.client.put(self.url(path.as_ref()).as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `PATCH` request.
|
|
|
|
pub fn patch(&self, path: impl AsRef<str>) -> ClientRequest {
|
|
|
|
self.client.patch(self.url(path.as_ref()).as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `DELETE` request.
|
|
|
|
pub fn delete(&self, path: impl AsRef<str>) -> ClientRequest {
|
|
|
|
self.client.delete(self.url(path.as_ref()).as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `OPTIONS` request.
|
|
|
|
pub fn options(&self, path: impl AsRef<str>) -> ClientRequest {
|
|
|
|
self.client.options(self.url(path.as_ref()).as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Connect request with given method and path.
|
|
|
|
pub fn request(&self, method: Method, path: impl AsRef<str>) -> ClientRequest {
|
|
|
|
self.client.request(method, path.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn load_body<S>(
|
|
|
|
&mut self,
|
|
|
|
mut response: ClientResponse<S>,
|
|
|
|
) -> Result<web::Bytes, PayloadError>
|
|
|
|
where
|
|
|
|
S: Stream<Item = Result<web::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
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get default HeaderMap of Client.
|
|
|
|
///
|
|
|
|
/// Returns Some(&mut HeaderMap) when Client object is unique
|
|
|
|
/// (No other clone of client exists at the same time).
|
|
|
|
pub fn client_headers(&mut self) -> Option<&mut HeaderMap> {
|
|
|
|
self.client.headers()
|
|
|
|
}
|
|
|
|
|
2021-11-19 14:04:12 +00:00
|
|
|
/// Stop HTTP server.
|
2021-11-15 04:03:33 +00:00
|
|
|
///
|
2021-11-19 14:04:12 +00:00
|
|
|
/// Waits for spawned `Server` and `System` to shutdown (force) shutdown.
|
2021-11-15 04:03:33 +00:00
|
|
|
pub async fn stop(mut self) {
|
|
|
|
// signal server to stop
|
2021-11-19 14:04:12 +00:00
|
|
|
self.server.stop(false).await;
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
// also signal system to stop
|
|
|
|
// though this is handled by `ServerBuilder::exit_system` too
|
2021-04-02 07:26:59 +00:00
|
|
|
self.system.stop();
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
// wait for thread to be stopped but don't care about result
|
|
|
|
let _ = self.thread_stop_rx.recv().await;
|
2021-04-02 07:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TestServer {
|
|
|
|
fn drop(&mut self) {
|
2021-11-15 04:03:33 +00:00
|
|
|
// calls in this Drop impl should be enough to shut down the server, system, and thread
|
|
|
|
// without needing to await anything
|
|
|
|
|
|
|
|
// signal server to stop
|
2023-01-01 20:56:27 +00:00
|
|
|
#[allow(clippy::let_underscore_future)]
|
2021-11-15 04:03:33 +00:00
|
|
|
let _ = self.server.stop(true);
|
|
|
|
|
|
|
|
// signal system to stop
|
|
|
|
self.system.stop();
|
2021-04-02 07:26:59 +00:00
|
|
|
}
|
|
|
|
}
|