Packages
Bypass provides a quick way to create a custom plug that can be put in place instead of an actual HTTP server to return prebaked responses to client requests. This is helpful when you want to create a mock HTTP server and test how your HTTP client handles different types of server responses.
Current section
Files
Jump to
Current section
Files
lib/bypass/plug.ex
defmodule Bypass.Plug do
def init([pid]), do: pid
def call(%{method: method, request_path: request_path} = conn, pid) do
route = Bypass.Instance.call(pid, {:get_route, method, request_path})
ref = make_ref()
case Bypass.Instance.call(pid, {:get_expect_fun, route}) do
fun when is_function(fun, 1) ->
retain_current_plug(pid, route, ref)
try do
fun.(conn)
else
conn ->
put_result(pid, route, ref, :ok_call)
conn
catch
class, reason ->
stacktrace = System.stacktrace
put_result(pid, route, ref, {:exit, {class, reason, stacktrace}})
:erlang.raise(class, reason, stacktrace)
end
{:error, error, route} ->
put_result(pid, route, ref, {:error, error, route})
raise "Route error"
end
end
defp retain_current_plug(pid, route, ref) do
Bypass.Instance.cast(pid, {:retain_plug_process, route, ref, self()})
end
defp put_result(pid, route, ref, result) do
Bypass.Instance.call(pid, {:put_expect_result, route, ref, result})
end
end