wovnrb 3.10.2 → 3.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,1097 +1,1097 @@
1
- require 'test_helper'
2
-
3
- module Wovnrb
4
- class UrlLanguageSwitcherTest < WovnMiniTest
5
- def test_add_lang_code
6
- lang_code = 'zh-cht'
7
- store_options = { 'url_pattern' => 'subdomain' }
8
-
9
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
10
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
11
-
12
- res = url_lang_switcher.add_lang_code('http://www.facebook.com', lang_code, headers)
13
-
14
- assert_equal('http://www.facebook.com', res)
15
- end
16
-
17
- def test_add_lang_code_relative_slash_href_url_with_path
18
- lang_code = 'fr'
19
-
20
- store_options = { 'url_pattern' => 'subdomain' }
21
-
22
- store, headers = store_headers_factory(store_options, 'http://favy.tips/topics/44')
23
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
24
-
25
- res = url_lang_switcher.add_lang_code('/topics/50', lang_code, headers)
26
-
27
- assert_equal('http://fr.favy.tips/topics/50', res)
28
- end
29
-
30
- def test_add_lang_code_relative_dot_href_url_with_path
31
- lang_code = 'fr'
32
-
33
- store_options = { 'url_pattern' => 'subdomain' }
34
-
35
- store, headers = store_headers_factory(store_options, 'http://favy.tips/topics/44')
36
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
37
-
38
- res = url_lang_switcher.add_lang_code('./topics/50', lang_code, headers)
39
-
40
- assert_equal('http://fr.favy.tips/topics/topics/50', res)
41
- end
42
-
43
- def test_add_lang_code_relative_two_dots_href_url_with_path
44
- lang_code = 'fr'
45
-
46
- store_options = { 'url_pattern' => 'subdomain' }
47
-
48
- store, headers = store_headers_factory(store_options, 'http://favy.tips/topics/44')
49
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
50
-
51
- res = url_lang_switcher.add_lang_code('../topics/50', lang_code, headers)
52
-
53
- assert_equal('http://fr.favy.tips/topics/50', res)
54
- end
55
-
56
- def test_add_lang_code_trad_chinese
57
- lang_code = 'zh-cht'
58
-
59
- store_options = { 'url_pattern' => 'subdomain' }
60
-
61
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
62
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
63
-
64
- res = url_lang_switcher.add_lang_code('http://favy.tips/topics/31', lang_code, headers)
65
-
66
- assert_equal('http://zh-cht.favy.tips/topics/31', res)
67
- end
68
-
69
- def test_add_lang_code_trad_chinese_two
70
- lang_code = 'zh-cht'
71
-
72
- store_options = { 'url_pattern' => 'subdomain' }
73
-
74
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
75
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
76
-
77
- res = url_lang_switcher.add_lang_code('/topics/31', lang_code, headers)
78
-
79
- assert_equal('http://zh-cht.favy.tips/topics/31', res)
80
- end
81
-
82
- def test_add_lang_code_trad_chinese_lang_in_link_already
83
- lang_code = 'zh-cht'
84
-
85
- store_options = { 'url_pattern' => 'subdomain' }
86
-
87
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
88
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
89
-
90
- res = url_lang_switcher.add_lang_code('http://zh-cht.favy.tips/topics/31', lang_code, headers)
91
-
92
- assert_equal('http://zh-cht.favy.tips/topics/31', res)
93
- end
94
-
95
- def test_add_lang_code_no_protocol
96
- lang_code = 'zh-cht'
97
-
98
- store_options = { 'url_pattern' => 'subdomain' }
99
-
100
- store, headers = store_headers_factory(store_options, 'https://google.com')
101
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
102
-
103
- res = url_lang_switcher.add_lang_code('//google.com', lang_code, headers)
104
-
105
- assert_equal('//zh-cht.google.com', res)
106
- end
107
-
108
- def test_add_lang_code_no_protocol_two
109
- lang_code = 'zh-cht'
110
-
111
- store_options = { 'url_pattern' => 'subdomain' }
112
-
113
- store, headers = store_headers_factory(store_options, 'https://favy.tips')
114
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
115
-
116
- res = url_lang_switcher.add_lang_code('//google.com', lang_code, headers)
117
-
118
- assert_equal('//google.com', res)
119
- end
120
-
121
- def test_add_lang_code_invalid_url
122
- lang_code = 'zh-cht'
123
- store_options = { 'url_pattern' => 'subdomain' }
124
-
125
- store, headers = store_headers_factory(store_options, 'https://favy.tips')
126
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
127
-
128
- res = url_lang_switcher.add_lang_code('http://www.facebook.com/sharer.php?u=http://favy.tips/topics/50&amp;amp;t=Gourmet Tofu World: Vegetarian-Friendly Japanese Food is Here!', lang_code, headers)
129
-
130
- assert_equal('http://www.facebook.com/sharer.php?u=http://favy.tips/topics/50&amp;amp;t=Gourmet Tofu World: Vegetarian-Friendly Japanese Food is Here!', res)
131
- end
132
-
133
- def test_add_lang_code_path_only_with_slash
134
- lang_code = 'zh-cht'
135
-
136
- store_options = { 'url_pattern' => 'subdomain' }
137
-
138
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
139
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
140
-
141
- res = url_lang_switcher.add_lang_code('/topics/31', lang_code, headers)
142
-
143
- assert_equal('http://zh-cht.favy.tips/topics/31', res)
144
- end
145
-
146
- def test_add_lang_code_path_only_no_slash
147
- lang_code = 'zh-cht'
148
- store_options = { 'url_pattern' => 'subdomain' }
149
-
150
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
151
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
152
-
153
- res = url_lang_switcher.add_lang_code('topics/31', lang_code, headers)
154
-
155
- assert_equal('http://zh-cht.favy.tips/topics/31', res)
156
- end
157
-
158
- def test_add_lang_code_path_explicit_page_no_slash
159
- lang_code = 'zh-cht'
160
-
161
- store_options = { 'url_pattern' => 'subdomain' }
162
-
163
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
164
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
165
-
166
- res = url_lang_switcher.add_lang_code('topics/31.html', lang_code, headers)
167
-
168
- assert_equal('http://zh-cht.favy.tips/topics/31.html', res)
169
- end
170
-
171
- def test_add_lang_code_path_explicit_page_with_slash
172
- lang_code = 'zh-cht'
173
-
174
- store_options = { 'url_pattern' => 'subdomain' }
175
-
176
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
177
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
178
-
179
- res = url_lang_switcher.add_lang_code('/topics/31.html', lang_code, headers)
180
-
181
- assert_equal('http://zh-cht.favy.tips/topics/31.html', res)
182
- end
183
-
184
- def test_add_lang_code_no_protocol_with_path_explicit_page
185
- lang_code = 'zh-cht'
186
-
187
- store_options = { 'url_pattern' => 'subdomain' }
188
-
189
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
190
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
191
-
192
- res = url_lang_switcher.add_lang_code('//www.google.com/topics/31.php', lang_code, headers)
193
-
194
- assert_equal('//www.google.com/topics/31.php', res)
195
- end
196
-
197
- def test_add_lang_code_protocol_with_path_explicit_page
198
- lang_code = 'zh-cht'
199
-
200
- store_options = { 'url_pattern' => 'subdomain' }
201
-
202
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
203
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
204
-
205
- res = url_lang_switcher.add_lang_code('http://www.google.com/topics/31.php', lang_code, headers)
206
-
207
- assert_equal('http://www.google.com/topics/31.php', res)
208
- end
209
-
210
- def test_add_lang_code_relative_path_double_period
211
- lang_code = 'zh-cht'
212
-
213
- store_options = { 'url_pattern' => 'subdomain' }
214
-
215
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
216
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
217
-
218
- res = url_lang_switcher.add_lang_code('../topics/31', lang_code, headers)
219
-
220
- assert_equal('http://zh-cht.favy.tips/topics/31', res)
221
- end
222
-
223
- def test_add_lang_code_relative_path_single_period
224
- lang_code = 'zh-cht'
225
-
226
- store_options = { 'url_pattern' => 'subdomain' }
227
-
228
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
229
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
230
-
231
- res = url_lang_switcher.add_lang_code('./topics/31', lang_code, headers)
232
-
233
- assert_equal('http://zh-cht.favy.tips/topics/31', res)
234
- end
235
-
236
- def test_add_lang_code_empty_href
237
- lang_code = 'zh-cht'
238
-
239
- store_options = { 'url_pattern' => 'subdomain' }
240
-
241
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
242
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
243
-
244
- res = url_lang_switcher.add_lang_code('', lang_code, headers)
245
-
246
- assert_equal('', res)
247
- end
248
-
249
- def test_add_lang_code_hash_href
250
- lang_code = 'zh-cht'
251
-
252
- store_options = { 'url_pattern' => 'subdomain' }
253
-
254
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
255
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
256
-
257
- res = url_lang_switcher.add_lang_code('#', lang_code, headers)
258
-
259
- assert_equal('#', res)
260
- end
261
-
262
- def test_add_lang_code_nil_href
263
- lang_code = 'en'
264
- store_options = { 'url_pattern' => 'subdomain' }
265
-
266
- store, headers = store_headers_factory(store_options, 'http://favy.tips')
267
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
268
-
269
- res = url_lang_switcher.add_lang_code(nil, lang_code, headers)
270
-
271
- assert_nil(res)
272
- end
273
-
274
- def test_add_lang_code_absolute_different_host
275
- lang_code = 'fr'
276
- store_options = { 'url_pattern' => 'subdomain' }
277
-
278
- store, headers = store_headers_factory(store_options, 'http://google.com')
279
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
280
-
281
- res = url_lang_switcher.add_lang_code('http://yahoo.co.jp', lang_code, headers)
282
-
283
- assert_equal('http://yahoo.co.jp', res)
284
- end
285
-
286
- def test_add_lang_code_absolute_subdomain_no_subdomain
287
- lang_code = 'fr'
288
- store_options = { 'url_pattern' => 'subdomain' }
289
-
290
- store, headers = store_headers_factory(store_options, 'http://google.com')
291
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
292
-
293
- res = url_lang_switcher.add_lang_code('http://google.com', lang_code, headers)
294
-
295
- assert_equal('http://fr.google.com', res)
296
- end
297
-
298
- def test_add_lang_code_absolute_subdomain_with_subdomain
299
- lang_code = 'fr'
300
-
301
- store_options = { 'url_pattern' => 'subdomain' }
302
-
303
- store, headers = store_headers_factory(store_options, 'http://home.google.com')
304
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
305
-
306
- res = url_lang_switcher.add_lang_code('http://home.google.com', lang_code, headers)
307
-
308
- assert_equal('http://fr.home.google.com', res)
309
- end
310
-
311
- def test_add_lang_code_absolute_query_no_query
312
- lang_code = 'fr'
313
-
314
- store_options = { 'url_pattern' => 'query' }
315
-
316
- store, headers = store_headers_factory(store_options, 'http://google.com')
317
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
318
-
319
- res = url_lang_switcher.add_lang_code('http://google.com', lang_code, headers)
320
-
321
- assert_equal('http://google.com?wovn=fr', res)
322
- end
323
-
324
- def test_add_lang_code_absolute_query_with_query
325
- lang_code = 'fr'
326
-
327
- store_options = { 'url_pattern' => 'query' }
328
-
329
- store, headers = store_headers_factory(store_options, 'http://google.com')
330
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
331
-
332
- res = url_lang_switcher.add_lang_code('http://google.com?hey=yo', lang_code, headers)
333
-
334
- assert_equal('http://google.com?hey=yo&wovn=fr', res)
335
- end
336
-
337
- def test_add_lang_code_absolute_query_with_hash
338
- lang_code = 'fr'
339
-
340
- store_options = { 'url_pattern' => 'query' }
341
-
342
- store, headers = store_headers_factory(store_options, 'http://google.com')
343
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
344
-
345
- res = url_lang_switcher.add_lang_code('http://google.com#test', lang_code, headers)
346
-
347
- assert_equal('http://google.com?wovn=fr#test', res)
348
- end
349
-
350
- def test_add_lang_code_absolute_query_and_lang_param_name_with_no_query
351
- lang_code = 'fr'
352
-
353
- store_options = { 'url_pattern' => 'query', 'lang_param_name' => 'test_param' }
354
-
355
- store, headers = store_headers_factory(store_options, 'http://google.com')
356
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
357
-
358
- res = url_lang_switcher.add_lang_code('http://google.com', lang_code, headers)
359
-
360
- assert_equal('http://google.com?test_param=fr', res)
361
- end
362
-
363
- def test_add_lang_code_absolute_query_and_lang_param_name_with_query
364
- lang_code = 'fr'
365
-
366
- store_options = { 'url_pattern' => 'query', 'lang_param_name' => 'test_param' }
367
-
368
- store, headers = store_headers_factory(store_options, 'http://google.com')
369
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
370
-
371
- res = url_lang_switcher.add_lang_code('http://google.com?hey=yo', lang_code, headers)
372
-
373
- assert_equal('http://google.com?hey=yo&test_param=fr', res)
374
- end
375
-
376
- def test_add_lang_code_absolute_query_and_lang_param_name_with_hash
377
- lang_code = 'fr'
378
-
379
- store_options = { 'url_pattern' => 'query', 'lang_param_name' => 'test_param' }
380
-
381
- store, headers = store_headers_factory(store_options, 'http://google.com')
382
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
383
-
384
- res = url_lang_switcher.add_lang_code('http://google.com#test', lang_code, headers)
385
-
386
- assert_equal('http://google.com?test_param=fr#test', res)
387
- end
388
-
389
- def test_add_lang_code_absolute_path_no_pathname
390
- lang_code = 'fr'
391
-
392
- store_options = { 'url_pattern' => 'path' }
393
-
394
- store, headers = store_headers_factory(store_options, 'http://google.com')
395
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
396
-
397
- res = url_lang_switcher.add_lang_code('http://google.com', lang_code, headers)
398
-
399
- assert_equal('http://google.com/fr', res)
400
- end
401
-
402
- def test_add_lang_code__requested_with_deep_path
403
- lang_code = 'fr'
404
-
405
- store_options = { 'url_pattern' => 'path' }
406
-
407
- store, headers = store_headers_factory(store_options, 'http://google.com/dir1/dir2')
408
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
409
-
410
- assert_equal('http://google.com/fr', url_lang_switcher.add_lang_code('http://google.com', lang_code, headers))
411
- assert_equal('/fr/', url_lang_switcher.add_lang_code('/', lang_code, headers))
412
- assert_equal('', url_lang_switcher.add_lang_code('', lang_code, headers))
413
- end
414
-
415
- def test_add_lang_code_absolute_path_with_pathname
416
- lang_code = 'fr'
417
-
418
- store_options = { 'url_pattern' => 'path' }
419
-
420
- store, headers = store_headers_factory(store_options, 'http://google.com')
421
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
422
-
423
- res = url_lang_switcher.add_lang_code('http://google.com/index.html', lang_code, headers)
424
-
425
- assert_equal('http://google.com/fr/index.html', res)
426
- end
427
-
428
- def test_add_lang_code_absolute_path_with_pathname_hash_is_preserved
429
- lang_code = 'fr'
430
-
431
- store_options = { 'url_pattern' => 'path' }
432
-
433
- store, headers = store_headers_factory(store_options, 'http://google.com')
434
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
435
-
436
- res = url_lang_switcher.add_lang_code('http://google.com/index.html?foo=bar#hash', lang_code, headers)
437
-
438
- assert_equal('http://google.com/fr/index.html?foo=bar#hash', res)
439
- end
440
-
441
- def test_add_lang_code_absolute_path_with_long_pathname
442
- lang_code = 'fr'
443
-
444
- store_options = { 'url_pattern' => 'path' }
445
-
446
- store, headers = store_headers_factory(store_options, 'http://google.com')
447
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
448
-
449
- res = url_lang_switcher.add_lang_code('http://google.com/hello/long/path/index.html', lang_code, headers)
450
-
451
- assert_equal('http://google.com/fr/hello/long/path/index.html', res)
452
- end
453
-
454
- def test_add_lang_code_relative_subdomain_leading_slash
455
- lang_code = 'fr'
456
-
457
- store_options = { 'url_pattern' => 'subdomain' }
458
-
459
- store, headers = store_headers_factory(store_options, 'http://google.com')
460
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
461
-
462
- res = url_lang_switcher.add_lang_code('/', lang_code, headers)
463
-
464
- assert_equal('http://fr.google.com/', res)
465
- end
466
-
467
- def test_add_lang_code_relative_subdomain_leading_slash_filename
468
- lang_code = 'fr'
469
-
470
- store_options = { 'url_pattern' => 'subdomain' }
471
-
472
- store, headers = store_headers_factory(store_options, 'http://google.com')
473
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
474
-
475
- res = url_lang_switcher.add_lang_code('/index.html', lang_code, headers)
476
-
477
- assert_equal('http://fr.google.com/index.html', res)
478
- end
479
-
480
- def test_add_lang_code_relative_subdomain_no_leading_slash_filename
481
- lang_code = 'fr'
482
-
483
- store_options = { 'url_pattern' => 'subdomain' }
484
-
485
- store, headers = store_headers_factory(store_options, 'http://google.com')
486
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
487
-
488
- res = url_lang_switcher.add_lang_code('index.html', lang_code, headers)
489
-
490
- assert_equal('http://fr.google.com/index.html', res)
491
- end
492
-
493
- def test_add_lang_code_relative_query_with_no_query
494
- lang_code = 'fr'
495
-
496
- store_options = { 'url_pattern' => 'query' }
497
-
498
- store, headers = store_headers_factory(store_options, 'http://google.com')
499
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
500
-
501
- res = url_lang_switcher.add_lang_code('/index.html', lang_code, headers)
502
-
503
- assert_equal('/index.html?wovn=fr', res)
504
- end
505
-
506
- def test_add_lang_code_relative_query_with_query
507
- lang_code = 'fr'
508
-
509
- store_options = { 'url_pattern' => 'query' }
510
-
511
- store, headers = store_headers_factory(store_options, 'http://google.com')
512
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
513
-
514
- res = url_lang_switcher.add_lang_code('/index.html?hey=yo', lang_code, headers)
515
-
516
- assert_equal('/index.html?hey=yo&wovn=fr', res)
517
- end
518
-
519
- def test_add_lang_code_relative_query_with_hash
520
- lang_code = 'fr'
521
-
522
- store_options = { 'url_pattern' => 'query' }
523
-
524
- store, headers = store_headers_factory(store_options, 'http://google.com')
525
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
526
-
527
- res = url_lang_switcher.add_lang_code('/index.html?hey=yo', lang_code, headers)
528
-
529
- assert_equal('/index.html?hey=yo&wovn=fr', res)
530
- end
531
-
532
- def test_add_lang_code_relative_query_and_lang_param_name_with_no_query
533
- lang_code = 'fr'
534
-
535
- store_options = { 'url_pattern' => 'query', 'lang_param_name' => 'test_param' }
536
-
537
- store, headers = store_headers_factory(store_options, 'http://google.com')
538
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
539
-
540
- res = url_lang_switcher.add_lang_code('/index.html', lang_code, headers)
541
-
542
- assert_equal('/index.html?test_param=fr', res)
543
- end
544
-
545
- def test_add_lang_code_relative_query_and_lang_param_name_with_query
546
- lang_code = 'fr'
547
-
548
- store_options = { 'url_pattern' => 'query', 'lang_param_name' => 'test_param' }
549
-
550
- store, headers = store_headers_factory(store_options, 'http://google.com')
551
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
552
-
553
- res = url_lang_switcher.add_lang_code('/index.html?hey=yo', lang_code, headers)
554
-
555
- assert_equal('/index.html?hey=yo&test_param=fr', res)
556
- end
557
-
558
- def test_add_lang_code_relative_query_and_lang_param_name_with_hash
559
- lang_code = 'fr'
560
-
561
- store_options = { 'url_pattern' => 'query', 'lang_param_name' => 'test_param' }
562
-
563
- store, headers = store_headers_factory(store_options, 'http://google.com')
564
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
565
-
566
- res = url_lang_switcher.add_lang_code('/index.html?hey=yo#hey', lang_code, headers)
567
-
568
- assert_equal('/index.html?hey=yo&test_param=fr#hey', res)
569
- end
570
-
571
- def test_add_lang_code_relative_path_with_leading_slash
572
- lang_code = 'fr'
573
-
574
- store_options = { 'url_pattern' => 'path' }
575
-
576
- store, headers = store_headers_factory(store_options, 'http://google.com')
577
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
578
-
579
- res = url_lang_switcher.add_lang_code('/index.html', lang_code, headers)
580
-
581
- assert_equal('/fr/index.html', res)
582
- end
583
-
584
- def test_add_lang_code_relative_path_without_leading_slash_different_pathname
585
- lang_code = 'fr'
586
-
587
- store_options = { 'url_pattern' => 'path' }
588
-
589
- store, headers = store_headers_factory(store_options, 'http://google.com/hello/tab.html')
590
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
591
-
592
- res = url_lang_switcher.add_lang_code('index.html', lang_code, headers)
593
-
594
- assert_equal('/fr/hello/index.html', res)
595
- end
596
-
597
- def test_add_lang_code_relative_path_without_leading_slash_and_dot_dot
598
- lang_code = 'fr'
599
-
600
- store_options = { 'url_pattern' => 'path' }
601
-
602
- store, headers = store_headers_factory(store_options, 'https://pre.avex.jp/wovn_aaa/news/')
603
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
604
-
605
- res = url_lang_switcher.add_lang_code('../news/', lang_code, headers)
606
-
607
- assert_equal("/#{lang_code}/wovn_aaa/news/", res)
608
- end
609
-
610
- def test_add_lang_code_relative_path_without_leading_slash_different_pathname2
611
- lang_code = 'fr'
612
-
613
- store_options = { 'url_pattern' => 'path' }
614
-
615
- store, headers = store_headers_factory(store_options, 'http://google.com/hello/tab.html')
616
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
617
-
618
- res = url_lang_switcher.add_lang_code('hey/index.html', lang_code, headers)
619
-
620
- assert_equal('/fr/hello/hey/index.html', res)
621
- end
622
-
623
- def test_add_lang_code_relative_path_at_root
624
- lang_code = 'fr'
625
-
626
- store_options = { 'url_pattern' => 'path' }
627
-
628
- store, headers = store_headers_factory(store_options, 'http://google.com/')
629
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
630
-
631
- res = url_lang_switcher.add_lang_code('index.html', lang_code, headers)
632
-
633
- assert_equal('/fr/index.html', res)
634
- end
635
-
636
- def test_add_lang_code__jp_domain__path_pattern__absolute_path
637
- lang_code = 'ja'
638
-
639
- store_options = { 'url_pattern' => 'path' }
640
-
641
- store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
642
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
643
-
644
- res = url_lang_switcher.add_lang_code('/page', lang_code, headers)
645
-
646
- assert_equal('/ja/page', res)
647
- end
648
-
649
- def test_add_lang_code__jp_domain__path_pattern__absolute_path_with_query
650
- lang_code = 'ja'
651
-
652
- store_options = { 'url_pattern' => 'path' }
653
-
654
- store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
655
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
656
-
657
- res = url_lang_switcher.add_lang_code('/page?user=tom', lang_code, headers)
658
-
659
- assert_equal('/ja/page?user=tom', res)
660
- end
661
-
662
- def test_add_lang_code__jp_domain__path_pattern__absolute_path_with_query__top_page
663
- lang_code = 'ja'
664
-
665
- store_options = { 'url_pattern' => 'path' }
666
-
667
- store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
668
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
669
-
670
- res = url_lang_switcher.add_lang_code('/?user=tom', lang_code, headers)
671
-
672
- assert_equal('/ja/?user=tom', res)
673
- end
674
-
675
- def test_add_lang_code__jp_domain__path_pattern__absolute_path_with_hash__top_page
676
- lang_code = 'ja'
677
-
678
- store_options = { 'url_pattern' => 'path' }
679
-
680
- store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
681
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
682
-
683
- res = url_lang_switcher.add_lang_code('/#top', lang_code, headers)
684
-
685
- assert_equal('/ja/#top', res)
686
- end
687
-
688
- def test_add_lang_code__jp_domain__path_pattern__absolute_url
689
- lang_code = 'ja'
690
-
691
- store_options = { 'url_pattern' => 'path' }
692
-
693
- store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
694
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
695
-
696
- res = url_lang_switcher.add_lang_code('http://favy.co.jp/page', lang_code, headers)
697
-
698
- assert_equal('http://favy.co.jp/ja/page', res)
699
- end
700
-
701
- def test_add_lang_code__jp_domain__path_pattern__absolute_url_with_query
702
- lang_code = 'ja'
703
-
704
- store_options = { 'url_pattern' => 'path' }
705
-
706
- store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
707
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
708
-
709
- res = url_lang_switcher.add_lang_code('http://favy.co.jp?user=tom', lang_code, headers)
710
-
711
- assert_equal('http://favy.co.jp/ja?user=tom', res)
712
- end
713
-
714
- def test_add_lang_code__jp_domain__path_pattern__absolute_url_with_trailing_slash_and_query
715
- lang_code = 'ja'
716
-
717
- store_options = { 'url_pattern' => 'path' }
718
-
719
- store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
720
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
721
-
722
- res = url_lang_switcher.add_lang_code('http://favy.co.jp/?user=tom', lang_code, headers)
723
-
724
- assert_equal('http://favy.co.jp/ja/?user=tom', res)
725
- end
726
-
727
- def test_add_lang_code__jp_domain__path_pattern__absolute_url_with_hash
728
- lang_code = 'ja'
729
-
730
- store_options = { 'url_pattern' => 'path' }
731
-
732
- store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
733
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
734
-
735
- res = url_lang_switcher.add_lang_code('http://favy.co.jp#top', lang_code, headers)
736
-
737
- assert_equal('http://favy.co.jp/ja#top', res)
738
- end
739
-
740
- def test_add_lang_code__absolute_url_with_default_lang_alias__replaces_lang_code
741
- lang_aliases = {
742
- 'en' => 'en'
743
- }
744
-
745
- store_options = { 'url_pattern' => 'path', 'custom_lang_aliases' => lang_aliases }
746
-
747
- store, headers = store_headers_factory(store_options, 'http://www.example.com/th/')
748
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
749
-
750
- href_no_trailing_slash = 'http://www.example.com/en'
751
- href_trailing_slash = 'http://www.example.com/en/'
752
-
753
- assert_equal('http://www.example.com/th', url_lang_switcher.add_lang_code(href_no_trailing_slash, 'th', headers))
754
- assert_equal('http://www.example.com/th/', url_lang_switcher.add_lang_code(href_trailing_slash, 'th', headers))
755
- end
756
-
757
- def test_add_lang_code__absolute_path_with_default_lang_alias__replaces_lang_code
758
- lang_aliases = {
759
- 'en' => 'en'
760
- }
761
-
762
- store_options = { 'url_pattern' => 'path', 'custom_lang_aliases' => lang_aliases }
763
-
764
- store, headers = store_headers_factory(store_options, 'http://www.example.com/th/')
765
- url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
766
-
767
- href_no_trailing_slash = '/en'
768
- href_trailing_slash = '/en/'
769
-
770
- assert_equal('/th', url_lang_switcher.add_lang_code(href_no_trailing_slash, 'th', headers))
771
- assert_equal('/th/', url_lang_switcher.add_lang_code(href_trailing_slash, 'th', headers))
772
- end
773
-
774
- def test_add_lang_code_with_custom_domain_langs
775
- custom_domain_langs = {
776
- 'en' => { 'url' => 'my-site.com' },
777
- 'en-US' => { 'url' => 'en-us.my-site.com' },
778
- 'ja' => { 'url' => 'my-site.com/ja' },
779
- 'zh-CHS' => { 'url' => 'my-site.com/zh/chs' },
780
- 'zh-Hant-HK' => { 'url' => 'zh-hant-hk.com/zh' }
781
- }
782
- test_cases = [
783
- # no_lang_url, lang_code, expected_url
784
- # absolute URL
785
- ['https://my-site.com', 'en', 'https://my-site.com'],
786
- ['https://my-site.com', 'ja', 'https://my-site.com/ja'],
787
- ['https://my-site.com/index.php', 'ja', 'https://my-site.com/ja/index.php'],
788
- ['https://my-site.com/a/b/', 'ja', 'https://my-site.com/ja/a/b/'],
789
- ['https://my-site.com/a/b/index.php', 'ja', 'https://my-site.com/ja/a/b/index.php'],
790
- ['https://my-site.com/index.php', 'en-US', 'https://en-us.my-site.com/index.php'],
791
- ['https://my-site.com/index.php', 'zh-CHS', 'https://my-site.com/zh/chs/index.php'],
792
- ['https://my-site.com/index.php', 'zh-Hant-HK', 'https://zh-hant-hk.com/zh/index.php'],
793
- ['https://my-site.com/index.php?a=1&b=2', 'zh-Hant-HK', 'https://zh-hant-hk.com/zh/index.php?a=1&b=2'],
794
- ['https://my-site.com/index.php#hash', 'zh-Hant-HK', 'https://zh-hant-hk.com/zh/index.php#hash'],
795
- ['https://my-site.com/index.php?a=1&b=2#hash', 'zh-Hant-HK', 'https://zh-hant-hk.com/zh/index.php?a=1&b=2#hash'],
796
-
797
- # absolute path
798
- ['/', 'en', 'http://my-site.com/'],
799
- ['/', 'ja', 'http://my-site.com/ja/'],
800
- ['/index.php', 'ja', 'http://my-site.com/ja/index.php'],
801
- ['/a/b/', 'ja', 'http://my-site.com/ja/a/b/'],
802
- ['/a/b/index.php', 'ja', 'http://my-site.com/ja/a/b/index.php'],
803
- ['/index.php', 'en-US', 'http://en-us.my-site.com/index.php'],
804
- ['/index.php', 'zh-CHS', 'http://my-site.com/zh/chs/index.php'],
805
- ['/index.php', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/index.php'],
806
- ['/index.php?a=1&b=2', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/index.php?a=1&b=2'],
807
- ['/index.php#hash', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/index.php#hash'],
808
- ['/index.php?a=1&b=2#hash', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/index.php?a=1&b=2#hash'],
809
-
810
- # relative path
811
- ['index.php', 'ja', 'http://my-site.com/ja/req_uri/index.php'],
812
- ['a/b/', 'ja', 'http://my-site.com/ja/req_uri/a/b/'],
813
- ['a/b/index.php', 'ja', 'http://my-site.com/ja/req_uri/a/b/index.php'],
814
- ['index.php', 'en-US', 'http://en-us.my-site.com/req_uri/index.php'],
815
- ['index.php', 'zh-CHS', 'http://my-site.com/zh/chs/req_uri/index.php'],
816
- ['index.php', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/req_uri/index.php'],
817
- ['index.php?a=1&b=2', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/req_uri/index.php?a=1&b=2'],
818
- ['index.php#hash', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/req_uri/index.php#hash'],
819
- ['index.php?a=1&b=2#hash', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/req_uri/index.php?a=1&b=2#hash'],
820
- ['?a=1&b=2', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/req_uri/?a=1&b=2'],
821
-
822
- # anchor links should not be changed
823
- ['#hash', 'zh-Hant-HK', '#hash']
824
- ]
825
-
826
- settings = {
827
- 'project_token' => 'T0k3N',
828
- 'default_lang' => 'en',
829
- 'supported_langs' => ['en'],
830
- 'url_pattern' => 'custom_domain',
831
- 'custom_domain_langs' => custom_domain_langs
832
- }
833
- additional_env = {
834
- 'HTTP_HOST' => 'my-site.com',
835
- 'REQUEST_URI' => '/req_uri/'
836
- }
837
-
838
- test_cases.each do |test_case|
839
- target_uri, lang, expected_uri = test_case
840
- store = Wovnrb::Store.instance
841
- store.update_settings(settings)
842
- url_lang_switcher = UrlLanguageSwitcher.new(store)
843
- headers = Wovnrb::Headers.new(
844
- Wovnrb.get_env(additional_env),
845
- store.settings,
846
- url_lang_switcher
847
- )
848
-
849
- assert_equal(expected_uri, url_lang_switcher.add_lang_code(target_uri, lang, headers))
850
- end
851
- end
852
-
853
- def test_remove_lang_query_with_lang_param_name
854
- settings = Wovnrb.get_settings('url_pattern' => 'query', 'lang_param_name' => 'lang')
855
- store = Wovnrb.get_store(settings)
856
- url_lang_switcher = UrlLanguageSwitcher.new(store)
857
-
858
- keys = Wovnrb::Lang::LANG.keys
859
- assert_equal(77, keys.size)
860
-
861
- keys.each do |key|
862
- uri_without_custom_lang_param = "wovn.io/?wovn=#{key}"
863
- unchanged_uri = url_lang_switcher.remove_lang_from_uri_component(uri_without_custom_lang_param, key)
864
- assert_equal(uri_without_custom_lang_param, unchanged_uri)
865
-
866
- uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component("wovn.io/?lang=#{key}", key)
867
- assert_equal('wovn.io/', uri_without_scheme)
868
-
869
- uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component("https://wovn.io?lang=#{key}", key)
870
- assert_equal('https://wovn.io', uri_with_scheme)
871
- end
872
- end
873
-
874
- def test_remove_lang_query_with_nil_lang
875
- settings = Wovnrb.get_settings('url_pattern' => 'query')
876
- store = Wovnrb.get_store(settings)
877
- url_lang_switcher = UrlLanguageSwitcher.new(store)
878
-
879
- keys = Wovnrb::Lang::LANG.keys
880
- assert_equal(77, keys.size)
881
-
882
- uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component('wovn.io', nil)
883
- assert_equal('wovn.io', uri_without_scheme)
884
-
885
- uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component('https://wovn.io/', nil)
886
- assert_equal('https://wovn.io/', uri_with_scheme)
887
- end
888
-
889
- def test_remove_lang_query_with_empty_lang
890
- settings = Wovnrb.get_settings('url_pattern' => 'query')
891
- store = Wovnrb.get_store(settings)
892
- url_lang_switcher = UrlLanguageSwitcher.new(store)
893
-
894
- uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component('wovn.io', '')
895
- assert_equal('wovn.io', uri_without_scheme)
896
-
897
- uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component('https://wovn.io/', '')
898
- assert_equal('https://wovn.io/', uri_with_scheme)
899
- end
900
-
901
- def test_remove_lang_subdomain
902
- settings = Wovnrb.get_settings('url_pattern' => 'subdomain')
903
- store = Wovnrb.get_store(settings)
904
- url_lang_switcher = UrlLanguageSwitcher.new(store)
905
-
906
- keys = Wovnrb::Lang::LANG.keys
907
- assert_equal(77, keys.size)
908
-
909
- keys.each do |key|
910
- uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component("#{key.downcase}.wovn.io/", key)
911
- assert_equal('wovn.io/', uri_without_scheme)
912
-
913
- uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component("https://#{key.downcase}.wovn.io", key)
914
- assert_equal('https://wovn.io', uri_with_scheme)
915
- end
916
- end
917
-
918
- def test_remove_lang_subdomain_with_nil_lang
919
- settings = Wovnrb.get_settings('url_pattern' => 'subdomain')
920
- store = Wovnrb.get_store(settings)
921
- url_lang_switcher = UrlLanguageSwitcher.new(store)
922
- keys = Wovnrb::Lang::LANG.keys
923
- assert_equal(77, keys.size)
924
-
925
- uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component('wovn.io', nil)
926
- assert_equal('wovn.io', uri_without_scheme)
927
-
928
- uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component('https://wovn.io/', nil)
929
- assert_equal('https://wovn.io/', uri_with_scheme)
930
- end
931
-
932
- def test_remove_lang_subdomain_with_empty_lang
933
- settings = Wovnrb.get_settings('url_pattern' => 'subdomain')
934
- store = Wovnrb.get_store(settings)
935
- url_lang_switcher = UrlLanguageSwitcher.new(store)
936
-
937
- uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component('wovn.io', '')
938
- assert_equal('wovn.io', uri_without_scheme)
939
-
940
- uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component('https://wovn.io/', '')
941
- assert_equal('https://wovn.io/', uri_with_scheme)
942
- end
943
-
944
- def test_remove_lang_subdomain_with_custom_lang_alias
945
- Store.instance.update_settings('custom_lang_aliases' => { 'fr' => 'staging-fr' }, 'url_pattern' => 'subdomain')
946
- url_lang_switcher = UrlLanguageSwitcher.new(Store.instance)
947
-
948
- uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component('staging-fr.wovn.io/', 'fr')
949
- assert_equal('wovn.io/', uri_without_scheme)
950
-
951
- uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component('https://staging-fr.wovn.io', 'fr')
952
- assert_equal('https://wovn.io', uri_with_scheme)
953
- end
954
-
955
- def test_remove_lang_path
956
- settings = Wovnrb.get_settings
957
- store = Wovnrb.get_store(settings)
958
- sut = UrlLanguageSwitcher.new(store)
959
-
960
- keys = Wovnrb::Lang::LANG.keys
961
- assert_equal(77, keys.size)
962
-
963
- keys.each do |key|
964
- assert_equal('/', sut.remove_lang_from_uri_component("/#{key}", key))
965
- assert_equal("/dir/#{key}/page.html", sut.remove_lang_from_uri_component("/#{key}/dir/#{key}/page.html", key))
966
- assert_equal('?query', sut.remove_lang_from_uri_component('?query', key))
967
- assert_equal('wovn.io/', sut.remove_lang_from_uri_component("wovn.io/#{key}", key))
968
- assert_equal("wovn.io/dir/#{key}/page.html", sut.remove_lang_from_uri_component("wovn.io/#{key}/dir/#{key}/page.html", key))
969
- assert_equal("wovn.io:5000/dir/#{key}/page.html", sut.remove_lang_from_uri_component("wovn.io:5000/#{key}/dir/#{key}/page.html", key))
970
- assert_equal('https://wovn.io/', sut.remove_lang_from_uri_component("https://wovn.io/#{key}/", key))
971
- assert_equal("https://wovn.io/dir/#{key}/page.html", sut.remove_lang_from_uri_component("https://wovn.io/#{key}/dir/#{key}/page.html", key))
972
- end
973
- end
974
-
975
- def test_remove_lang_path_with_nil_lang
976
- settings = Wovnrb.get_settings
977
- store = Wovnrb.get_store(settings)
978
- url_lang_switcher = UrlLanguageSwitcher.new(store)
979
-
980
- keys = Wovnrb::Lang::LANG.keys
981
- assert_equal(77, keys.size)
982
-
983
- uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component('wovn.io', nil)
984
- assert_equal('wovn.io', uri_without_scheme)
985
-
986
- uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component('https://wovn.io/', nil)
987
- assert_equal('https://wovn.io/', uri_with_scheme)
988
- end
989
-
990
- def test_remove_lang_path_with_empty_lang
991
- settings = Wovnrb.get_settings
992
- store = Wovnrb.get_store(settings)
993
- url_lang_switcher = UrlLanguageSwitcher.new(store)
994
-
995
- uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component('wovn.io', '')
996
- assert_equal('wovn.io', uri_without_scheme)
997
-
998
- uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component('https://wovn.io/', '')
999
- assert_equal('https://wovn.io/', uri_with_scheme)
1000
- end
1001
-
1002
- def test_remove_lang_custom_domain
1003
- custom_domain_langs = {
1004
- 'en' => { 'url' => 'my-site.com' },
1005
- 'en-US' => { 'url' => 'en-us.my-site.com' },
1006
- 'ja' => { 'url' => 'my-site.com/ja' },
1007
- 'zh-CHS' => { 'url' => 'my-site.com/zh/chs' },
1008
- 'zh-Hant-HK' => { 'url' => 'zh-hant-hk.com/zh' }
1009
- }
1010
- test_cases = [
1011
- # target_uri, lang, expected_uri, env
1012
- # absolute URL
1013
- ['https://my-site.com', 'en', 'https://my-site.com', {}],
1014
- ['https://my-site.com/ja', 'ja', 'https://my-site.com', { 'REQUEST_URI' => '/ja' }],
1015
- ['https://my-site.com/ja/index.php', 'ja', 'https://my-site.com/index.php', { 'REQUEST_URI' => '/ja/index.php' }],
1016
- ['https://my-site.com/ja/a/b/', 'ja', 'https://my-site.com/a/b/', { 'REQUEST_URI' => '/ja/a/b/' }],
1017
- ['https://my-site.com/ja/a/b/index.php', 'ja', 'https://my-site.com/a/b/index.php', { 'REQUEST_URI' => '/ja/a/b/index.php' }],
1018
- ['https://en-us.my-site.com/index.php', 'en-US', 'https://my-site.com/index.php', { 'HTTP_HOST' => 'en-us.my-site.com', 'SERVER_NAME' => 'en-us.my-site.com', 'REQUEST_URI' => '/index.php' }],
1019
- ['https://my-site.com/zh/chs/index.php', 'zh-CHS', 'https://my-site.com/index.php', { 'REQUEST_URI' => '/zh/chs/index.php' }],
1020
- ['https://zh-hant-hk.com/zh/index.php', 'zh-Hant-HK', 'https://my-site.com/index.php', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1021
- ['https://zh-hant-hk.com/zh/index.php?a=1&b=2', 'zh-Hant-HK', 'https://my-site.com/index.php?a=1&b=2', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1022
- ['https://zh-hant-hk.com/zh/index.php#hash', 'zh-Hant-HK', 'https://my-site.com/index.php#hash', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1023
- ['https://zh-hant-hk.com/zh/index.php?a=1&b=2#hash', 'zh-Hant-HK', 'https://my-site.com/index.php?a=1&b=2#hash', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1024
-
1025
- # absolute path
1026
- ['/', 'en', '/', {}],
1027
- ['/ja/', 'ja', '/', { 'REQUEST_URI' => '/ja' }],
1028
- ['/ja/index.php', 'ja', '/index.php', { 'REQUEST_URI' => '/ja/index.php' }],
1029
- ['/ja/a/b/', 'ja', '/a/b/', { 'REQUEST_URI' => '/ja/a/b/' }],
1030
- ['/ja/a/b/index.php', 'ja', '/a/b/index.php', { 'REQUEST_URI' => '/ja/a/b/index.php' }],
1031
- ['/index.php', 'en-US', '/index.php', { 'HTTP_HOST' => 'en-us.my-site.com', 'SERVER_NAME' => 'en-us.my-site.com', 'REQUEST_URI' => '/index.php' }],
1032
- ['/zh/chs/index.php', 'zh-CHS', '/index.php', { 'REQUEST_URI' => '/zh/chs/index.php' }],
1033
- ['/zh/index.php', 'zh-Hant-HK', '/index.php', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1034
- ['/zh/index.php?a=1&b=2', 'zh-Hant-HK', '/index.php?a=1&b=2', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1035
- ['/zh/index.php#hash', 'zh-Hant-HK', '/index.php#hash', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1036
- ['/zh/index.php?a=1&b=2#hash', 'zh-Hant-HK', '/index.php?a=1&b=2#hash', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1037
-
1038
- # other patterns should not be changed
1039
- ['?a=1&b=2', 'en-US', '?a=1&b=2', { 'HTTP_HOST' => 'en-us.my-site.com', 'SERVER_NAME' => 'en-us.my-site.com', 'REQUEST_URI' => '/' }],
1040
- ['#hash', 'en-US', '#hash', { 'HTTP_HOST' => 'en-us.my-site.com', 'SERVER_NAME' => 'en-us.my-site.com', 'REQUEST_URI' => '/' }]
1041
- ]
1042
-
1043
- settings = {
1044
- 'project_token' => 'T0k3N',
1045
- 'default_lang' => 'en',
1046
- 'supported_langs' => %w[en en-US ja zh-CHS zh-Hant-HK],
1047
- 'url_pattern' => 'custom_domain',
1048
- 'custom_domain_langs' => custom_domain_langs
1049
- }
1050
- base_env = {
1051
- 'HTTP_HOST' => 'my-site.com',
1052
- 'REQUEST_URI' => '/req_uri/'
1053
- }
1054
-
1055
- test_cases.each do |test_case|
1056
- target_uri, lang, expected_uri, env = test_case
1057
- additional_env = base_env.merge(env)
1058
- store = Wovnrb::Store.instance
1059
- store.update_settings(settings)
1060
- url_lang_switcher = UrlLanguageSwitcher.new(store)
1061
- headers = Wovnrb::Headers.new(
1062
- Wovnrb.get_env(additional_env),
1063
- store.settings,
1064
- url_lang_switcher
1065
- )
1066
-
1067
- assert_equal(expected_uri, url_lang_switcher.remove_lang_from_uri_component(target_uri, lang, headers))
1068
- end
1069
- end
1070
-
1071
- def store_headers_factory(setting_opts = {}, url = 'http://my-site.com')
1072
- settings = default_store_settings.merge(setting_opts)
1073
- store = Wovnrb::Store.instance
1074
- store.update_settings(settings)
1075
- url_lang_switcher = UrlLanguageSwitcher.new(store)
1076
-
1077
- headers = Wovnrb::Headers.new(
1078
- Wovnrb.get_env('url' => url),
1079
- store.settings,
1080
- url_lang_switcher
1081
- )
1082
-
1083
- [store, headers]
1084
- end
1085
-
1086
- def default_store_settings
1087
- {
1088
- 'project_token' => '123456',
1089
- 'custom_lang_aliases' => {},
1090
- 'default_lang' => 'en',
1091
- 'url_pattern' => 'query',
1092
- 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|)',
1093
- 'supported_langs' => %w[en fr ja vi]
1094
- }
1095
- end
1096
- end
1097
- end
1
+ require 'test_helper'
2
+
3
+ module Wovnrb
4
+ class UrlLanguageSwitcherTest < WovnMiniTest
5
+ def test_add_lang_code
6
+ lang_code = 'zh-cht'
7
+ store_options = { 'url_pattern' => 'subdomain' }
8
+
9
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
10
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
11
+
12
+ res = url_lang_switcher.add_lang_code('http://www.facebook.com', lang_code, headers)
13
+
14
+ assert_equal('http://www.facebook.com', res)
15
+ end
16
+
17
+ def test_add_lang_code_relative_slash_href_url_with_path
18
+ lang_code = 'fr'
19
+
20
+ store_options = { 'url_pattern' => 'subdomain' }
21
+
22
+ store, headers = store_headers_factory(store_options, 'http://favy.tips/topics/44')
23
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
24
+
25
+ res = url_lang_switcher.add_lang_code('/topics/50', lang_code, headers)
26
+
27
+ assert_equal('http://fr.favy.tips/topics/50', res)
28
+ end
29
+
30
+ def test_add_lang_code_relative_dot_href_url_with_path
31
+ lang_code = 'fr'
32
+
33
+ store_options = { 'url_pattern' => 'subdomain' }
34
+
35
+ store, headers = store_headers_factory(store_options, 'http://favy.tips/topics/44')
36
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
37
+
38
+ res = url_lang_switcher.add_lang_code('./topics/50', lang_code, headers)
39
+
40
+ assert_equal('http://fr.favy.tips/topics/topics/50', res)
41
+ end
42
+
43
+ def test_add_lang_code_relative_two_dots_href_url_with_path
44
+ lang_code = 'fr'
45
+
46
+ store_options = { 'url_pattern' => 'subdomain' }
47
+
48
+ store, headers = store_headers_factory(store_options, 'http://favy.tips/topics/44')
49
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
50
+
51
+ res = url_lang_switcher.add_lang_code('../topics/50', lang_code, headers)
52
+
53
+ assert_equal('http://fr.favy.tips/topics/50', res)
54
+ end
55
+
56
+ def test_add_lang_code_trad_chinese
57
+ lang_code = 'zh-cht'
58
+
59
+ store_options = { 'url_pattern' => 'subdomain' }
60
+
61
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
62
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
63
+
64
+ res = url_lang_switcher.add_lang_code('http://favy.tips/topics/31', lang_code, headers)
65
+
66
+ assert_equal('http://zh-cht.favy.tips/topics/31', res)
67
+ end
68
+
69
+ def test_add_lang_code_trad_chinese_two
70
+ lang_code = 'zh-cht'
71
+
72
+ store_options = { 'url_pattern' => 'subdomain' }
73
+
74
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
75
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
76
+
77
+ res = url_lang_switcher.add_lang_code('/topics/31', lang_code, headers)
78
+
79
+ assert_equal('http://zh-cht.favy.tips/topics/31', res)
80
+ end
81
+
82
+ def test_add_lang_code_trad_chinese_lang_in_link_already
83
+ lang_code = 'zh-cht'
84
+
85
+ store_options = { 'url_pattern' => 'subdomain' }
86
+
87
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
88
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
89
+
90
+ res = url_lang_switcher.add_lang_code('http://zh-cht.favy.tips/topics/31', lang_code, headers)
91
+
92
+ assert_equal('http://zh-cht.favy.tips/topics/31', res)
93
+ end
94
+
95
+ def test_add_lang_code_no_protocol
96
+ lang_code = 'zh-cht'
97
+
98
+ store_options = { 'url_pattern' => 'subdomain' }
99
+
100
+ store, headers = store_headers_factory(store_options, 'https://google.com')
101
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
102
+
103
+ res = url_lang_switcher.add_lang_code('//google.com', lang_code, headers)
104
+
105
+ assert_equal('//zh-cht.google.com', res)
106
+ end
107
+
108
+ def test_add_lang_code_no_protocol_two
109
+ lang_code = 'zh-cht'
110
+
111
+ store_options = { 'url_pattern' => 'subdomain' }
112
+
113
+ store, headers = store_headers_factory(store_options, 'https://favy.tips')
114
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
115
+
116
+ res = url_lang_switcher.add_lang_code('//google.com', lang_code, headers)
117
+
118
+ assert_equal('//google.com', res)
119
+ end
120
+
121
+ def test_add_lang_code_invalid_url
122
+ lang_code = 'zh-cht'
123
+ store_options = { 'url_pattern' => 'subdomain' }
124
+
125
+ store, headers = store_headers_factory(store_options, 'https://favy.tips')
126
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
127
+
128
+ res = url_lang_switcher.add_lang_code('http://www.facebook.com/sharer.php?u=http://favy.tips/topics/50&amp;amp;t=Gourmet Tofu World: Vegetarian-Friendly Japanese Food is Here!', lang_code, headers)
129
+
130
+ assert_equal('http://www.facebook.com/sharer.php?u=http://favy.tips/topics/50&amp;amp;t=Gourmet Tofu World: Vegetarian-Friendly Japanese Food is Here!', res)
131
+ end
132
+
133
+ def test_add_lang_code_path_only_with_slash
134
+ lang_code = 'zh-cht'
135
+
136
+ store_options = { 'url_pattern' => 'subdomain' }
137
+
138
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
139
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
140
+
141
+ res = url_lang_switcher.add_lang_code('/topics/31', lang_code, headers)
142
+
143
+ assert_equal('http://zh-cht.favy.tips/topics/31', res)
144
+ end
145
+
146
+ def test_add_lang_code_path_only_no_slash
147
+ lang_code = 'zh-cht'
148
+ store_options = { 'url_pattern' => 'subdomain' }
149
+
150
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
151
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
152
+
153
+ res = url_lang_switcher.add_lang_code('topics/31', lang_code, headers)
154
+
155
+ assert_equal('http://zh-cht.favy.tips/topics/31', res)
156
+ end
157
+
158
+ def test_add_lang_code_path_explicit_page_no_slash
159
+ lang_code = 'zh-cht'
160
+
161
+ store_options = { 'url_pattern' => 'subdomain' }
162
+
163
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
164
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
165
+
166
+ res = url_lang_switcher.add_lang_code('topics/31.html', lang_code, headers)
167
+
168
+ assert_equal('http://zh-cht.favy.tips/topics/31.html', res)
169
+ end
170
+
171
+ def test_add_lang_code_path_explicit_page_with_slash
172
+ lang_code = 'zh-cht'
173
+
174
+ store_options = { 'url_pattern' => 'subdomain' }
175
+
176
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
177
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
178
+
179
+ res = url_lang_switcher.add_lang_code('/topics/31.html', lang_code, headers)
180
+
181
+ assert_equal('http://zh-cht.favy.tips/topics/31.html', res)
182
+ end
183
+
184
+ def test_add_lang_code_no_protocol_with_path_explicit_page
185
+ lang_code = 'zh-cht'
186
+
187
+ store_options = { 'url_pattern' => 'subdomain' }
188
+
189
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
190
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
191
+
192
+ res = url_lang_switcher.add_lang_code('//www.google.com/topics/31.php', lang_code, headers)
193
+
194
+ assert_equal('//www.google.com/topics/31.php', res)
195
+ end
196
+
197
+ def test_add_lang_code_protocol_with_path_explicit_page
198
+ lang_code = 'zh-cht'
199
+
200
+ store_options = { 'url_pattern' => 'subdomain' }
201
+
202
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
203
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
204
+
205
+ res = url_lang_switcher.add_lang_code('http://www.google.com/topics/31.php', lang_code, headers)
206
+
207
+ assert_equal('http://www.google.com/topics/31.php', res)
208
+ end
209
+
210
+ def test_add_lang_code_relative_path_double_period
211
+ lang_code = 'zh-cht'
212
+
213
+ store_options = { 'url_pattern' => 'subdomain' }
214
+
215
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
216
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
217
+
218
+ res = url_lang_switcher.add_lang_code('../topics/31', lang_code, headers)
219
+
220
+ assert_equal('http://zh-cht.favy.tips/topics/31', res)
221
+ end
222
+
223
+ def test_add_lang_code_relative_path_single_period
224
+ lang_code = 'zh-cht'
225
+
226
+ store_options = { 'url_pattern' => 'subdomain' }
227
+
228
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
229
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
230
+
231
+ res = url_lang_switcher.add_lang_code('./topics/31', lang_code, headers)
232
+
233
+ assert_equal('http://zh-cht.favy.tips/topics/31', res)
234
+ end
235
+
236
+ def test_add_lang_code_empty_href
237
+ lang_code = 'zh-cht'
238
+
239
+ store_options = { 'url_pattern' => 'subdomain' }
240
+
241
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
242
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
243
+
244
+ res = url_lang_switcher.add_lang_code('', lang_code, headers)
245
+
246
+ assert_equal('', res)
247
+ end
248
+
249
+ def test_add_lang_code_hash_href
250
+ lang_code = 'zh-cht'
251
+
252
+ store_options = { 'url_pattern' => 'subdomain' }
253
+
254
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
255
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
256
+
257
+ res = url_lang_switcher.add_lang_code('#', lang_code, headers)
258
+
259
+ assert_equal('#', res)
260
+ end
261
+
262
+ def test_add_lang_code_nil_href
263
+ lang_code = 'en'
264
+ store_options = { 'url_pattern' => 'subdomain' }
265
+
266
+ store, headers = store_headers_factory(store_options, 'http://favy.tips')
267
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
268
+
269
+ res = url_lang_switcher.add_lang_code(nil, lang_code, headers)
270
+
271
+ assert_nil(res)
272
+ end
273
+
274
+ def test_add_lang_code_absolute_different_host
275
+ lang_code = 'fr'
276
+ store_options = { 'url_pattern' => 'subdomain' }
277
+
278
+ store, headers = store_headers_factory(store_options, 'http://google.com')
279
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
280
+
281
+ res = url_lang_switcher.add_lang_code('http://yahoo.co.jp', lang_code, headers)
282
+
283
+ assert_equal('http://yahoo.co.jp', res)
284
+ end
285
+
286
+ def test_add_lang_code_absolute_subdomain_no_subdomain
287
+ lang_code = 'fr'
288
+ store_options = { 'url_pattern' => 'subdomain' }
289
+
290
+ store, headers = store_headers_factory(store_options, 'http://google.com')
291
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
292
+
293
+ res = url_lang_switcher.add_lang_code('http://google.com', lang_code, headers)
294
+
295
+ assert_equal('http://fr.google.com', res)
296
+ end
297
+
298
+ def test_add_lang_code_absolute_subdomain_with_subdomain
299
+ lang_code = 'fr'
300
+
301
+ store_options = { 'url_pattern' => 'subdomain' }
302
+
303
+ store, headers = store_headers_factory(store_options, 'http://home.google.com')
304
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
305
+
306
+ res = url_lang_switcher.add_lang_code('http://home.google.com', lang_code, headers)
307
+
308
+ assert_equal('http://fr.home.google.com', res)
309
+ end
310
+
311
+ def test_add_lang_code_absolute_query_no_query
312
+ lang_code = 'fr'
313
+
314
+ store_options = { 'url_pattern' => 'query' }
315
+
316
+ store, headers = store_headers_factory(store_options, 'http://google.com')
317
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
318
+
319
+ res = url_lang_switcher.add_lang_code('http://google.com', lang_code, headers)
320
+
321
+ assert_equal('http://google.com?wovn=fr', res)
322
+ end
323
+
324
+ def test_add_lang_code_absolute_query_with_query
325
+ lang_code = 'fr'
326
+
327
+ store_options = { 'url_pattern' => 'query' }
328
+
329
+ store, headers = store_headers_factory(store_options, 'http://google.com')
330
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
331
+
332
+ res = url_lang_switcher.add_lang_code('http://google.com?hey=yo', lang_code, headers)
333
+
334
+ assert_equal('http://google.com?hey=yo&wovn=fr', res)
335
+ end
336
+
337
+ def test_add_lang_code_absolute_query_with_hash
338
+ lang_code = 'fr'
339
+
340
+ store_options = { 'url_pattern' => 'query' }
341
+
342
+ store, headers = store_headers_factory(store_options, 'http://google.com')
343
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
344
+
345
+ res = url_lang_switcher.add_lang_code('http://google.com#test', lang_code, headers)
346
+
347
+ assert_equal('http://google.com?wovn=fr#test', res)
348
+ end
349
+
350
+ def test_add_lang_code_absolute_query_and_lang_param_name_with_no_query
351
+ lang_code = 'fr'
352
+
353
+ store_options = { 'url_pattern' => 'query', 'lang_param_name' => 'test_param' }
354
+
355
+ store, headers = store_headers_factory(store_options, 'http://google.com')
356
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
357
+
358
+ res = url_lang_switcher.add_lang_code('http://google.com', lang_code, headers)
359
+
360
+ assert_equal('http://google.com?test_param=fr', res)
361
+ end
362
+
363
+ def test_add_lang_code_absolute_query_and_lang_param_name_with_query
364
+ lang_code = 'fr'
365
+
366
+ store_options = { 'url_pattern' => 'query', 'lang_param_name' => 'test_param' }
367
+
368
+ store, headers = store_headers_factory(store_options, 'http://google.com')
369
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
370
+
371
+ res = url_lang_switcher.add_lang_code('http://google.com?hey=yo', lang_code, headers)
372
+
373
+ assert_equal('http://google.com?hey=yo&test_param=fr', res)
374
+ end
375
+
376
+ def test_add_lang_code_absolute_query_and_lang_param_name_with_hash
377
+ lang_code = 'fr'
378
+
379
+ store_options = { 'url_pattern' => 'query', 'lang_param_name' => 'test_param' }
380
+
381
+ store, headers = store_headers_factory(store_options, 'http://google.com')
382
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
383
+
384
+ res = url_lang_switcher.add_lang_code('http://google.com#test', lang_code, headers)
385
+
386
+ assert_equal('http://google.com?test_param=fr#test', res)
387
+ end
388
+
389
+ def test_add_lang_code_absolute_path_no_pathname
390
+ lang_code = 'fr'
391
+
392
+ store_options = { 'url_pattern' => 'path' }
393
+
394
+ store, headers = store_headers_factory(store_options, 'http://google.com')
395
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
396
+
397
+ res = url_lang_switcher.add_lang_code('http://google.com', lang_code, headers)
398
+
399
+ assert_equal('http://google.com/fr', res)
400
+ end
401
+
402
+ def test_add_lang_code__requested_with_deep_path
403
+ lang_code = 'fr'
404
+
405
+ store_options = { 'url_pattern' => 'path' }
406
+
407
+ store, headers = store_headers_factory(store_options, 'http://google.com/dir1/dir2')
408
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
409
+
410
+ assert_equal('http://google.com/fr', url_lang_switcher.add_lang_code('http://google.com', lang_code, headers))
411
+ assert_equal('/fr/', url_lang_switcher.add_lang_code('/', lang_code, headers))
412
+ assert_equal('', url_lang_switcher.add_lang_code('', lang_code, headers))
413
+ end
414
+
415
+ def test_add_lang_code_absolute_path_with_pathname
416
+ lang_code = 'fr'
417
+
418
+ store_options = { 'url_pattern' => 'path' }
419
+
420
+ store, headers = store_headers_factory(store_options, 'http://google.com')
421
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
422
+
423
+ res = url_lang_switcher.add_lang_code('http://google.com/index.html', lang_code, headers)
424
+
425
+ assert_equal('http://google.com/fr/index.html', res)
426
+ end
427
+
428
+ def test_add_lang_code_absolute_path_with_pathname_hash_is_preserved
429
+ lang_code = 'fr'
430
+
431
+ store_options = { 'url_pattern' => 'path' }
432
+
433
+ store, headers = store_headers_factory(store_options, 'http://google.com')
434
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
435
+
436
+ res = url_lang_switcher.add_lang_code('http://google.com/index.html?foo=bar#hash', lang_code, headers)
437
+
438
+ assert_equal('http://google.com/fr/index.html?foo=bar#hash', res)
439
+ end
440
+
441
+ def test_add_lang_code_absolute_path_with_long_pathname
442
+ lang_code = 'fr'
443
+
444
+ store_options = { 'url_pattern' => 'path' }
445
+
446
+ store, headers = store_headers_factory(store_options, 'http://google.com')
447
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
448
+
449
+ res = url_lang_switcher.add_lang_code('http://google.com/hello/long/path/index.html', lang_code, headers)
450
+
451
+ assert_equal('http://google.com/fr/hello/long/path/index.html', res)
452
+ end
453
+
454
+ def test_add_lang_code_relative_subdomain_leading_slash
455
+ lang_code = 'fr'
456
+
457
+ store_options = { 'url_pattern' => 'subdomain' }
458
+
459
+ store, headers = store_headers_factory(store_options, 'http://google.com')
460
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
461
+
462
+ res = url_lang_switcher.add_lang_code('/', lang_code, headers)
463
+
464
+ assert_equal('http://fr.google.com/', res)
465
+ end
466
+
467
+ def test_add_lang_code_relative_subdomain_leading_slash_filename
468
+ lang_code = 'fr'
469
+
470
+ store_options = { 'url_pattern' => 'subdomain' }
471
+
472
+ store, headers = store_headers_factory(store_options, 'http://google.com')
473
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
474
+
475
+ res = url_lang_switcher.add_lang_code('/index.html', lang_code, headers)
476
+
477
+ assert_equal('http://fr.google.com/index.html', res)
478
+ end
479
+
480
+ def test_add_lang_code_relative_subdomain_no_leading_slash_filename
481
+ lang_code = 'fr'
482
+
483
+ store_options = { 'url_pattern' => 'subdomain' }
484
+
485
+ store, headers = store_headers_factory(store_options, 'http://google.com')
486
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
487
+
488
+ res = url_lang_switcher.add_lang_code('index.html', lang_code, headers)
489
+
490
+ assert_equal('http://fr.google.com/index.html', res)
491
+ end
492
+
493
+ def test_add_lang_code_relative_query_with_no_query
494
+ lang_code = 'fr'
495
+
496
+ store_options = { 'url_pattern' => 'query' }
497
+
498
+ store, headers = store_headers_factory(store_options, 'http://google.com')
499
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
500
+
501
+ res = url_lang_switcher.add_lang_code('/index.html', lang_code, headers)
502
+
503
+ assert_equal('/index.html?wovn=fr', res)
504
+ end
505
+
506
+ def test_add_lang_code_relative_query_with_query
507
+ lang_code = 'fr'
508
+
509
+ store_options = { 'url_pattern' => 'query' }
510
+
511
+ store, headers = store_headers_factory(store_options, 'http://google.com')
512
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
513
+
514
+ res = url_lang_switcher.add_lang_code('/index.html?hey=yo', lang_code, headers)
515
+
516
+ assert_equal('/index.html?hey=yo&wovn=fr', res)
517
+ end
518
+
519
+ def test_add_lang_code_relative_query_with_hash
520
+ lang_code = 'fr'
521
+
522
+ store_options = { 'url_pattern' => 'query' }
523
+
524
+ store, headers = store_headers_factory(store_options, 'http://google.com')
525
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
526
+
527
+ res = url_lang_switcher.add_lang_code('/index.html?hey=yo', lang_code, headers)
528
+
529
+ assert_equal('/index.html?hey=yo&wovn=fr', res)
530
+ end
531
+
532
+ def test_add_lang_code_relative_query_and_lang_param_name_with_no_query
533
+ lang_code = 'fr'
534
+
535
+ store_options = { 'url_pattern' => 'query', 'lang_param_name' => 'test_param' }
536
+
537
+ store, headers = store_headers_factory(store_options, 'http://google.com')
538
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
539
+
540
+ res = url_lang_switcher.add_lang_code('/index.html', lang_code, headers)
541
+
542
+ assert_equal('/index.html?test_param=fr', res)
543
+ end
544
+
545
+ def test_add_lang_code_relative_query_and_lang_param_name_with_query
546
+ lang_code = 'fr'
547
+
548
+ store_options = { 'url_pattern' => 'query', 'lang_param_name' => 'test_param' }
549
+
550
+ store, headers = store_headers_factory(store_options, 'http://google.com')
551
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
552
+
553
+ res = url_lang_switcher.add_lang_code('/index.html?hey=yo', lang_code, headers)
554
+
555
+ assert_equal('/index.html?hey=yo&test_param=fr', res)
556
+ end
557
+
558
+ def test_add_lang_code_relative_query_and_lang_param_name_with_hash
559
+ lang_code = 'fr'
560
+
561
+ store_options = { 'url_pattern' => 'query', 'lang_param_name' => 'test_param' }
562
+
563
+ store, headers = store_headers_factory(store_options, 'http://google.com')
564
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
565
+
566
+ res = url_lang_switcher.add_lang_code('/index.html?hey=yo#hey', lang_code, headers)
567
+
568
+ assert_equal('/index.html?hey=yo&test_param=fr#hey', res)
569
+ end
570
+
571
+ def test_add_lang_code_relative_path_with_leading_slash
572
+ lang_code = 'fr'
573
+
574
+ store_options = { 'url_pattern' => 'path' }
575
+
576
+ store, headers = store_headers_factory(store_options, 'http://google.com')
577
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
578
+
579
+ res = url_lang_switcher.add_lang_code('/index.html', lang_code, headers)
580
+
581
+ assert_equal('/fr/index.html', res)
582
+ end
583
+
584
+ def test_add_lang_code_relative_path_without_leading_slash_different_pathname
585
+ lang_code = 'fr'
586
+
587
+ store_options = { 'url_pattern' => 'path' }
588
+
589
+ store, headers = store_headers_factory(store_options, 'http://google.com/hello/tab.html')
590
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
591
+
592
+ res = url_lang_switcher.add_lang_code('index.html', lang_code, headers)
593
+
594
+ assert_equal('/fr/hello/index.html', res)
595
+ end
596
+
597
+ def test_add_lang_code_relative_path_without_leading_slash_and_dot_dot
598
+ lang_code = 'fr'
599
+
600
+ store_options = { 'url_pattern' => 'path' }
601
+
602
+ store, headers = store_headers_factory(store_options, 'https://pre.avex.jp/wovn_aaa/news/')
603
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
604
+
605
+ res = url_lang_switcher.add_lang_code('../news/', lang_code, headers)
606
+
607
+ assert_equal("/#{lang_code}/wovn_aaa/news/", res)
608
+ end
609
+
610
+ def test_add_lang_code_relative_path_without_leading_slash_different_pathname2
611
+ lang_code = 'fr'
612
+
613
+ store_options = { 'url_pattern' => 'path' }
614
+
615
+ store, headers = store_headers_factory(store_options, 'http://google.com/hello/tab.html')
616
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
617
+
618
+ res = url_lang_switcher.add_lang_code('hey/index.html', lang_code, headers)
619
+
620
+ assert_equal('/fr/hello/hey/index.html', res)
621
+ end
622
+
623
+ def test_add_lang_code_relative_path_at_root
624
+ lang_code = 'fr'
625
+
626
+ store_options = { 'url_pattern' => 'path' }
627
+
628
+ store, headers = store_headers_factory(store_options, 'http://google.com/')
629
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
630
+
631
+ res = url_lang_switcher.add_lang_code('index.html', lang_code, headers)
632
+
633
+ assert_equal('/fr/index.html', res)
634
+ end
635
+
636
+ def test_add_lang_code__jp_domain__path_pattern__absolute_path
637
+ lang_code = 'ja'
638
+
639
+ store_options = { 'url_pattern' => 'path' }
640
+
641
+ store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
642
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
643
+
644
+ res = url_lang_switcher.add_lang_code('/page', lang_code, headers)
645
+
646
+ assert_equal('/ja/page', res)
647
+ end
648
+
649
+ def test_add_lang_code__jp_domain__path_pattern__absolute_path_with_query
650
+ lang_code = 'ja'
651
+
652
+ store_options = { 'url_pattern' => 'path' }
653
+
654
+ store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
655
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
656
+
657
+ res = url_lang_switcher.add_lang_code('/page?user=tom', lang_code, headers)
658
+
659
+ assert_equal('/ja/page?user=tom', res)
660
+ end
661
+
662
+ def test_add_lang_code__jp_domain__path_pattern__absolute_path_with_query__top_page
663
+ lang_code = 'ja'
664
+
665
+ store_options = { 'url_pattern' => 'path' }
666
+
667
+ store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
668
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
669
+
670
+ res = url_lang_switcher.add_lang_code('/?user=tom', lang_code, headers)
671
+
672
+ assert_equal('/ja/?user=tom', res)
673
+ end
674
+
675
+ def test_add_lang_code__jp_domain__path_pattern__absolute_path_with_hash__top_page
676
+ lang_code = 'ja'
677
+
678
+ store_options = { 'url_pattern' => 'path' }
679
+
680
+ store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
681
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
682
+
683
+ res = url_lang_switcher.add_lang_code('/#top', lang_code, headers)
684
+
685
+ assert_equal('/ja/#top', res)
686
+ end
687
+
688
+ def test_add_lang_code__jp_domain__path_pattern__absolute_url
689
+ lang_code = 'ja'
690
+
691
+ store_options = { 'url_pattern' => 'path' }
692
+
693
+ store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
694
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
695
+
696
+ res = url_lang_switcher.add_lang_code('http://favy.co.jp/page', lang_code, headers)
697
+
698
+ assert_equal('http://favy.co.jp/ja/page', res)
699
+ end
700
+
701
+ def test_add_lang_code__jp_domain__path_pattern__absolute_url_with_query
702
+ lang_code = 'ja'
703
+
704
+ store_options = { 'url_pattern' => 'path' }
705
+
706
+ store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
707
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
708
+
709
+ res = url_lang_switcher.add_lang_code('http://favy.co.jp?user=tom', lang_code, headers)
710
+
711
+ assert_equal('http://favy.co.jp/ja?user=tom', res)
712
+ end
713
+
714
+ def test_add_lang_code__jp_domain__path_pattern__absolute_url_with_trailing_slash_and_query
715
+ lang_code = 'ja'
716
+
717
+ store_options = { 'url_pattern' => 'path' }
718
+
719
+ store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
720
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
721
+
722
+ res = url_lang_switcher.add_lang_code('http://favy.co.jp/?user=tom', lang_code, headers)
723
+
724
+ assert_equal('http://favy.co.jp/ja/?user=tom', res)
725
+ end
726
+
727
+ def test_add_lang_code__jp_domain__path_pattern__absolute_url_with_hash
728
+ lang_code = 'ja'
729
+
730
+ store_options = { 'url_pattern' => 'path' }
731
+
732
+ store, headers = store_headers_factory(store_options, 'http://favy.co.jp')
733
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
734
+
735
+ res = url_lang_switcher.add_lang_code('http://favy.co.jp#top', lang_code, headers)
736
+
737
+ assert_equal('http://favy.co.jp/ja#top', res)
738
+ end
739
+
740
+ def test_add_lang_code__absolute_url_with_default_lang_alias__replaces_lang_code
741
+ lang_aliases = {
742
+ 'en' => 'en'
743
+ }
744
+
745
+ store_options = { 'url_pattern' => 'path', 'custom_lang_aliases' => lang_aliases }
746
+
747
+ store, headers = store_headers_factory(store_options, 'http://www.example.com/th/')
748
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
749
+
750
+ href_no_trailing_slash = 'http://www.example.com/en'
751
+ href_trailing_slash = 'http://www.example.com/en/'
752
+
753
+ assert_equal('http://www.example.com/th', url_lang_switcher.add_lang_code(href_no_trailing_slash, 'th', headers))
754
+ assert_equal('http://www.example.com/th/', url_lang_switcher.add_lang_code(href_trailing_slash, 'th', headers))
755
+ end
756
+
757
+ def test_add_lang_code__absolute_path_with_default_lang_alias__replaces_lang_code
758
+ lang_aliases = {
759
+ 'en' => 'en'
760
+ }
761
+
762
+ store_options = { 'url_pattern' => 'path', 'custom_lang_aliases' => lang_aliases }
763
+
764
+ store, headers = store_headers_factory(store_options, 'http://www.example.com/th/')
765
+ url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
766
+
767
+ href_no_trailing_slash = '/en'
768
+ href_trailing_slash = '/en/'
769
+
770
+ assert_equal('/th', url_lang_switcher.add_lang_code(href_no_trailing_slash, 'th', headers))
771
+ assert_equal('/th/', url_lang_switcher.add_lang_code(href_trailing_slash, 'th', headers))
772
+ end
773
+
774
+ def test_add_lang_code_with_custom_domain_langs
775
+ custom_domain_langs = {
776
+ 'en' => { 'url' => 'my-site.com' },
777
+ 'en-US' => { 'url' => 'en-us.my-site.com' },
778
+ 'ja' => { 'url' => 'my-site.com/ja' },
779
+ 'zh-CHS' => { 'url' => 'my-site.com/zh/chs' },
780
+ 'zh-Hant-HK' => { 'url' => 'zh-hant-hk.com/zh' }
781
+ }
782
+ test_cases = [
783
+ # no_lang_url, lang_code, expected_url
784
+ # absolute URL
785
+ ['https://my-site.com', 'en', 'https://my-site.com'],
786
+ ['https://my-site.com', 'ja', 'https://my-site.com/ja'],
787
+ ['https://my-site.com/index.php', 'ja', 'https://my-site.com/ja/index.php'],
788
+ ['https://my-site.com/a/b/', 'ja', 'https://my-site.com/ja/a/b/'],
789
+ ['https://my-site.com/a/b/index.php', 'ja', 'https://my-site.com/ja/a/b/index.php'],
790
+ ['https://my-site.com/index.php', 'en-US', 'https://en-us.my-site.com/index.php'],
791
+ ['https://my-site.com/index.php', 'zh-CHS', 'https://my-site.com/zh/chs/index.php'],
792
+ ['https://my-site.com/index.php', 'zh-Hant-HK', 'https://zh-hant-hk.com/zh/index.php'],
793
+ ['https://my-site.com/index.php?a=1&b=2', 'zh-Hant-HK', 'https://zh-hant-hk.com/zh/index.php?a=1&b=2'],
794
+ ['https://my-site.com/index.php#hash', 'zh-Hant-HK', 'https://zh-hant-hk.com/zh/index.php#hash'],
795
+ ['https://my-site.com/index.php?a=1&b=2#hash', 'zh-Hant-HK', 'https://zh-hant-hk.com/zh/index.php?a=1&b=2#hash'],
796
+
797
+ # absolute path
798
+ ['/', 'en', 'http://my-site.com/'],
799
+ ['/', 'ja', 'http://my-site.com/ja/'],
800
+ ['/index.php', 'ja', 'http://my-site.com/ja/index.php'],
801
+ ['/a/b/', 'ja', 'http://my-site.com/ja/a/b/'],
802
+ ['/a/b/index.php', 'ja', 'http://my-site.com/ja/a/b/index.php'],
803
+ ['/index.php', 'en-US', 'http://en-us.my-site.com/index.php'],
804
+ ['/index.php', 'zh-CHS', 'http://my-site.com/zh/chs/index.php'],
805
+ ['/index.php', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/index.php'],
806
+ ['/index.php?a=1&b=2', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/index.php?a=1&b=2'],
807
+ ['/index.php#hash', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/index.php#hash'],
808
+ ['/index.php?a=1&b=2#hash', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/index.php?a=1&b=2#hash'],
809
+
810
+ # relative path
811
+ ['index.php', 'ja', 'http://my-site.com/ja/req_uri/index.php'],
812
+ ['a/b/', 'ja', 'http://my-site.com/ja/req_uri/a/b/'],
813
+ ['a/b/index.php', 'ja', 'http://my-site.com/ja/req_uri/a/b/index.php'],
814
+ ['index.php', 'en-US', 'http://en-us.my-site.com/req_uri/index.php'],
815
+ ['index.php', 'zh-CHS', 'http://my-site.com/zh/chs/req_uri/index.php'],
816
+ ['index.php', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/req_uri/index.php'],
817
+ ['index.php?a=1&b=2', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/req_uri/index.php?a=1&b=2'],
818
+ ['index.php#hash', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/req_uri/index.php#hash'],
819
+ ['index.php?a=1&b=2#hash', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/req_uri/index.php?a=1&b=2#hash'],
820
+ ['?a=1&b=2', 'zh-Hant-HK', 'http://zh-hant-hk.com/zh/req_uri/?a=1&b=2'],
821
+
822
+ # anchor links should not be changed
823
+ ['#hash', 'zh-Hant-HK', '#hash']
824
+ ]
825
+
826
+ settings = {
827
+ 'project_token' => 'T0k3N',
828
+ 'default_lang' => 'en',
829
+ 'supported_langs' => ['en'],
830
+ 'url_pattern' => 'custom_domain',
831
+ 'custom_domain_langs' => custom_domain_langs
832
+ }
833
+ additional_env = {
834
+ 'HTTP_HOST' => 'my-site.com',
835
+ 'REQUEST_URI' => '/req_uri/'
836
+ }
837
+
838
+ test_cases.each do |test_case|
839
+ target_uri, lang, expected_uri = test_case
840
+ store = Wovnrb::Store.instance
841
+ store.update_settings(settings)
842
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
843
+ headers = Wovnrb::Headers.new(
844
+ Wovnrb.get_env(additional_env),
845
+ store.settings,
846
+ url_lang_switcher
847
+ )
848
+
849
+ assert_equal(expected_uri, url_lang_switcher.add_lang_code(target_uri, lang, headers))
850
+ end
851
+ end
852
+
853
+ def test_remove_lang_query_with_lang_param_name
854
+ settings = Wovnrb.get_settings('url_pattern' => 'query', 'lang_param_name' => 'lang')
855
+ store = Wovnrb.get_store(settings)
856
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
857
+
858
+ keys = Wovnrb::Lang::LANG.keys
859
+ assert(!keys.empty?)
860
+
861
+ keys.each do |key|
862
+ uri_without_custom_lang_param = "wovn.io/?wovn=#{key}"
863
+ unchanged_uri = url_lang_switcher.remove_lang_from_uri_component(uri_without_custom_lang_param, key)
864
+ assert_equal(uri_without_custom_lang_param, unchanged_uri)
865
+
866
+ uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component("wovn.io/?lang=#{key}", key)
867
+ assert_equal('wovn.io/', uri_without_scheme)
868
+
869
+ uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component("https://wovn.io?lang=#{key}", key)
870
+ assert_equal('https://wovn.io', uri_with_scheme)
871
+ end
872
+ end
873
+
874
+ def test_remove_lang_query_with_nil_lang
875
+ settings = Wovnrb.get_settings('url_pattern' => 'query')
876
+ store = Wovnrb.get_store(settings)
877
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
878
+
879
+ keys = Wovnrb::Lang::LANG.keys
880
+ assert(!keys.empty?)
881
+
882
+ uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component('wovn.io', nil)
883
+ assert_equal('wovn.io', uri_without_scheme)
884
+
885
+ uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component('https://wovn.io/', nil)
886
+ assert_equal('https://wovn.io/', uri_with_scheme)
887
+ end
888
+
889
+ def test_remove_lang_query_with_empty_lang
890
+ settings = Wovnrb.get_settings('url_pattern' => 'query')
891
+ store = Wovnrb.get_store(settings)
892
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
893
+
894
+ uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component('wovn.io', '')
895
+ assert_equal('wovn.io', uri_without_scheme)
896
+
897
+ uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component('https://wovn.io/', '')
898
+ assert_equal('https://wovn.io/', uri_with_scheme)
899
+ end
900
+
901
+ def test_remove_lang_subdomain
902
+ settings = Wovnrb.get_settings('url_pattern' => 'subdomain')
903
+ store = Wovnrb.get_store(settings)
904
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
905
+
906
+ keys = Wovnrb::Lang::LANG.keys
907
+ assert(!keys.empty?)
908
+
909
+ keys.each do |key|
910
+ uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component("#{key.downcase}.wovn.io/", key)
911
+ assert_equal('wovn.io/', uri_without_scheme)
912
+
913
+ uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component("https://#{key.downcase}.wovn.io", key)
914
+ assert_equal('https://wovn.io', uri_with_scheme)
915
+ end
916
+ end
917
+
918
+ def test_remove_lang_subdomain_with_nil_lang
919
+ settings = Wovnrb.get_settings('url_pattern' => 'subdomain')
920
+ store = Wovnrb.get_store(settings)
921
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
922
+ keys = Wovnrb::Lang::LANG.keys
923
+ assert(!keys.empty?)
924
+
925
+ uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component('wovn.io', nil)
926
+ assert_equal('wovn.io', uri_without_scheme)
927
+
928
+ uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component('https://wovn.io/', nil)
929
+ assert_equal('https://wovn.io/', uri_with_scheme)
930
+ end
931
+
932
+ def test_remove_lang_subdomain_with_empty_lang
933
+ settings = Wovnrb.get_settings('url_pattern' => 'subdomain')
934
+ store = Wovnrb.get_store(settings)
935
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
936
+
937
+ uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component('wovn.io', '')
938
+ assert_equal('wovn.io', uri_without_scheme)
939
+
940
+ uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component('https://wovn.io/', '')
941
+ assert_equal('https://wovn.io/', uri_with_scheme)
942
+ end
943
+
944
+ def test_remove_lang_subdomain_with_custom_lang_alias
945
+ Store.instance.update_settings('custom_lang_aliases' => { 'fr' => 'staging-fr' }, 'url_pattern' => 'subdomain')
946
+ url_lang_switcher = UrlLanguageSwitcher.new(Store.instance)
947
+
948
+ uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component('staging-fr.wovn.io/', 'fr')
949
+ assert_equal('wovn.io/', uri_without_scheme)
950
+
951
+ uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component('https://staging-fr.wovn.io', 'fr')
952
+ assert_equal('https://wovn.io', uri_with_scheme)
953
+ end
954
+
955
+ def test_remove_lang_path
956
+ settings = Wovnrb.get_settings
957
+ store = Wovnrb.get_store(settings)
958
+ sut = UrlLanguageSwitcher.new(store)
959
+
960
+ keys = Wovnrb::Lang::LANG.keys
961
+ assert(!keys.empty?)
962
+
963
+ keys.each do |key|
964
+ assert_equal('/', sut.remove_lang_from_uri_component("/#{key}", key))
965
+ assert_equal("/dir/#{key}/page.html", sut.remove_lang_from_uri_component("/#{key}/dir/#{key}/page.html", key))
966
+ assert_equal('?query', sut.remove_lang_from_uri_component('?query', key))
967
+ assert_equal('wovn.io/', sut.remove_lang_from_uri_component("wovn.io/#{key}", key))
968
+ assert_equal("wovn.io/dir/#{key}/page.html", sut.remove_lang_from_uri_component("wovn.io/#{key}/dir/#{key}/page.html", key))
969
+ assert_equal("wovn.io:5000/dir/#{key}/page.html", sut.remove_lang_from_uri_component("wovn.io:5000/#{key}/dir/#{key}/page.html", key))
970
+ assert_equal('https://wovn.io/', sut.remove_lang_from_uri_component("https://wovn.io/#{key}/", key))
971
+ assert_equal("https://wovn.io/dir/#{key}/page.html", sut.remove_lang_from_uri_component("https://wovn.io/#{key}/dir/#{key}/page.html", key))
972
+ end
973
+ end
974
+
975
+ def test_remove_lang_path_with_nil_lang
976
+ settings = Wovnrb.get_settings
977
+ store = Wovnrb.get_store(settings)
978
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
979
+
980
+ keys = Wovnrb::Lang::LANG.keys
981
+ assert(!keys.empty?)
982
+
983
+ uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component('wovn.io', nil)
984
+ assert_equal('wovn.io', uri_without_scheme)
985
+
986
+ uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component('https://wovn.io/', nil)
987
+ assert_equal('https://wovn.io/', uri_with_scheme)
988
+ end
989
+
990
+ def test_remove_lang_path_with_empty_lang
991
+ settings = Wovnrb.get_settings
992
+ store = Wovnrb.get_store(settings)
993
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
994
+
995
+ uri_without_scheme = url_lang_switcher.remove_lang_from_uri_component('wovn.io', '')
996
+ assert_equal('wovn.io', uri_without_scheme)
997
+
998
+ uri_with_scheme = url_lang_switcher.remove_lang_from_uri_component('https://wovn.io/', '')
999
+ assert_equal('https://wovn.io/', uri_with_scheme)
1000
+ end
1001
+
1002
+ def test_remove_lang_custom_domain
1003
+ custom_domain_langs = {
1004
+ 'en' => { 'url' => 'my-site.com' },
1005
+ 'en-US' => { 'url' => 'en-us.my-site.com' },
1006
+ 'ja' => { 'url' => 'my-site.com/ja' },
1007
+ 'zh-CHS' => { 'url' => 'my-site.com/zh/chs' },
1008
+ 'zh-Hant-HK' => { 'url' => 'zh-hant-hk.com/zh' }
1009
+ }
1010
+ test_cases = [
1011
+ # target_uri, lang, expected_uri, env
1012
+ # absolute URL
1013
+ ['https://my-site.com', 'en', 'https://my-site.com', {}],
1014
+ ['https://my-site.com/ja', 'ja', 'https://my-site.com', { 'REQUEST_URI' => '/ja' }],
1015
+ ['https://my-site.com/ja/index.php', 'ja', 'https://my-site.com/index.php', { 'REQUEST_URI' => '/ja/index.php' }],
1016
+ ['https://my-site.com/ja/a/b/', 'ja', 'https://my-site.com/a/b/', { 'REQUEST_URI' => '/ja/a/b/' }],
1017
+ ['https://my-site.com/ja/a/b/index.php', 'ja', 'https://my-site.com/a/b/index.php', { 'REQUEST_URI' => '/ja/a/b/index.php' }],
1018
+ ['https://en-us.my-site.com/index.php', 'en-US', 'https://my-site.com/index.php', { 'HTTP_HOST' => 'en-us.my-site.com', 'SERVER_NAME' => 'en-us.my-site.com', 'REQUEST_URI' => '/index.php' }],
1019
+ ['https://my-site.com/zh/chs/index.php', 'zh-CHS', 'https://my-site.com/index.php', { 'REQUEST_URI' => '/zh/chs/index.php' }],
1020
+ ['https://zh-hant-hk.com/zh/index.php', 'zh-Hant-HK', 'https://my-site.com/index.php', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1021
+ ['https://zh-hant-hk.com/zh/index.php?a=1&b=2', 'zh-Hant-HK', 'https://my-site.com/index.php?a=1&b=2', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1022
+ ['https://zh-hant-hk.com/zh/index.php#hash', 'zh-Hant-HK', 'https://my-site.com/index.php#hash', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1023
+ ['https://zh-hant-hk.com/zh/index.php?a=1&b=2#hash', 'zh-Hant-HK', 'https://my-site.com/index.php?a=1&b=2#hash', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1024
+
1025
+ # absolute path
1026
+ ['/', 'en', '/', {}],
1027
+ ['/ja/', 'ja', '/', { 'REQUEST_URI' => '/ja' }],
1028
+ ['/ja/index.php', 'ja', '/index.php', { 'REQUEST_URI' => '/ja/index.php' }],
1029
+ ['/ja/a/b/', 'ja', '/a/b/', { 'REQUEST_URI' => '/ja/a/b/' }],
1030
+ ['/ja/a/b/index.php', 'ja', '/a/b/index.php', { 'REQUEST_URI' => '/ja/a/b/index.php' }],
1031
+ ['/index.php', 'en-US', '/index.php', { 'HTTP_HOST' => 'en-us.my-site.com', 'SERVER_NAME' => 'en-us.my-site.com', 'REQUEST_URI' => '/index.php' }],
1032
+ ['/zh/chs/index.php', 'zh-CHS', '/index.php', { 'REQUEST_URI' => '/zh/chs/index.php' }],
1033
+ ['/zh/index.php', 'zh-Hant-HK', '/index.php', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1034
+ ['/zh/index.php?a=1&b=2', 'zh-Hant-HK', '/index.php?a=1&b=2', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1035
+ ['/zh/index.php#hash', 'zh-Hant-HK', '/index.php#hash', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1036
+ ['/zh/index.php?a=1&b=2#hash', 'zh-Hant-HK', '/index.php?a=1&b=2#hash', { 'HTTP_HOST' => 'zh-hant-hk.com', 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.php' }],
1037
+
1038
+ # other patterns should not be changed
1039
+ ['?a=1&b=2', 'en-US', '?a=1&b=2', { 'HTTP_HOST' => 'en-us.my-site.com', 'SERVER_NAME' => 'en-us.my-site.com', 'REQUEST_URI' => '/' }],
1040
+ ['#hash', 'en-US', '#hash', { 'HTTP_HOST' => 'en-us.my-site.com', 'SERVER_NAME' => 'en-us.my-site.com', 'REQUEST_URI' => '/' }]
1041
+ ]
1042
+
1043
+ settings = {
1044
+ 'project_token' => 'T0k3N',
1045
+ 'default_lang' => 'en',
1046
+ 'supported_langs' => %w[en en-US ja zh-CHS zh-Hant-HK],
1047
+ 'url_pattern' => 'custom_domain',
1048
+ 'custom_domain_langs' => custom_domain_langs
1049
+ }
1050
+ base_env = {
1051
+ 'HTTP_HOST' => 'my-site.com',
1052
+ 'REQUEST_URI' => '/req_uri/'
1053
+ }
1054
+
1055
+ test_cases.each do |test_case|
1056
+ target_uri, lang, expected_uri, env = test_case
1057
+ additional_env = base_env.merge(env)
1058
+ store = Wovnrb::Store.instance
1059
+ store.update_settings(settings)
1060
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
1061
+ headers = Wovnrb::Headers.new(
1062
+ Wovnrb.get_env(additional_env),
1063
+ store.settings,
1064
+ url_lang_switcher
1065
+ )
1066
+
1067
+ assert_equal(expected_uri, url_lang_switcher.remove_lang_from_uri_component(target_uri, lang, headers))
1068
+ end
1069
+ end
1070
+
1071
+ def store_headers_factory(setting_opts = {}, url = 'http://my-site.com')
1072
+ settings = default_store_settings.merge(setting_opts)
1073
+ store = Wovnrb::Store.instance
1074
+ store.update_settings(settings)
1075
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
1076
+
1077
+ headers = Wovnrb::Headers.new(
1078
+ Wovnrb.get_env('url' => url),
1079
+ store.settings,
1080
+ url_lang_switcher
1081
+ )
1082
+
1083
+ [store, headers]
1084
+ end
1085
+
1086
+ def default_store_settings
1087
+ {
1088
+ 'project_token' => '123456',
1089
+ 'custom_lang_aliases' => {},
1090
+ 'default_lang' => 'en',
1091
+ 'url_pattern' => 'query',
1092
+ 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|)',
1093
+ 'supported_langs' => %w[en fr ja vi]
1094
+ }
1095
+ end
1096
+ end
1097
+ end