Skip to content

Commit

Permalink
refactor: Rename Headers#path_lang to url_language (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanzerrudo authored Mar 20, 2023
1 parent 7441004 commit 55ed5e7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion lib/wovnrb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def amp_page?(html_body)

def explicit_default_lang?(headers)
default_lang, url_pattern = @store.settings.values_at('default_lang', 'url_pattern')
default_lang == headers.path_lang && url_pattern != 'custom_domain'
default_lang == headers.url_language && url_pattern != 'custom_domain'
end
end
end
10 changes: 5 additions & 5 deletions lib/wovnrb/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ def unmasked_pathname_without_trailing_slash
#
# @return [String] The lang code of the current page
def lang_code
path_lang && !path_lang.empty? ? path_lang : @settings['default_lang']
url_language && !url_language.empty? ? url_language : @settings['default_lang']
end

# picks up language code from requested URL by using url_pattern_reg setting.
# when language code is invalid, this method returns empty string.
# if you want examples, please see test/lib/headers_test.rb.
#
# @return [String] language code in requrested URL.
def path_lang
if @path_lang.nil?
def url_language
if @url_language.nil?
full_url = if @settings['use_proxy'] && @env.key?('HTTP_X_FORWARDED_HOST')
"#{@env['HTTP_X_FORWARDED_HOST']}#{@env['REQUEST_URI']}"
else
Expand All @@ -96,9 +96,9 @@ def path_lang
match = full_url.match(rp)
new_lang_code = Lang.get_code(match[:lang]) if match && match[:lang] && Lang.get_lang(match[:lang])
end
@path_lang = new_lang_code.presence || ''
@url_language = new_lang_code.presence || ''
end
@path_lang
@url_language
end

