Skip to content

Commit

Permalink
Improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Sep 13, 2022
1 parent 34142da commit dc7051c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,30 @@ end)
TestServer.plug(MyPlug)
```

WebSocket endpoint can also be set up. By default a response with `ECHO #{inspect frame}` will be returned.
WebSocket endpoint can also be set up. By default the handler will echo what was received.

```elixir
{:ok, socket} = TestServer.websocket_init("/ws")
test "WebSocketClient" do
{:ok, socket} = TestServer.websocket_init("/ws")

TestServer.websocket_handle(socket)
TestServer.websocket_handle(socket, to: fn _frame_, state -> {:reply, "pong", state})
TestServer.websocket_handle(socket, match: fn {_opcode, message}, _state -> messsage == "ping")
{:ok, client} = WebSocketClient.start_link(TestServer.url("/ws"))

TestServer.websocket_info(socket, fn state -> {:reply, {:text, "ping"}, state} end)
:ok = TestServer.websocket_handle(socket)
:ok = TestServer.websocket_handle(socket, to: fn {:text, "ping"}, state -> {:reply, "pong", state})
:ok = TestServer.websocket_handle(socket, match: fn {:text, message}, _state -> message == "hi")

:ok = WebSocketClient.send(client, "hello")
{:ok, "hello"} = WebSocketClient.receive(client)

:ok = WebSocketClient.send(client, "ping")
{:ok, "pong"} = WebSocketClient.receive(client)

:ok = WebSocketClient.send("hi")
{:ok, "hi"} = WebSocketClient.receive(client)

:ok = TestServer.websocket_info(socket, fn state -> {:reply, {:text, "ping"}, state} end)
{:ok, "ping"} = WebSocketClient.receive(client)
end
```

<!-- MDOC !-->
Expand Down
6 changes: 3 additions & 3 deletions lib/test_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ defmodule TestServer do
* `:via` - matches the route against some specific HTTP method(s) specified as an atom, like `:get` or `:put`, or a list, like `[:get, :post]`.
* `:match` - an anonymous function that will be called to see if a route matches, defaults to matching with arguments of uri and `:via` option.
* `:to` - a Plug or anonymous function that will be called when the route matches.
* `:to` - a Plug or anonymous function that will be called when the route matches, defaults to return the http scheme.
"""
@spec add(binary(), keyword()) :: :ok
def add(uri, options) when is_binary(uri) do
Expand Down Expand Up @@ -420,7 +420,7 @@ defmodule TestServer do
## Options
* `:match` - an anonymous function that will be called to see if a message matches, defaults to matching anything.
* `:to` - an anonymous function that will be called when the message matches.
* `:to` - an anonymous function that will be called when the message matches, defaults to returning received message.
"""
@spec websocket_handle(websocket_socket(), keyword()) :: :ok
def websocket_handle({instance, _route_ref} = socket, options) do
Expand All @@ -436,7 +436,7 @@ defmodule TestServer do
end

defp default_websocket_handle(frame, state),
do: {:reply, {:text, "ECHO #{inspect(frame)}"}, state}
do: {:reply, frame, state}

@doc """
Sends an message to a websocket instance.
Expand Down
2 changes: 1 addition & 1 deletion test/test_server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ defmodule TestServerTest do
assert :ok =
TestServer.websocket_handle(socket, match: fn _frame, %{custom: true} -> true end)

assert WebSocketClient.send_message(client, "ping") == {:ok, "ECHO {:text, \"ping\"}"}
assert WebSocketClient.send_message(client, "hello") == {:ok, "hello"}
end
end

Expand Down

0 comments on commit dc7051c

Please sign in to comment.