fix: get_from_cookie now correctly handles list return from get_req_header
This commit is contained in:
parent
217ed632fa
commit
6cf955b024
2 changed files with 36 additions and 6 deletions
|
|
@ -69,17 +69,17 @@ defmodule MvWeb.MemberLive.Index.FieldSelection do
|
|||
Returns a map of field names (strings) to boolean visibility values.
|
||||
Returns empty map if no cookie is present.
|
||||
|
||||
Note: This function requires the connection to have cookies parsed.
|
||||
In LiveView, cookies are typically accessed via get_connect_info.
|
||||
Note: This function parses the raw Cookie header. In LiveView, cookies
|
||||
are typically accessed via get_connect_info.
|
||||
"""
|
||||
@spec get_from_cookie(Plug.Conn.t()) :: %{String.t() => boolean()}
|
||||
def get_from_cookie(conn) do
|
||||
# get_req_header always returns a list ([] if no header, [value] if present)
|
||||
case Plug.Conn.get_req_header(conn, "cookie") do
|
||||
nil ->
|
||||
[] ->
|
||||
%{}
|
||||
|
||||
cookie_header ->
|
||||
# Parse cookies manually from header
|
||||
[cookie_header | _rest] ->
|
||||
cookies = parse_cookie_header(cookie_header)
|
||||
|
||||
case Map.get(cookies, @cookie_name) do
|
||||
|
|
|
|||
|
|
@ -93,7 +93,15 @@ defmodule MvWeb.MemberLive.Index.FieldSelectionTest do
|
|||
end
|
||||
|
||||
describe "get_from_cookie/1" do
|
||||
test "returns empty map when cookie is missing" do
|
||||
test "returns empty map when cookie header is missing" do
|
||||
conn = %Plug.Conn{}
|
||||
|
||||
result = FieldSelection.get_from_cookie(conn)
|
||||
|
||||
assert result == %{}
|
||||
end
|
||||
|
||||
test "returns empty map when cookie is empty string" do
|
||||
conn = Plug.Conn.put_req_header(%Plug.Conn{}, "cookie", "")
|
||||
|
||||
result = FieldSelection.get_from_cookie(conn)
|
||||
|
|
@ -101,6 +109,17 @@ defmodule MvWeb.MemberLive.Index.FieldSelectionTest do
|
|||
assert result == %{}
|
||||
end
|
||||
|
||||
test "parses valid JSON from cookie" do
|
||||
selection = %{"first_name" => true, "email" => false}
|
||||
cookie_value = selection |> Jason.encode!() |> URI.encode()
|
||||
cookie_header = "member_field_selection=#{cookie_value}"
|
||||
conn = %Plug.Conn{} |> Plug.Conn.put_req_header("cookie", cookie_header)
|
||||
|
||||
result = FieldSelection.get_from_cookie(conn)
|
||||
|
||||
assert result == selection
|
||||
end
|
||||
|
||||
test "handles invalid JSON in cookie gracefully" do
|
||||
cookie_value = URI.encode("invalid{[")
|
||||
cookie_header = "member_field_selection=#{cookie_value}"
|
||||
|
|
@ -110,6 +129,17 @@ defmodule MvWeb.MemberLive.Index.FieldSelectionTest do
|
|||
|
||||
assert result == %{}
|
||||
end
|
||||
|
||||
test "handles cookie with other values" do
|
||||
selection = %{"street" => true}
|
||||
cookie_value = selection |> Jason.encode!() |> URI.encode()
|
||||
cookie_header = "other_cookie=value; member_field_selection=#{cookie_value}; another=test"
|
||||
conn = %Plug.Conn{} |> Plug.Conn.put_req_header("cookie", cookie_header)
|
||||
|
||||
result = FieldSelection.get_from_cookie(conn)
|
||||
|
||||
assert result == selection
|
||||
end
|
||||
end
|
||||
|
||||
describe "save_to_cookie/2" do
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue