wovnrb 3.8.0 → 3.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/wovnrb/store.rb CHANGED
@@ -2,6 +2,7 @@ require 'net/http'
2
2
  require 'uri'
3
3
  require 'cgi'
4
4
  require 'singleton'
5
+ require 'wovnrb/custom_domain/custom_domain_langs'
5
6
  require 'wovnrb/services/wovn_logger'
6
7
  require 'wovnrb/services/glob'
7
8
  require 'wovnrb/settings'
@@ -38,7 +39,8 @@ module Wovnrb
38
39
  'widget_url' => 'https://j.wovn.io/1',
39
40
  'wovn_dev_mode' => false,
40
41
  'compress_api_requests' => true,
41
- 'translate_canonical_tag' => true
42
+ 'translate_canonical_tag' => true,
43
+ 'custom_domain_langs' => {}
42
44
  )
43
45
  end
44
46
 
@@ -51,6 +53,7 @@ module Wovnrb
51
53
  # @return [nil]
52
54
  def reset
53
55
  @settings = Store.default_settings
56
+ @custom_domain_langs = nil
54
57
  # When Store is initialized, the Rails.configuration object is not yet initialized
55
58
  @config_loaded = false
56
59
  end
@@ -152,6 +155,8 @@ module Wovnrb
152
155
  @settings['url_pattern_reg'] = "((\\?.*&)|\\?)#{@settings['lang_param_name']}=(?<lang>[^&]+)(&|$)"
153
156
  when 'subdomain'
154
157
  @settings['url_pattern_reg'] = '^(?<lang>[^.]+)\.'
158
+ when 'custom_domain'
159
+ # Do not use regex
155
160
  end
156
161
 
157
162
  @settings['test_mode'] = !(@settings['test_mode'] != true || @settings['test_mode'] != 'on')
@@ -200,6 +205,10 @@ module Wovnrb
200
205
  @settings['url_pattern']
201
206
  end
202
207
 
208
+ def custom_domain_langs
209
+ @custom_domain_langs ||= CustomDomainLangs.new(@settings['custom_domain_langs'])
210
+ end
211
+
203
212
  private
204
213
 
205
214
  def stringify_keys!(hash)
@@ -1,3 +1,4 @@
1
+ require 'wovnrb/custom_domain/custom_domain_lang_url_handler'
1
2
  require 'wovnrb/services/url'
2
3
 
3
4
  module Wovnrb
@@ -28,10 +29,11 @@ module Wovnrb
28
29
 
29
30
  # Removes language code from a URI component (path, hostname, query etc), and returns it.
30
31
  #
31
- # @param uri [String] original URI component
32
- # @param lang [String] language code
33
- # @return [String] removed URI
34
- def remove_lang_from_uri_component(uri, lang)
32
+ # @param uri [String] original URI component
33
+ # @param lang [String] language code
34
+ # @param headers [Header] headers
35
+ # @return [String] removed URI
36
+ def remove_lang_from_uri_component(uri, lang, headers = nil)
35
37
  lang_code = @store.settings['custom_lang_aliases'][lang] || lang
36
38
 
37
39
  return uri if lang_code.blank?
@@ -49,6 +51,25 @@ module Wovnrb
49
51
  # (/|$) 3: path or end-of-string
