require 'test_helper' require 'wovnrb' class WovnrbTest < Minitest::Test def setup Wovnrb::Store.instance.reset end def test_initialize i = Wovnrb::Interceptor.new(get_app) refute_nil(i) end def test_switch_lang body = '

Mr. Belvedere Fan Club

Hello

' expected_body = [ '', "", '', '', '

ベルベデアさんファンクラブ

', '

こんにちは

', '' ].join assert_switch_lang('en', 'ja', body, expected_body, true) end def test_switch_lang_with_input_tags body = [ '', '', '', '', '', '', '' ].join expected_body = [ '', "", '', '', '', '', '', '', '', '

こんにちは

', '' ].join assert_switch_lang('en', 'ja', body, expected_body, true) end def test_switch_lang_of_html_fragment_with_japanese_translations bodies = ['Hello'].join expected_bodies = ['こんにちは'].join assert_switch_lang('en', 'ja', bodies, expected_bodies, true) end def test_switch_lang_splitted_body bodies = ['

Mr. Belvedere Fan Club

', '

Hello

', ''].join expected_bodies = ["

Mr. Belvedere Fan Club

Hello

"].join assert_switch_lang('en', 'ja', bodies, expected_bodies, true) end def test_switch_lang_of_html_fragment_in_splitted_body body = [''].join expected_body = [''].join assert_switch_lang('en', 'ja', body, expected_body, true) end def test_switch_lang_missing_values body = "

Mr. Belvedere Fan Club

Hello

" expected_body = "

Mr. Belvedere Fan Club

Hello

" assert_switch_lang('en', 'ja', body, expected_body, true) end def test_switch_lang_on_fragment_with_translate_fragment_false body = "

Mr. Belvedere Fan Club

Hello

" Wovnrb::Store.instance.settings['translate_fragment'] = false assert_switch_lang('en', 'ja', body, body, false) end def test_switch_lang_on_fragment_with_translate_fragment_true body = "

Mr. Belvedere Fan Club

Hello

" expected_body = "

ベルベデアさんファンクラブ

こんにちは

" Wovnrb::Store.instance.settings['translate_fragment'] = true assert_switch_lang('en', 'ja', body, expected_body, true) end def test_switch_lang_ignores_amp body = <

Mr. Belvedere Fan Club

Hello

HTML assert_switch_lang('en', 'ja', body, body, false) end def test_switch_lang_ignores_amp_defined_with_symbol_attribute body = <

Mr. Belvedere Fan Club

Hello

HTML assert_switch_lang('en', 'ja', body, body, false) end def test_call_without_path_ignored_should_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' => {}, 'HTTP_HOST' => 'test.com', 'REQUEST_URI' => '/en/not_ignored', 'PATH_INFO' => '/en/not_ignored' } assert_call_affects_env(settings, env, mock_api: true, affected: true) end def test_call_with_path_ignored_with_language_code_should_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' => {}, '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' => {}, 'HTTP_HOST' => 'test.com', 'REQUEST_URI' => '/en/ignored', 'PATH_INFO' => '/en/ignored' } assert_call_affects_env(settings, env, mock_api: false, affected: true) end def test_call_with_path_ignored_without_language_code_in_original_language_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' => {}, '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_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' => {}, 'HTTP_HOST' => 'test.com', 'REQUEST_URI' => '/en/ignored', 'PATH_INFO' => '/en/ignored' } assert_call_affects_env(settings, env, mock_api: false, affected: false) end private def assert_call_affects_env(settings, env, mock_api:, affected:) app_mock = get_app sut = Wovnrb::Interceptor.new(app_mock, settings) unaffected_env = env mock_translation_api_response('', '') if mock_api sut.call(env.clone) assert_equal(unaffected_env != app_mock.env, affected) end def assert_switch_lang(original_lang, target_lang, body, expected_body, api_expected = true) subdomain = target_lang == original_lang ? '' : "#{target_lang}." interceptor = Wovnrb::Interceptor.new(get_app) store, headers = store_headers_factory(subdomain, original_lang) if api_expected dom = Wovnrb::Helpers::NokogumboHelper.parse_html(body) converter = Wovnrb::HtmlConverter.new(dom, store, headers) apified_body, = converter.build_api_compatible_html mock_translation_api_response(apified_body, expected_body) end actual_bodies = interceptor.switch_lang(headers, [body]) assert_same_elements([expected_body], actual_bodies) end def store_headers_factory(subdomain, original_lang) settings = { 'project_token' => '123456', 'custom_lang_aliases' => {}, 'default_lang' => original_lang, 'url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?[^.]+).' } store = Wovnrb::Store.instance store.update_settings(settings) headers = Wovnrb::Headers.new( Wovnrb.get_env('url' => "http://#{subdomain}page.com"), Wovnrb.get_settings(settings) ) [store, headers] end def mock_translation_api_response(body, expected_body) Wovnrb::ApiTranslator.any_instance .expects(:translate) .once .with(body) .returns(expected_body) end def get_app(opts = {}) RackMock.new(opts) end class RackMock attr_accessor :params, :env def initialize(opts = {}) @params = {} if opts.key?(:params) && opts[:params].class == Hash opts[:params].each do |key, val| @params[key] = val end end @request_headers = {} end def call(env) @env = env unless @params.empty? req = Rack::Request.new(@env) @params.each do |key, val| req.update_param(key, val) end end [200, { 'Content-Type' => 'text/html' }, ['']] end def [](key) @env[key] end end end