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?