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

Crowdhailer
Experimenting with this code. OK.try do user &lt;- fetch_user(1) cart &lt;- fetch_cart(1) order = checkout(cart, user) save_orde...
New
treble37
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
pkrawat1
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
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
sasajuric
I’d like to announce a small library called boundaries. This is an experimental project which explores the idea of enforcing boundaries ...
New
tmbb
PhoenixWS - Websockets over Phoenix Channels Source code on Github here: GitHub - tmbb/phoenix_ws: Websockets implemented over Phoenix Ch...
New
scohen
Lexical Lexical is a next-generation language server for the Elixir programming language. Features Context aware code completion As-you...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
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
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement