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

tmbb
PhoenixWS - Websockets over Phoenix Channels Source code on Github here: GitHub - tmbb/phoenix_ws: Websockets implemented over Phoenix Ch...
New
mikehostetler
I’m excited to announce Jido, a framework providing foundational primitives for building autonomous agent systems in Elixir. While develo...
New
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
622 18474 194
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
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
hpopp
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
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
marcuslankenau
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
mattludwigs
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
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

Other popular topics Top

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
aesmail
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
vegabook
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
RisingFromAshes
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
jason.o
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
rms.mrcs
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
nsuchy
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
sergio
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
lanycrost
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement