-
Notifications
You must be signed in to change notification settings - Fork 89
Description
When using multiple mock servers, the first one should still be referenced when the second one is to be built. Otherwise, the second one may take the same port as the first one.
Here's a simple example demonstrating the issue:
use wiremock::MockServer;
#[tokio::main]
async fn main() {
mock_server().await;
mock_server().await;
}
async fn mock_server() {
let server = MockServer::start().await;
println!("{}", server.uri());
}When executed, it prints the following output:
http://127.0.0.1:53089
http://127.0.0.1:53089
Note that both servers use the same port.
However, if the mock_server function returns the server itself and this data is now own by the main function, then the ports are different:
use wiremock::MockServer;
#[tokio::main]
async fn main() {
let server1 = mock_server().await;
let server2 = mock_server().await;
}
async fn mock_server() -> MockServer {
let server = MockServer::start().await;
println!("{}", server.uri());
server
}=>
http://127.0.0.1:53109
http://127.0.0.1:53111
As per my tests, this is always reproduced on my machine (Windows 10). I'm not sure whether this is a bug, or if the documentation lacks this information. It simply states the following:
Each instance of MockServer is fully isolated: start takes care of finding a random port available on your local machine which is assigned to the new MockServer.
You should use one instance of MockServer for each REST API that your application interacts with and needs mocking for testing purposes.
If this is the desired behavior, then it should be included that all mounted servers should still be referenced for the random port finding code to work.