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_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 private 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 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