From 95e40141eb0d5aff4b21caf8431432c148fc224d Mon Sep 17 00:00:00 2001 From: Kristian Gaylord Date: Fri, 6 Jan 2023 16:17:57 -0500 Subject: [PATCH] allow dynamic port setting --- actix-test/CHANGES.md | 1 + actix-test/src/lib.rs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/actix-test/CHANGES.md b/actix-test/CHANGES.md index c8fe54203..a2733b1fb 100644 --- a/actix-test/CHANGES.md +++ b/actix-test/CHANGES.md @@ -2,6 +2,7 @@ ## Unreleased - 2022-xx-xx - Minimum supported Rust version (MSRV) is now 1.59 due to transitive `time` dependency. +- Ability to set port number in TestServerConfig ## 0.1.0 - 2022-07-24 diff --git a/actix-test/src/lib.rs b/actix-test/src/lib.rs index 1aff2dc83..031bebc7e 100644 --- a/actix-test/src/lib.rs +++ b/actix-test/src/lib.rs @@ -145,10 +145,11 @@ 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 srv_cfg = cfg.clone(); + let port = srv_cfg.port; + let tcp = net::TcpListener::bind(format!("127.0.0.1:{port}")).unwrap(); let local_addr = tcp.local_addr().unwrap(); let factory = factory.clone(); - let srv_cfg = cfg.clone(); let timeout = cfg.client_request_timeout; let builder = Server::build().workers(1).disable_signals().system_exit(); @@ -390,6 +391,7 @@ pub struct TestServerConfig { tp: HttpVer, stream: StreamType, client_request_timeout: Duration, + port: u16, } impl Default for TestServerConfig { @@ -405,6 +407,7 @@ impl TestServerConfig { tp: HttpVer::Both, stream: StreamType::Tcp, client_request_timeout: Duration::from_secs(5), + port: 0, } } @@ -439,6 +442,11 @@ impl TestServerConfig { self.client_request_timeout = dur; self } + + 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