issue-1643 add possibility to start test server on particular port

Signed-off-by: Aleksandrov Vladimir <invis87@gmail.com>
This commit is contained in:
Aleksandrov Vladimir 2020-08-24 11:23:22 +03:00
parent 75d86a6beb
commit c12049f5de
No known key found for this signature in database
GPG Key ID: CF84A9185F9615AE
3 changed files with 17 additions and 3 deletions

View File

@ -1,5 +1,11 @@
# Changes # Changes
## Unreleased - 2020-xx-xx
* add ability to set address for `TestServer` [#1645]
[#1645]: https://github.com/actix/actix-web/pull/1645
## [2.0.0-alpha.1] - 2020-05-23 ## [2.0.0-alpha.1] - 2020-05-23
* Update the `time` dependency to 0.2.7 * Update the `time` dependency to 0.2.7

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-http-test" name = "actix-http-test"
version = "2.0.0-alpha.1" version = "2.0.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix HTTP test server" description = "Actix HTTP test server"
readme = "README.md" readme = "README.md"

View File

@ -44,12 +44,20 @@ pub use actix_testing::*;
/// } /// }
/// ``` /// ```
pub async fn test_server<F: ServiceFactory<TcpStream>>(factory: F) -> TestServer { pub async fn test_server<F: ServiceFactory<TcpStream>>(factory: F) -> TestServer {
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 {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
// run server in separate thread // run server in separate thread
thread::spawn(move || { thread::spawn(move || {
let sys = System::new("actix-test-server"); 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 local_addr = tcp.local_addr().unwrap();
Server::build() Server::build()
@ -90,7 +98,7 @@ pub async fn test_server<F: ServiceFactory<TcpStream>>(factory: F) -> TestServer
} }
}; };
Client::build().connector(connector).finish() Client::builder().connector(connector).finish()
}; };
actix_connect::start_default_resolver().await.unwrap(); actix_connect::start_default_resolver().await.unwrap();