Skip to content

Instantly share code, notes, and snippets.

@sb8244
Created January 7, 2021 04:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sb8244/6ff6af1bf01febddddb76f0cb00b4b8a to your computer and use it in GitHub Desktop.
Save sb8244/6ff6af1bf01febddddb76f0cb00b4b8a to your computer and use it in GitHub Desktop.
defmodule TestRouter do
def build(routes) do
contents =
quote do
use Plug.Router
plug :match
plug :dispatch
Enum.map(unquote(Macro.escape(routes)), fn %{match: match, assign: assign} ->
get match, [assigns: assign] do
var!(conn)
end
end)
end
Module.create(__MODULE__.Handler, contents, Macro.Env.location(__ENV__))
end
end
router = TestRouter.build([
%{match: "/hello", assign: %{a: 1, b: 2}},
%{match: "/other", assign: %{c: 3, d: "a"}},
%{match: "/widgets/:id/something/:nested", assign: %{}},
])
conn = TestRouter.Handler.call(Plug.Test.conn(:get, "/widgets/1/something/else", ""), TestRouter.Handler.init([]))
# Will use conn then to provide `params` and certain assigned variables (stored in DB) into the Front Controller
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment