fix: get_from_cookie now correctly handles list return from get_req_header

This commit is contained in:
Moritz 2025-12-03 18:37:51 +01:00
parent 217ed632fa
commit 6cf955b024
2 changed files with 36 additions and 6 deletions

View file

@ -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