Skip to content

Commit

Permalink
Add Plug.Test.init_test_session/2 for initializing a session (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Guyon authored and josevalim committed Oct 22, 2016
1 parent cc5c502 commit 1c1b1b4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/plug/session.ex
Expand Up @@ -55,7 +55,10 @@ defmodule Plug.Session do
end end


def call(conn, config) do def call(conn, config) do
Conn.put_private(conn, :plug_session_fetch, fetch_session(config)) conn
|> Conn.put_private(:plug_session_init, conn.private[:plug_session] || %{})
|> Conn.put_private(:plug_session, nil)
|> Conn.put_private(:plug_session_fetch, fetch_session(config))
end end


defp convert_store(store) do defp convert_store(store) do
Expand All @@ -76,6 +79,8 @@ defmodule Plug.Session do
{nil, %{}} {nil, %{}}
end end


session = Map.merge(session, conn.private.plug_session_init)

conn conn
|> Conn.put_private(:plug_session, session) |> Conn.put_private(:plug_session, session)
|> Conn.put_private(:plug_session_fetch, :done) |> Conn.put_private(:plug_session_fetch, :done)
Expand Down
22 changes: 22 additions & 0 deletions lib/plug/test.ex
Expand Up @@ -123,4 +123,26 @@ defmodule Plug.Test do
{key, value}, acc -> put_req_cookie(acc, to_string(key), value) {key, value}, acc -> put_req_cookie(acc, to_string(key), value)
end end
end end

@doc """
Initializes the session with the given contents.
If the session has already been initialized, the new contents will be merged
with the previous ones.
"""
@spec init_test_session(Conn.t, %{(String.t | atom) => any}) :: Conn.t
def init_test_session(conn, session) do
conn =
if conn.private[:plug_session_fetch] do
Conn.fetch_session(conn)
else
conn
|> Conn.put_private(:plug_session, %{})
|> Conn.put_private(:plug_session_fetch, :done)
end

Enum.reduce(session, conn, fn {key, value}, conn ->
Conn.put_session(conn, key, value)
end)
end
end end
47 changes: 47 additions & 0 deletions test/plug/session_test.exs
Expand Up @@ -127,4 +127,51 @@ defmodule Plug.SessionTest do
opts = Plug.Session.init(store: :ets, key: "foobar", table: :some_table) opts = Plug.Session.init(store: :ets, key: "foobar", table: :some_table)
assert opts.store == Plug.Session.ETS assert opts.store == Plug.Session.ETS
end end

test "init_test_session/2" do
conn = conn(:get, "/") |> init_test_session(foo: "bar")
assert get_session(conn, :foo) == "bar"

conn = fetch_session(conn)
assert get_session(conn, :foo) == "bar"

conn = put_session(conn, :bar, "foo")
assert get_session(conn, :bar) == "foo"

conn = delete_session(conn, :bar)
refute get_session(conn, :bar)

conn = clear_session(conn)
refute get_session(conn, :foo)
end

test "init_test_session/2 merges values when called after Plug.Session" do
conn = conn(:get, "/") |> fetch_cookies

opts = Plug.Session.init(store: ProcessStore, key: "foobar")
conn = Plug.Session.call(conn, opts) |> fetch_session
conn = conn |> put_session(:foo, "bar") |> put_session(:bar, "foo")
conn = init_test_session(conn, bar: "bar", other: "other")

assert get_session(conn, :foo) == "bar"
assert get_session(conn, :other) == "other"
assert get_session(conn, :bar) == "bar"
end

test "init_test_session/2 merges values when called before Plug.Session" do
opts = Plug.Session.init(store: ProcessStore, key: "foobar")

conn = conn(:get, "/") |> fetch_cookies
conn = Plug.Session.call(conn, opts) |> fetch_session
conn = conn |> put_session(:foo, "bar") |> put_session(:bar, "foo")
conn = send_resp(conn, 200, "")

conn = conn(:get, "/") |> recycle_cookies(conn)
conn = init_test_session(conn, bar: "bar", other: "other")
conn = Plug.Session.call(conn, opts) |> fetch_session

assert get_session(conn, :foo) == "bar"
assert get_session(conn, :other) == "other"
assert get_session(conn, :bar) == "bar"
end
end end

0 comments on commit 1c1b1b4

Please sign in to comment.