Skip to content
Permalink
Browse files

Add Plug.Test.init_test_session/2 for initializing a session (#469)

  • Loading branch information...
jguyon authored and josevalim committed Oct 22, 2016
1 parent cc5c502 commit 1c1b1b41b0b9f535847862d7804f7194d0f0379d
Showing with 75 additions and 1 deletion.
  1. +6 −1 lib/plug/session.ex
  2. +22 −0 lib/plug/test.ex
  3. +47 −0 test/plug/session_test.exs
@@ -55,7 +55,10 @@ defmodule Plug.Session do
end

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

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

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

conn
|> Conn.put_private(:plug_session, session)
|> Conn.put_private(:plug_session_fetch, :done)
@@ -123,4 +123,26 @@ defmodule Plug.Test do
{key, value}, acc -> put_req_cookie(acc, to_string(key), value)
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
@@ -127,4 +127,51 @@ defmodule Plug.SessionTest do
opts = Plug.Session.init(store: :ets, key: "foobar", table: :some_table)
assert opts.store == Plug.Session.ETS
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

0 comments on commit 1c1b1b4

Please sign in to comment.
You can’t perform that action at this time.