pascal1
Websocket_mock - Testing library for WebSocket based interfaces
Hello everyone,
I created websocket_mock - a library to test WebSocket based interfaces of clients and servers.
It allows to preconfigure a web server with responses based on the messages it receives. You can also send messages to individual clients without receiving a message first. The library also includes a small WebSocket client which is just a thin wrapper around WebSockex with some utility methods for testing. It’s useful when you need to assert that multiple clients received messages.
defmodule MyAppTest do
use ExUnit.Case
alias WebSocketMock.MockServer
alias WebSocketMock.MockClient
setup do
{:ok, server} = MockServer.start()
on_exit(fn -> MockServer.stop(server) end)
%{server: server}
end
test "manual interaction", %{server: server} do
{:ok, client} = MockClient.start(server.url)
[%{client_id: client_id}] = MockServer.list_clients(server)
MockServer.send_message(server, client_id, {:text, "Hello!"})
assert MockClient.received_messages(client) == [{:text, "Hello!"}]
MockClient.send_message(client, {:text, "world"})
assert {:text, "world"} in MockServer.received_messages(server, client_id)
end
test "auto-replies", %{server: server} do
MockServer.reply_with(server, {:text, "ping"}, {:text, "pong"})
# Responds to any message that is exactly "secret"
MockServer.reply_with(server, fn {_op, msg} -> msg == "secret" end, {:text, "unlocked"})
# Uses the incoming message to construct the reply
MockServer.reply_with(server, "echo", fn {op, msg} -> {op, msg <> " pong"} end)
{:ok, client} = MockClient.start(server.url)
MockClient.send_message(client, {:text, "ping"})
Process.sleep(20)
assert {:text, "pong"} in MockClient.received_messages(client)
MockClient.send_message(client, {:text, "secret"})
Process.sleep(20)
assert {:text, "unlocked"} in MockClient.received_messages(client)
MockClient.send_message(client, {:text, "echo"})
Process.sleep(20)
assert {:text, "echo pong"} in MockClient.received_messages(client)
end
end
Feedback and contributions are always welcome!
Popular in Announcing
Hello all,
I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New
I’ve cleaned up and open sourced three financial libraries I was using for my company. They are bindings for the APIs of these three comp...
New
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
New
EDIT: since Ecto 3.0 final version is out, this post was amended to use the final versions in the instructions below.
Hi everyone,
We a...
New
I’d like to announce a small library called boundaries.
This is an experimental project which explores the idea of enforcing boundaries ...
New
With HEEX released we decided to start a components library using Tailwind CSS - check it out here: Petal Components.
We also have a boi...
New
Hello all,
I’ve been working on Svonix - a library for quickly integrating Svelte components into Phoenix views. It’s a much-needed succ...
New
Other popular topics
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









