Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore_paths are now checked before we do any translation logic #241

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/wovnrb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ def call(env)
request_lang = headers.lang_code
is_get_request = request.get?

# if path is ignored, do nothing
if ignore_path?(headers.unmasked_pathname_without_trailing_slash)
status, res_headers, body = @app.call(env)

return output(headers, status, res_headers, body)
end

if @store.settings['use_cookie_lang'] && cookie_lang.present? && request_lang != cookie_lang && request_lang == @store.default_lang && is_get_request
redirect_headers = headers.redirect(cookie_lang)
return [302, redirect_headers, ['']]
Expand All @@ -51,12 +58,6 @@ def call(env)
return [307, redirect_headers, ['']]
end

# if path containing language code is ignored, do nothing
if headers.lang_code != default_lang && ignore_path?(headers.unmasked_pathname_without_trailing_slash)
status, res_headers, body = @app.call(env)

return output(headers, status, res_headers, body)
end
# pass to application
status, res_headers, body = @app.call(headers.request_out)

Expand Down
6 changes: 5 additions & 1 deletion lib/wovnrb/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ def format_settings

if @settings['wovn_dev_mode']
if @settings['api_url'] == self.class.default_settings['api_url']
@settings['api_url'] = 'http://dev-wovn.io:3001'
@settings['api_url'] = 'https://dev-wovn.io'
end

if @settings['widget_url'] == self.class.default_settings['widget_url']
@settings['widget_url'] = 'https://j.dev-wovn.io/1'
end

if @settings['api_timeout_seconds'] == self.class.default_settings['api_timeout_seconds']
Expand Down
2 changes: 1 addition & 1 deletion lib/wovnrb/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Wovnrb
VERSION = '3.11.1'.freeze
VERSION = '4.0.0'.freeze
end
2 changes: 1 addition & 1 deletion test/lib/api_translator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def stub_translation_api_request(store, original_html, translated_html, response
if response
cache_key = generate_cache_key(store, original_html)
api_host = if store.dev_mode?
'dev-wovn.io:3001'
'dev-wovn.io'
else
'wovn.global.ssl.fastly.net'
end
Expand Down
77 changes: 17 additions & 60 deletions test/lib/wovnrb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def test_call_without_path_ignored_should_change_environment
assert_call_affects_env(settings, env, mock_api: true, affected: true)
end

def test_call_with_path_ignored_with_language_code_should_change_environment
def test_call_with_path_ignored_should_not_change_environment
settings = {
'project_token' => '123456',
'url_pattern' => 'path',
Expand All @@ -172,80 +172,37 @@ def test_call_with_path_ignored_with_language_code_should_change_environment
'rack.request.query_hash' => {},
'rack.request.form_input' => '',
'rack.request.form_hash' => {},
'HTTP_HOST' => 'test.com',
'REQUEST_URI' => '/ignored',
'PATH_INFO' => '/ignored'
}

assert_call_affects_env(settings, env, mock_api: false, affected: true)
end

def test_call_with_path_ignored_without_language_code_should_change_environment
settings = {
'project_token' => '123456',
'url_pattern' => 'path',
'default_lang' => 'ja',
'supported_langs' => %w[ja en],
'ignore_paths' => ['/ignored']
}
env = {
'rack.input' => '',
'rack.request.query_string' => '',
'rack.request.query_hash' => {},
'rack.request.form_input' => '',
'rack.request.form_hash' => {},
'rack.request.form_pairs' => [],
'rack.request.cookie_hash' => {},
'HTTP_HOST' => 'test.com',
'REQUEST_URI' => '/en/ignored',
'PATH_INFO' => '/en/ignored'
}

assert_call_affects_env(settings, env, mock_api: false, affected: true)
assert_call_affects_env(settings, env, mock_api: false, affected: false)
end

def test_call_with_path_ignored_without_language_code_in_original_language_should_change_environment
def test_call__with_use_cookie_lang_true__is_ignored_path__does_nothing
settings = {
'project_token' => '123456',
'url_pattern' => 'path',
'default_lang' => 'ja',
'supported_langs' => %w[ja en],
'ignore_paths' => ['/ignored']
}
env = {
'rack.input' => '',
'rack.request.query_string' => '',
'rack.request.query_hash' => {},
'rack.request.form_input' => '',
'rack.request.form_hash' => {},
'HTTP_HOST' => 'test.com',
'REQUEST_URI' => '/ignored',
'PATH_INFO' => '/ignored'
'ignore_paths' => %w[/ignored/**],
'use_cookie_lang' => true
}
env = Wovnrb.get_env(
{
'url' => 'http://test.com/ignored/foo',
'HTTP_COOKIE' => 'wovn_selected_lang=en'
}
)

assert_call_affects_env(settings, env, mock_api: false, affected: true)
end

def test_call_with_path_ignored_should_not_change_environment
settings = {
'project_token' => '123456',
'url_pattern' => 'path',
'default_lang' => 'ja',
'supported_langs' => %w[ja en],
'ignore_paths' => ['/en/ignored']
}
env = {
'rack.input' => '',
'rack.request.query_string' => '',
'rack.request.query_hash' => {},
'rack.request.form_input' => '',
'rack.request.form_hash' => {},
'rack.request.form_pairs' => [],
'rack.request.cookie_hash' => {},
'HTTP_HOST' => 'test.com',
'REQUEST_URI' => '/en/ignored',
'PATH_INFO' => '/en/ignored'
}
sut = Wovnrb::Interceptor.new(get_app, settings)
status, res_headers, _body = sut.call(env)

assert_call_affects_env(settings, env, mock_api: false, affected: false)
assert_equal(200, status)
assert_nil(res_headers['Location'])
end

def test_call__with_use_cookie_lang_true__cookie_lang_is_target_lang__should_redirect
Expand Down