summaryrefslogtreecommitdiff
path: root/test/pleroma/web/plugs/set_locale_plug_test.exs
blob: 773f48a5bcd7a62ab7b8fd0bfb68d61d021e3db7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
  use ExUnit.Case, async: true
  use Plug.Test

  alias Pleroma.Web.Plugs.SetLocalePlug
  alias Plug.Conn

  test "default locale is `en`" do
    conn =
      :get
      |> conn("/cofe")
      |> SetLocalePlug.call([])

    assert "en" == Gettext.get_locale()
    assert %{locale: "en"} == conn.assigns
  end

  test "use supported locale from `accept-language`" do
    conn =
      :get
      |> conn("/cofe")
      |> Conn.put_req_header(
        "accept-language",
        "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
      )
      |> SetLocalePlug.call([])

    assert "ru" == Gettext.get_locale()
    assert %{locale: "ru"} == conn.assigns
  end

  test "use default locale if locale from `accept-language` is not supported" do
    conn =
      :get
      |> conn("/cofe")
      |> Conn.put_req_header("accept-language", "tlh")
      |> SetLocalePlug.call([])

    assert "en" == Gettext.get_locale()
    assert %{locale: "en"} == conn.assigns
  end
end