wovnrb 3.10.1 → 3.10.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,342 +1,342 @@
1
- require 'test_helper'
2
- require 'wovnrb'
3
-
4
- class WovnrbTest < Minitest::Test
5
- def setup
6
- Wovnrb::Store.instance.reset
7
- end
8
-
9
- def test_initialize
10
- i = Wovnrb::Interceptor.new(get_app)
11
- refute_nil(i)
12
- end
13
-
14
- def test_switch_lang
15
- body = '<html lang="ja"><body><h1>Mr. Belvedere Fan Club</h1><div><p>Hello</p></div></body></html>'
16
-
17
- expected_body = [
18
- '<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
19
- "<script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script>",
20
- '<link rel="alternate" hreflang="ja" href="http://ja.page.com/">',
21
- '<link rel="alternate" hreflang="en" href="http://page.com/"></head>',
22
- '<body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>',
23
- '<div><p><!--wovn-src:Hello-->こんにちは</p></div>',
24
- '</body></html>'
25
- ].join
26
-
27
- assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
28
- end
29
-
30
- def test_switch_lang_with_input_tags
31
- body = [
32
- '<html lang="ja">',
33
- '<body>',
34
- '<input type="hidden" value="test1">',
35
- '<input type="hidden" value="test2">',
36
- '<input type="hidden" value="">',
37
- '<input value="test3">',
38
- '</body></html>'
39
- ].join
40
-
41
- expected_body = [
42
- '<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
43
- "<script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true&currentLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script>",
44
- '<link rel="alternate" hreflang="ja" href="http://ja.page.com/">',
45
- '<link rel="alternate" hreflang="en" href="http://page.com/"></head>',
46
- '<body>',
47
- '<input type="hidden" value="test1">',
48
- '<input type="hidden" value="test2">',
49
- '<input type="hidden" value="">',
50
- '<input value="test3">',
51
- '<p><!--wovn-src:Hello-->こんにちは</p>',
52
- '</body></html>'
53
- ].join
54
-
55
- assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
56
- end
57
-
58
- def test_switch_lang_of_html_fragment_with_japanese_translations
59
- bodies = ['<span>Hello</span>'].join
60
- expected_bodies = ['<span><!--wovn-src:Hello-->こんにちは</span>'].join
61
-
62
- assert_switch_lang('en', 'ja', bodies, expected_bodies, api_expected: true)
63
- end
64
-
65
- def test_switch_lang_splitted_body
66
- bodies = ['<html><body><h1>Mr. Belvedere Fan Club</h1>',
67
- '<div><p>Hello</p></div>',
68
- '</body></html>'].join
69
- expected_bodies = ["<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=subdomain&amp;langCodeAliases={}&amp;version=WOVN.rb_#{Wovnrb::VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1>Mr. Belvedere Fan Club</h1><div><p>Hello</p></div></body></html>"].join
70
-
71
- assert_switch_lang('en', 'ja', bodies, expected_bodies, api_expected: true)
72
- end
73
-
74
- def test_switch_lang_of_html_fragment_in_splitted_body
75
- body = ['<select name="test"><option value="1">1</option>',
76
- '<option value="2">2</option></select>'].join
77
- expected_body = ['<select name="test"><option value="1">1</option><option value="2">2</option></select>'].join
78
-
79
- assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
80
- end
81
-
82
- def test_switch_lang_missing_values
83
- body = "<html><body><h1>Mr. Belvedere Fan Club</h1>
84
- <div><p>Hello</p></div>
85
- </body></html>"
86
- expected_body = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script></head><body><h1>Mr. Belvedere Fan Club</h1>
87
- <div><p>Hello</p></div>
88
- </body></html>
89
- "
90
-
91
- assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
92
- end
93
-
94
- def test_switch_lang_on_fragment_with_translate_fragment_false
95
- body = "<h1>Mr. Belvedere Fan Club</h1>
96
- <div><p>Hello</p></div>"
97
-
98
- Wovnrb::Store.instance.settings['translate_fragment'] = false
99
- assert_switch_lang('en', 'ja', body, body, api_expected: false)
100
- end
101
-
102
- def test_switch_lang_on_fragment_with_translate_fragment_true
103
- body = "<h1>Mr. Belvedere Fan Club</h1>
104
- <div><p>Hello</p></div>"
105
- expected_body = "<h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>
106
- <div><p><!--wovn-src:Hello-->こんにちは</p></div>"
107
-
108
- Wovnrb::Store.instance.settings['translate_fragment'] = true
109
- assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
110
- end
111
-
112
- def test_switch_lang_ignores_amp
113
- body = <<HTML
114
- <html amp>
115
- <head><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript></head>
116
- <body>
117
- <h1>Mr. Belvedere Fan Club</h1>
118
- <div><p>Hello</p></div>
119
- </body>
120
- </html>
121
- HTML
122
-
123
- assert_switch_lang('en', 'ja', body, body, api_expected: false)
124
- end
125
-
126
- def test_switch_lang_ignores_amp_defined_with_symbol_attribute
127
- body = <<HTML
128
- <html ⚡>
129
- <body>
130
- <h1>Mr. Belvedere Fan Club</h1>
131
- <div><p>Hello</p></div>
132
- </body>
133
- </html>
134
- HTML
135
-
136
- assert_switch_lang('en', 'ja', body, body, api_expected: false)
137
- end
138
-
139
- def test_call_without_path_ignored_should_change_environment
140
- settings = {
141
- 'project_token' => '123456',
142
- 'url_pattern' => 'path',
143
- 'default_lang' => 'ja',
144
- 'supported_langs' => %w[ja en],
145
- 'ignore_paths' => ['/en/ignored']
146
- }
147
- env = {
148
- 'rack.input' => '',
149
- 'rack.request.query_string' => '',
150
- 'rack.request.query_hash' => {},
151
- 'rack.request.form_input' => '',
152
- 'rack.request.form_hash' => {},
153
- 'HTTP_HOST' => 'test.com',
154
- 'REQUEST_URI' => '/en/not_ignored',
155
- 'PATH_INFO' => '/en/not_ignored'
156
- }
157
-
158
- assert_call_affects_env(settings, env, mock_api: true, affected: true)
159
- end
160
-
161
- def test_call_with_path_ignored_with_language_code_should_change_environment
162
- settings = {
163
- 'project_token' => '123456',
164
- 'url_pattern' => 'path',
165
- 'default_lang' => 'ja',
166
- 'supported_langs' => %w[ja en],
167
- 'ignore_paths' => ['/en/ignored']
168
- }
169
- env = {
170
- 'rack.input' => '',
171
- 'rack.request.query_string' => '',
172
- 'rack.request.query_hash' => {},
173
- 'rack.request.form_input' => '',
174
- 'rack.request.form_hash' => {},
175
- 'HTTP_HOST' => 'test.com',
176
- 'REQUEST_URI' => '/ignored',
177
- 'PATH_INFO' => '/ignored'
178
- }
179
-
180
- assert_call_affects_env(settings, env, mock_api: false, affected: true)
181
- end
182
-
183
- def test_call_with_path_ignored_without_language_code_should_change_environment
184
- settings = {
185
- 'project_token' => '123456',
186
- 'url_pattern' => 'path',
187
- 'default_lang' => 'ja',
188
- 'supported_langs' => %w[ja en],
189
- 'ignore_paths' => ['/ignored']
190
- }
191
- env = {
192
- 'rack.input' => '',
193
- 'rack.request.query_string' => '',
194
- 'rack.request.query_hash' => {},
195
- 'rack.request.form_input' => '',
196
- 'rack.request.form_hash' => {},
197
- 'HTTP_HOST' => 'test.com',
198
- 'REQUEST_URI' => '/en/ignored',
199
- 'PATH_INFO' => '/en/ignored'
200
- }
201
-
202
- assert_call_affects_env(settings, env, mock_api: false, affected: true)
203
- end
204
-
205
- def test_call_with_path_ignored_without_language_code_in_original_language_should_change_environment
206
- settings = {
207
- 'project_token' => '123456',
208
- 'url_pattern' => 'path',
209
- 'default_lang' => 'ja',
210
- 'supported_langs' => %w[ja en],
211
- 'ignore_paths' => ['/ignored']
212
- }
213
- env = {
214
- 'rack.input' => '',
215
- 'rack.request.query_string' => '',
216
- 'rack.request.query_hash' => {},
217
- 'rack.request.form_input' => '',
218
- 'rack.request.form_hash' => {},
219
- 'HTTP_HOST' => 'test.com',
220
- 'REQUEST_URI' => '/ignored',
221
- 'PATH_INFO' => '/ignored'
222
- }
223
-
224
- assert_call_affects_env(settings, env, mock_api: false, affected: true)
225
- end
226
-
227
- def test_call_with_path_ignored_should_not_change_environment
228
- settings = {
229
- 'project_token' => '123456',
230
- 'url_pattern' => 'path',
231
- 'default_lang' => 'ja',
232
- 'supported_langs' => %w[ja en],
233
- 'ignore_paths' => ['/en/ignored']
234
- }
235
- env = {
236
- 'rack.input' => '',
237
- 'rack.request.query_string' => '',
238
- 'rack.request.query_hash' => {},
239
- 'rack.request.form_input' => '',
240
- 'rack.request.form_hash' => {},
241
- 'rack.request.form_pairs' => [],
242
- 'HTTP_HOST' => 'test.com',
243
- 'REQUEST_URI' => '/en/ignored',
244
- 'PATH_INFO' => '/en/ignored'
245
- }
246
-
247
- assert_call_affects_env(settings, env, mock_api: false, affected: false)
248
- end
249
-
250
- private
251
-
252
- def assert_call_affects_env(settings, env, mock_api:, affected:)
253
- app_mock = get_app
254
- sut = Wovnrb::Interceptor.new(app_mock, settings)
255
- unaffected_env = env
256
-
257
- mock_translation_api_response('', '') if mock_api
258
- sut.call(env.clone)
259
-
260
- assert_equal(unaffected_env != app_mock.env, affected)
261
- end
262
-
263
- def assert_switch_lang(original_lang, target_lang, body, expected_body, api_expected: true)
264
- subdomain = target_lang == original_lang ? '' : "#{target_lang}."
265
- interceptor = Wovnrb::Interceptor.new(get_app)
266
-
267
- store, headers = store_headers_factory(subdomain, original_lang)
268
- if api_expected
269
- dom = Wovnrb::Helpers::NokogumboHelper.parse_html(body)
270
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
271
- converter = Wovnrb::HtmlConverter.new(dom, store, headers, url_lang_switcher)
272
- apified_body, = converter.build_api_compatible_html
273
- mock_translation_api_response(apified_body, expected_body)
274
- end
275
-
276
- actual_bodies = interceptor.switch_lang(headers, [body])
277
-
278
- assert_same_elements([expected_body], actual_bodies)
279
- end
280
-
281
- def store_headers_factory(subdomain, original_lang)
282
- settings = {
283
- 'project_token' => '123456',
284
- 'custom_lang_aliases' => {},
285
- 'default_lang' => original_lang,
286
- 'url_pattern' => 'subdomain',
287
- 'url_pattern_reg' => '^(?<lang>[^.]+)\.'
288
- }
289
-
290
- store = Wovnrb::Store.instance
291
- store.update_settings(settings)
292
-
293
- headers = Wovnrb::Headers.new(
294
- Wovnrb.get_env('url' => "http://#{subdomain}page.com"),
295
- Wovnrb.get_settings(settings),
296
- Wovnrb::UrlLanguageSwitcher.new(store)
297
- )
298
-
299
- [store, headers]
300
- end
301
-
302
- def mock_translation_api_response(body, expected_body)
303
- Wovnrb::ApiTranslator.any_instance
304
- .expects(:translate)
305
- .once
306
- .with(body)
307
- .returns(expected_body)
308
- end
309
-
310
- def get_app(opts = {})
311
- RackMock.new(opts)
312
- end
313
-
314
- class RackMock
315
- attr_accessor :params, :env
316
-
317
- def initialize(opts = {})
318
- @params = {}
319
- if opts.key?(:params) && opts[:params].instance_of?(Hash)
320
- opts[:params].each do |key, val|
321
- @params[key] = val
322
- end
323
- end
324
- @request_headers = {}
325
- end
326
-
327
- def call(env)
328
- @env = env
329
- unless @params.empty?
330
- req = Rack::Request.new(@env)
331
- @params.each do |key, val|
332
- req.update_param(key, val)
333
- end
334
- end
335
- [200, { 'Content-Type' => 'text/html' }, ['']]
336
- end
337
-
338
- def [](key)
339
- @env[key]
340
- end
341
- end
342
- end
1
+ require 'test_helper'
2
+ require 'wovnrb'
3
+
4
+ class WovnrbTest < Minitest::Test
5
+ def setup
6
+ Wovnrb::Store.instance.reset
7
+ end
8
+
9
+ def test_initialize
10
+ i = Wovnrb::Interceptor.new(get_app)
11
+ refute_nil(i)
12
+ end
13
+
14
+ def test_switch_lang
15
+ body = '<html lang="ja"><body><h1>Mr. Belvedere Fan Club</h1><div><p>Hello</p></div></body></html>'
16
+
17
+ expected_body = [
18
+ '<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
19
+ "<script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script>",
20
+ '<link rel="alternate" hreflang="ja" href="http://ja.page.com/">',
21
+ '<link rel="alternate" hreflang="en" href="http://page.com/"></head>',
22
+ '<body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>',
23
+ '<div><p><!--wovn-src:Hello-->こんにちは</p></div>',
24
+ '</body></html>'
25
+ ].join
26
+
27
+ assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
28
+ end
29
+
30
+ def test_switch_lang_with_input_tags
31
+ body = [
32
+ '<html lang="ja">',
33
+ '<body>',
34
+ '<input type="hidden" value="test1">',
35
+ '<input type="hidden" value="test2">',
36
+ '<input type="hidden" value="">',
37
+ '<input value="test3">',
38
+ '</body></html>'
39
+ ].join
40
+
41
+ expected_body = [
42
+ '<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
43
+ "<script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true&currentLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script>",
44
+ '<link rel="alternate" hreflang="ja" href="http://ja.page.com/">',
45
+ '<link rel="alternate" hreflang="en" href="http://page.com/"></head>',
46
+ '<body>',
47
+ '<input type="hidden" value="test1">',
48
+ '<input type="hidden" value="test2">',
49
+ '<input type="hidden" value="">',
50
+ '<input value="test3">',
51
+ '<p><!--wovn-src:Hello-->こんにちは</p>',
52
+ '</body></html>'
53
+ ].join
54
+
55
+ assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
56
+ end
57
+
58
+ def test_switch_lang_of_html_fragment_with_japanese_translations
59
+ bodies = ['<span>Hello</span>'].join
60
+ expected_bodies = ['<span><!--wovn-src:Hello-->こんにちは</span>'].join
61
+
62
+ assert_switch_lang('en', 'ja', bodies, expected_bodies, api_expected: true)
63
+ end
64
+
65
+ def test_switch_lang_splitted_body
66
+ bodies = ['<html><body><h1>Mr. Belvedere Fan Club</h1>',
67
+ '<div><p>Hello</p></div>',
68
+ '</body></html>'].join
69
+ expected_bodies = ["<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=subdomain&amp;langCodeAliases={}&amp;version=WOVN.rb_#{Wovnrb::VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1>Mr. Belvedere Fan Club</h1><div><p>Hello</p></div></body></html>"].join
70
+
71
+ assert_switch_lang('en', 'ja', bodies, expected_bodies, api_expected: true)
72
+ end
73
+
74
+ def test_switch_lang_of_html_fragment_in_splitted_body
75
+ body = ['<select name="test"><option value="1">1</option>',
76
+ '<option value="2">2</option></select>'].join
77
+ expected_body = ['<select name="test"><option value="1">1</option><option value="2">2</option></select>'].join
78
+
79
+ assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
80
+ end
81
+
82
+ def test_switch_lang_missing_values
83
+ body = "<html><body><h1>Mr. Belvedere Fan Club</h1>
84
+ <div><p>Hello</p></div>
85
+ </body></html>"
86
+ expected_body = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script></head><body><h1>Mr. Belvedere Fan Club</h1>
87
+ <div><p>Hello</p></div>
88
+ </body></html>
89
+ "
90
+
91
+ assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
92
+ end
93
+
94
+ def test_switch_lang_on_fragment_with_translate_fragment_false
95
+ body = "<h1>Mr. Belvedere Fan Club</h1>
96
+ <div><p>Hello</p></div>"
97
+
98
+ Wovnrb::Store.instance.settings['translate_fragment'] = false
99
+ assert_switch_lang('en', 'ja', body, body, api_expected: false)
100
+ end
101
+
102
+ def test_switch_lang_on_fragment_with_translate_fragment_true
103
+ body = "<h1>Mr. Belvedere Fan Club</h1>
104
+ <div><p>Hello</p></div>"
105
+ expected_body = "<h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>
106
+ <div><p><!--wovn-src:Hello-->こんにちは</p></div>"
107
+
108
+ Wovnrb::Store.instance.settings['translate_fragment'] = true
109
+ assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
110
+ end
111
+
112
+ def test_switch_lang_ignores_amp
113
+ body = <<HTML
114
+ <html amp>
115
+ <head><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript></head>
116
+ <body>
117
+ <h1>Mr. Belvedere Fan Club</h1>
118
+ <div><p>Hello</p></div>
119
+ </body>
120
+ </html>
121
+ HTML
122
+
123
+ assert_switch_lang('en', 'ja', body, body, api_expected: false)
124
+ end
125
+
126
+ def test_switch_lang_ignores_amp_defined_with_symbol_attribute
127
+ body = <<HTML
128
+ <html ⚡>
129
+ <body>
130
+ <h1>Mr. Belvedere Fan Club</h1>
131
+ <div><p>Hello</p></div>
132
+ </body>
133
+ </html>
134
+ HTML
135
+
136
+ assert_switch_lang('en', 'ja', body, body, api_expected: false)
137
+ end
138
+
139
+ def test_call_without_path_ignored_should_change_environment
140
+ settings = {
141
+ 'project_token' => '123456',
142
+ 'url_pattern' => 'path',
143
+ 'default_lang' => 'ja',
144
+ 'supported_langs' => %w[ja en],
145
+ 'ignore_paths' => ['/en/ignored']
146
+ }
147
+ env = {
148
+ 'rack.input' => '',
149
+ 'rack.request.query_string' => '',
150
+ 'rack.request.query_hash' => {},
151
+ 'rack.request.form_input' => '',
152
+ 'rack.request.form_hash' => {},
153
+ 'HTTP_HOST' => 'test.com',
154
+ 'REQUEST_URI' => '/en/not_ignored',
155
+ 'PATH_INFO' => '/en/not_ignored'
156
+ }
157
+
158
+ assert_call_affects_env(settings, env, mock_api: true, affected: true)
159
+ end
160
+
161
+ def test_call_with_path_ignored_with_language_code_should_change_environment
162
+ settings = {
163
+ 'project_token' => '123456',
164
+ 'url_pattern' => 'path',
165
+ 'default_lang' => 'ja',
166
+ 'supported_langs' => %w[ja en],
167
+ 'ignore_paths' => ['/en/ignored']
168
+ }
169
+ env = {
170
+ 'rack.input' => '',
171
+ 'rack.request.query_string' => '',
172
+ 'rack.request.query_hash' => {},
173
+ 'rack.request.form_input' => '',
174
+ 'rack.request.form_hash' => {},
175
+ 'HTTP_HOST' => 'test.com',
176
+ 'REQUEST_URI' => '/ignored',
177
+ 'PATH_INFO' => '/ignored'
178
+ }
179
+
180
+ assert_call_affects_env(settings, env, mock_api: false, affected: true)
181
+ end
182
+
183
+ def test_call_with_path_ignored_without_language_code_should_change_environment
184
+ settings = {
185
+ 'project_token' => '123456',
186
+ 'url_pattern' => 'path',
187
+ 'default_lang' => 'ja',
188
+ 'supported_langs' => %w[ja en],
189
+ 'ignore_paths' => ['/ignored']
190
+ }
191
+ env = {
192
+ 'rack.input' => '',
193
+ 'rack.request.query_string' => '',
194
+ 'rack.request.query_hash' => {},
195
+ 'rack.request.form_input' => '',
196
+ 'rack.request.form_hash' => {},
197
+ 'HTTP_HOST' => 'test.com',
198
+ 'REQUEST_URI' => '/en/ignored',
199
+ 'PATH_INFO' => '/en/ignored'
200
+ }
201
+
202
+ assert_call_affects_env(settings, env, mock_api: false, affected: true)
203
+ end
204
+
205
+ def test_call_with_path_ignored_without_language_code_in_original_language_should_change_environment
206
+ settings = {
207
+ 'project_token' => '123456',
208
+ 'url_pattern' => 'path',
209
+ 'default_lang' => 'ja',
210
+ 'supported_langs' => %w[ja en],
211
+ 'ignore_paths' => ['/ignored']
212
+ }
213
+ env = {
214
+ 'rack.input' => '',
215
+ 'rack.request.query_string' => '',
216
+ 'rack.request.query_hash' => {},
217
+ 'rack.request.form_input' => '',
218
+ 'rack.request.form_hash' => {},
219
+ 'HTTP_HOST' => 'test.com',
220
+ 'REQUEST_URI' => '/ignored',
221
+ 'PATH_INFO' => '/ignored'
222
+ }
223
+
224
+ assert_call_affects_env(settings, env, mock_api: false, affected: true)
225
+ end
226
+
227
+ def test_call_with_path_ignored_should_not_change_environment
228
+ settings = {
229
+ 'project_token' => '123456',
230
+ 'url_pattern' => 'path',
231
+ 'default_lang' => 'ja',
232
+ 'supported_langs' => %w[ja en],
233
+ 'ignore_paths' => ['/en/ignored']
234
+ }
235
+ env = {
236
+ 'rack.input' => '',
237
+ 'rack.request.query_string' => '',
238
+ 'rack.request.query_hash' => {},
239
+ 'rack.request.form_input' => '',
240
+ 'rack.request.form_hash' => {},
241
+ 'rack.request.form_pairs' => [],
242
+ 'HTTP_HOST' => 'test.com',
243
+ 'REQUEST_URI' => '/en/ignored',
244
+ 'PATH_INFO' => '/en/ignored'
245
+ }
246
+
247
+ assert_call_affects_env(settings, env, mock_api: false, affected: false)
248
+ end
249
+
250
+ private
251
+
252
+ def assert_call_affects_env(settings, env, mock_api:, affected:)
253
+ app_mock = get_app
254
+ sut = Wovnrb::Interceptor.new(app_mock, settings)
255
+ unaffected_env = env
256
+
257
+ mock_translation_api_response('', '') if mock_api
258
+ sut.call(env.clone)
259
+
260
+ assert_equal(unaffected_env != app_mock.env, affected)
261
+ end
262
+
263
+ def assert_switch_lang(original_lang, target_lang, body, expected_body, api_expected: true)
264
+ subdomain = target_lang == original_lang ? '' : "#{target_lang}."
265
+ interceptor = Wovnrb::Interceptor.new(get_app)
266
+
267
+ store, headers = store_headers_factory(subdomain, original_lang)
268
+ if api_expected
269
+ dom = Wovnrb::Helpers::NokogumboHelper.parse_html(body)
270
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
271
+ converter = Wovnrb::HtmlConverter.new(dom, store, headers, url_lang_switcher)
272
+ apified_body, = converter.build_api_compatible_html
273
+ mock_translation_api_response(apified_body, expected_body)
274
+ end
275
+
276
+ actual_bodies = interceptor.switch_lang(headers, [body])
277
+
278
+ assert_same_elements([expected_body], actual_bodies)
279
+ end
280
+
281
+ def store_headers_factory(subdomain, original_lang)
282
+ settings = {
283
+ 'project_token' => '123456',
284
+ 'custom_lang_aliases' => {},
285
+ 'default_lang' => original_lang,
286
+ 'url_pattern' => 'subdomain',
287
+ 'url_pattern_reg' => '^(?<lang>[^.]+)\.'
288
+ }
289
+
290
+ store = Wovnrb::Store.instance
291
+ store.update_settings(settings)
292
+
293
+ headers = Wovnrb::Headers.new(
294
+ Wovnrb.get_env('url' => "http://#{subdomain}page.com"),
295
+ Wovnrb.get_settings(settings),
296
+ Wovnrb::UrlLanguageSwitcher.new(store)
297
+ )
298
+
299
+ [store, headers]
300
+ end
301
+
302
+ def mock_translation_api_response(body, expected_body)
303
+ Wovnrb::ApiTranslator.any_instance
304
+ .expects(:translate)
305
+ .once
306
+ .with(body)
307
+ .returns(expected_body)
308
+ end
309
+
310
+ def get_app(opts = {})
311
+ RackMock.new(opts)
312
+ end
313
+
314
+ class RackMock
315
+ attr_accessor :params, :env
316
+
317
+ def initialize(opts = {})
318
+ @params = {}
319
+ if opts.key?(:params) && opts[:params].instance_of?(Hash)
320
+ opts[:params].each do |key, val|
321
+ @params[key] = val
322
+ end
323
+ end
324
+ @request_headers = {}
325
+ end
326
+
327
+ def call(env)
328
+ @env = env
329
+ unless @params.empty?
330
+ req = Rack::Request.new(@env)
331
+ @params.each do |key, val|
332
+ req.update_param(key, val)
333
+ end
334
+ end
335
+ [200, { 'Content-Type' => 'text/html' }, ['']]
336
+ end
337
+
338
+ def [](key)
339
+ @env[key]
340
+ end
341
+ end
342
+ end