pascal1

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!

Where Next?

Popular in Announcing Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
anshuman23
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
bryanjos
Hi, I wanted share a small library we at Revelry Labs made for rendering react components from the server side. There are instructions fo...
New
devonestes
Introducing assertions, the library that helps you write really great test assertions! GitHub: GitHub - devonestes/assertions: Helpful a...
New
mathieuprog
Hello :waving_hand: Allow me to introduce you to Tz, an alternative time zone database support to Tzdata. Why another library? First a...
New
tfwright
After working on it for a couple of months and using it in production for most of that time, today I’ve released LiveAdmin, a LiveView ba...
New
bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
384 14721 119
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
senggen
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

We're in Beta

About us Mission Statement