Skip to content

Commit

Permalink
fix: fix locale detecting for non-standard url from referrer
Browse files Browse the repository at this point in the history
  • Loading branch information
c4710n committed Jan 21, 2024
1 parent 119caa3 commit 46cd0b2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/plug_locale/browser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,8 @@ defmodule PlugLocale.Browser do
"/" <> Enum.join(path_info, "/")
end

defp build_path_info(nil), do: []

defp build_path_info(path) when is_binary(path) do
String.split(path, "/", trim: true)
end
Expand Down
16 changes: 14 additions & 2 deletions test/plug_locale/browser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ defmodule PlugLocale.BrowserTest do
assert conn.resp_body =~ "\"/zh-Hans/posts/7\""
end

test "is redirected to a path detected from referrer header" do
test "is redirected to a path detected from referrer header - detect locale from standard path" do
conn =
conn(:get, "/unknown-locale/posts/7")
|> put_req_header("referer", "/zh-Hans/origin")
|> put_req_header("referer", "http://example.com/zh-Hans/origin")

conn = DemoRouter.call(conn, @opts)

Expand All @@ -192,6 +192,18 @@ defmodule PlugLocale.BrowserTest do
assert conn.resp_body =~ "\"/zh-Hans/posts/7\""
end

test "is redirected to a path detected from referrer header - fallback to default locale for non-standard path" do
conn =
conn(:get, "/unknown-locale/posts/7")
|> put_req_header("referer", "http://example.com")

conn = DemoRouter.call(conn, @opts)

assert conn.status == 302
assert conn.assigns[:locale] == nil
assert conn.resp_body =~ "\"/en/posts/7\""
end

test "is redirected to a path detected from accept-language header" do
conn =
conn(:get, "/unknown-locale/posts/7")
Expand Down

0 comments on commit 46cd0b2

Please sign in to comment.