Skip to content

Commit dc7051c

Browse files
committed
Improve documentation
1 parent 34142da commit dc7051c

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,30 @@ end)
113113
TestServer.plug(MyPlug)
114114
```
115115

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

118118
```elixir
119-
{:ok, socket} = TestServer.websocket_init("/ws")
119+
test "WebSocketClient" do
120+
{:ok, socket} = TestServer.websocket_init("/ws")
120121

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

125-
TestServer.websocket_info(socket, fn state -> {:reply, {:text, "ping"}, state} end)
124+
:ok = TestServer.websocket_handle(socket)
125+
:ok = TestServer.websocket_handle(socket, to: fn {:text, "ping"}, state -> {:reply, "pong", state})
126+
:ok = TestServer.websocket_handle(socket, match: fn {:text, message}, _state -> message == "hi")
127+
128+
:ok = WebSocketClient.send(client, "hello")
129+
{:ok, "hello"} = WebSocketClient.receive(client)
130+
131+
:ok = WebSocketClient.send(client, "ping")
132+
{:ok, "pong"} = WebSocketClient.receive(client)
133+
134+
:ok = WebSocketClient.send("hi")
135+
{:ok, "hi"} = WebSocketClient.receive(client)
136+
137+
:ok = TestServer.websocket_info(socket, fn state -> {:reply, {:text, "ping"}, state} end)
138+
{:ok, "ping"} = WebSocketClient.receive(client)
139+
end
126140
```
127141

128142
<!-- MDOC !-->

lib/test_server.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ defmodule TestServer do
249249
250250
* `:via` - matches the route against some specific HTTP method(s) specified as an atom, like `:get` or `:put`, or a list, like `[:get, :post]`.
251251
* `:match` - an anonymous function that will be called to see if a route matches, defaults to matching with arguments of uri and `:via` option.
252-
* `:to` - a Plug or anonymous function that will be called when the route matches.
252+
* `:to` - a Plug or anonymous function that will be called when the route matches, defaults to return the http scheme.
253253
"""
254254
@spec add(binary(), keyword()) :: :ok
255255
def add(uri, options) when is_binary(uri) do
@@ -420,7 +420,7 @@ defmodule TestServer do
420420
## Options
421421
422422
* `:match` - an anonymous function that will be called to see if a message matches, defaults to matching anything.
423-
* `:to` - an anonymous function that will be called when the message matches.
423+
* `:to` - an anonymous function that will be called when the message matches, defaults to returning received message.
424424
"""
425425
@spec websocket_handle(websocket_socket(), keyword()) :: :ok
426426
def websocket_handle({instance, _route_ref} = socket, options) do
@@ -436,7 +436,7 @@ defmodule TestServer do
436436
end
437437

438438
defp default_websocket_handle(frame, state),
439-
do: {:reply, {:text, "ECHO #{inspect(frame)}"}, state}
439+
do: {:reply, frame, state}
440440

441441
@doc """
442442
Sends an message to a websocket instance.

test/test_server_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ defmodule TestServerTest do
583583
assert :ok =
584584
TestServer.websocket_handle(socket, match: fn _frame, %{custom: true} -> true end)
585585

586-
assert WebSocketClient.send_message(client, "ping") == {:ok, "ECHO {:text, \"ping\"}"}
586+
assert WebSocketClient.send_message(client, "hello") == {:ok, "hello"}
587587
end
588588
end
589589

0 commit comments

Comments
 (0)