summaryrefslogtreecommitdiff
path: root/test/integration/locale_path_test.rb
blob: 22293dcc84b2c0dcea33030c1dbe51899ac034a6 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'test_helper'

#
# Test how we handle redirections and locales.
#
# The basic rules are:
#
# (1) If the browser header Accept-Language matches default locale, then don't do a locale prefix.
# (2) If browser locale is supported in available_locales, but is not the default, then redirect.
# (3) If browser locale is not in available_locales, use the default locale with no prefix.
#
# Settings in defaults.yml
#
#  default_locale: :en
#  available_locales:
#    - :en
#    - :de
#
# NOTE: Although the browser sends the header Accept-Language, this is parsed by
# ruby as HTTP_ACCEPT_LANGUAGE
#

class LocalePathTest < ActionDispatch::IntegrationTest

  teardown do
    I18n.locale = 'en'
  end

  test "redirect if accept-language is not default locale" do
    get_via_redirect '/', {}, 'HTTP_ACCEPT_LANGUAGE' => 'de'
    assert_equal '/de', path
    assert_equal({:locale => :de}, default_url_options)
  end

  test "no locale prefix" do
    get_via_redirect '/', {}, 'HTTP_ACCEPT_LANGUAGE' => 'en'
    assert_equal '/', path
    assert_equal({:locale => nil}, default_url_options)

    get_via_redirect '/', {}, 'HTTP_ACCEPT_LANGUAGE' => 'pt'
    assert_equal '/', path
    assert_equal({:locale => nil}, default_url_options)
  end

  test "no redirect if locale explicit" do
    get_via_redirect '/de', {}, 'HTTP_ACCEPT_LANGUAGE' => 'en'
    assert_equal '/de', path
    assert_equal({:locale => :de}, default_url_options)
  end

  test "strip prefix from url options if locale is default" do
    get_via_redirect '/en', {}, 'HTTP_ACCEPT_LANGUAGE' => 'en'
    assert_equal '/en', path
    assert_equal({:locale => nil}, default_url_options)
  end

  protected

  def default_url_options
    @controller.send(:default_url_options)
  end

end