wovnrb 3.8.0 → 3.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,215 +1,217 @@
1
- require 'test_helper'
2
-
3
- module Wovnrb
4
- REQUEST_UUID = 'ABCD'.freeze
5
-
6
- class ApiTranslatorTest < WovnMiniTest
7
- def test_translate
8
- assert_translation('test.html', 'test_translated.html', true)
9
- end
10
-
11
- def test_translate_falls_back_to_original_body_if_exception
12
- Net::HTTP.any_instance.expects(:request).raises
13
- assert_translation('test.html', 'test_translated.html', false, response: nil)
14
- end
15
-
16
- def test_translate_falls_back_to_original_body_if_api_error
17
- assert_translation('test.html', 'test_translated.html', false, response: { encoding: 'text/json', status_code: 500 })
18
- end
19
-
20
- def test_translate_continues_if_api_response_is_not_compressed
21
- assert_translation('test.html', 'test_translated.html', true, response: { encoding: 'unknown', status: 200 }, compress_data: false)
22
- end
23
-
24
- def test_translate_continues_if_api_response_is_compressed
25
- assert_translation('test.html', 'test_translated.html', true, response: { encoding: 'unknown', status: 200 })
26
- end
27
-
28
- def test_translate_accepts_uncompressed_response_from_api_in_dev_mode
29
- Wovnrb::Store.instance.update_settings('wovn_dev_mode' => true)
30
- assert_translation('test.html', 'test_translated.html', true, response: { encoding: 'text/json', status: 200 }, compress_data: false)
31
- end
32
-
33
- def test_translate_without_api_compression_sends_json
34
- Wovnrb::Store.instance.update_settings('compress_api_requests' => false)
35
- sut, _store, _headers = create_sut
36
- html_body = 'foo'
37
-
38
- stub_request(:post, %r{http://wovn\.global\.ssl\.fastly\.net/v0/translation\?cache_key=.*})
39
- .to_return(status: 200, body: { 'body' => 'translated_body' }.to_json)
40
-
41
- sut.translate(html_body)
42
-
43
- assert_requested :post, %r{http://wovn\.global\.ssl\.fastly\.net/v0/translation\?cache_key=.*},
44
- headers: {
45
- 'Accept' => '*/*',
46
- 'Accept-Encoding' => 'gzip',
47
- 'Content-Type' => 'application/json',
48
- 'User-Agent' => 'Ruby',
49
- 'X-Request-Id' => REQUEST_UUID
50
- },
51
- body: {
52
- 'url' => 'http://wovn.io/test',
53
- 'token' => '123456',
54
- 'lang_code' => 'fr',
55
- 'url_pattern' => 'subdomain',
56
- 'lang_param_name' => 'lang',
57
- 'translate_canonical_tag' => true,
58
- 'product' => 'WOVN.rb',
59
- 'version' => VERSION,
60
- 'body' => 'foo',
61
- 'custom_lang_aliases' => { 'ja' => 'Japanese' }.to_json
62
- }.to_json,
63
- times: 1
64
- end
65
-
66
- def test_api_timeout_is_search_engine_user_higher_default
67
- settings = {
68
- 'project_token' => '123456',
69
- 'custom_lang_aliases' => { 'ja' => 'Japanese' },
70
- 'default_lang' => 'en',
71
- 'url_pattern' => 'subdomain',
72
- 'url_pattern_reg' => '^(?<lang>[^.]+)\.',
73
- 'lang_param_name' => 'lang'
74
- }
75
- store = Wovnrb::Store.instance
76
- store.update_settings(settings)
77
- headers = Wovnrb::Headers.new(
78
- Wovnrb.get_env('url' => 'http://fr.wovn.io/test', 'HTTP_USER_AGENT' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'),
79
- Wovnrb.get_settings(settings),
80
- UrlLanguageSwitcher.new(store)
81
- )
82
- api_translator = ApiTranslator.new(store, headers, REQUEST_UUID)
83
- assert_equal(5.0, api_translator.send(:api_timeout))
84
- end
85
-
86
- def test_api_timeout_no_user_agent_use_normal_default
87
- settings = {
88
- 'project_token' => '123456',
89
- 'custom_lang_aliases' => { 'ja' => 'Japanese' },
90
- 'default_lang' => 'en',
91
- 'url_pattern' => 'subdomain',
92
- 'url_pattern_reg' => '^(?<lang>[^.]+)\.',
93
- 'lang_param_name' => 'lang'
94
- }
95
- store = Wovnrb::Store.instance
96
- store.update_settings(settings)
97
- env = Wovnrb.get_env('url' => 'http://fr.wovn.io/test')
98
- env.delete('HTTP_USER_AGENT')
99
- headers = Wovnrb::Headers.new(
100
- env,
101
- Wovnrb.get_settings(settings),
102
- UrlLanguageSwitcher.new(store)
103
- )
104
- api_translator = ApiTranslator.new(store, headers, REQUEST_UUID)
105
- assert_equal(1.0, api_translator.send(:api_timeout))
106
- end
107
-
108
- private
109
-
110
- def assert_translation(original_html_fixture, translated_html_fixture, success_expected, response: { encoding: 'gzip', status_code: 200 }, compress_data: true)
111
- original_html = File.read("test/fixtures/html/#{original_html_fixture}")
112
- translated_html = File.read("test/fixtures/html/#{translated_html_fixture}")
113
- actual_translated_html = translate(original_html, translated_html, response, compress_data: compress_data)
114
-
115
- if success_expected
116
- assert_equal(actual_translated_html, translated_html)
117
- else
118
- assert_equal(actual_translated_html, original_html)
119
- end
120
- end
121
-
122
- def translate(original_html, translated_html, response, compress_data: true)
123
- api_translator, store, _headers = create_sut
124
- translation_request_stub = stub_translation_api_request(store, original_html, translated_html, response, compress_data: compress_data)
125
-
126
- expected_api_timeout = store.settings['api_timeout_seconds']
127
- assert_equal(expected_api_timeout, api_translator.send(:api_timeout))
128
- actual_translated_html = api_translator.translate(original_html)
129
- assert_requested(translation_request_stub, times: 1) if translation_request_stub
130
- actual_translated_html
131
- end
132
-
133
- def create_sut
134
- settings = {
135
- 'project_token' => '123456',
136
- 'custom_lang_aliases' => { 'ja' => 'Japanese' },
137
- 'default_lang' => 'en',
138
- 'url_pattern' => 'subdomain',
139
- 'url_pattern_reg' => '^(?<lang>[^.]+)\.',
140
- 'lang_param_name' => 'lang'
141
- }
142
- store = Wovnrb::Store.instance
143
- store.update_settings(settings)
144
- headers = Wovnrb::Headers.new(
145
- Wovnrb.get_env('url' => 'http://fr.wovn.io/test'),
146
- Wovnrb.get_settings(settings),
147
- UrlLanguageSwitcher.new(store)
148
- )
149
- api_translator = ApiTranslator.new(store, headers, REQUEST_UUID)
150
-
151
- [api_translator, store, headers]
152
- end
153
-
154
- def stub_translation_api_request(store, original_html, translated_html, response, compress_data: true)
155
- if response
156
- cache_key = generate_cache_key(store, original_html)
157
- api_host = if store.dev_mode?
158
- 'dev-wovn.io:3001'
159
- else
160
- 'wovn.global.ssl.fastly.net'
161
- end
162
- api_url = "http://#{api_host}/v0/translation?cache_key=#{cache_key}"
163
- compressed_data = compress(generate_data(original_html))
164
- headers = {
165
- 'Accept' => '*/*',
166
- 'Accept-Encoding' => 'gzip',
167
- 'Content-Length' => compressed_data.bytesize,
168
- 'Content-Type' => 'application/json',
169
- 'Content-Encoding' => 'gzip',
170
- 'User-Agent' => 'Ruby'
171
- }
172
- stub_response_json = "{\"body\":\"#{translated_html.gsub("\n", '\n')}\"}"
173
- stub_response = if compress_data
174
- compress(stub_response_json)
175
- else
176
- stub_response_json
177
- end
178
- response_headers = { 'Content-Encoding' => response[:encoding] || 'gzip' }
179
- stub_request(:post, api_url)
180
- .with(body: compressed_data, headers: headers)
181
- .to_return(status: response[:status_code] || 200, body: stub_response, headers: response_headers)
182
-
183
- end
184
- end
185
-
186
- def generate_cache_key(store, original_html)
187
- settings_hash = Digest::MD5.hexdigest(JSON.dump(store.settings))
188
- body_hash = Digest::MD5.hexdigest(original_html)
189
- escaped_key = CGI.escape("token=123456&settings_hash=#{settings_hash}&body_hash=#{body_hash}&path=/test&lang=fr&version=wovnrb_#{VERSION}")
190
-
191
- "(#{escaped_key})"
192
- end
193
-
194
- def generate_data(original_html)
195
- data = {
196
- 'url' => 'http://wovn.io/test',
197
- 'token' => '123456',
198
- 'lang_code' => 'fr',
199
- 'url_pattern' => 'subdomain',
200
- 'lang_param_name' => 'lang',
201
- 'translate_canonical_tag' => true,
202
- 'product' => 'WOVN.rb',
203
- 'version' => VERSION,
204
- 'body' => original_html,
205
- 'custom_lang_aliases' => '{"ja":"Japanese"}'
206
- }
207
-
208
- data.to_json
209
- end
210
-
211
- def compress(string)
212
- ActiveSupport::Gzip.compress(string)
213
- end
214
- end
215
- end
1
+ require 'test_helper'
2
+
3
+ module Wovnrb
4
+ REQUEST_UUID = 'ABCD'.freeze
5
+
6
+ class ApiTranslatorTest < WovnMiniTest
7
+ def test_translate
8
+ assert_translation('test.html', 'test_translated.html', true)
9
+ end
10
+
11
+ def test_translate_falls_back_to_original_body_if_exception
12
+ Net::HTTP.any_instance.expects(:request).raises
13
+ assert_translation('test.html', 'test_translated.html', false, response: nil)
14
+ end
15
+
16
+ def test_translate_falls_back_to_original_body_if_api_error
17
+ assert_translation('test.html', 'test_translated.html', false, response: { encoding: 'text/json', status_code: 500 })
18
+ end
19
+
20
+ def test_translate_continues_if_api_response_is_not_compressed
21
+ assert_translation('test.html', 'test_translated.html', true, response: { encoding: 'unknown', status: 200 }, compress_data: false)
22
+ end
23
+
24
+ def test_translate_continues_if_api_response_is_compressed
25
+ assert_translation('test.html', 'test_translated.html', true, response: { encoding: 'unknown', status: 200 })
26
+ end
27
+
28
+ def test_translate_accepts_uncompressed_response_from_api_in_dev_mode
29
+ Wovnrb::Store.instance.update_settings('wovn_dev_mode' => true)
30
+ assert_translation('test.html', 'test_translated.html', true, response: { encoding: 'text/json', status: 200 }, compress_data: false)
31
+ end
32
+
33
+ def test_translate_without_api_compression_sends_json
34
+ Wovnrb::Store.instance.update_settings('compress_api_requests' => false)
35
+ sut, _store, _headers = create_sut
36
+ html_body = 'foo'
37
+
38
+ stub_request(:post, %r{http://wovn\.global\.ssl\.fastly\.net/v0/translation\?cache_key=.*})
39
+ .to_return(status: 200, body: { 'body' => 'translated_body' }.to_json)
40
+
41
+ sut.translate(html_body)
42
+
43
+ assert_requested :post, %r{http://wovn\.global\.ssl\.fastly\.net/v0/translation\?cache_key=.*},
44
+ headers: {
45
+ 'Accept' => '*/*',
46
+ 'Accept-Encoding' => 'gzip',
47
+ 'Content-Type' => 'application/json',
48
+ 'User-Agent' => 'Ruby',
49
+ 'X-Request-Id' => REQUEST_UUID
50
+ },
51
+ body: {
52
+ 'url' => 'http://wovn.io/test',
53
+ 'token' => '123456',
54
+ 'lang_code' => 'fr',
55
+ 'url_pattern' => 'subdomain',
56
+ 'lang_param_name' => 'lang',
57
+ 'translate_canonical_tag' => true,
58
+ 'insert_hreflangs' => true,
59
+ 'product' => 'WOVN.rb',
60
+ 'version' => VERSION,
61
+ 'body' => 'foo',
62
+ 'custom_lang_aliases' => { 'ja' => 'Japanese' }.to_json
63
+ }.to_json,
64
+ times: 1
65
+ end
66
+
67
+ def test_api_timeout_is_search_engine_user_higher_default
68
+ settings = {
69
+ 'project_token' => '123456',
70
+ 'custom_lang_aliases' => { 'ja' => 'Japanese' },
71
+ 'default_lang' => 'en',
72
+ 'url_pattern' => 'subdomain',
73
+ 'url_pattern_reg' => '^(?<lang>[^.]+)\.',
74
+ 'lang_param_name' => 'lang'
75
+ }
76
+ store = Wovnrb::Store.instance
77
+ store.update_settings(settings)
78
+ headers = Wovnrb::Headers.new(
79
+ Wovnrb.get_env('url' => 'http://fr.wovn.io/test', 'HTTP_USER_AGENT' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'),
80
+ Wovnrb.get_settings(settings),
81
+ UrlLanguageSwitcher.new(store)
82
+ )
83
+ api_translator = ApiTranslator.new(store, headers, REQUEST_UUID)
84
+ assert_equal(5.0, api_translator.send(:api_timeout))
85
+ end
86
+
87
+ def test_api_timeout_no_user_agent_use_normal_default
88
+ settings = {
89
+ 'project_token' => '123456',
90
+ 'custom_lang_aliases' => { 'ja' => 'Japanese' },
91
+ 'default_lang' => 'en',
92
+ 'url_pattern' => 'subdomain',
93
+ 'url_pattern_reg' => '^(?<lang>[^.]+)\.',
94
+ 'lang_param_name' => 'lang'
95
+ }
96
+ store = Wovnrb::Store.instance
97
+ store.update_settings(settings)
98
+ env = Wovnrb.get_env('url' => 'http://fr.wovn.io/test')
99
+ env.delete('HTTP_USER_AGENT')
100
+ headers = Wovnrb::Headers.new(
101
+ env,
102
+ Wovnrb.get_settings(settings),
103
+ UrlLanguageSwitcher.new(store)
104
+ )
105
+ api_translator = ApiTranslator.new(store, headers, REQUEST_UUID)
106
+ assert_equal(1.0, api_translator.send(:api_timeout))
107
+ end
108
+
109
+ private
110
+
111
+ def assert_translation(original_html_fixture, translated_html_fixture, success_expected, response: { encoding: 'gzip', status_code: 200 }, compress_data: true)
112
+ original_html = File.read("test/fixtures/html/#{original_html_fixture}")
113
+ translated_html = File.read("test/fixtures/html/#{translated_html_fixture}")
114
+ actual_translated_html = translate(original_html, translated_html, response, compress_data: compress_data)
115
+
116
+ if success_expected
117
+ assert_equal(actual_translated_html, translated_html)
118
+ else
119
+ assert_equal(actual_translated_html, original_html)
120
+ end
121
+ end
122
+
123
+ def translate(original_html, translated_html, response, compress_data: true)
124
+ api_translator, store, _headers = create_sut
125
+ translation_request_stub = stub_translation_api_request(store, original_html, translated_html, response, compress_data: compress_data)
126
+
127
+ expected_api_timeout = store.settings['api_timeout_seconds']
128
+ assert_equal(expected_api_timeout, api_translator.send(:api_timeout))
129
+ actual_translated_html = api_translator.translate(original_html)
130
+ assert_requested(translation_request_stub, times: 1) if translation_request_stub
131
+ actual_translated_html
132
+ end
133
+
134
+ def create_sut
135
+ settings = {
136
+ 'project_token' => '123456',
137
+ 'custom_lang_aliases' => { 'ja' => 'Japanese' },
138
+ 'default_lang' => 'en',
139
+ 'url_pattern' => 'subdomain',
140
+ 'url_pattern_reg' => '^(?<lang>[^.]+)\.',
141
+ 'lang_param_name' => 'lang'
142
+ }
143
+ store = Wovnrb::Store.instance
144
+ store.update_settings(settings)
145
+ headers = Wovnrb::Headers.new(
146
+ Wovnrb.get_env('url' => 'http://fr.wovn.io/test'),
147
+ Wovnrb.get_settings(settings),
148
+ UrlLanguageSwitcher.new(store)
149
+ )
150
+ api_translator = ApiTranslator.new(store, headers, REQUEST_UUID)
151
+
152
+ [api_translator, store, headers]
153
+ end
154
+
155
+ def stub_translation_api_request(store, original_html, translated_html, response, compress_data: true)
156
+ if response
157
+ cache_key = generate_cache_key(store, original_html)
158
+ api_host = if store.dev_mode?
159
+ 'dev-wovn.io:3001'
160
+ else
161
+ 'wovn.global.ssl.fastly.net'
162
+ end
163
+ api_url = "http://#{api_host}/v0/translation?cache_key=#{cache_key}"
164
+ compressed_data = compress(generate_data(original_html))
165
+ headers = {
166
+ 'Accept' => '*/*',
167
+ 'Accept-Encoding' => 'gzip',
168
+ 'Content-Length' => compressed_data.bytesize,
169
+ 'Content-Type' => 'application/json',
170
+ 'Content-Encoding' => 'gzip',
171
+ 'User-Agent' => 'Ruby'
172
+ }
173
+ stub_response_json = "{\"body\":\"#{translated_html.gsub("\n", '\n')}\"}"
174
+ stub_response = if compress_data
175
+ compress(stub_response_json)
176
+ else
177
+ stub_response_json
178
+ end
179
+ response_headers = { 'Content-Encoding' => response[:encoding] || 'gzip' }
180
+ stub_request(:post, api_url)
181
+ .with(body: compressed_data, headers: headers)
182
+ .to_return(status: response[:status_code] || 200, body: stub_response, headers: response_headers)
183
+
184
+ end
185
+ end
186
+
187
+ def generate_cache_key(store, original_html)
188
+ settings_hash = Digest::MD5.hexdigest(JSON.dump(store.settings))
189
+ body_hash = Digest::MD5.hexdigest(original_html)
190
+ escaped_key = CGI.escape("token=123456&settings_hash=#{settings_hash}&body_hash=#{body_hash}&path=/test&lang=fr&version=wovnrb_#{VERSION}")
191
+
192
+ "(#{escaped_key})"
193
+ end
194
+
195
+ def generate_data(original_html)
196
+ data = {
197
+ 'url' => 'http://wovn.io/test',
198
+ 'token' => '123456',
199
+ 'lang_code' => 'fr',
200
+ 'url_pattern' => 'subdomain',
201
+ 'lang_param_name' => 'lang',
202
+ 'translate_canonical_tag' => true,
203
+ 'insert_hreflangs' => true,
204
+ 'product' => 'WOVN.rb',
205
+ 'version' => VERSION,
206
+ 'body' => original_html,
207
+ 'custom_lang_aliases' => '{"ja":"Japanese"}'
208
+ }
209
+
210
+ data.to_json
211
+ end
212
+
213
+ def compress(string)
214
+ ActiveSupport::Gzip.compress(string)
215
+ end
216
+ end
217
+ 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