micheloliveira
I would like to make a test to assert if specific status codes were used when a websocket connection is closed.
In my code, I raise {:stop, :normal, {1009, "message-too-big"}, :init}, {:stop, :normal, {1007, "invalid-json"}, :init}, {:stop, :normal, {4000, "missing-required-field"}, :init} and others.
When I interact in websocket using wscat:
wscat -c ws://localhost:4000/v1/ws
(I provoke an error resulting in a connection closed by server)
I get the closed connection messages as:
Disconnected (code: 1007, reason: "invalid-json")
My test would be something like that:
test "sends a invalid JSON" do
assert {:ok, client} = WebsocketClient.connect(self(), @path, :noop)
WebsocketClient.send(client, {:text, '{"foo": "bar", something_wrong_here}')})
# assert if the connection was closed with status code 1007 and reason invalid-json
end
How could I write the status code and reason asserts?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex











Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
What is
WebsocketClient? I couldn’t find anything with a similar API in any of the likely hits for “websocket” onhex.pm.As far as the specifics of an assertion, I find it useful to assert clearly-wrong things first and then let the failure messages clue me into what’s going on. For instance:
Running a spec like this and seeing the
assertfail produces several important pieces of data:demonstrates that
do_something_complicatedat least runs (important when the setup is tricky)inspects
resultin the failure messagemicheloliveira
I am sorry. Missing information: Websocketclient is my class to abstract GenServer lib:
micheloliveira
About the test itself, in the example, I want to send a bad formated JSON message, since
something_wrong_hereis not inside quotation marks and is not a key/value, it is a invalid structure in JSON:My application is intolerant to client errors, so, the behaviour is immediately do a disconnection. There is no message sent back, only the disconnection. I want to test if the client was disconnected and if the correct status code and reason for disconnection were sent (using the websocket protocol). I expect to get the same message shown when I make this call using the
wscatcommand line:I do not know how to get this “disconnection” state and data.
al2o3cr
This part of the code gives a little bit of information about what might happen. The implementation of
sendtells me that the test will be waiting for a reply.HOWEVER
It tells nothing else about what might happen when the connection malfunctions. Does the
handle_call({:send, msg}handler crash (and cause the test to exit as well)? Does it reply with{:error, ????}? Something else entirely?micheloliveira
I close connection from server returning a
{:stop, :normal, {result["code"], result["msg"]}, :init}fromhandle_infunction like this: