2026-02-18 21:23:15 +00:00
|
|
|
defmodule BerrypodWeb.Plugs.CountryDetectTest do
|
|
|
|
|
use BerrypodWeb.ConnCase, async: true
|
2026-02-14 10:48:00 +00:00
|
|
|
|
2026-02-18 21:23:15 +00:00
|
|
|
alias BerrypodWeb.Plugs.CountryDetect
|
2026-02-14 10:48:00 +00:00
|
|
|
|
|
|
|
|
defp with_cookies(conn) do
|
|
|
|
|
Plug.Conn.fetch_cookies(conn)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "call/2" do
|
|
|
|
|
test "detects GB from en-GB header", %{conn: conn} do
|
|
|
|
|
conn =
|
|
|
|
|
conn
|
|
|
|
|
|> init_test_session(%{})
|
|
|
|
|
|> with_cookies()
|
|
|
|
|
|> put_req_header("accept-language", "en-GB,en;q=0.9")
|
|
|
|
|
|> CountryDetect.call([])
|
|
|
|
|
|
|
|
|
|
assert get_session(conn, "country_code") == "GB"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "detects DE from de-DE header", %{conn: conn} do
|
|
|
|
|
conn =
|
|
|
|
|
conn
|
|
|
|
|
|> init_test_session(%{})
|
|
|
|
|
|> with_cookies()
|
|
|
|
|
|> put_req_header("accept-language", "de-DE,de;q=0.9,en;q=0.8")
|
|
|
|
|
|> CountryDetect.call([])
|
|
|
|
|
|
|
|
|
|
assert get_session(conn, "country_code") == "DE"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "detects FR from fr-FR header", %{conn: conn} do
|
|
|
|
|
conn =
|
|
|
|
|
conn
|
|
|
|
|
|> init_test_session(%{})
|
|
|
|
|
|> with_cookies()
|
|
|
|
|
|> put_req_header("accept-language", "fr-FR,fr;q=0.9")
|
|
|
|
|
|> CountryDetect.call([])
|
|
|
|
|
|
|
|
|
|
assert get_session(conn, "country_code") == "FR"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "defaults to GB when no country in header", %{conn: conn} do
|
|
|
|
|
conn =
|
|
|
|
|
conn
|
|
|
|
|
|> init_test_session(%{})
|
|
|
|
|
|> with_cookies()
|
|
|
|
|
|> put_req_header("accept-language", "en;q=0.9")
|
|
|
|
|
|> CountryDetect.call([])
|
|
|
|
|
|
|
|
|
|
assert get_session(conn, "country_code") == "GB"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "defaults to GB when no Accept-Language header", %{conn: conn} do
|
|
|
|
|
conn =
|
|
|
|
|
conn
|
|
|
|
|
|> init_test_session(%{})
|
|
|
|
|
|> with_cookies()
|
|
|
|
|
|> CountryDetect.call([])
|
|
|
|
|
|
|
|
|
|
assert get_session(conn, "country_code") == "GB"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "does not overwrite existing country_code in session", %{conn: conn} do
|
|
|
|
|
conn =
|
|
|
|
|
conn
|
|
|
|
|
|> init_test_session(%{"country_code" => "US"})
|
|
|
|
|
|> with_cookies()
|
|
|
|
|
|> put_req_header("accept-language", "en-GB,en;q=0.9")
|
|
|
|
|
|> CountryDetect.call([])
|
|
|
|
|
|
|
|
|
|
assert get_session(conn, "country_code") == "US"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "cookie overrides session country", %{conn: conn} do
|
|
|
|
|
conn =
|
|
|
|
|
conn
|
|
|
|
|
|> init_test_session(%{"country_code" => "GB"})
|
|
|
|
|
|> put_req_cookie("shipping_country", "DE")
|
|
|
|
|
|> with_cookies()
|
|
|
|
|
|> CountryDetect.call([])
|
|
|
|
|
|
|
|
|
|
assert get_session(conn, "country_code") == "DE"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "cookie overrides Accept-Language detection", %{conn: conn} do
|
|
|
|
|
conn =
|
|
|
|
|
conn
|
|
|
|
|
|> init_test_session(%{})
|
|
|
|
|
|> put_req_cookie("shipping_country", "US")
|
|
|
|
|
|> with_cookies()
|
|
|
|
|
|> put_req_header("accept-language", "en-GB,en;q=0.9")
|
|
|
|
|
|> CountryDetect.call([])
|
|
|
|
|
|
|
|
|
|
assert get_session(conn, "country_code") == "US"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|