wovnrb 3.8.0 → 3.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,82 @@
1
+ require 'test_helper'
2
+ require 'wovnrb/custom_domain/custom_domain_langs'
3
+
4
+ module Wovnrb
5
+ class CustomDomainLangsTest < WovnMiniTest
6
+ def setup
7
+ custom_domain_langs_setting = {
8
+ 'fr' => { 'url' => 'foo.com/' },
9
+ 'ja' => { 'url' => 'foo.com/path', 'source' => 'japan.foo.com/' },
10
+ 'zh-CHS' => { 'url' => 'foo.com/dir/path' },
11
+ 'en' => { 'url' => 'english.foo.com/', 'source' => 'global.foo.com/' }
12
+ }
13
+ @custom_domain_langs = CustomDomainLangs.new(custom_domain_langs_setting)
14
+ end
15
+
16
+ def test_get_custom_domain_lang_by_lang
17
+ assert_nil(@custom_domain_langs.custom_domain_lang_by_lang('unknown'))
18
+
19
+ assert_equal('fr', lang_for(@custom_domain_langs.custom_domain_lang_by_lang('fr')))
20
+ assert_equal('ja', lang_for(@custom_domain_langs.custom_domain_lang_by_lang('ja')))
21
+ assert_equal('zh-CHS', lang_for(@custom_domain_langs.custom_domain_lang_by_lang('zh-CHS')))
22
+ assert_equal('en', lang_for(@custom_domain_langs.custom_domain_lang_by_lang('en')))
23
+ end
24
+
25
+ def test_get_custom_domain_lang_by_url
26
+ assert_nil(@custom_domain_langs.custom_domain_lang_by_url('http://otherdomain.com'))
27
+ assert_nil(@custom_domain_langs.custom_domain_lang_by_url('http://otherdomain.com/path/test.html'))
28
+ assert_nil(@custom_domain_langs.custom_domain_lang_by_url('http://otherdomain.com/dir/path/test.html'))
29
+
30
+ assert_equal('fr', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com')))
31
+ assert_equal('fr', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/')))
32
+ assert_equal('fr', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/test.html')))
33
+
34
+ assert_equal('ja', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/path')))
35
+ assert_equal('ja', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/path/')))
36
+ assert_equal('ja', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/path/dir')))
37
+ assert_equal('ja', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/path/test.html')))
38
+
39
+ assert_equal('zh-CHS', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/dir/path')))
40
+ assert_equal('zh-CHS', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/dir/path/')))
41
+ assert_equal('zh-CHS', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/dir/path/dir')))
42
+ assert_equal('zh-CHS', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://foo.com/dir/path/test.html')))
43
+
44
+ assert_equal('en', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://english.foo.com/dir/path')))
45
+ assert_equal('en', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://english.foo.com/dir/path/')))
46
+ assert_equal('en', lang_for(@custom_domain_langs.custom_domain_lang_by_url('http://english.foo.com/dir/path/test.html')))
47
+ end
48
+
49
+ def test_get_custom_domain_lang_by_url_with_nested_paths
50
+ custom_domain_langs_setting = {
51
+ 'ja' => { 'url' => 'foo.com/path' },
52
+ 'en' => { 'url' => 'foo.com/path/en' },
53
+ 'fr' => { 'url' => 'foo.com/path/fr' }
54
+ }
55
+ custom_domain_langs = CustomDomainLangs.new(custom_domain_langs_setting)
56
+ assert_equal('ja', lang_for(custom_domain_langs.custom_domain_lang_by_url('http://foo.com/path')))
57
+ assert_equal('en', lang_for(custom_domain_langs.custom_domain_lang_by_url('http://foo.com/path/en')))
58
+ assert_equal('fr', lang_for(custom_domain_langs.custom_domain_lang_by_url('http://foo.com/path/fr')))
59
+ end
60
+
61
+ def test_to_html_swapper_hash
62
+ expected = {
63
+ 'foo.com' => 'fr',
64
+ 'foo.com/path' => 'ja',
65
+ 'foo.com/dir/path' => 'zh-CHS',
66
+ 'english.foo.com' => 'en'
67
+ }
68
+
69
+ assert(hash_equals(expected, @custom_domain_langs.to_html_swapper_hash))
70
+ end
71
+
72
+ private
73
+
74
+ def lang_for(custom_domain_lang)
75
+ custom_domain_lang.lang
76
+ end
77
+
78
+ def hash_equals(orig_hash, test_hash)
79
+ (orig_hash <=> test_hash) == 0
80
+ end
81
+ end
82
+ end
@@ -55,7 +55,7 @@ module Wovnrb
55
55
  {
56
56
  'env' => { 'url' => 'https://wovn.io/en' },
57
57
  'setting' => { 'url_pattern' => 'path' },
58
- 'name' => 'test_initialize_with_path_language',
58
+ 'name' => 'test_initialize_with_url_language',
59
59
  'expected' => {
60
60
  'url' => 'wovn.io/'
61
61
  }
@@ -71,7 +71,7 @@ module Wovnrb
71
71
  {
72
72
  'env' => { 'url' => 'https://wovn.io/en/?wovn=zh-CHS' },
73
73
  'setting' => { 'url_pattern' => 'path' },
74
- 'name' => 'test_initialize_with_path_language_with_query',
74
+ 'name' => 'test_initialize_with_url_language_with_query',
75
75
  'expected' => {
76
76
  'url' => 'wovn.io/?wovn=zh-CHS'
77
77
  }
@@ -87,7 +87,7 @@ module Wovnrb
87
87
  {
88
88
  'env' => { 'url' => 'https://wovn.io/en?wovn=zh-CHS' },
89
89
  'setting' => { 'url_pattern' => 'path' },
90
- 'name' => 'test_initialize_with_path_language_with_query_without_slash',
90
+ 'name' => 'test_initialize_with_url_language_with_query_without_slash',
91
91
  'expected' => {
92
92
  'url' => 'wovn.io/?wovn=zh-CHS'
93
93
  }
@@ -177,7 +177,7 @@ module Wovnrb
177
177
  {
178
178
  'env' => { 'REQUEST_URI' => 'http://page.com/ja/test/' },
179
179
  'setting' => { 'url_pattern' => 'path', 'url_pattern_reg' => '/(?<lang>[^/.?]+)' },
180
- 'name' => 'test_pathname_with_trailing_slash_if_present_with_path_lang_when_trailing_slash_is_present',
180
+ 'name' => 'test_pathname_with_trailing_slash_if_present_with_url_language_when_trailing_slash_is_present',
181
181
  'expected' => {
182
182
  'pathname_with_trailing_slash_if_present' => '/test/'
183
183
  }
@@ -308,7 +308,7 @@ module Wovnrb
308
308
  url_lang_switcher = UrlLanguageSwitcher.new(store)
309
309
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
310
310
 
311
- request_out_env = header.request_out('ja')
311
+ request_out_env = header.request_out
312
312
  assert_equal('ja', request_out_env['wovnrb.target_lang'])
313
313
  end
314
314
 
@@ -321,7 +321,7 @@ module Wovnrb
321
321
  url_lang_switcher = UrlLanguageSwitcher.new(store)
322
322
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
323
323
 
324
- request_out_env = header.request_out('ja')
324
+ request_out_env = header.request_out
325
325
  assert_equal('ja', request_out_env['wovnrb.target_lang'])
326
326
  end
327
327
 
@@ -338,7 +338,7 @@ module Wovnrb
338
338
  url_lang_switcher = UrlLanguageSwitcher.new(store)
339
339
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
340
340
 
341
- request_out_env = header.request_out('ja')
341
+ request_out_env = header.request_out
342
342
  assert_equal('ja', request_out_env['wovnrb.target_lang'])
343
343
  end
344
344
 
@@ -355,7 +355,7 @@ module Wovnrb
355
355
  url_lang_switcher = UrlLanguageSwitcher.new(store)
356
356
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
357
357
 
358
- request_out_env = header.request_out('ja')
358
+ request_out_env = header.request_out
359
359
  assert_equal('ja.wovn.io', request_out_env['HTTP_X_FORWARDED_HOST'])
360
360
  end
361
361
 
@@ -373,7 +373,7 @@ module Wovnrb
373
373
  url_lang_switcher = UrlLanguageSwitcher.new(store)
374
374
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
375
375
 
376
- request_out_env = header.request_out('ja')
376
+ request_out_env = header.request_out
377
377
  assert_equal('wovn.io', request_out_env['HTTP_X_FORWARDED_HOST'])
378
378
  end
379
379
 
@@ -391,7 +391,7 @@ module Wovnrb
391
391
  url_lang_switcher = UrlLanguageSwitcher.new(store)
392
392
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
393
393
 
394
- request_out_env = header.request_out('ja')
394
+ request_out_env = header.request_out
395
395
  assert_equal('http://wovn.io/test', request_out_env['HTTP_REFERER'])
396
396
  end
397
397
 
@@ -405,7 +405,7 @@ module Wovnrb
405
405
  url_lang_switcher = UrlLanguageSwitcher.new(store)
406
406
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
407
407
 
408
- request_out_env = header.request_out('ja')
408
+ request_out_env = header.request_out
409
409
  assert_equal('http://wovn.io/test', request_out_env['HTTP_REFERER'])
410
410
  end
411
411
 
@@ -424,10 +424,32 @@ module Wovnrb
424
424
  url_lang_switcher = UrlLanguageSwitcher.new(store)
425
425
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
426
426
 
427
- request_out_env = header.request_out('ja')
427
+ request_out_env = header.request_out
428
428
  assert_equal('http://wovn.io/test', request_out_env['HTTP_REFERER'])
429
429
  end
430
430
 
431
+ def test_request_out_custom_domain
432
+ settings = Wovnrb.get_settings({
433
+ 'url_pattern' => 'custom_domain',
434
+ 'custom_domain_langs' => {
435
+ 'en' => { 'url' => 'wovn.io' },
436
+ 'ja' => { 'url' => 'ja.wovn.io' }
437
+ }
438
+ })
439
+ store = Wovnrb.get_store(settings)
440
+ env = Wovnrb.get_env({
441
+ 'HTTP_REFERER' => 'ja.wovn.io',
442
+ 'SERVER_NAME' => 'ja.wovn.io',
443
+ 'REQUEST_URI' => '/dummy'
444
+ })
445
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
446
+ header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
447
+
448
+ request_out_env = header.request_out
449
+ assert_equal('wovn.io', request_out_env['HTTP_REFERER'])
450
+ assert_equal('wovn.io', request_out_env['SERVER_NAME'])
451
+ end
452
+
431
453
  def test_out_should_add_lang_code_to_redirection
432
454
  settings = Wovnrb.get_settings({
433
455
  'default_lang' => 'en',
@@ -485,7 +507,7 @@ module Wovnrb
485
507
  })
486
508
  url_lang_switcher = UrlLanguageSwitcher.new(store)
487
509
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
488
- headers = header.request_out('ja')
510
+ headers = header.request_out
489
511
  assert_equal('http://wovn.io/test', headers['HTTP_REFERER'])
490
512
  headers['Location'] = headers['HTTP_REFERER']
491
513
  assert_equal('http://staging-ja.wovn.io/test', header.out(headers)['Location'])
@@ -504,7 +526,7 @@ module Wovnrb
504
526
  })
505
527
  url_lang_switcher = UrlLanguageSwitcher.new(store)
506
528
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
507
- headers = header.request_out(header.lang_code)
529
+ headers = header.request_out
508
530
  assert_equal('http://wovn.io/test', headers['HTTP_REFERER'])
509
531
  headers['Location'] = headers['HTTP_REFERER']
510
532
  assert_equal('http://wovn.io/test', header.out(headers)['Location'])
@@ -523,7 +545,7 @@ module Wovnrb
523
545
  })
524
546
  url_lang_switcher = UrlLanguageSwitcher.new(store)
525
547
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
526
- headers = header.request_out(header.lang_code)
548
+ headers = header.request_out
527
549
  assert_equal('http://wovn.io/test', headers['HTTP_REFERER'])
528
550
  headers['Location'] = headers['HTTP_REFERER']
529
551
  assert_equal('http://wovn.io/test', header.out(headers)['Location'])
@@ -542,7 +564,7 @@ module Wovnrb
542
564
  })
543
565
  url_lang_switcher = UrlLanguageSwitcher.new(store)
544
566
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
545
- headers = header.request_out(header.lang_code)
567
+ headers = header.request_out
546
568
  assert_equal('http://wovn.io/test', headers['HTTP_REFERER'])
547
569
  headers['Location'] = headers['HTTP_REFERER']
548
570
  assert_equal('http://wovn.io/test', header.out(headers)['Location'])
@@ -561,7 +583,7 @@ module Wovnrb
561
583
  })
562
584
  url_lang_switcher = UrlLanguageSwitcher.new(store)
563
585
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
564
- headers = header.out(header.request_out('ja'))
586
+ headers = header.out(header.request_out)
565
587
  assert_equal('ja', headers['wovnrb.target_lang'])
566
588
  end
567
589
 
@@ -574,7 +596,7 @@ module Wovnrb
574
596
  })
575
597
  url_lang_switcher = UrlLanguageSwitcher.new(store)
576
598
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
577
- headers = header.out(header.request_out('ja'))
599
+ headers = header.out(header.request_out)
578
600
  assert_equal('ja', headers['wovnrb.target_lang'])
579
601
  end
580
602
 
@@ -590,7 +612,7 @@ module Wovnrb
590
612
  })
591
613
  url_lang_switcher = UrlLanguageSwitcher.new(store)
592
614
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
593
- headers = header.out(header.request_out('ja'))
615
+ headers = header.out(header.request_out)
594
616
  assert_equal('ja', headers['wovnrb.target_lang'])
595
617
  end
596
618
 
@@ -607,10 +629,102 @@ module Wovnrb
607
629
  })
608
630
  url_lang_switcher = UrlLanguageSwitcher.new(store)
609
631
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
610
- headers = header.out(header.request_out('ja'))
632
+ headers = header.out(header.request_out)
611
633
  assert_equal('ja', headers['wovnrb.target_lang'])
612
634
  end
613
635
 
636
+ def test_out_with_custom_domain
637
+ settings = Wovnrb.get_settings({
638
+ 'default_lang' => 'en',
639
+ 'url_pattern' => 'custom_domain',
640
+ 'custom_domain_langs' => {
641
+ 'en' => { 'url' => 'wovn.io' },
642
+ 'ja' => { 'url' => 'wovn.io/ja' }
643
+ }
644
+ })
645
+ store = Wovnrb.get_store(settings)
646
+ env = Wovnrb.get_env({
647
+ 'SERVER_NAME' => 'wovn.io',
648
+ 'REQUEST_URI' => '/ja/test',
649
+ 'HTTP_REFERER' => 'http://wovn.io/ja/test'
650
+ })
651
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
652
+ header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
653
+ headers = {
654
+ 'Location' => 'http://wovn.io/'
655
+ }
656
+ assert_equal('http://wovn.io/ja/', header.out(headers)['Location'])
657
+ end
658
+
659
+ def test_out_with_custom_domain__absolute_url_redirect
660
+ settings = Wovnrb.get_settings({
661
+ 'default_lang' => 'en',
662
+ 'url_pattern' => 'custom_domain',
663
+ 'custom_domain_langs' => {
664
+ 'en' => { 'url' => 'wovn.io' },
665
+ 'ja' => { 'url' => 'wovn.io/ja' }
666
+ }
667
+ })
668
+ store = Wovnrb.get_store(settings)
669
+ env = Wovnrb.get_env({
670
+ 'SERVER_NAME' => 'wovn.io',
671
+ 'REQUEST_URI' => '/ja/test',
672
+ 'HTTP_REFERER' => 'http://wovn.io/ja/test'
673
+ })
674
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
675
+ header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
676
+ headers = {
677
+ 'Location' => 'http://wovn.io/foo'
678
+ }
679
+ assert_equal('http://wovn.io/ja/foo', header.out(headers)['Location'])
680
+ end
681
+
682
+ def test_out_with_custom_domain__absolute_path_redirect
683
+ settings = Wovnrb.get_settings({
684
+ 'default_lang' => 'en',
685
+ 'url_pattern' => 'custom_domain',
686
+ 'custom_domain_langs' => {
687
+ 'en' => { 'url' => 'wovn.io' },
688
+ 'ja' => { 'url' => 'wovn.io/ja' }
689
+ }
690
+ })
691
+ store = Wovnrb.get_store(settings)
692
+ env = Wovnrb.get_env({
693
+ 'SERVER_NAME' => 'wovn.io',
694
+ 'REQUEST_URI' => '/ja/test',
695
+ 'HTTP_REFERER' => 'http://wovn.io/ja/test'
696
+ })
697
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
698
+ header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
699
+ headers = {
700
+ 'Location' => '/foo'
701
+ }
702
+ assert_equal('http://wovn.io/ja/foo', header.out(headers)['Location'])
703
+ end
704
+
705
+ def test_out_with_custom_domain__relative_url_redirect
706
+ settings = Wovnrb.get_settings({
707
+ 'default_lang' => 'en',
708
+ 'url_pattern' => 'custom_domain',
709
+ 'custom_domain_langs' => {
710
+ 'en' => { 'url' => 'wovn.io' },
711
+ 'ja' => { 'url' => 'wovn.io/ja' }
712
+ }
713
+ })
714
+ store = Wovnrb.get_store(settings)
715
+ env = Wovnrb.get_env({
716
+ 'SERVER_NAME' => 'wovn.io',
717
+ 'REQUEST_URI' => '/ja/test',
718
+ 'HTTP_REFERER' => 'http://wovn.io/ja/test'
719
+ })
720
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
721
+ header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
722
+ headers = {
723
+ 'Location' => 'foo/page.html'
724
+ }
725
+ assert_equal('http://wovn.io/ja/foo/page.html', header.out(headers)['Location'])
726
+ end
727
+
614
728
  def test_get_settings_valid
615
729
  # TODO: check if Wovnrb.get_settings is valid (store.rb, valid_settings)
616
730
  # s = Wovnrb::Store.new
@@ -627,42 +741,42 @@ module Wovnrb
627
741
  name = "test path lang subdomain #{lang}"
628
742
  env = { 'url' => "https://#{subdomain}.wovn.io" }
629
743
  settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
630
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
744
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
631
745
 
632
746
  name = "test path lang subdomain #{lang} with slash"
633
747
  env = { 'url' => "https://#{subdomain}.wovn.io/" }
634
748
  settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
635
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
749
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
636
750
 
637
751
  name = "test path lang subdomain #{lang} with port"
638
752
  env = { 'url' => "https://#{subdomain}.wovn.io:1234" }
639
753
  settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
640
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
754
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
641
755
 
642
756
  name = "test path lang subdomain #{lang} with slash and port"
643
757
  env = { 'url' => "https://#{subdomain}.wovn.io:1234/" }
644
758
  settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
645
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
759
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
646
760
 
647
761
  name = "test path lang subdomain #{lang} insecure"
648
762
  env = { 'url' => "http://#{subdomain}.wovn.io" }
649
763
  settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
650
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
764
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
651
765
 
652
766
  name = "test path lang subdomain #{lang} insecure with slash"
653
767
  env = { 'url' => "http://#{subdomain}.wovn.io/" }
654
768
  settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
655
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
769
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
656
770
 
657
771
  name = "test path lang subdomain #{lang} insecure with port"
658
772
  env = { 'url' => "http://#{subdomain}.wovn.io:1234" }
659
773
  settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
660
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
774
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
661
775
 
662
776
  name = "test path lang subdomain #{lang} insecure with port and slash"
663
777
  env = { 'url' => "http://#{subdomain}.wovn.io:1234/" }
664
778
  settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
665
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
779
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
666
780
  end
667
781
  end
668
782
 
@@ -672,42 +786,42 @@ module Wovnrb
672
786
  name = "test path lang query #{lang}"
673
787
  env = { 'url' => "https://wovn.io?wovn=#{query}" }
674
788
  settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
675
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
789
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
676
790
 
677
791
  name = "test path lang query #{lang} with slash"
678
792
  env = { 'url' => "https://wovn.io/?wovn=#{query}" }
679
793
  settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
680
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
794
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
681
795
 
682
796
  name = "test path lang query #{lang} with port"
683
797
  env = { 'url' => "https://wovn.io:1234?wovn=#{query}" }
684
798
  settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
685
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
799
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
686
800
 
687
801
  name = "test path lang query #{lang} with port and slash"
688
802
  env = { 'url' => "https://wovn.io:1234/?wovn=#{query}" }
689
803
  settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
690
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
804
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
691
805
 
692
806
  name = "test path lang query #{lang} insecure"
693
807
  env = { 'url' => "http://wovn.io?wovn=#{query}" }
694
808
  settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
695
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
809
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
696
810
 
697
811
  name = "test path lang query #{lang} with slash insecure"
698
812
  env = { 'url' => "http://wovn.io/?wovn=#{query}" }
699
813
  settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
700
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
814
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
701
815
 
702
816
  name = "test path lang query #{lang} with port insecure"
703
817
  env = { 'url' => "http://wovn.io:1234?wovn=#{query}" }
704
818
  settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
705
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
819
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
706
820
 
707
821
  name = "test path lang query #{lang} with port and slash insecure"
708
822
  env = { 'url' => "http://wovn.io:1234/?wovn=#{query}" }
709
823
  settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
710
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
824
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
711
825
  end
712
826
  end
713
827
 
@@ -717,46 +831,46 @@ module Wovnrb
717
831
  name = "test path lang path #{lang}"
718
832
  env = { 'url' => "https://wovn.io/#{query}" }
719
833
  settings = { 'url_pattern' => 'path' }
720
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
834
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
721
835
 
722
836
  name = "test path lang path #{lang} with slash"
723
837
  env = { 'url' => "https://wovn.io/#{query}/" }
724
838
  settings = { 'url_pattern' => 'path' }
725
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
839
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
726
840
 
727
841
  name = "test path lang path #{lang} with port"
728
842
  env = { 'url' => "https://wovn.io:1234/#{query}" }
729
843
  settings = { 'url_pattern' => 'path' }
730
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
844
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
731
845
 
732
846
  name = "test path lang path #{lang} with port and slash"
733
847
  env = { 'url' => "https://wovn.io:1234/#{query}/" }
734
848
  settings = { 'url_pattern' => 'path' }
735
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
849
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
736
850
 
737
851
  name = "test path lang path #{lang} insecure"
738
852
  env = { 'url' => "http://wovn.io/#{query}" }
739
853
  settings = { 'url_pattern' => 'path' }
740
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
854
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
741
855
 
742
856
  name = "test path lang path #{lang} with slash insecure"
743
857
  env = { 'url' => "http://wovn.io/#{query}/" }
744
858
  settings = { 'url_pattern' => 'path' }
745
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
859
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
746
860
 
747
861
  name = "test path lang path #{lang} with port insecure"
748
862
  env = { 'url' => "http://wovn.io:1234/#{query}" }
749
863
  settings = { 'url_pattern' => 'path' }
750
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
864
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
751
865
 
752
866
  name = "test path lang path #{lang} with port and slash insecure"
753
867
  env = { 'url' => "http://wovn.io:1234/#{query}/" }
754
868
  settings = { 'url_pattern' => 'path' }
755
- utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
869
+ utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
756
870
  end
757
871
  end
758
872
 
759
- def test_path_lang_sudomain_with_use_proxy_false
873
+ def test_url_language_sudomain_with_use_proxy_false
760
874
  settings = Wovnrb.get_settings({
761
875
  'url_pattern' => 'subdomain',
762
876
  'url_pattern_reg' => '^(?<lang>[^.]+)\.'
@@ -768,10 +882,10 @@ module Wovnrb
768
882
  })
769
883
  url_lang_switcher = UrlLanguageSwitcher.new(store)
770
884
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
771
- assert_equal('', header.path_lang)
885
+ assert_equal('', header.url_language)
772
886
  end
773
887
 
774
- def test_path_lang_sudomain_with_use_proxy_true
888
+ def test_url_language_sudomain_with_use_proxy_true
775
889
  settings = Wovnrb.get_settings({
776
890
  'url_pattern' => 'subdomain',
777
891
  'url_pattern_reg' => '^(?<lang>[^.]+)\.',
@@ -784,7 +898,54 @@ module Wovnrb
784
898
  })
785
899
  url_lang_switcher = UrlLanguageSwitcher.new(store)
786
900
  header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
787
- assert_equal('zh-CHT', header.path_lang)
901
+ assert_equal('zh-CHT', header.url_language)
902
+ end
903
+
904
+ def test_url_language_with_custom_domain
905
+ custom_domain_langs = {
906
+ 'en' => { 'url' => 'my-site.com' },
907
+ 'en-US' => { 'url' => 'en-us.my-site.com' },
908
+ 'ja' => { 'url' => 'my-site.com/ja' },
909
+ 'zh-CHS' => { 'url' => 'my-site.com/zh/chs' },
910
+ 'zh-Hant-HK' => { 'url' => 'zh-hant-hk.com/zh' }
911
+ }
912
+ settings = {
913
+ 'url_pattern' => 'custom_domain',
914
+ 'custom_domain_langs' => custom_domain_langs
915
+ }
916
+ test_cases = [
917
+ [{ 'SERVER_NAME' => 'my-site.com', 'REQUEST_URI' => '/' }, 'en'],
918
+ [{ 'SERVER_NAME' => 'en-us.my-site.com', 'REQUEST_URI' => '/' }, 'en-US'],
919
+ [{ 'SERVER_NAME' => 'my-site.com', 'REQUEST_URI' => '/ja' }, 'ja'],
920
+ [{ 'SERVER_NAME' => 'my-site.com', 'REQUEST_URI' => '/zh/chs' }, 'zh-CHS'],
921
+ [{ 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh' }, 'zh-Hant-HK'],
922
+
923
+ # request uri pattern
924
+ [{ 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh' }, 'zh-Hant-HK'],
925
+ [{ 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/' }, 'zh-Hant-HK'],
926
+ [{ 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/index.html' }, 'zh-Hant-HK'],
927
+ [{ 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/dir' }, 'zh-Hant-HK'],
928
+ [{ 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/dir/' }, 'zh-Hant-HK'],
929
+ [{ 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh/dir/index.html' }, 'zh-Hant-HK'],
930
+ [{ 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh?query=1' }, 'zh-Hant-HK'],
931
+ [{ 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/zh#hash' }, 'zh-Hant-HK'],
932
+
933
+ # should be default lang
934
+ [{ 'SERVER_NAME' => 'my-site.com', 'REQUEST_URI' => '/japan' }, 'en'],
935
+ [{ 'SERVER_NAME' => 'my-site.com', 'REQUEST_URI' => '/' }, 'en'],
936
+ [{ 'SERVER_NAME' => 'zh-hant-hk.com', 'REQUEST_URI' => '/' }, '']
937
+ ]
938
+
939
+ test_cases.each do |test_case|
940
+ env_hash, expected_lang_code = test_case
941
+ settings = Wovnrb.get_settings(settings)
942
+ store = Wovnrb.get_store(settings)
943
+ env = Wovnrb.get_env(env_hash)
944
+ url_lang_switcher = UrlLanguageSwitcher.new(store)
945
+ header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
946
+
947
+ assert_equal(expected_lang_code, header.url_language)
948
+ end
788
949
  end
789
950
  end
790
951
  end