def redirect(lang)
Expand Down
68 changes: 34 additions & 34 deletions test/lib/headers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_initialize
{
'env' => { 'url' => 'https://wovn.io/en' },
'setting' => { 'url_pattern' => 'path' },
'name' => 'test_initialize_with_path_language',
'name' => 'test_initialize_with_url_language',
'expected' => {
'url' => 'wovn.io/'
}
Expand All @@ -71,7 +71,7 @@ def test_initialize
{
'env' => { 'url' => 'https://wovn.io/en/?wovn=zh-CHS' },
'setting' => { 'url_pattern' => 'path' },
'name' => 'test_initialize_with_path_language_with_query',
'name' => 'test_initialize_with_url_language_with_query',
'expected' => {
'url' => 'wovn.io/?wovn=zh-CHS'
}
Expand All @@ -87,7 +87,7 @@ def test_initialize
{
'env' => { 'url' => 'https://wovn.io/en?wovn=zh-CHS' },
'setting' => { 'url_pattern' => 'path' },
'name' => 'test_initialize_with_path_language_with_query_without_slash',
'name' => 'test_initialize_with_url_language_with_query_without_slash',
'expected' => {
'url' => 'wovn.io/?wovn=zh-CHS'
}
Expand Down Expand Up @@ -177,7 +177,7 @@ def utility_assert_pathname_with_trailing_slash_if_present(env_opt, setting_opt,
{
'env' => { 'REQUEST_URI' => 'http://page.com/ja/test/' },
'setting' => { 'url_pattern' => 'path', 'url_pattern_reg' => '/(?<lang>[^/.?]+)' },
'name' => 'test_pathname_with_trailing_slash_if_present_with_path_lang_when_trailing_slash_is_present',
'name' => 'test_pathname_with_trailing_slash_if_present_with_url_language_when_trailing_slash_is_present',
'expected' => {
'pathname_with_trailing_slash_if_present' => '/test/'
}
Expand Down Expand Up @@ -741,42 +741,42 @@ def test_lang_detection_subdomain
name = "test path lang subdomain #{lang}"
env = { 'url' => "https://#{subdomain}.wovn.io" }
settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang subdomain #{lang} with slash"
env = { 'url' => "https://#{subdomain}.wovn.io/" }
settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang subdomain #{lang} with port"
env = { 'url' => "https://#{subdomain}.wovn.io:1234" }
settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang subdomain #{lang} with slash and port"
env = { 'url' => "https://#{subdomain}.wovn.io:1234/" }
settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang subdomain #{lang} insecure"
env = { 'url' => "http://#{subdomain}.wovn.io" }
settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang subdomain #{lang} insecure with slash"
env = { 'url' => "http://#{subdomain}.wovn.io/" }
settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang subdomain #{lang} insecure with port"
env = { 'url' => "http://#{subdomain}.wovn.io:1234" }
settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang subdomain #{lang} insecure with port and slash"
env = { 'url' => "http://#{subdomain}.wovn.io:1234/" }
settings = { 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+)\.' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
end
end

Expand All @@ -786,42 +786,42 @@ def test_lang_detection_query
name = "test path lang query #{lang}"
env = { 'url' => "https://wovn.io?wovn=#{query}" }
settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang query #{lang} with slash"
env = { 'url' => "https://wovn.io/?wovn=#{query}" }
settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang query #{lang} with port"
env = { 'url' => "https://wovn.io:1234?wovn=#{query}" }
settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang query #{lang} with port and slash"
env = { 'url' => "https://wovn.io:1234/?wovn=#{query}" }
settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang query #{lang} insecure"
env = { 'url' => "http://wovn.io?wovn=#{query}" }
settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang query #{lang} with slash insecure"
env = { 'url' => "http://wovn.io/?wovn=#{query}" }
settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang query #{lang} with port insecure"
env = { 'url' => "http://wovn.io:1234?wovn=#{query}" }
settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang query #{lang} with port and slash insecure"
env = { 'url' => "http://wovn.io:1234/?wovn=#{query}" }
settings = { 'url_pattern' => 'query', 'url_pattern_reg' => '((\?.*&)|\?)wovn=(?<lang>[^&]+)(&|$)' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
end
end

Expand All @@ -831,46 +831,46 @@ def test_lang_detection_path
name = "test path lang path #{lang}"
env = { 'url' => "https://wovn.io/#{query}" }
settings = { 'url_pattern' => 'path' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang path #{lang} with slash"
env = { 'url' => "https://wovn.io/#{query}/" }
settings = { 'url_pattern' => 'path' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang path #{lang} with port"
env = { 'url' => "https://wovn.io:1234/#{query}" }
settings = { 'url_pattern' => 'path' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang path #{lang} with port and slash"
env = { 'url' => "https://wovn.io:1234/#{query}/" }
settings = { 'url_pattern' => 'path' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang path #{lang} insecure"
env = { 'url' => "http://wovn.io/#{query}" }
settings = { 'url_pattern' => 'path' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang path #{lang} with slash insecure"
env = { 'url' => "http://wovn.io/#{query}/" }
settings = { 'url_pattern' => 'path' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang path #{lang} with port insecure"
env = { 'url' => "http://wovn.io:1234/#{query}" }
settings = { 'url_pattern' => 'path' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)

name = "test path lang path #{lang} with port and slash insecure"
env = { 'url' => "http://wovn.io:1234/#{query}/" }
settings = { 'url_pattern' => 'path' }
utility_universal_assertion(env, settings, { 'path_lang' => lang }, name)
utility_universal_assertion(env, settings, { 'url_language' => lang }, name)
end
end

def test_path_lang_sudomain_with_use_proxy_false
def test_url_language_sudomain_with_use_proxy_false
settings = Wovnrb.get_settings({
'url_pattern' => 'subdomain',
'url_pattern_reg' => '^(?<lang>[^.]+)\.'
Expand All @@ -882,10 +882,10 @@ def test_path_lang_sudomain_with_use_proxy_false
})
url_lang_switcher = UrlLanguageSwitcher.new(store)
header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
assert_equal('', header.path_lang)
assert_equal('', header.url_language)
end

def test_path_lang_sudomain_with_use_proxy_true
def test_url_language_sudomain_with_use_proxy_true
settings = Wovnrb.get_settings({
'url_pattern' => 'subdomain',
'url_pattern_reg' => '^(?<lang>[^.]+)\.',
Expand All @@ -898,10 +898,10 @@ def test_path_lang_sudomain_with_use_proxy_true
})
url_lang_switcher = UrlLanguageSwitcher.new(store)
header = Wovnrb::Headers.new(env, settings, url_lang_switcher)
assert_equal('zh-CHT', header.path_lang)
assert_equal('zh-CHT', header.url_language)
end

def test_path_lang_with_custom_domain
def test_url_language_with_custom_domain
custom_domain_langs = {
'en' => { 'url' => 'my-site.com' },
'en-US' => { 'url' => 'en-us.my-site.com' },
Expand Down Expand Up @@ -944,7 +944,7 @@ def test_path_lang_with_custom_domain
url_lang_switcher = UrlLanguageSwitcher.new(store)
header = Wovnrb::Headers.new(env, settings, url_lang_switcher)

assert_equal(expected_lang_code, header.path_lang)
assert_equal(expected_lang_code, header.url_language)
end
end
end
Expand Down

0 comments on commit 55ed5e7

Please sign in to comment.