Skip to content

Commit

Permalink
Fix script_name accumulation during forward
Browse files Browse the repository at this point in the history
Previously, the order of path segments was reversed
  • Loading branch information
yrashk committed Jan 26, 2015
1 parent a5ad08f commit 2e0223e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/plug/router/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule Plug.Router.Utils do
"""
def forward(%Plug.Conn{path_info: path, script_name: script} = conn, new_path, target, opts) do
{base, ^new_path} = Enum.split(path, length(path) - length(new_path))
conn = %{conn | path_info: new_path, script_name: base ++ script} |> target.call(opts)
conn = %{conn | path_info: new_path, script_name: script ++ base} |> target.call(opts)
%{conn | path_info: path, script_name: script}
end

Expand Down
36 changes: 36 additions & 0 deletions test/plug/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,35 @@ defmodule Plug.RouterTest do
forward "/forward", to: Forward
forward "/nested/forward", to: Forward

defmodule Forward1 do
use Plug.Router
use Plug.ErrorHandler

plug :match
plug :dispatch

defmodule Forward2 do
use Plug.Router
use Plug.ErrorHandler

plug :match
plug :dispatch

get "/script_name" do
conn |> resp(200, Enum.join(conn.script_name, ","))
end

get "/full_path" do
conn |> resp(200, Plug.Conn.full_path(conn))
end

end

forward "/step2", to: Forward2
end
forward "/step1", to: Forward1


match _ do
conn |> resp(404, "oops")
end
Expand Down Expand Up @@ -177,6 +206,13 @@ defmodule Plug.RouterTest do
assert conn.resp_body == "nested,forward"
end

test "multiple forwarding orders script_name and full_path correctly" do
conn = call(Sample, conn(:get, "/step1/step2/script_name"))
assert conn.resp_body == "step1,step2"
conn = call(Sample, conn(:get, "/step1/step2/full_path"))
assert conn.resp_body == "/step1/step2/full_path"
end

test "dispatch any verb" do
conn = call(Sample, conn(:get, "/6/bar"))
assert conn.resp_body == "ok"
Expand Down

0 comments on commit 2e0223e

Please sign in to comment.