Packages
bandit
0.5.3
1.12.0
1.11.1
1.11.0
1.10.4
1.10.3
1.10.2
1.10.1
1.10.0
retired
1.9.0
1.8.0
1.7.0
1.6.11
1.6.10
1.6.9
1.6.8
1.6.7
1.6.6
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.7
1.5.6
1.5.5
1.5.4
1.5.3
1.5.2
1.5.1
1.5.0
1.4.2
1.4.1
1.4.0
1.3.0
1.2.3
1.2.2
1.2.1
1.2.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.0
1.0.0-pre.18
1.0.0-pre.17
1.0.0-pre.16
1.0.0-pre.15
1.0.0-pre.14
1.0.0-pre.13
1.0.0-pre.12
1.0.0-pre.11
1.0.0-pre.10
1.0.0-pre.9
1.0.0-pre.8
1.0.0-pre.7
1.0.0-pre.6
1.0.0-pre.5
1.0.0-pre.4
1.0.0-pre.3
1.0.0-pre.2
1.0.0-pre.1
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.11
0.6.10
0.6.9
0.6.8
0.6.7
0.6.6
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.11
0.5.10
0.5.9
0.5.8
0.5.7
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.9
0.3.8
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.2.3
0.2.2
0.2.1
0.2.0
0.1.1
0.1.0
A pure-Elixir HTTP server built for Plug & WebSock apps
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
test/support/websocket_server_helpers.ex
defmodule WebSocketServerHelpers do
@moduledoc false
use ExUnit.CaseTemplate
using do
quote location: :keep do
@behaviour Sock
alias Bandit.WebSocket.{Frame, Socket}
def http1_websocket_server(context) do
{:ok, server_pid} =
[
plug: {__MODULE__, %{startup_opts: :ok}},
sock: {__MODULE__, %{startup_opts: :ok}},
options: [port: 0, read_timeout: 1000, transport_options: [ip: :loopback]]
]
|> Bandit.child_spec()
|> start_supervised()
{:ok, %{port: port}} = ThousandIsland.listener_info(server_pid)
[base: "http://localhost:#{port}", port: port, server_pid: server_pid]
end
@impl Sock
def init(opts), do: Map.put(opts, :init_opts, :ok)
@impl Sock
def negotiate(conn, opts) do
conn = Plug.Conn.fetch_query_params(conn)
calls =
~w(negotiate handle_connection handle_text_frame handle_binary_frame handle_ping_frame handle_pong_frame handle_close handle_error handle_timeout handle_info)a
|> Enum.map(fn call_name ->
function_name =
conn.query_params
|> Map.get(to_string(call_name), "noop_#{call_name}")
|> String.to_atom()
{call_name, function_name}
end)
|> Enum.into(%{})
opts = Map.merge(opts, calls)
apply(__MODULE__, calls[:negotiate], [conn, opts])
end
def noop_negotiate(conn, opts), do: {:accept, conn, opts, []}
@impl Sock
def handle_connection(socket, opts) do
function = Map.get(opts, :handle_connection)
apply(__MODULE__, function, [socket, opts])
end
def noop_handle_connection(_socket, opts), do: {:continue, opts}
@impl Sock
def handle_text_frame(data, socket, opts) do
function = Map.get(opts, :handle_text_frame)
apply(__MODULE__, function, [data, socket, opts])
end
def noop_handle_text_frame(_data, _socket, opts), do: {:continue, opts}
@impl Sock
def handle_binary_frame(data, socket, opts) do
function = Map.get(opts, :handle_binary_frame)
apply(__MODULE__, function, [data, socket, opts])
end
def noop_handle_binary_frame(_data, _socket, opts), do: {:continue, opts}
@impl Sock
def handle_ping_frame(data, socket, opts) do
function = Map.get(opts, :handle_ping_frame)
apply(__MODULE__, function, [data, socket, opts])
end
def noop_handle_ping_frame(_data, _socket, opts), do: {:continue, opts}
@impl Sock
def handle_pong_frame(data, socket, opts) do
function = Map.get(opts, :handle_pong_frame)
apply(__MODULE__, function, [data, socket, opts])
end
def noop_handle_pong_frame(_data, _socket, opts), do: {:continue, opts}
@impl Sock
def handle_close(reason, socket, opts) do
function = Map.get(opts, :handle_close)
apply(__MODULE__, function, [reason, socket, opts])
end
def noop_handle_close(_reason, _socket, _opts), do: :ok
@impl Sock
def handle_error(reason, socket, opts) do
function = Map.get(opts, :handle_error)
apply(__MODULE__, function, [reason, socket, opts])
end
def noop_handle_error(_reason, _socket, _opts), do: :ok
@impl Sock
def handle_timeout(socket, opts) do
function = Map.get(opts, :handle_timeout)
apply(__MODULE__, function, [socket, opts])
end
def noop_handle_timeout(_socket, _opts), do: :ok
@impl Sock
def handle_info(msg, socket, opts) do
function = Map.get(opts, :handle_info)
apply(__MODULE__, function, [msg, socket, opts])
end
def noop_handle_info(_msg, _socket, opts), do: {:continue, opts}
def call(conn, _) do
function = String.to_atom(List.first(conn.path_info))
apply(__MODULE__, function, [conn])
end
end
end
end