 
   defmodule Controller do
     use Phoenix.Controller
-    alias Phoenix.Router.ForwardTest.AdminDashboard
 
-    plug :put_assigns
+    plug :assign_fwd_conn
 
     def index(conn, _params), do: text(conn, "admin index")
     def stats(conn, _params), do: text(conn, "stats")
     def api_users(conn, _params), do: text(conn, "api users")
     def api_root(conn, _params), do: text(conn, "api root")
-
-    defp put_assigns(conn, _) do
-      page_path = AdminDashboard.Helpers.page_path(conn, :stats)
-
-      conn
-      |> assign(:internal_admin_page_path, page_path)
-      |> assign(:fwd_script_name, conn.script_name)
-    end
+    defp assign_fwd_conn(conn, _), do: assign(conn, :fwd_conn, conn)
   end
 
   defmodule ApiRouter do


  test "forwards path to plug" do
    conn = call(Router, :get, "admin")
    assert conn.script_name == []
    assert conn.assigns[:fwd_script_name] == ["admin"]
    assert conn.status == 200
    assert conn.resp_body == "admin index"
  end

  test "forwards any request starting with forward path" do
    conn = call(Router, :get, "admin/stats")
    assert conn.script_name == []
    assert conn.assigns[:fwd_script_name] == ["admin"]
    assert conn.status == 200
    assert conn.resp_body == "stats"
  end

  end

  test "helpers cascade script name across forwards based on main router" do
    import AdminDashboard.Helpers
    assert page_path(%Plug.Conn{}, :stats) == "/stats"

    conn = call(Router, :get, "stats")
    fwd_conn = conn.assigns[:fwd_conn]
    assert page_path(fwd_conn, :stats) == "/admin/stats"
    assert page_path(conn, :stats) == "/admin/stats"

    conn = call(Router, :get, "stats", _params = nil, _headers = [], ["phx"])
    fwd_conn = conn.assigns[:fwd_conn]
    assert page_path(fwd_conn, :stats) == "/phx/admin/stats"
    assert page_path(conn, :stats) == "/phx/admin/stats"

    conn = call(Router, :get, "admin/stats")
    fwd_conn = conn.assigns[:fwd_conn]
    assert page_path(fwd_conn, :stats) == "/admin/stats"
    assert page_path(conn, :stats) == "/admin/stats"

    conn = call(Router, :get, "admin/stats", _params = nil, _headers = [], ["phx"])
    fwd_conn = conn.assigns[:fwd_conn]
    assert page_path(fwd_conn, :stats) == "/phx/admin/stats"
    assert page_path(conn, :stats) == "/phx/admin/stats"
  end
end
