From fbfff3e7515d7afc4fbe089275cb627e8c300b27 Mon Sep 17 00:00:00 2001 From: Kristian Gaylord Date: Sat, 25 Feb 2023 22:25:36 -0700 Subject: [PATCH] actix-test: allow dynamic port setting (#2960) Co-authored-by: Rob Ede --- actix-test/CHANGES.md | 1 + actix-test/src/lib.rs | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/actix-test/CHANGES.md b/actix-test/CHANGES.md index 23f4b4db..5dec9cf1 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 1aff2dc8..18453b59 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