File: test_detect_cgi.rb

package info (click to toggle)
ruby-locale 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 444 kB
  • ctags: 293
  • sloc: ruby: 3,203; makefile: 4
file content (217 lines) | stat: -rw-r--r-- 7,033 bytes parent folder | download | duplicates (4)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# -*- mode: ruby; coding: utf-8 -*-
#
# Copyright (C) 2012  Kouhei Sutou <kou@clear-code.com>
# Copyright (C) 2009-2010  Masao Mutoh
#
# License: Ruby's or LGPL
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'cgi'
require 'locale'
require 'test/unit'

class CustomizableCGI < CGI
  def initialize(options={})
    yield(self)
    super(options)
  end
end

class TestDetectCGI < Test::Unit::TestCase
  def setup_cgi(env={})
    Locale.init(:driver => :cgi)
    cgi = CustomizableCGI.new do |_cgi|
      default_env = {
        "REQUEST_METHOD" => "GET",
      }
      env = default_env.merge(env)
      stub(_cgi).env_table {env}
    end
    Locale.cgi = cgi
    Locale.clear_all
  end

  def test_query_string
    #query string
    setup_cgi("QUERY_STRING" => "lang=ja_JP")
    lang = Locale.current[0]
    assert_equal(Locale::Tag::Simple, lang.class)
    assert_equal("ja_JP", lang.to_s)

    setup_cgi("QUERY_STRING" => "lang=ja-jp")
    lang = Locale.current[0]
    assert_equal(Locale::Tag::Simple, lang.class)
    assert_equal("ja_JP", lang.to_s)
    assert_equal("ja-JP", lang.to_rfc.to_s)
    setup_cgi("QUERY_STRING" => "lang=ja-jp")
    assert_equal("ja_JP", lang.to_s)
    assert_equal("ja-JP", lang.to_rfc.to_s)

  end

  def test_cookie
    #cockie
    setup_cgi("HTTP_COOKIE" => "lang=en-us")
    assert_equal("en_US", Locale.current.to_s)
  end

  def test_accept_language
    setup_cgi("HTTP_ACCEPT_LANGUAGE" => "",
              "HTTP_ACCEPT_CHARSET" => "")
    lang = Locale.current[0]
    assert_equal(Locale::Tag::Simple, lang.class)
    assert_equal("en", lang.to_s)
    assert_equal("en", lang.to_rfc.to_s)

    setup_cgi("HTTP_ACCEPT_LANGUAGE" => "ja,en-us;q=0.7,en;q=0.3")
    lang1, lang2, lang3 = Locale.current
    assert_equal("ja", lang1.to_rfc.to_s)
    assert_equal("en-US", lang2.to_rfc.to_s)
    assert_equal("en", lang3.to_rfc.to_s)

    setup_cgi("HTTP_ACCEPT_LANGUAGE" => "en-us,ja;q=0.7,en;q=0.3")
    lang1, lang2, lang3 = Locale.current
    assert_equal("en-US", lang1.to_rfc.to_s)
    assert_equal("ja", lang2.to_rfc.to_s)
    assert_equal("en", lang3.to_rfc.to_s)

    setup_cgi("HTTP_ACCEPT_LANGUAGE" => "en")
    lang = Locale.current[0]
    assert_equal("en", lang.to_rfc.to_s)
  end

  def test_accept_charset
    #accept charset
    setup_cgi("HTTP_ACCEPT_CHARSET" => "Shift_JIS")
    assert_equal("Shift_JIS", Locale.charset)

    setup_cgi("HTTP_ACCEPT_CHARSET" => "EUC-JP,*,utf-8")
    assert_equal("EUC-JP", Locale.charset)

    setup_cgi("HTTP_ACCEPT_CHARSET" => "*")
    assert_equal("UTF-8", Locale.charset)

    setup_cgi("HTTP_ACCEPT_CHARSET" => "")
    assert_equal("UTF-8", Locale.charset)
  end

  def test_default
    Locale.set_default(nil)
    Locale.set_default("ja-JP")
    setup_cgi("HTTP_ACCEPT_LANGUAGE" => "",
              "HTTP_ACCEPT_CHARSET" => "")
    assert_equal("ja-JP", Locale.default.to_rfc.to_s)
    assert_equal("ja-JP", Locale.current.to_rfc.to_s)
    Locale.set_default(nil)
  end

  def common(*ary)
    ary.map{|v| Locale::Tag::Common.parse(v)}
  end

  def rfc(*ary)
    ary.map{|v| Locale::Tag::Rfc.parse(v)}
  end

  def cldr(*ary)
    ary.map{|v| Locale::Tag::Cldr.parse(v)}
  end

  def simple(*ary)
    ary.map{|v| Locale::Tag::Simple.parse(v)}
  end

  def test_candidates

    setup_cgi("HTTP_ACCEPT_LANGUAGE" => "fr-fr,zh_CN;q=0.7,zh_TW;q=0.2,ja_JP;q=0.1")

    assert_equal common("fr-FR", "zh-CN", "zh-TW", "ja-JP", 
                        "fr", "zh", "ja", "en"), Locale.candidates
    
    assert_equal rfc("fr-FR", "zh-CN", "zh-TW", "ja-JP", "fr", 
                     "zh", "ja", "en"), Locale.candidates(:type => :rfc)

    assert_equal cldr("fr_FR", "zh_CN", "zh_TW", "ja_JP", "fr", 
                      "zh", "ja", "en"), Locale.candidates(:type => :cldr)

    assert_equal simple("fr-FR", "zh-CN", "zh-TW", "ja-JP",  
                        "fr", "zh", "ja", "en"), Locale.candidates(:type => :simple)

    taglist = Locale.candidates(:type => :rfc)
    assert_equal Locale::TagList, taglist.class
    assert_equal "fr", taglist.language
    assert_equal "FR", taglist.region

  end

  def test_candidates_with_supported_language_tags
    setup_cgi("HTTP_ACCEPT_LANGUAGE" => "fr-fr,zh_CN;q=0.7,zh_TW;q=0.2,ja_JP;q=0.1")

    assert_equal common("fr_FR", "zh", "ja"), Locale.candidates(:type => :common, 
                                                                :supported_language_tags => ["fr_FR", "ja", "zh"])

    assert_equal simple("fr-FR", "zh", "ja"), Locale.candidates(:type => :simple, 
                                                                :supported_language_tags => ["fr-FR", "ja", "zh"])
    #supported_language_tags includes "pt" as not in HTTP_ACCEPT_LANGUAGE
    assert_equal simple("fr-FR", "zh", "ja"), 
    Locale.candidates(:type => :simple, 
                      :supported_language_tags => ["fr-FR", "ja", "zh", "pt"])

  end

  def test_candidates_with_default
    setup_cgi("HTTP_ACCEPT_LANGUAGE" => "fr-fr,zh_CN;q=0.7,zh_TW;q=0.2,ja_JP;q=0.1")

    Locale.default = "zh_TW"
    assert_equal simple("fr-FR", "zh", "ja"), 
    Locale.candidates(:type => :simple, 
                      :supported_language_tags => ["fr-FR", "ja", "zh", "pt"])

    Locale.default = "pt"
    assert_equal simple("fr-FR", "zh", "ja", "pt"), 
    Locale.candidates(:type => :simple, 
                      :supported_language_tags => ["fr-FR", "ja", "zh", "pt"])

    # default value is selected even if default is not in supported_language_tags.
    assert_equal simple("pt"), Locale.candidates(:type => :simple, 
                                                 :supported_language_tags => ["aa"])
    Locale.default = "en"
  end


  def test_candidates_with_app_language_tags
    Locale.set_app_language_tags("fr-FR", "ja")

    setup_cgi("HTTP_ACCEPT_LANGUAGE" => "fr-fr,zh_CN;q=0.7,zh_TW;q=0.2,ja_JP;q=0.1")

    assert_equal common("fr-FR", "ja"), Locale.candidates

    # default value is selected if default is not in app_language_tags.
    Locale.set_app_language_tags("no", "pt")
    Locale.default = "zh"
    assert_equal common("zh"), Locale.candidates

    Locale.default = "en"
    Locale.set_app_language_tags(nil)
  end

  def test_request
    Locale.set_request(["ja"], [""], "", "")
    assert_equal common("ja", "en"), Locale.candidates

    Locale.set_request(["en"], [""], "", "")
    assert_equal common("en"), Locale.candidates #Cache should be cleared.
  end
end