Hi everyone!
TestServer
is an easy way to mock third-party services in ExUnit.
Features
- HTTP/1
- HTTP/2
- WebSocket
- TLS with self-signed certificates
- Flexible FIFO match rules
- Catches unexpected requests
- When test finishes verifies there’s no pending routes or websocket handlers to call
Example
test "fetch_url/0" do
# The test server will autostart the current test server, if not already running
TestServer.add("/", via: :get)
TestServer.add("/", via: :get, to: fn conn -> Plug.Conn.send_resp(conn, 200, "second call") end)
# The URL is derived from the current test server instance
Application.put_env(:my_app, :url, TestServer.url())
assert {:ok, "HTTP"} = MyApp.fetch_url()
assert {:ok, "second call"} = MyApp.fetch_url()
end
Enabling TLS
TestServer.start(scheme: :https)
The key and certificate is generated with x509 on the fly.
WebSocket Example
test "WebSocketClient" do
{:ok, socket} = TestServer.websocket_init("/ws")
:ok = TestServer.websocket_handle(socket, to: fn {:text, "ping"}, state -> {:reply, {:text, "pong"}, state})
{:ok, client} = WebSocketClient.start_link(TestServer.url("/ws"))
:ok = WebSocketClient.send(client, "ping")
{:ok, "pong"} = WebSocketClient.receive(client)
:ok = TestServer.websocket_info(socket, fn state -> {:reply, {:text, "ping"}, state} end)
{:ok, "ping"} = WebSocketClient.receive(client)
end
I’ve been using this for testing a JSON RPC endpoint and testing the SSL configuration for http adapters in assent
.
I hope you find it useful, feel free to contribute!