danschultzer
Pow Core Team
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! ![]()
Trending in Announcing
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
I released Doggo, a collection of unstyled Phoenix components.
https://github.com/woylie/doggo
Features
Unstyled Phoenix components....
New
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hello
Published a new library - ProcessHub!
ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
This showed up on my feed.. anyone heard of it? Just hype?
Ox Alpha is a reasoning model designed for coding, sustained ag...
New
Hey folks,
I just published a post about Hologram’s funding and where the project goes next - the short version:
Curiosum as Main Spons...
New
:microphone: ElixirConf 2026 - Call for Talks is open!
We’re heading to Chicago :united_states:
:round_pushpin: In person + virtual
:d...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
trisolaran
Hi @danschultzer thanks for releasing this. This looks like a considerable improvement over Bypass. Especially being able to define multiple rules for the same url, which is something you can’t do in Bypass and has always annoyed me. Will try this out as soon as I have to write the next test for an external service.
danschultzer
True, it was what prompted me to build this library. JSON RPC was impossible to test well with bypass. On top of that I couldn’t use bypass to test handling of bad SSL certificates, and I wished it was a lot more ergonomic for request matching.
danschultzer
New exiciting release!
v0.1.8no longer hasPlug.Cowboyas a required dependency, and instead will useBandit,Plug.Cowboy, or:httpddepending what is available (in that order). You can also set up a custom web server.My own belief is that libraries should attempt limit the dependency graph as much as possible. This helps prevent dreaded dependency conflicts, improve auditing, and maybe even helps with perfomance/build time gains.
And I didn’t know
:httpdwas a thing! Included in OTP so of courseTestServershould support it as the default web server ifBanditorPlug.Cowboyis not available. I’ve seen almost no love for:httpd, and maybe that’s for a reason.All to say, now there’s only two required dependency left in
TestServer- the x509 package andPlug.Try it out and let me know what you think!
https://github.com/danschultzer/test_server
danschultzer
v0.1.9is out!This release makes it a lot easier to test IPv6-only networks. All you need to do is set the
:ipfamilyoption:rhcarvalho
I’ve been trying to replace Bypass + Mox with TestServer, and I really appreciate the simplification and ease of understanding it brought back to my tests
The one case that’s been bugging me and preventing me from shipping it is how to handle a test involving
Phoenix.Presence. As far as I understand, depending on timing/scheduling my test server gets hit with a different number of requests.If I
TestServer.addone too little I get a warning in the test output, even though the test passes:If I
TestServer.addone too many, then the test fails:Is there a blessed way in
TestServerto say “this handler should match at least once (or N times)?”Alternatively, any other ideas of what I could consider doing? Thanks!
Schultzer
Are you sure you don’t have conflicting tests?
This could happen with async tests.
rhcarvalho
Tests are run synchronously, and the issue is reproducible running a single test case in isolation as well.
I could be doing something wrong in the “testing
Phoenix.Presence” side of things. I’ve read the few threads on the topic I could find in this forum, including Handling Phoenix Presence during testing - #3 by luizpvasc. I originally worked on this already some time ago, memory is a bit vague, but I think it is related to how Presence “fetchers” work asynchronously, process linking and termination.So I considered posting on that topic, but then thought maybe
TestServercould just be less strict about the number of calls. Reading the implementation a bit more last night, I believe that’s not possible, though.https://github.com/danschultzer/test_server/blob/21ec25c4c9ab9c1988643d2d1acadba5210d3abd/lib/test_server.ex#L67-L75
https://github.com/danschultzer/test_server/blob/21ec25c4c9ab9c1988643d2d1acadba5210d3abd/lib/test_server.ex#L77-L89
https://github.com/danschultzer/test_server/blob/21ec25c4c9ab9c1988643d2d1acadba5210d3abd/lib/test_server.ex#L91-L103
https://github.com/danschultzer/test_server/blob/21ec25c4c9ab9c1988643d2d1acadba5210d3abd/lib/test_server.ex#L117-L132
TestServeralways sets up an ExUniton_exitcallback that checks routes: if anything is left unvisited it errors out.And when an unexpected request comes, it produces the warning:
https://github.com/danschultzer/test_server/blob/21ec25c4c9ab9c1988643d2d1acadba5210d3abd/lib/test_server/plug.ex#L18-L22
I think either I need to understand how to test
Phoenix.Presencein a more deterministic way, spin up a dumb server that doesn’t assert on the number of mocked calls, or mock at the HTTP client level.rhcarvalho
Solved it with the snippet in Phoenix.Presence — Phoenix v1.8.8. Waiting for fetchers to terminate has worked fine coupled with TestServer, while combining that with Bypass+Mox didn’t – so I’m double happy for removing the mental complexity of Bypass and Mox from the code base
Thanks!