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
Hi everyone,
We would like to announce that Plataformatec is working on a new MySQL driver called MyXQL. Our goal is to eventually integ...
New
Yes, yet another parser combinator library!
Most of the parser combinators in the ecosystem are either compile-time, often using AST tra...
New
Hello everyone!
I want to share with you something that I’m really proud of: https://stillstatic.io/
Still is a static site builder for...
New
I am pleased to announce an initial release of the Membrane Framework - an Elixir-based framework with special focus on processing multim...
New
I’ve just released stable versions of my Prometheus Elixir libs:
Elixir client [docs];
Ecto collector [docs];
Plugs instrumenter/Export...
New
Hey everyone!
Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps:
bureaucrat - which contains a bunch ...
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
I released Doggo, a collection of unstyled Phoenix components.
https://github.com/woylie/doggo
Features
Unstyled Phoenix components....
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
Other popular topics
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
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Hi everyone,
I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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
- #javascript
- #code-sync
- #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








