1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/test/test_web_helpers.rb
Johan Lundström 3b96430046 Improve parsing of HTTP_ACCEPT_LANGUAGE (continue Nate's work) (#3449)
* Add test case for sv locale

* Use Rack::Utils to parse locale header

* Take "q" value into account

* Make '*' match the default locale.

* Add test for available_locales

* Correct test case sv -> en

* Add missing test cases for Safari requests

* Add missing require needed to run a single test file

* Reimplement WebHelpers#locale to handle regions in header

Implementation inspired by:

https://github.com/iain/http_accept_language/blob/master/lib/http_accept_language/parser.rb

Also see:

https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4

* Add docs and references

* Add failing test cases for pt-br, pt-pt, pt (examples taken from Chrome & Safari)

* Add more test cases for Mac + Chrome + UK English + US English

* Make test cases for 'pt-PT,pt;q=0.8,en-US;q=0.6,en;q=0.4' and 'pt-pt' pass

* Make special case 'ru,en' work (equal qvalues)
2017-05-01 10:47:51 -07:00

98 lines
2.4 KiB
Ruby

# frozen_string_literal: true
require_relative 'helper'
require 'sidekiq/web'
class TestWebHelpers < Sidekiq::Test
class Helpers
include Sidekiq::WebHelpers
def initialize(params={})
@thehash = default.merge(params)
end
def request
self
end
def settings
self
end
def locales
['web/locales']
end
def env
@thehash
end
def default
{
}
end
end
def test_locale_determination
obj = Helpers.new
assert_equal 'en', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ru;q=0.2')
assert_equal 'fr', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4,ru;q=0.2')
assert_equal 'zh-cn', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'en-US,sv-SE;q=0.8,sv;q=0.6,en;q=0.4')
assert_equal 'en', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'nb-NO,nb;q=0.2')
assert_equal 'nb', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'en-us')
assert_equal 'en', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'sv-se')
assert_equal 'sv', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4')
assert_equal 'pt-br', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'pt-PT,pt;q=0.8,en-US;q=0.6,en;q=0.4')
assert_equal 'pt', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'pt-br')
assert_equal 'pt-br', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'pt-pt')
assert_equal 'pt', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'pt')
assert_equal 'pt', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'en-us; *')
assert_equal 'en', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8')
assert_equal 'en', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'en-GB,en-US;q=0.8,en;q=0.6')
assert_equal 'en', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'ru,en')
assert_equal 'ru', obj.locale
obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => '*')
assert_equal 'en', obj.locale
end
def test_available_locales
obj = Helpers.new
expected = %w(
ar cs da de el en es fa fr he hi it ja
ko nb nl pl pt-br pt ru sv ta uk ur
zh-cn zh-tw
)
assert_equal expected, obj.available_locales
end
end