Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/fun_with_flags/ui/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,12 @@ defmodule FunWithFlags.UI.Router do


defp extract_namespace(conn, opts) do
ns = opts[:namespace] || ""
ns = opts[:namespace] || namespace_from_script_name(conn.script_name)
Plug.Conn.assign(conn, :namespace, "/" <> ns)
end

defp namespace_from_script_name([]), do: ""
defp namespace_from_script_name(list), do: Path.join(list)

defp assign_csrf_token(conn, _opts) do
csrf_token = Plug.CSRFProtection.get_csrf_token()
Expand Down
16 changes: 14 additions & 2 deletions test/fun_with_flags/ui/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ defmodule FunWithFlags.UI.RouterTest do

@opts Router.init([])

describe "script_name" do
test "redirects to /pear/flags" do
conn =
conn(:get, "/")
|> Map.put(:script_name, ["pear"])
|> Router.call(@opts)

assert 302 = conn.status
assert ["/pear/flags"] = get_resp_header(conn, "location")
end
end

describe "GET /" do
test "redirects to /flags" do
conn = request!(:get, "/")
Expand Down Expand Up @@ -112,7 +124,7 @@ defmodule FunWithFlags.UI.RouterTest do
describe "DELETE /flags/:name/boolean" do
test "when the flag exists, it deletes its boolean gate and redirects to the flag page" do
{:ok, true} = FunWithFlags.enable :frozen_yogurt
{:ok, true} = FunWithFlags.enable :frozen_yogurt, for_group: "some_group"
{:ok, true} = FunWithFlags.enable :frozen_yogurt, for_group: "some_group"

assert %Flag{name: :frozen_yogurt, gates: [%Gate{type: :boolean}, %Gate{type: :group}]} = FunWithFlags.get_flag(:frozen_yogurt)

Expand All @@ -128,7 +140,7 @@ defmodule FunWithFlags.UI.RouterTest do
describe "DELETE /flags/:name/percentage" do
test "when the flag exists, it deletes its current percentage gate and redirects to the flag page" do
{:ok, true} = FunWithFlags.enable :pizza, for_percentage_of: {:time, 0.5}
{:ok, true} = FunWithFlags.enable :pizza, for_group: "some_group"
{:ok, true} = FunWithFlags.enable :pizza, for_group: "some_group"

assert %Flag{name: :pizza, gates: [%Gate{type: :percentage_of_time, for: 0.5}, %Gate{type: :group}]} = FunWithFlags.get_flag(:pizza)

Expand Down