2019-01-27 19:59:07 +01:00
|
|
|
//! Various helpers for Actix applications to use during testing.
|
|
|
|
use std::sync::mpsc;
|
2019-03-13 22:41:40 +01:00
|
|
|
use std::{net, thread, time};
|
2019-01-27 19:59:07 +01:00
|
|
|
|
|
|
|
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
2019-12-02 18:33:39 +01:00
|
|
|
use actix_rt::{net::TcpStream, System};
|
2019-11-19 04:55:17 +01:00
|
|
|
use actix_server::{Server, ServiceFactory};
|
2019-03-30 02:51:07 +01:00
|
|
|
use awc::{error::PayloadError, ws, Client, ClientRequest, ClientResponse, Connector};
|
|
|
|
use bytes::Bytes;
|
2020-05-18 04:47:33 +02:00
|
|
|
use futures_core::stream::Stream;
|
2019-01-27 19:59:07 +01:00
|
|
|
use http::Method;
|
2020-05-19 02:25:51 +02:00
|
|
|
use socket2::{Domain, Protocol, Socket, Type};
|
2019-01-27 19:59:07 +01:00
|
|
|
|
2019-11-19 04:55:17 +01:00
|
|
|
pub use actix_testing::*;
|
2019-04-12 20:15:58 +02:00
|
|
|
|
2019-12-12 18:08:38 +01:00
|
|
|
/// Start test server
|
2019-01-27 19:59:07 +01:00
|
|
|
///
|
|
|
|
/// `TestServer` is very simple test server that simplify process of writing
|
|
|
|
/// integration tests cases for actix web applications.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-26 20:50:51 +01:00
|
|
|
/// use actix_http::HttpService;
|
2019-11-26 06:25:50 +01:00
|
|
|
/// use actix_http_test::TestServer;
|
2019-11-20 19:52:38 +01:00
|
|
|
/// use actix_web::{web, App, HttpResponse, Error};
|
2019-03-30 05:13:39 +01:00
|
|
|
///
|
2019-11-20 19:52:38 +01:00
|
|
|
/// async fn my_handler() -> Result<HttpResponse, Error> {
|
|
|
|
/// Ok(HttpResponse::Ok().into())
|
2019-03-26 20:50:51 +01:00
|
|
|
/// }
|
2019-01-27 19:59:07 +01:00
|
|
|
///
|
2019-11-26 06:25:50 +01:00
|
|
|
/// #[actix_rt::test]
|
|
|
|
/// async fn test_example() {
|
|
|
|
/// let mut srv = TestServer::start(
|
|
|
|
/// || HttpService::new(
|
|
|
|
/// App::new().service(
|
|
|
|
/// web::resource("/").to(my_handler))
|
|
|
|
/// )
|
|
|
|
/// );
|
2019-01-27 19:59:07 +01:00
|
|
|
///
|
2019-11-26 06:25:50 +01:00
|
|
|
/// let req = srv.get("/");
|
|
|
|
/// let response = req.send().await.unwrap();
|
|
|
|
/// assert!(response.status().is_success());
|
2019-03-26 20:50:51 +01:00
|
|
|
/// }
|
2019-01-27 19:59:07 +01:00
|
|
|
/// ```
|
2020-03-08 08:42:26 +01:00
|
|
|
pub async fn test_server<F: ServiceFactory<TcpStream>>(factory: F) -> TestServer {
|
2020-09-15 13:09:16 +02:00
|
|
|
let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap();
|
|
|
|
test_server_with_addr(tcp, factory).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start [`test server`](./fn.test_server.html) on a concrete Address
|
|
|
|
pub async fn test_server_with_addr<F: ServiceFactory<TcpStream>>(
|
|
|
|
tcp: net::TcpListener,
|
|
|
|
factory: F,
|
|
|
|
) -> TestServer {
|
2019-12-12 18:08:38 +01:00
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
// run server in separate thread
|
|
|
|
thread::spawn(move || {
|
|
|
|
let sys = System::new("actix-test-server");
|
|
|
|
let local_addr = tcp.local_addr().unwrap();
|
|
|
|
|
|
|
|
Server::build()
|
|
|
|
.listen("test", tcp, factory)?
|
|
|
|
.workers(1)
|
|
|
|
.disable_signals()
|
|
|
|
.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))
|
2019-12-25 17:52:20 +01:00
|
|
|
.timeout(time::Duration::from_millis(30000))
|
2019-12-12 18:08:38 +01:00
|
|
|
.ssl(builder.build())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "openssl"))]
|
|
|
|
{
|
|
|
|
Connector::new()
|
|
|
|
.conn_lifetime(time::Duration::from_secs(0))
|
2019-12-25 17:52:20 +01:00
|
|
|
.timeout(time::Duration::from_millis(30000))
|
2019-12-12 18:08:38 +01:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-11 10:24:39 +02:00
|
|
|
Client::builder().connector(connector).finish()
|
2019-12-12 18:08:38 +01:00
|
|
|
};
|
2020-03-08 08:42:26 +01:00
|
|
|
actix_connect::start_default_resolver().await.unwrap();
|
2019-12-12 18:08:38 +01:00
|
|
|
|
|
|
|
TestServer {
|
|
|
|
addr,
|
|
|
|
client,
|
|
|
|
system,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get first available unused address
|
|
|
|
pub fn unused_addr() -> net::SocketAddr {
|
|
|
|
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
2020-05-22 06:54:17 +02:00
|
|
|
let socket =
|
|
|
|
Socket::new(Domain::ipv4(), Type::stream(), Some(Protocol::tcp())).unwrap();
|
2020-05-19 02:25:51 +02:00
|
|
|
socket.bind(&addr.into()).unwrap();
|
|
|
|
socket.set_reuse_address(true).unwrap();
|
|
|
|
let tcp = socket.into_tcp_listener();
|
2019-12-12 18:08:38 +01:00
|
|
|
tcp.local_addr().unwrap()
|
|
|
|
}
|
2019-01-27 19:59:07 +01:00
|
|
|
|
2019-03-13 22:41:40 +01:00
|
|
|
/// Test server controller
|
2019-12-12 18:08:38 +01:00
|
|
|
pub struct TestServer {
|
2019-01-27 19:59:07 +01:00
|
|
|
addr: net::SocketAddr,
|
2019-03-26 19:43:22 +01:00
|
|
|
client: Client,
|
2019-09-17 17:45:06 +02:00
|
|
|
system: System,
|
2019-01-27 19:59:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if uri.starts_with('/') {
|
2019-07-31 22:02:56 +02:00
|
|
|
format!("http://localhost:{}{}", self.addr.port(), uri)
|
2019-01-27 19:59:07 +01:00
|
|
|
} else {
|
2019-07-31 22:02:56 +02:00
|
|
|
format!("http://localhost:{}/{}", self.addr.port(), uri)
|
2019-01-27 19:59:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 20:44:15 +01:00
|
|
|
/// Construct test https server url
|
|
|
|
pub fn surl(&self, uri: &str) -> String {
|
|
|
|
if uri.starts_with('/') {
|
2019-07-31 22:02:56 +02:00
|
|
|
format!("https://localhost:{}{}", self.addr.port(), uri)
|
2019-02-06 20:44:15 +01:00
|
|
|
} else {
|
2019-07-31 22:02:56 +02:00
|
|
|
format!("https://localhost:{}/{}", self.addr.port(), uri)
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-27 19:59:07 +01:00
|
|
|
/// Create `GET` request
|
2019-03-30 15:53:45 +01:00
|
|
|
pub fn get<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
|
|
|
self.client.get(self.url(path.as_ref()).as_str())
|
2019-01-27 19:59:07 +01:00
|
|
|
}
|
|
|
|
|
2019-03-12 00:42:33 +01:00
|
|
|
/// Create https `GET` request
|
2019-03-30 15:53:45 +01:00
|
|
|
pub fn sget<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
|
|
|
self.client.get(self.surl(path.as_ref()).as_str())
|
2019-03-12 00:42:33 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:59:07 +01:00
|
|
|
/// Create `POST` request
|
2019-03-30 15:53:45 +01:00
|
|
|
pub fn post<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
|
|
|
self.client.post(self.url(path.as_ref()).as_str())
|
2019-01-27 19:59:07 +01:00
|
|
|
}
|
|
|
|
|
2019-03-26 19:43:22 +01:00
|
|
|
/// Create https `POST` request
|
2019-03-30 15:53:45 +01:00
|
|
|
pub fn spost<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
|
|
|
self.client.post(self.surl(path.as_ref()).as_str())
|
2019-01-27 19:59:07 +01:00
|
|
|
}
|
|
|
|
|
2019-03-26 19:43:22 +01:00
|
|
|
/// Create `HEAD` request
|
2019-03-30 15:53:45 +01:00
|
|
|
pub fn head<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
|
|
|
self.client.head(self.url(path.as_ref()).as_str())
|
2019-01-27 19:59:07 +01:00
|
|
|
}
|
|
|
|
|
2019-03-26 19:43:22 +01:00
|
|
|
/// Create https `HEAD` request
|
2019-03-30 15:53:45 +01:00
|
|
|
pub fn shead<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
|
|
|
self.client.head(self.surl(path.as_ref()).as_str())
|
2019-01-27 19:59:07 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 12:47:00 +02:00
|
|
|
/// Create `PUT` request
|
|
|
|
pub fn put<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
|
|
|
self.client.put(self.url(path.as_ref()).as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create https `PUT` request
|
|
|
|
pub fn sput<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
|
|
|
self.client.put(self.surl(path.as_ref()).as_str())
|
|
|
|
}
|
|
|
|
|
2019-07-16 06:19:28 +02:00
|
|
|
/// Create `PATCH` request
|
|
|
|
pub fn patch<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
|
|
|
self.client.patch(self.url(path.as_ref()).as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create https `PATCH` request
|
|
|
|
pub fn spatch<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
|
|
|
self.client.patch(self.surl(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 https `DELETE` request
|
|
|
|
pub fn sdelete<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
|
|
|
self.client.delete(self.surl(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())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create https `OPTIONS` request
|
|
|
|
pub fn soptions<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
|
|
|
self.client.options(self.surl(path.as_ref()).as_str())
|
|
|
|
}
|
|
|
|
|
2019-03-26 19:43:22 +01:00
|
|
|
/// Connect to test http server
|
|
|
|
pub fn request<S: AsRef<str>>(&self, method: Method, path: S) -> ClientRequest {
|
|
|
|
self.client.request(method, path.as_ref())
|
2019-01-27 19:59:07 +01:00
|
|
|
}
|
|
|
|
|
2019-11-19 13:54:19 +01:00
|
|
|
pub async fn load_body<S>(
|
2019-03-30 02:51:07 +01:00
|
|
|
&mut self,
|
2019-04-02 23:27:54 +02:00
|
|
|
mut response: ClientResponse<S>,
|
2019-03-30 02:51:07 +01:00
|
|
|
) -> Result<Bytes, PayloadError>
|
|
|
|
where
|
2019-11-19 04:55:17 +01:00
|
|
|
S: Stream<Item = Result<Bytes, PayloadError>> + Unpin + 'static,
|
2019-03-30 02:51:07 +01:00
|
|
|
{
|
2019-11-19 13:54:19 +01:00
|
|
|
response.body().limit(10_485_760).await
|
2019-01-27 19:59:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Connect to websocket server at a given path
|
2019-11-19 13:54:19 +01:00
|
|
|
pub async fn ws_at(
|
2019-01-27 19:59:07 +01:00
|
|
|
&mut self,
|
|
|
|
path: &str,
|
2019-03-28 02:53:19 +01:00
|
|
|
) -> Result<Framed<impl AsyncRead + AsyncWrite, ws::Codec>, awc::error::WsClientError>
|
|
|
|
{
|
2019-01-27 19:59:07 +01:00
|
|
|
let url = self.url(path);
|
2019-03-28 02:53:19 +01:00
|
|
|
let connect = self.client.ws(url).connect();
|
2019-11-19 13:54:19 +01:00
|
|
|
connect.await.map(|(_, framed)| framed)
|
2019-01-27 19:59:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Connect to a websocket server
|
2019-11-19 13:54:19 +01:00
|
|
|
pub async fn ws(
|
2019-01-27 19:59:07 +01:00
|
|
|
&mut self,
|
2019-03-28 02:53:19 +01:00
|
|
|
) -> Result<Framed<impl AsyncRead + AsyncWrite, ws::Codec>, awc::error::WsClientError>
|
|
|
|
{
|
2019-11-19 13:54:19 +01:00
|
|
|
self.ws_at("/").await
|
2019-01-27 19:59:07 +01:00
|
|
|
}
|
2019-03-30 02:51:07 +01:00
|
|
|
|
|
|
|
/// Stop http server
|
|
|
|
fn stop(&mut self) {
|
2019-09-17 17:45:06 +02:00
|
|
|
self.system.stop();
|
2019-03-30 02:51:07 +01:00
|
|
|
}
|
2019-01-27 19:59:07 +01:00
|
|
|
}
|
|
|
|
|
2019-12-12 18:08:38 +01:00
|
|
|
impl Drop for TestServer {
|
2019-01-27 19:59:07 +01:00
|
|
|
fn drop(&mut self) {
|
|
|
|
self.stop()
|
|
|
|
}
|
|
|
|
}
|