Packages

A wrapper around Bypass. Double Bypass makes configuring and initializing Bypass easy. It also removes redundant test assertion code, and ecourages testing external requests in a self documenting manner.

Current section

Files

Jump to
double_bypass lib double_bypass.ex
Raw

lib/double_bypass.ex

defmodule DoubleBypass do
@moduledoc """
Responsible for initializing and configuring the Bypass servers.
"""
use ExUnit.CaseTemplate
def setup_bypass?(tags, bypass_tags) do
Enum.any?(bypass_tags, fn({bypass_tag, _host}) ->
bypass_tag in Map.keys(tags)
end)
end
def setup_bypass(tags, bypass_tags), do: init(%{}, tags, bypass_tags)
defp init(acc, _tags, []), do: acc
defp init(acc, tags, _bypass_tags) when tags == %{}, do: acc
defp init(acc, tags, [{bypass_tag, host} | t]) do
case tags[bypass_tag] do
nil -> init(acc, tags, t)
map ->
acc
|> Map.put(bypass_tag, init_server(map, host))
|> init(tags, t)
end
end
defp init_server(opts, host) do
bypass = Bypass.open
url = System.get_env(host)
System.put_env(host, "http://localhost:#{bypass.port}")
if map_size(opts) > 0 do
Bypass.expect(bypass, &DoubleBypass.Assertions.run(&1, opts))
end
onexit(host, url)
bypass
end
defp onexit(env, url) do
on_exit fn ->
if url do
System.put_env(env, url)
else
System.delete_env(env)
end
:ok
end
end
end