File: spec_rack_locale.rb

package info (click to toggle)
ruby-rack-contrib 1.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 496 kB
  • ctags: 273
  • sloc: ruby: 3,036; makefile: 3
file content (59 lines) | stat: -rw-r--r-- 1,839 bytes parent folder | download
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
require 'minitest/autorun'
require 'rack/mock'

begin
  require './lib/rack/contrib/locale'

  describe "Rack::Locale" do

    before(:all) do
      # Set the locales that will be used at various points in the tests
      I18n.config.available_locales = [I18n.default_locale, :dk, :'en-gb', :es, :zh, :en]
    end

    def app
      @app ||= Rack::Builder.new do
        use Rack::Locale
        run lambda { |env| [ 200, {}, [ I18n.locale.to_s ] ] }
      end
    end

    def response_with_languages(accept_languages)
      Rack::MockRequest.new(app).get('/', { 'HTTP_ACCEPT_LANGUAGE' => accept_languages } )
    end

    specify 'should use I18n.default_locale if no languages are requested' do
      I18n.default_locale = :zh
      response_with_languages(nil).body.must_equal('zh')
    end

    specify 'should treat an empty qvalue as 1.0' do
      response_with_languages('en,es;q=0.95').body.must_equal('en')
    end

    specify 'should set the Content-Language response header' do
      headers = response_with_languages('de;q=0.7,dk;q=0.9').headers
      headers['Content-Language'].must_equal('dk')
    end

    specify 'should pick the language with the highest qvalue' do
      response_with_languages('en;q=0.9,es;q=0.95').body.must_equal('es')
    end

    specify 'should retain full language codes' do
      response_with_languages('en-gb,en-us;q=0.95;en').body.must_equal('en-gb')
    end

    specify 'should treat a * as "all other languages"' do
      response_with_languages('*,en;q=0.5').body.must_equal( I18n.default_locale.to_s )
    end

    specify 'should reset the I18n locale after the response' do
      I18n.locale = :es
      response_with_languages('en,de;q=0.8')
      I18n.locale.must_equal(:es)
    end
  end
rescue LoadError
  STDERR.puts "WARN: Skipping Rack::Locale tests (i18n not installed)"
end