Skip to content

Commit 1f7918c

Browse files
committed
Add script name support using minimal change
1 parent 8dc2ff3 commit 1f7918c

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/fun_with_flags/ui/router.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,12 @@ defmodule FunWithFlags.UI.Router do
283283

284284

285285
defp extract_namespace(conn, opts) do
286-
ns = opts[:namespace] || ""
286+
ns = opts[:namespace] || namespace_from_script_name(conn.script_name)
287287
Plug.Conn.assign(conn, :namespace, "/" <> ns)
288288
end
289289

290+
defp namespace_from_script_name([]), do: ""
291+
defp namespace_from_script_name(list), do: Path.join(list)
290292

291293
defp assign_csrf_token(conn, _opts) do
292294
csrf_token = Plug.CSRFProtection.get_csrf_token()
@@ -297,7 +299,7 @@ defmodule FunWithFlags.UI.Router do
297299
# Custom CSRF protection plug. It wraps the default plug provided
298300
# by `Plug`, it calls `Plug.Conn.fetch_session/1` (no-op if already
299301
# fetched), and it bails out gracefully if no session is configured.
300-
#
302+
#
301303
defp protect_from_forgery(conn, opts) do
302304
try do
303305
conn

test/fun_with_flags/ui/router_test.exs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ defmodule FunWithFlags.UI.RouterTest do
1818

1919
@opts Router.init([])
2020

21+
describe "script_name" do
22+
test "redirects to /flags" do
23+
conn =
24+
conn(:get, "/")
25+
|> Map.put(:script_name, ["pear"])
26+
|> Router.call(@opts)
27+
28+
assert 302 = conn.status
29+
assert ["/pear/flags"] = get_resp_header(conn, "location")
30+
end
31+
end
32+
2133
describe "GET /" do
2234
test "redirects to /flags" do
2335
conn = request!(:get, "/")
@@ -112,7 +124,7 @@ defmodule FunWithFlags.UI.RouterTest do
112124
describe "DELETE /flags/:name/boolean" do
113125
test "when the flag exists, it deletes its boolean gate and redirects to the flag page" do
114126
{:ok, true} = FunWithFlags.enable :frozen_yogurt
115-
{:ok, true} = FunWithFlags.enable :frozen_yogurt, for_group: "some_group"
127+
{:ok, true} = FunWithFlags.enable :frozen_yogurt, for_group: "some_group"
116128

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

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

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

0 commit comments

Comments
 (0)