]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/http_accept_language/test/http_accept_language_test.rb
Merged 17256:18123 from trunk.
[rails.git] / vendor / plugins / http_accept_language / test / http_accept_language_test.rb
1 $:.unshift(File.dirname(__FILE__) + '/../lib')
2 require 'http_accept_language'
3 require 'test/unit'
4
5 class MockedCgiRequest
6   include HttpAcceptLanguage
7   def env
8     @env ||= {'HTTP_ACCEPT_LANGUAGE' => 'en-us,en-gb;q=0.8,en;q=0.6'}
9   end
10 end
11
12 class HttpAcceptLanguageTest < Test::Unit::TestCase
13   def test_should_return_empty_array
14     request.env['HTTP_ACCEPT_LANGUAGE'] = nil
15     assert_equal [], request.user_preferred_languages
16   end
17
18   def test_should_properly_split
19     assert_equal %w{en-US en-GB en}, request.user_preferred_languages
20   end
21
22   def test_should_ignore_jambled_header
23     request.env['HTTP_ACCEPT_LANGUAGE'] = 'odkhjf89fioma098jq .,.,'
24     assert_equal [], request.user_preferred_languages
25   end
26
27   def test_should_find_first_available_language
28     assert_equal 'en-GB', request.preferred_language_from(%w{en en-GB})
29   end
30
31   def test_should_find_first_compatible_language
32     assert_equal 'en-hk', request.compatible_language_from(%w{en-hk})
33     assert_equal 'en', request.compatible_language_from(%w{en})
34   end
35
36   def test_should_find_first_compatible_from_user_preferred
37     request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us,de-de'
38     assert_equal 'en', request.compatible_language_from(%w{de en})
39   end
40
41   private
42   def request
43     @request ||= MockedCgiRequest.new
44   end
45 end