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
PhoenixWS - Websockets over Phoenix Channels
Source code on Github here: GitHub - tmbb/phoenix_ws: Websockets implemented over Phoenix Ch...
New
I’m excited to announce Jido, a framework providing foundational primitives for building autonomous agent systems in Elixir. While develo...
New
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir.
I...
New
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections.
Fo...
New
Just looking for a little feedback on a tiny helper library I built -
Sometimes I find the need to convert maps with atom keys to maps w...
New
After just over two years in development, this latest version of Pigeon is what I finally consider done in regards to my original vision ...
New
I released Doggo, a collection of unstyled Phoenix components.
https://github.com/woylie/doggo
Features
Unstyled Phoenix components....
New
I feel kind of stuck with the absence of a proper xml library for Elixir. Currently I use SweetXML which was ok for me more or less to pa...
New
Grizzly is a library for working with Z-Wave devices. Z-Wave is a low-frequency radio protocol for controlling smart home devices on a me...
New
Hey guyz
We at @aviabird are working on a payment library in elixir/phoenix. We are targeting March 2018 to add 56 Gateways to it.
Have...
New
Other popular topics
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








