diff --git a/actix-test/CHANGES.md b/actix-test/CHANGES.md index 23f4b4db1..5dec9cf1f 100644 --- a/actix-test/CHANGES.md +++ b/actix-test/CHANGES.md @@ -2,6 +2,7 @@ ## Unreleased - 2022-xx-xx +- Add `TestServerConfig::port()` setter method. - Minimum supported Rust version (MSRV) is now 1.59 due to transitive `time` dependency. ## 0.1.0 - 2022-07-24 diff --git a/actix-test/src/lib.rs b/actix-test/src/lib.rs index 1aff2dc83..18453b599 100644 --- a/actix-test/src/lib.rs +++ b/actix-test/src/lib.rs @@ -145,7 +145,7 @@ where // run server in separate orphaned thread thread::spawn(move || { rt::System::new().block_on(async move { - let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap(); + let tcp = net::TcpListener::bind(("127.0.0.1", cfg.port)).unwrap(); let local_addr = tcp.local_addr().unwrap(); let factory = factory.clone(); let srv_cfg = cfg.clone(); @@ -390,6 +390,7 @@ pub struct TestServerConfig { tp: HttpVer, stream: StreamType, client_request_timeout: Duration, + port: u16, } impl Default for TestServerConfig { @@ -405,6 +406,7 @@ impl TestServerConfig { tp: HttpVer::Both, stream: StreamType::Tcp, client_request_timeout: Duration::from_secs(5), + port: 0, } } @@ -439,6 +441,14 @@ impl TestServerConfig { self.client_request_timeout = dur; self } + + /// 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 + } } /// A basic HTTP server controller that simplifies the process of writing integration tests for