If you need to start multiple servers with the same plug handler, my previous post will not work with Cowboy. You will have to build a module on the fly with an unique name that will delegate to your actual handler (MyPlugModule in my previous example).
With Bandit it works well though:
def mock_http_server(handler) when is_function(handler, 2) do
pid =
ExUnit.Callbacks.start_link_supervised!(
{Bandit,
plug: {MockablePlug, handler},
scheme: :http,
port: 0,
thousand_island_options: [num_acceptors: 1, num_connections: 1]}
)
port =
pid
|> Supervisor.which_children()
|> List.keyfind(:listener, 0)
|> elem(1)
|> :sys.get_state()
|> Map.fetch!(:local_info)
|> Map.fetch!(:port)
base = "http://localhost:#{port}"
Logger.debug("starting http mock server on #{base}/")
base
end