50
52
  lang_code_pattern = %r{^(.*://|//)?([^/]*/)?#{lang_code}(/|$)}
51
53
  uri.sub(lang_code_pattern, '\1\2')
54
+ when 'custom_domain'
55
+ custom_domain_langs = @store.custom_domain_langs
56
+ custom_domain_lang_to_remove = custom_domain_langs.custom_domain_lang_by_lang(lang_code)
57
+ default_custom_domain_lang = custom_domain_langs.custom_domain_lang_by_lang(@store.default_lang)
58
+ new_uri = uri
59
+ if Wovnrb::URL.absolute_url?(uri)
60
+ new_uri = CustomDomainLangUrlHandler.change_to_new_custom_domain_lang(uri, custom_domain_lang_to_remove, default_custom_domain_lang)
61
+ elsif Wovnrb::URL.absolute_path?(uri)
62
+ absolute_url = "#{headers.protocol}://#{headers.unmasked_host}#{uri}"
63
+ absolute_url_with_lang = CustomDomainLangUrlHandler.change_to_new_custom_domain_lang(absolute_url, custom_domain_lang_to_remove, default_custom_domain_lang)
64
+ segments = make_segments_from_absolute_url(absolute_url_with_lang)
65
+ new_uri = segments['others']
66
+ elsif uri == custom_domain_lang_to_remove.host
67
+ absolute_url = "#{headers.protocol}://#{uri}#{headers.unmasked_pathname}"
68
+ absolute_url_with_lang = CustomDomainLangUrlHandler.change_to_new_custom_domain_lang(absolute_url, custom_domain_lang_to_remove, default_custom_domain_lang)
69
+ segments = make_segments_from_absolute_url(absolute_url_with_lang)
70
+ new_uri = segments['host']
71
+ end
72
+ new_uri
52
73
  else
53
74
  raise RuntimeError("Invalid URL pattern: #{@store.settings['url_pattern']}")
54
75
  end
@@ -80,6 +101,8 @@ module Wovnrb
80
101
  end
81
102
  when 'query'
82
103
  add_query_lang_code(href, code_to_add)
104
+ when 'custom_domain'
105
+ CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url(href, code_to_add, @store.custom_domain_langs)
83
106
  else # path
84
107
  href_uri.path = add_lang_code_for_path(href_uri.path, code_to_add, headers)
85
108
  href_uri.to_s
@@ -105,6 +128,9 @@ module Wovnrb
105
128
  "#{headers.protocol}://#{code_to_add.downcase}.#{headers.host}#{abs_path}"
106
129
  when 'query'
107
130
  add_query_lang_code(href, code_to_add)
131
+ when 'custom_domain'
132
+ absolute_url = "#{headers.protocol}://#{headers.host}#{abs_path}"
133
+ CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url(absolute_url, code_to_add, @store.custom_domain_langs)
108
134
  else # path
109
135
  add_lang_code_for_path(href, code_to_add, headers)
110
136
  end
@@ -148,5 +174,19 @@ module Wovnrb
148
174
  def build_lang_path(lang_code)
149
175
  lang_code.blank? ? '' : URL.prepend_path_slash(lang_code)
150
176
  end
177
+
178
+ def make_segments_from_absolute_url(absolute_uri)
179
+ # 1: schema (optional) like https://
180
+ # 2: host (optional) like wovn.io
181
+ # 3: path with query or hash
182
+ regex = %r{^(.*://|//)?([^/?]*)?((?:/|\?|#).*)?$}
183
+ matches = regex.match(absolute_uri)
184
+
185
+ {
186
+ 'schema' => matches[1],
187
+ 'host' => matches[2],
188
+ 'others' => matches[3]
189
+ }.transform_values(&:to_s)
190
+ end
151
191
  end
152
192
  end
@@ -1,3 +1,3 @@
1
1
  module Wovnrb
2
- VERSION = '3.8.0'.freeze
2
+ VERSION = '3.9.0'.freeze
3
3
  end
data/lib/wovnrb.rb CHANGED
@@ -36,7 +36,7 @@ module Wovnrb
36
36
  return @app.call(env) if @store.settings['test_mode'] && @store.settings['test_url'] != headers.url
37
37
 
38
38
  # redirect if the path is set to the default language (for SEO purposes)
39
- if headers.path_lang == default_lang
39
+ if explicit_default_lang?(headers)
40
40
  redirect_headers = headers.redirect(default_lang)
41
41
  return [307, redirect_headers, ['']]
42
42
  end
@@ -51,7 +51,7 @@ module Wovnrb
51
51
  status, res_headers, body = @app.call(headers.request_out)
52
52
 
53
53
  # disabled by next Rack middleware
54
- return output(headers, status, res_headers, body) unless /html/.match?(res_headers['Content-Type'])
54
+ return output(headers, status, res_headers, body) unless res_headers['Content-Type']&.include?('html')
55
55
 
56
56
  request = Rack::Request.new(env)
57
57
 
@@ -129,5 +129,10 @@ module Wovnrb
129
129
 
130
130
  !!(html_attributes['amp'] || html_attributes["\u26A1"])
131
131
  end
132
+
133
+ def explicit_default_lang?(headers)
134
+ default_lang, url_pattern = @store.settings.values_at('default_lang', 'url_pattern')
135
+ default_lang == headers.url_language && url_pattern != 'custom_domain'
136
+ end
132
137
  end
133
138
  end
@@ -0,0 +1,85 @@
1
+ require 'test_helper'
2
+ require 'wovnrb/custom_domain/custom_domain_lang'
3
+
4
+ module Wovnrb
5
+ class CustomDomainLangTest < WovnMiniTest
6
+ def setup
7
+ @custom_domain_root_path = CustomDomainLang.new('foo.com', '/', 'fr')
8
+ @custom_domain_with_path_no_trailing_slash = CustomDomainLang.new('foo.com', '/path', 'fr')
9
+ @custom_domain_with_path_trailing_slash = CustomDomainLang.new('foo.com', '/path/', 'fr')
10
+ @custom_domain_path_encoded_spaces = CustomDomainLang.new('foo.com', '/dir%20path', 'fr')
11
+ end
12
+
13
+ def test_custom_domain_lang_params
14
+ assert_equal('foo.com', @custom_domain_root_path.host)
15
+ assert_equal('/', @custom_domain_root_path.path)
16
+ assert_equal('fr', @custom_domain_root_path.lang)
17
+ assert_equal('foo.com', @custom_domain_root_path.host_and_path_without_trailing_slash)
18
+
19
+ assert_equal('foo.com', @custom_domain_with_path_no_trailing_slash.host)
20
+ assert_equal('/path/', @custom_domain_with_path_no_trailing_slash.path)
21
+ assert_equal('fr', @custom_domain_with_path_no_trailing_slash.lang)
22
+ assert_equal('foo.com/path', @custom_domain_with_path_no_trailing_slash.host_and_path_without_trailing_slash)
23
+
24
+ assert_equal('foo.com', @custom_domain_with_path_trailing_slash.host)
25
+ assert_equal('/path/', @custom_domain_with_path_trailing_slash.path)
26
+ assert_equal('fr', @custom_domain_with_path_trailing_slash.lang)
27
+ assert_equal('foo.com/path', @custom_domain_with_path_trailing_slash.host_and_path_without_trailing_slash)
28
+
29
+ assert_equal('foo.com', @custom_domain_path_encoded_spaces.host)
30
+ assert_equal('/dir%20path/', @custom_domain_path_encoded_spaces.path)
31
+ assert_equal('fr', @custom_domain_path_encoded_spaces.lang)
32
+ assert_equal('foo.com/dir%20path', @custom_domain_path_encoded_spaces.host_and_path_without_trailing_slash)
33
+ end
34
+
35
+ def test_is_match_with_different_domain
36
+ refute(@custom_domain_root_path.match?(Addressable::URI.parse('http://otherdomain.com/other/test.html')))
37
+ end
38
+
39
+ def test_is_match_with_different_port_number_should_be_ignored
40
+ assert(@custom_domain_root_path.match?(Addressable::URI.parse('http://foo.com:3000/other/test.html')))
41
+ assert(@custom_domain_root_path.match?(Addressable::URI.parse('http://foo.com:80/other/test.html')))
42
+ assert(@custom_domain_root_path.match?(Addressable::URI.parse('http://foo.com/other/test.html')))
43
+ end
44
+
45
+ def test_is_match_with_domain_containing_substring_should_be_false
46
+ refute(@custom_domain_root_path.match?(Addressable::URI.parse('http://en.foo.com/other/test.html')))
47
+ end
48
+
49
+ def test_is_match_with_same_domain_should_be_true
50
+ assert(@custom_domain_root_path.match?(Addressable::URI.parse('http://foo.com/other/test.html')))
51
+ end
52
+
53
+ def test_is_match_with_same_domain_different_casing_should_be_true
54
+ assert(@custom_domain_root_path.match?(Addressable::URI.parse('http://foo.com/other/test.html')))
55
+ end
56
+
57
+ def test_is_match_with_path_starts_with_custom_path_should_be_true
58
+ assert(@custom_domain_root_path.match?(Addressable::URI.parse('http://foo.com')))
59
+ assert(@custom_domain_root_path.match?(Addressable::URI.parse('http://foo.com/')))
60
+ assert(@custom_domain_root_path.match?(Addressable::URI.parse('http://foo.com/other/test.html?foo=bar')))
61
+
62
+ assert(@custom_domain_with_path_no_trailing_slash.match?(Addressable::URI.parse('http://foo.com/path')))
63
+ assert(@custom_domain_with_path_no_trailing_slash.match?(Addressable::URI.parse('http://foo.com/path/')))
64
+ assert(@custom_domain_with_path_no_trailing_slash.match?(Addressable::URI.parse('http://foo.com/path/other/test.html?foo=bar')))
65
+
66
+ assert(@custom_domain_with_path_trailing_slash.match?(Addressable::URI.parse('http://foo.com/path')))
67
+ assert(@custom_domain_with_path_trailing_slash.match?(Addressable::URI.parse('http://foo.com/path/')))
68
+ assert(@custom_domain_with_path_trailing_slash.match?(Addressable::URI.parse('http://foo.com/path/other/test.html?foo=bar')))
69
+
70
+ assert(@custom_domain_path_encoded_spaces.match?(Addressable::URI.parse('http://foo.com/dir%20path')))
71
+ assert(@custom_domain_path_encoded_spaces.match?(Addressable::URI.parse('http://foo.com/dir%20path?foo=bar')))
72
+ end
73
+
74
+ def test_is_match_with_path_matches_substring_should_be_false
75
+ refute(@custom_domain_with_path_no_trailing_slash.match?(Addressable::URI.parse('http://foo.com/pathsuffix/other/test.html')))
76
+ refute(@custom_domain_with_path_trailing_slash.match?(Addressable::URI.parse('http://foo.com/pathsuffix/other/test.html')))
77
+ refute(@custom_domain_path_encoded_spaces.match?(Addressable::URI.parse('http://foo.com/dir%20pathsuffix/other/test.html')))
78
+ end
79
+
80
+ def test_is_match_with_path_matches_custom_path_as_suffix_should_be_false
81
+ refute(@custom_domain_with_path_no_trailing_slash.match?(Addressable::URI.parse('http://foo.com/images/path/foo.png')))
82
+ refute(@custom_domain_with_path_trailing_slash.match?(Addressable::URI.parse('http://foo.com/images/path/foo.png')))
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,75 @@
1
+ require 'test_helper'
2
+ require 'wovnrb/custom_domain/custom_domain_langs'
3
+ require 'wovnrb/custom_domain/custom_domain_lang_url_handler'
4
+
5
+ module Wovnrb
6
+ class CustomDomainLangUrlHandlerTest < WovnMiniTest
7
+ def setup
8
+ custom_domain_langs_setting = {
9
+ 'fr' => { 'url' => 'foo.com/' },
10
+ 'ja' => { 'url' => 'foo.com/path' },
11
+ 'zh-CHS' => { 'url' => 'foo.com/dir/path' },
12
+ 'en' => { 'url' => 'english.foo.com/' },
13
+ 'zh-Hant-HK' => { 'url' => 'zh-hant-hk.foo.com/zh' },
14
+ 'pt' => { 'url' => '17797-trial2.foo.com/' }
15
+ }
16
+ @custom_domain_langs = CustomDomainLangs.new(custom_domain_langs_setting)
17
+ end
18
+
19
+ def test_add_custom_domain_lang_to_absolute_url
20
+ # apply to original lang
21
+ assert_equal('foo.com', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('foo.com', 'fr', @custom_domain_langs))
22
+ assert_equal('foo.com/path', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('foo.com', 'ja', @custom_domain_langs))
23
+ assert_equal('foo.com/dir/path', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('foo.com', 'zh-CHS', @custom_domain_langs))
24
+ assert_equal('english.foo.com', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('foo.com', 'en', @custom_domain_langs))
25
+ assert_equal('zh-hant-hk.foo.com/zh', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('foo.com', 'zh-Hant-HK', @custom_domain_langs))
26
+
27
+ # apply to target lang
28
+ assert_equal('foo.com', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('zh-hant-hk.foo.com/zh', 'fr', @custom_domain_langs))
29
+ assert_equal('foo.com/path', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('zh-hant-hk.foo.com/zh', 'ja', @custom_domain_langs))
30
+ assert_equal('foo.com/dir/path', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('zh-hant-hk.foo.com/zh', 'zh-CHS', @custom_domain_langs))
31
+ assert_equal('english.foo.com', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('zh-hant-hk.foo.com/zh', 'en', @custom_domain_langs))
32
+ assert_equal('zh-hant-hk.foo.com/zh', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('zh-hant-hk.foo.com/zh', 'zh-Hant-HK', @custom_domain_langs))
33
+
34
+ assert_equal('zh-hant-hk.foo.com/zh', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('foo.com/path', 'zh-Hant-HK', @custom_domain_langs))
35
+ assert_equal('zh-hant-hk.foo.com/zh/', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('foo.com/path/', 'zh-Hant-HK', @custom_domain_langs))
36
+ assert_equal('zh-hant-hk.foo.com/zh/index.html', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('foo.com/path/index.html', 'zh-Hant-HK', @custom_domain_langs))
37
+ assert_equal('zh-hant-hk.foo.com/zh/path2/index.html', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('foo.com/path/path2/index.html', 'zh-Hant-HK', @custom_domain_langs))
38
+ assert_equal('zh-hant-hk.foo.com/zh/path2/index.html?test=1', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('foo.com/path/path2/index.html?test=1', 'zh-Hant-HK', @custom_domain_langs))
39
+ assert_equal('zh-hant-hk.foo.com/zh/path2/index.html#hash', CustomDomainLangUrlHandler.add_custom_domain_lang_to_absolute_url('foo.com/path/path2/index.html#hash', 'zh-Hant-HK', @custom_domain_langs))
40
+ end
41
+
42
+ def test_change_to_new_custom_domain_lang
43
+ fr = @custom_domain_langs.custom_domain_lang_by_lang('fr')
44
+ ja = @custom_domain_langs.custom_domain_lang_by_lang('ja')
45
+ zh_chs = @custom_domain_langs.custom_domain_lang_by_lang('zh-CHS')
46
+ en = @custom_domain_langs.custom_domain_lang_by_lang('en')
47
+ zh_hant_hk = @custom_domain_langs.custom_domain_lang_by_lang('zh-Hant-HK')
48
+ pt = @custom_domain_langs.custom_domain_lang_by_lang('pt')
49
+
50
+ assert_equal('foo.com', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('foo.com', fr, fr))
51
+ assert_equal('foo.com/path', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('foo.com', fr, ja))
52
+ assert_equal('foo.com/dir/path', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('foo.com', fr, zh_chs))
53
+ assert_equal('english.foo.com', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('foo.com', fr, en))
54
+ assert_equal('zh-hant-hk.foo.com/zh', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('foo.com', fr, zh_hant_hk))
55
+
56
+ assert_equal('foo.com', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('zh-hant-hk.foo.com/zh', zh_hant_hk, fr))
57
+ assert_equal('foo.com/path', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('zh-hant-hk.foo.com/zh', zh_hant_hk, ja))
58
+ assert_equal('foo.com/dir/path', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('zh-hant-hk.foo.com/zh', zh_hant_hk, zh_chs))
59
+ assert_equal('english.foo.com', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('zh-hant-hk.foo.com/zh', zh_hant_hk, en))
60
+ assert_equal('zh-hant-hk.foo.com/zh', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('zh-hant-hk.foo.com/zh', zh_hant_hk, zh_hant_hk))
61
+
62
+ assert_equal('foo.com/path', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('zh-hant-hk.foo.com/zh', zh_hant_hk, ja))
63
+ assert_equal('foo.com/path/', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('zh-hant-hk.foo.com/zh/', zh_hant_hk, ja))
64
+ assert_equal('foo.com/path/index.html', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('zh-hant-hk.foo.com/zh/index.html', zh_hant_hk, ja))
65
+ assert_equal('foo.com/path/path', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('zh-hant-hk.foo.com/zh/path', zh_hant_hk, ja))
66
+ assert_equal('foo.com/path/path/index.html', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('zh-hant-hk.foo.com/zh/path/index.html', zh_hant_hk, ja))
67
+ assert_equal('foo.com/path/path/index.html?test=1', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('zh-hant-hk.foo.com/zh/path/index.html?test=1', zh_hant_hk, ja))
68
+ assert_equal('foo.com/path/path/index.html#hash', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('zh-hant-hk.foo.com/zh/path/index.html#hash', zh_hant_hk, ja))
69
+
70
+ assert_equal('zh-hant-hk.foo.com/zhtrap', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('zh-hant-hk.foo.com/zhtrap', zh_hant_hk, ja))
71
+ assert_equal('english.foo.com/zhtrap', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('17797-trial2.foo.com/zhtrap', pt, en))
72
+ assert_equal('17797-trial2.foo.com/zhtrap', CustomDomainLangUrlHandler.change_to_new_custom_domain_lang('17797-trial2.foo.com/zhtrap', pt, pt))
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,82 @@
1
+ require 'test_helper'
2
+ require 'wovnrb/custom_domain/custom_domain_langs'
3
+
4
+ module Wovnrb
5
+ class CustomDomainLangsTest < WovnMiniTest
6
+ def setup
7
+ custom_domain_langs_setting = {
8
+ 'fr' => { 'url' => 'foo.com/' },
9
+ 'ja' => { 'url' => 'foo.com/path', 'source' => 'japan.foo.com/' },
10
+ 'zh-CHS' => { 'url' => 'foo.com/dir/path' },
11
+ 'en' => { 'url' => 'english.foo.com/', 'source' => 'global.foo.com/' }
12
+ }
13
+ @custom_domain_langs = CustomDomainLangs.new(custom_domain_langs_setting)
14
+ end
15
+
16
+ def test_get_custom_domain_lang_by_lang
17
+ assert_nil(@custom_domain_langs.custom_domain_lang_by_lang('unknown'))
18
+
19
+ assert_equal('fr', lang_for(@custom_domain_langs.custom_domain_lang_by_lang('fr')))
20
+ assert_equal('ja', lang_for(@custom_domain_langs.custom_domain_lang_by_lang('ja')))
21
+ assert_equal('zh-CHS', lang_for(@custom_domain_langs.custom_domain_lang_by_lang('zh-CHS')))
22
+ assert_equal('en', lang_for(@custom_domain_langs.custom_domain_lang_by_lang('en')))
23
+ end
24
+
25
+ def test_get_custom_domain_lang_by_url
26
+ assert_nil(@custom_domain_langs.custom_domain_lang_by_url('http://otherdomain.com'))
27
+ assert_nil(@custom_domain_langs.custom_domain_lang_by_url('http://otherdomain.com/path/test.html'))
28
+ assert_nil(@custom_domain_langs.custom_domain_lang_by_url('http://otherdomain.com/dir/path/test.html'))
29
+
30
+ assert_equal('fr', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com')))
31
+ assert_equal('fr', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/')))
32
+ assert_equal('fr', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/test.html')))
33
+
34
+ assert_equal('ja', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/path')))
35
+ assert_equal('ja', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/path/')))
36
+ assert_equal('ja', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/path/dir')))
37
+ assert_equal('ja', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/path/test.html')))
38
+
39
+ assert_equal('zh-CHS', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/dir/path')))
40
+ assert_equal('zh-CHS', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/dir/path/')))
41
+ assert_equal('zh-CHS', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/dir/path/dir')))
42
+ assert_equal('zh-CHS', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/dir/path/test.html')))
43
+
44
+ assert_equal('en', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://english.foo.com/dir/path')))
45
+ assert_equal('en', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://english.foo.com/dir/path/')))
46
+ assert_equal('en', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://english.foo.com/dir/path/test.html')))
47
+ end
48
+
49
+ def test_get_custom_domain_lang_by_url_with_nested_paths
50
+ custom_domain_langs_setting = {
51
+ 'ja' => { 'url' => 'foo.com/path' },
52
+ 'en' => { 'url' => 'foo.com/path/en' },
53
+ 'fr' => { 'url' => 'foo.com/path/fr' }
54
+ }
55
+ custom_domain_langs = CustomDomainLangs.new(custom_domain_langs_setting)
56
+ assert_equal('ja', lang_for(custom_domain_langs.custom_domain_lang_by_url('http://foo.com/path')))
57
+ assert_equal('en', lang_for(custom_domain_langs.custom_domain_lang_by_url('http://foo.com/path/en')))
58
+ assert_equal('fr', lang_for(custom_domain_langs.custom_domain_lang_by_url('http://foo.com/path/fr')))
59
+ end
60
+
61
+ def test_to_html_swapper_hash
62
+ expected = {
63
+ 'foo.com' => 'fr',
64
+ 'foo.com/path' => 'ja',
65
+ 'foo.com/dir/path' => 'zh-CHS',
66
+ 'english.foo.com' => 'en'
67
+ }
68
+
69
+ assert(hash_equals(expected, @custom_domain_langs.to_html_swapper_hash))
70
+ end
71
+
72
+ private
73
+
74
+ def lang_for(custom_domain_lang)
75
+ custom_domain_lang.lang
76
+ end
77
+
78
+ def hash_equals(orig_hash, test_hash)
79
+ (orig_hash <=> test_hash) == 0
80
+ end
81
+ end
82
+ end