wovnrb 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +1 -0
- data/.gitignore +2 -0
- data/.rubocop.yml +1 -0
- data/.rubocop_todo.yml +308 -0
- data/Rakefile +13 -14
- data/lib/wovnrb.rb +43 -98
- data/lib/wovnrb/api_translator.rb +143 -0
- data/lib/wovnrb/headers.rb +78 -92
- data/lib/wovnrb/helpers/nokogumbo_helper.rb +1 -1
- data/lib/wovnrb/lang.rb +93 -125
- data/lib/wovnrb/railtie.rb +5 -7
- data/lib/wovnrb/services/glob.rb +3 -3
- data/lib/wovnrb/services/html_converter.rb +192 -0
- data/lib/wovnrb/services/html_replace_marker.rb +38 -0
- data/lib/wovnrb/services/wovn_logger.rb +8 -4
- data/lib/wovnrb/settings.rb +5 -3
- data/lib/wovnrb/store.rb +35 -26
- data/lib/wovnrb/text_caches/cache_base.rb +3 -2
- data/lib/wovnrb/text_caches/memory_cache.rb +2 -2
- data/lib/wovnrb/version.rb +1 -1
- data/test/fixtures/html/test.html +8 -0
- data/test/fixtures/html/test_translated.html +8 -0
- data/test/lib/api_translator_test.rb +109 -0
- data/test/lib/headers_test.rb +84 -55
- data/test/lib/lang_test.rb +157 -357
- data/test/lib/services/glob_test.rb +1 -1
- data/test/lib/services/html_converter_test.rb +166 -0
- data/test/lib/services/html_replace_marker_test.rb +75 -0
- data/test/lib/services/wovn_logger_test.rb +6 -6
- data/test/lib/store_test.rb +25 -69
- data/test/lib/text_caches/cache_base_test.rb +1 -1
- data/test/lib/text_caches/memory_cache_test.rb +10 -11
- data/test/lib/wovnrb_test.rb +77 -310
- data/test/test_helper.rb +22 -32
- data/wovnrb.gemspec +35 -44
- metadata +86 -205
- data/ext/dom/Makefile +0 -239
- data/lib/wovnrb/api_data.rb +0 -59
- data/lib/wovnrb/html_replacers/image_replacer.rb +0 -69
- data/lib/wovnrb/html_replacers/input_replacer.rb +0 -38
- data/lib/wovnrb/html_replacers/link_replacer.rb +0 -78
- data/lib/wovnrb/html_replacers/meta_replacer.rb +0 -28
- data/lib/wovnrb/html_replacers/replacer_base.rb +0 -49
- data/lib/wovnrb/html_replacers/script_replacer.rb +0 -39
- data/lib/wovnrb/html_replacers/text_replacer.rb +0 -21
- data/lib/wovnrb/html_replacers/unified_values/dst_swapping_targets_creator.rb +0 -76
- data/lib/wovnrb/html_replacers/unified_values/element_category.rb +0 -242
- data/lib/wovnrb/html_replacers/unified_values/node_swapping_targets_creator.rb +0 -134
- data/lib/wovnrb/html_replacers/unified_values/text_replacer.rb +0 -35
- data/lib/wovnrb/html_replacers/unified_values/text_scraper.rb +0 -152
- data/lib/wovnrb/html_replacers/unified_values/values_stack.rb +0 -65
- data/lib/wovnrb/services/url.rb +0 -12
- data/lib/wovnrb/services/value_agent.rb +0 -9
- data/test/fixtures/unified_values/site_html/simple_actual.html +0 -96
- data/test/fixtures/unified_values/site_html/simple_expected.json +0 -251
- data/test/fixtures/unified_values/site_html/wovn.io_actual.html +0 -686
- data/test/fixtures/unified_values/site_html/wovn.io_expected.json +0 -543
- data/test/fixtures/unified_values/site_html/www.yahoo.co.jp_actual.html +0 -1024
- data/test/fixtures/unified_values/site_html/www.yahoo.co.jp_expected.json +0 -3345
- data/test/fixtures/unified_values/small_html/block_inside_inline_actual.html +0 -12
- data/test/fixtures/unified_values/small_html/block_inside_inline_expected.json +0 -22
- data/test/fixtures/unified_values/small_html/br_tag_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/br_tag_expected.json +0 -12
- data/test/fixtures/unified_values/small_html/comment_tag_actual.html +0 -12
- data/test/fixtures/unified_values/small_html/comment_tag_expected.json +0 -10
- data/test/fixtures/unified_values/small_html/complex_text_with_html_entity_actual.html +0 -7
- data/test/fixtures/unified_values/small_html/complex_text_with_html_entity_expected.json +0 -11
- data/test/fixtures/unified_values/small_html/deep_nested_block_actual.html +0 -14
- data/test/fixtures/unified_values/small_html/deep_nested_block_expected.json +0 -8
- data/test/fixtures/unified_values/small_html/deep_nested_inline_actual.html +0 -20
- data/test/fixtures/unified_values/small_html/deep_nested_inline_expected.json +0 -20
- data/test/fixtures/unified_values/small_html/empty_tag_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/empty_tag_expected.json +0 -12
- data/test/fixtures/unified_values/small_html/empty_text_actual.html +0 -12
- data/test/fixtures/unified_values/small_html/empty_text_expected.json +0 -1
- data/test/fixtures/unified_values/small_html/ignore_tag_actual.html +0 -12
- data/test/fixtures/unified_values/small_html/ignore_tag_expected.json +0 -16
- data/test/fixtures/unified_values/small_html/ignored_class_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/ignored_class_expected.json +0 -13
- data/test/fixtures/unified_values/small_html/img_actual.html +0 -12
- data/test/fixtures/unified_values/small_html/img_expected.json +0 -23
- data/test/fixtures/unified_values/small_html/nested_and_complex_wovn_ignore_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/nested_and_complex_wovn_ignore_expected.json +0 -16
- data/test/fixtures/unified_values/small_html/nested_text_value_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/nested_text_value_expected.json +0 -12
- data/test/fixtures/unified_values/small_html/nested_text_value_mixed_plan_text_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/nested_text_value_mixed_plan_text_expected.json +0 -14
- data/test/fixtures/unified_values/small_html/option_tag_actual.html +0 -9
- data/test/fixtures/unified_values/small_html/option_tag_expected.json +0 -13
- data/test/fixtures/unified_values/small_html/text_different_inline_each_other_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/text_different_inline_each_other_expected.json +0 -22
- data/test/fixtures/unified_values/small_html/text_in_svg_actual.html +0 -9
- data/test/fixtures/unified_values/small_html/text_in_svg_expected.json +0 -8
- data/test/fixtures/unified_values/small_html/text_with_html_entity_actual.html +0 -6
- data/test/fixtures/unified_values/small_html/text_with_html_entity_expected.json +0 -8
- data/test/fixtures/unified_values/small_html/unknown_or_custom_tag_actual.html +0 -12
- data/test/fixtures/unified_values/small_html/unknown_or_custom_tag_expected.json +0 -24
- data/test/fixtures/unified_values/small_html/unnecessay_top_end_tag_actual.html +0 -12
- data/test/fixtures/unified_values/small_html/unnecessay_top_end_tag_expected.json +0 -14
- data/test/fixtures/unified_values/small_html/wovn_ignore_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/wovn_ignore_expected.json +0 -13
- data/test/lib/api_data_test.rb +0 -83
- data/test/lib/html_replacers/image_replacer_test.rb +0 -165
- data/test/lib/html_replacers/input_replacer_test.rb +0 -140
- data/test/lib/html_replacers/link_replacer_test.rb +0 -328
- data/test/lib/html_replacers/meta_replacer_test.rb +0 -157
- data/test/lib/html_replacers/replacer_base_test.rb +0 -128
- data/test/lib/html_replacers/script_replacer_test.rb +0 -139
- data/test/lib/html_replacers/text_replacer_test.rb +0 -99
- data/test/lib/html_replacers/unified_values/dst_swapping_targets_creator_test.rb +0 -137
- data/test/lib/html_replacers/unified_values/element_category_test.rb +0 -49
- data/test/lib/html_replacers/unified_values/node_swapping_targets_creator_test.rb +0 -137
- data/test/lib/html_replacers/unified_values/text_replacer_test.rb +0 -270
- data/test/lib/html_replacers/unified_values/text_scraper_test.rb +0 -121
- data/test/lib/html_replacers/unified_values/values_stack_test.rb +0 -122
- data/test/lib/services/url_test.rb +0 -9
- data/test/lib/services/value_agent_test.rb +0 -32
- data/test/services/url_test.rb +0 -163
- data/values/values +0 -1
@@ -1,4 +1,3 @@
|
|
1
|
-
# -*- encoding: UTF-8 -*-
|
2
1
|
require 'wovnrb/text_caches/cache_base'
|
3
2
|
require 'wovnrb/text_caches/memory_cache'
|
4
3
|
require 'minitest/autorun'
|
@@ -6,10 +5,10 @@ require 'timecop'
|
|
6
5
|
|
7
6
|
class MemoryCacheTest < Minitest::Test
|
8
7
|
def test_initialize
|
9
|
-
memory = Wovnrb::MemoryCache.new(
|
8
|
+
memory = Wovnrb::MemoryCache.new(
|
10
9
|
'cache_megabytes' => 1,
|
11
10
|
'ttl_seconds' => 1
|
12
|
-
|
11
|
+
)
|
13
12
|
option = memory.options
|
14
13
|
assert_equal(1.megabytes, option[:size])
|
15
14
|
assert_equal(1.megabytes, option[:size])
|
@@ -17,18 +16,18 @@ class MemoryCacheTest < Minitest::Test
|
|
17
16
|
end
|
18
17
|
|
19
18
|
def test_initialize_without_cache_megabytes
|
20
|
-
memory = Wovnrb::MemoryCache.new(
|
19
|
+
memory = Wovnrb::MemoryCache.new(
|
21
20
|
'ttl_seconds' => 1
|
22
|
-
|
21
|
+
)
|
23
22
|
option = memory.options
|
24
23
|
assert_equal(200.megabytes, option[:size])
|
25
24
|
assert_equal(1.seconds, option[:expires_in])
|
26
25
|
end
|
27
26
|
|
28
27
|
def test_initialize_without_ttl
|
29
|
-
memory = Wovnrb::MemoryCache.new(
|
30
|
-
'cache_megabytes' => 1
|
31
|
-
|
28
|
+
memory = Wovnrb::MemoryCache.new(
|
29
|
+
'cache_megabytes' => 1
|
30
|
+
)
|
32
31
|
option = memory.options
|
33
32
|
assert_equal(1.megabytes, option[:size])
|
34
33
|
assert_equal(300.seconds, option[:expires_in])
|
@@ -75,9 +74,9 @@ class MemoryCacheTest < Minitest::Test
|
|
75
74
|
|
76
75
|
def test_get_with_over_memory
|
77
76
|
# ActiveSupport::Cache::MemoryStore has 240 bytes overhead per instance
|
78
|
-
memory = Wovnrb::MemoryCache.new(
|
79
|
-
'cache_megabytes' => 400.0 / 1000 / 1000
|
80
|
-
|
77
|
+
memory = Wovnrb::MemoryCache.new(
|
78
|
+
'cache_megabytes' => 400.0 / 1000 / 1000
|
79
|
+
)
|
81
80
|
memory.put('a', 'c')
|
82
81
|
memory.put('b', 'd')
|
83
82
|
assert_nil(memory.get('a'))
|
data/test/lib/wovnrb_test.rb
CHANGED
@@ -1,11 +1,5 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
1
|
require 'test_helper'
|
3
2
|
require 'wovnrb'
|
4
|
-
require 'wovnrb/headers'
|
5
|
-
require 'minitest/autorun'
|
6
|
-
require 'webmock/minitest'
|
7
|
-
require 'rack'
|
8
|
-
require 'pry'
|
9
3
|
|
10
4
|
class WovnrbTest < Minitest::Test
|
11
5
|
def setup
|
@@ -17,225 +11,77 @@ class WovnrbTest < Minitest::Test
|
|
17
11
|
refute_nil(i)
|
18
12
|
end
|
19
13
|
|
20
|
-
|
21
|
-
# def test_call(env)
|
22
|
-
# end
|
23
|
-
|
24
|
-
|
25
|
-
# def test_switch_lang(body, values, url, lang=STORE.settings['default_lang'], headers)
|
26
|
-
# end
|
27
|
-
|
28
|
-
def test_api_call
|
29
|
-
settings = Wovnrb.get_settings
|
30
|
-
token = settings['project_token']
|
31
|
-
url = 'wovn.io/dashboard'
|
32
|
-
stub = stub_request(:get, "#{settings['api_url']}?token=#{token}&url=#{url}").
|
33
|
-
to_return(:body => '{"test_body": "a"}')
|
34
|
-
|
35
|
-
i = Wovnrb::Interceptor.new(get_app, settings)
|
36
|
-
|
37
|
-
i.call(Wovnrb.get_env)
|
38
|
-
assert_requested(stub, :times => 1)
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_api_call_with_cache
|
42
|
-
settings = Wovnrb.get_settings
|
43
|
-
token = settings['project_token']
|
44
|
-
url = 'wovn.io/dashboard'
|
45
|
-
stub = stub_request(:get, "#{settings['api_url']}?token=#{token}&url=#{url}").
|
46
|
-
to_return(:body => '{"test_body": "a"}')
|
47
|
-
|
48
|
-
i = Wovnrb::Interceptor.new(get_app, settings)
|
49
|
-
|
50
|
-
i.call(Wovnrb.get_env)
|
51
|
-
i.call(Wovnrb.get_env)
|
52
|
-
assert_requested(stub, :times => 1)
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_request_wovn_token
|
56
|
-
settings = Wovnrb.get_settings
|
57
|
-
default_token = 'token0'
|
58
|
-
request_token = 'token1'
|
59
|
-
settings['project_token'] = default_token
|
60
|
-
url = 'wovn.io/dashboard'
|
61
|
-
stub = stub_request(:get, "#{settings['api_url']}?token=#{request_token}&url=#{url}").
|
62
|
-
to_return(:body => '{"test_body": "a"}')
|
63
|
-
|
64
|
-
app = get_app(:params => {'wovn_token' => request_token})
|
65
|
-
i = Wovnrb::Interceptor.new(app, settings)
|
66
|
-
|
67
|
-
env = Wovnrb.get_env
|
68
|
-
i.call(env)
|
69
|
-
assert_requested(stub, :times => 1)
|
70
|
-
|
71
|
-
# check use default a token after use dynamic token
|
72
|
-
stub_default = stub_request(:get, "#{settings['api_url']}?token=#{default_token}&url=#{url}").
|
73
|
-
to_return(:body => '{"test_body": "a"}')
|
74
|
-
app.params.clear
|
75
|
-
env['rack.request.query_hash'] = {}
|
76
|
-
i.call(env)
|
77
|
-
assert_requested(stub_default, :times => 1)
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_request_invalid_wovn_token
|
81
|
-
settings = Wovnrb.get_settings
|
82
|
-
settings['project_token'] = 'token0'
|
83
|
-
request_token = 'invalidtoken1'
|
84
|
-
url = 'wovn.io/dashboard'
|
85
|
-
stub = stub_request(:get, "#{settings['api_url']}?token=#{settings['project_token']}&url=#{url}").
|
86
|
-
to_return(:body => '{"test_body": "a"}')
|
87
|
-
|
88
|
-
i = Wovnrb::Interceptor.new(get_app(:params => {'wovn_token' => settings['project_token']}), settings)
|
89
|
-
|
90
|
-
env = Wovnrb.get_env
|
91
|
-
i.call(env)
|
92
|
-
assert_requested(stub, :times => 1)
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_request_wovn_ignore_paths
|
96
|
-
settings = Wovnrb.get_settings
|
97
|
-
url = 'wovn.io/dashboard'
|
98
|
-
stub = stub_request(:get, "#{settings['api_url']}?token=#{settings['project_token']}&url=#{url}").
|
99
|
-
to_return(:body => '{"test_body": "a"}')
|
100
|
-
|
101
|
-
app = get_app(:params => {'wovn_ignore_paths' => ['/dashboard']})
|
102
|
-
i = Wovnrb::Interceptor.new(app, settings)
|
103
|
-
|
104
|
-
env = Wovnrb.get_env
|
105
|
-
i.call(env)
|
106
|
-
assert_requested(stub, :times => 0)
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_request_wovn_disable
|
110
|
-
settings = Wovnrb.get_settings
|
111
|
-
token = settings['project_token']
|
112
|
-
url = 'wovn.io/dashboard'
|
113
|
-
stub = stub_request(:get, "#{settings['api_url']}?token=#{token}&url=#{url}").
|
114
|
-
to_return(:body => '{"test_body": "a"}')
|
115
|
-
|
116
|
-
i = Wovnrb::Interceptor.new(get_app(:params => {'wovn_disable' => true}), settings)
|
117
|
-
|
118
|
-
env = Wovnrb.get_env
|
119
|
-
i.call(env)
|
120
|
-
assert_requested(stub, :times => 0)
|
121
|
-
end
|
122
|
-
|
123
14
|
def test_switch_lang
|
124
|
-
|
125
|
-
h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
|
126
|
-
body = "<html><body><h1>Mr. Belvedere Fan Club</h1>
|
127
|
-
<div><p>Hello</p></div>
|
128
|
-
</body></html>"
|
129
|
-
values = generate_values
|
130
|
-
url = h.url
|
131
|
-
swapped_body = i.switch_lang([body], values, url, 'ja', h)
|
132
|
-
|
133
|
-
expected_body = "<html lang=\"ja\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true&currentLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script><link rel=\"alternate\" hreflang=\"ja\" href=\"http://ja.page.com/\"><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>
|
134
|
-
<div><p><!--wovn-src:Hello-->こんにちは</p></div>
|
135
|
-
</body></html>
|
136
|
-
"
|
137
|
-
assert_equal([expected_body], swapped_body)
|
138
|
-
end
|
15
|
+
body = '<html lang="ja"><body><h1>Mr. Belvedere Fan Club</h1><div><p>Hello</p></div></body></html>'
|
139
16
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
17
|
+
expected_body = [
|
18
|
+
'<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
|
19
|
+
"<script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true&currentLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script>",
|
20
|
+
'<link rel="alternate" hreflang="ja" href="http://ja.page.com/">',
|
21
|
+
'<link rel="alternate" hreflang="en" href="http://page.com/"></head>',
|
22
|
+
'<body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>',
|
23
|
+
'<div><p><!--wovn-src:Hello-->こんにちは</p></div>',
|
24
|
+
'</body></html>'
|
25
|
+
].join
|
147
26
|
|
148
|
-
|
27
|
+
assert_switch_lang('en', 'ja', body, expected_body, true)
|
149
28
|
end
|
150
29
|
|
151
30
|
def test_switch_lang_of_html_fragment_with_japanese_translations
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
values = generate_values
|
157
|
-
url = h.url
|
158
|
-
swapped_body = i.switch_lang([body], values, url, 'ja', h)
|
159
|
-
|
160
|
-
assert_equal([expected_body], swapped_body)
|
31
|
+
bodies = ['<span>Hello</span>'].join
|
32
|
+
expected_bodies = ['<span><!--wovn-src:Hello-->こんにちは</span>'].join
|
33
|
+
|
34
|
+
assert_switch_lang('en', 'ja', bodies, expected_bodies, true)
|
161
35
|
end
|
162
36
|
|
163
37
|
def test_switch_lang_splitted_body
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
url = h.url
|
171
|
-
swapped_body = i.switch_lang(bodies, values, url, 'ja', h)
|
172
|
-
|
173
|
-
expected_body = "<html lang=\"ja\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true&currentLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script><link rel=\"alternate\" hreflang=\"ja\" href=\"http://ja.page.com/\"><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1><div><p><!--wovn-src:Hello-->こんにちは</p></div></body></html>
|
174
|
-
"
|
175
|
-
assert_equal([expected_body], swapped_body)
|
38
|
+
bodies = ['<html><body><h1>Mr. Belvedere Fan Club</h1>',
|
39
|
+
'<div><p>Hello</p></div>',
|
40
|
+
'</body></html>'].join
|
41
|
+
expected_bodies = ["<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=subdomain&amp;langCodeAliases={}&amp;version=WOVN.rb_#{Wovnrb::VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1>Mr. Belvedere Fan Club</h1><div><p>Hello</p></div></body></html>"].join
|
42
|
+
|
43
|
+
assert_switch_lang('en', 'ja', bodies, expected_bodies, true)
|
176
44
|
end
|
177
45
|
|
178
46
|
def test_switch_lang_of_html_fragment_in_splitted_body
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
values = generate_values
|
185
|
-
url = h.url
|
186
|
-
swapped_body = i.switch_lang(bodies, values, url, 'ja', h)
|
187
|
-
|
188
|
-
assert_equal([expected_body], swapped_body)
|
47
|
+
body = ['<select name="test"><option value="1">1</option>',
|
48
|
+
'<option value="2">2</option></select>'].join
|
49
|
+
expected_body = ['<select name="test"><option value="1">1</option><option value="2">2</option></select>'].join
|
50
|
+
|
51
|
+
assert_switch_lang('en', 'ja', body, expected_body, true)
|
189
52
|
end
|
190
53
|
|
191
54
|
def test_switch_lang_missing_values
|
192
|
-
|
193
|
-
h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
|
194
|
-
body = "<html><body><h1>Mr. Belvedere Fan Club</h1>
|
55
|
+
body = "<html><body><h1>Mr. Belvedere Fan Club</h1>
|
195
56
|
<div><p>Hello</p></div>
|
196
57
|
</body></html>"
|
197
|
-
values = generate_missing_values
|
198
|
-
url = h.url
|
199
|
-
swapped_body = i.switch_lang([body], values, url, 'ja', h)
|
200
|
-
|
201
58
|
expected_body = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true&currentLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script></head><body><h1>Mr. Belvedere Fan Club</h1>
|
202
59
|
<div><p>Hello</p></div>
|
203
60
|
</body></html>
|
204
61
|
"
|
205
|
-
|
62
|
+
|
63
|
+
assert_switch_lang('en', 'ja', body, expected_body, true)
|
206
64
|
end
|
207
65
|
|
208
66
|
def test_switch_lang_on_fragment_with_translate_fragment_false
|
209
|
-
|
210
|
-
i = Wovnrb::Interceptor.new(get_app)
|
211
|
-
h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
|
212
|
-
body = "<h1>Mr. Belvedere Fan Club</h1>
|
67
|
+
body = "<h1>Mr. Belvedere Fan Club</h1>
|
213
68
|
<div><p>Hello</p></div>"
|
214
|
-
values = generate_values
|
215
|
-
url = h.url
|
216
|
-
swapped_body = i.switch_lang([body], values, url, 'ja', h)
|
217
69
|
|
218
|
-
|
70
|
+
Wovnrb::Store.instance.settings['translate_fragment'] = false
|
71
|
+
assert_switch_lang('en', 'ja', body, body, false)
|
219
72
|
end
|
220
73
|
|
221
74
|
def test_switch_lang_on_fragment_with_translate_fragment_true
|
222
|
-
|
223
|
-
i = Wovnrb::Interceptor.new(get_app)
|
224
|
-
h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
|
225
|
-
body = "<h1>Mr. Belvedere Fan Club</h1>
|
75
|
+
body = "<h1>Mr. Belvedere Fan Club</h1>
|
226
76
|
<div><p>Hello</p></div>"
|
227
|
-
values = generate_values
|
228
|
-
url = h.url
|
229
|
-
swapped_body = i.switch_lang([body], values, url, 'ja', h)
|
230
77
|
expected_body = "<h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>
|
231
78
|
<div><p><!--wovn-src:Hello-->こんにちは</p></div>"
|
232
79
|
|
233
|
-
|
80
|
+
Wovnrb::Store.instance.settings['translate_fragment'] = true
|
81
|
+
assert_switch_lang('en', 'ja', body, expected_body, true)
|
234
82
|
end
|
235
83
|
|
236
84
|
def test_switch_lang_ignores_amp
|
237
|
-
interceptor = Wovnrb::Interceptor.new(get_app)
|
238
|
-
headers = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
|
239
85
|
body = <<HTML
|
240
86
|
<html amp>
|
241
87
|
<head><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript></head>
|
@@ -245,25 +91,11 @@ class WovnrbTest < Minitest::Test
|
|
245
91
|
</body>
|
246
92
|
</html>
|
247
93
|
HTML
|
248
|
-
expected_body = <<HTML
|
249
|
-
<html amp=""><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript></head>
|
250
|
-
<body>
|
251
|
-
<h1>Mr. Belvedere Fan Club</h1>
|
252
|
-
<div><p>Hello</p></div>
|
253
|
-
|
254
|
-
|
255
|
-
</body></html>
|
256
|
-
HTML
|
257
|
-
values = generate_values
|
258
|
-
url = headers.url
|
259
|
-
swapped_bodies = interceptor.switch_lang([body], values, url, 'ja', headers)
|
260
94
|
|
261
|
-
|
95
|
+
assert_switch_lang('en', 'ja', body, body, false)
|
262
96
|
end
|
263
97
|
|
264
98
|
def test_switch_lang_ignores_amp_defined_with_symbol_attribute
|
265
|
-
interceptor = Wovnrb::Interceptor.new(get_app)
|
266
|
-
headers = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
|
267
99
|
body = <<HTML
|
268
100
|
<html ⚡>
|
269
101
|
<body>
|
@@ -272,132 +104,67 @@ HTML
|
|
272
104
|
</body>
|
273
105
|
</html>
|
274
106
|
HTML
|
275
|
-
expected_body = <<HTML
|
276
|
-
<html ⚡=""><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body>
|
277
|
-
<h1>Mr. Belvedere Fan Club</h1>
|
278
|
-
<div><p>Hello</p></div>
|
279
|
-
|
280
|
-
|
281
|
-
</body></html>
|
282
|
-
HTML
|
283
|
-
values = generate_values
|
284
|
-
url = headers.url
|
285
|
-
swapped_bodies = interceptor.switch_lang([body], values, url, 'ja', headers)
|
286
107
|
|
287
|
-
|
108
|
+
assert_switch_lang('en', 'ja', body, body, false)
|
288
109
|
end
|
289
110
|
|
290
|
-
|
291
|
-
i = Wovnrb::Interceptor.new(get_app)
|
292
|
-
h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
|
293
|
-
body = "<html><head><noscript><div>test</div></noscript></head><body><h1>Mr. Belvedere Fan Club</h1>
|
294
|
-
<div><p>Hello</p></div>
|
295
|
-
</body></html>"
|
296
|
-
values = generate_values
|
297
|
-
url = h.url
|
298
|
-
swapped_body = i.switch_lang([body], values, url, 'ja', h)
|
111
|
+
private
|
299
112
|
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
"
|
304
|
-
assert_equal([expected_body], swapped_body)
|
305
|
-
end
|
113
|
+
def assert_switch_lang(original_lang, target_lang, body, expected_body, api_expected = true)
|
114
|
+
subdomain = target_lang == original_lang ? '' : "#{target_lang}."
|
115
|
+
interceptor = Wovnrb::Interceptor.new(get_app)
|
306
116
|
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
</body></html>"
|
315
|
-
values = generate_values
|
316
|
-
url = h.url
|
317
|
-
swapped_body = i.switch_lang([body], values, url, 'ja', h)
|
318
|
-
|
319
|
-
expected_body = "<html lang=\"ja\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true&currentLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script><noscript>
|
320
|
-
<div>test</div>
|
321
|
-
</noscript><link rel=\"alternate\" hreflang=\"ja\" href=\"http://ja.page.com/\"><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>
|
322
|
-
<div><p><!--wovn-src:Hello-->こんにちは</p></div>
|
323
|
-
</body></html>
|
324
|
-
"
|
325
|
-
assert_equal([expected_body], swapped_body)
|
326
|
-
end
|
117
|
+
store, headers = store_headers_factory(subdomain, original_lang)
|
118
|
+
if api_expected
|
119
|
+
dom = Wovnrb::Helpers::NokogumboHelper.parse_html(body)
|
120
|
+
converter = Wovnrb::HtmlConverter.new(dom, store, headers)
|
121
|
+
apified_body, = converter.build_api_compatible_html
|
122
|
+
mock_translation_api_response(apified_body, expected_body)
|
123
|
+
end
|
327
124
|
|
328
|
-
|
329
|
-
i = Wovnrb::Interceptor.new(get_app)
|
330
|
-
h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
|
331
|
-
body = "<html><head><noscript><div>test</div></noscript><title>plop</title><noscript><div>test2</div></noscript></head><body><h1>Mr. Belvedere Fan Club</h1>
|
332
|
-
<div><p>Hello</p></div>
|
333
|
-
</body></html>"
|
334
|
-
values = generate_values
|
335
|
-
url = h.url
|
336
|
-
swapped_body = i.switch_lang([body], values, url, 'ja', h)
|
125
|
+
actual_bodies = interceptor.switch_lang(headers, [body])
|
337
126
|
|
338
|
-
expected_body
|
339
|
-
<div><p><!--wovn-src:Hello-->こんにちは</p></div>
|
340
|
-
</body></html>
|
341
|
-
"
|
342
|
-
assert_equal([expected_body], swapped_body)
|
127
|
+
assert_same_elements([expected_body], actual_bodies)
|
343
128
|
end
|
344
129
|
|
345
|
-
def
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
swapped_body = i.switch_lang([body], values, url, 'ja', h)
|
354
|
-
|
355
|
-
expected_body = "<html lang=\"ja\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true&currentLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script><noscript><!-- --><div>test</div></noscript><link rel=\"alternate\" hreflang=\"ja\" href=\"http://ja.page.com/\"><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>
|
356
|
-
<div><p><!--wovn-src:Hello-->こんにちは</p></div>
|
357
|
-
</body></html>
|
358
|
-
"
|
359
|
-
assert_equal([expected_body], swapped_body)
|
360
|
-
end
|
130
|
+
def store_headers_factory(subdomain, original_lang)
|
131
|
+
settings = {
|
132
|
+
'project_token' => '123456',
|
133
|
+
'custom_lang_aliases' => {},
|
134
|
+
'default_lang' => original_lang,
|
135
|
+
'url_pattern' => 'subdomain',
|
136
|
+
'url_pattern_reg' => '^(?<lang>[^.]+).'
|
137
|
+
}
|
361
138
|
|
362
|
-
|
363
|
-
|
364
|
-
h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
|
365
|
-
body = "<html><head><!--<noscript><div>test</div></noscript>--></head><body><h1>Mr. Belvedere Fan Club</h1>
|
366
|
-
<div><p>Hello</p></div>
|
367
|
-
</body></html>"
|
368
|
-
values = generate_values
|
369
|
-
url = h.url
|
370
|
-
swapped_body = i.switch_lang([body], values, url, 'ja', h)
|
139
|
+
store = Wovnrb::Store.instance
|
140
|
+
store.update_settings(settings)
|
371
141
|
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
assert_equal([expected_body], swapped_body)
|
377
|
-
end
|
142
|
+
headers = Wovnrb::Headers.new(
|
143
|
+
Wovnrb.get_env('url' => "http://#{subdomain}page.com"),
|
144
|
+
Wovnrb.get_settings(settings)
|
145
|
+
)
|
378
146
|
|
379
|
-
|
380
|
-
RackMock.new(opts)
|
147
|
+
[store, headers]
|
381
148
|
end
|
382
149
|
|
383
|
-
def
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
150
|
+
def mock_translation_api_response(body, expected_body)
|
151
|
+
Wovnrb::ApiTranslator.any_instance
|
152
|
+
.expects(:translate)
|
153
|
+
.once
|
154
|
+
.with(body)
|
155
|
+
.returns(expected_body)
|
388
156
|
end
|
389
157
|
|
390
|
-
def
|
391
|
-
|
392
|
-
return {}
|
158
|
+
def get_app(opts = {})
|
159
|
+
RackMock.new(opts)
|
393
160
|
end
|
394
161
|
|
395
162
|
class RackMock
|
396
163
|
attr_accessor :params
|
397
164
|
|
398
|
-
def initialize(opts={})
|
165
|
+
def initialize(opts = {})
|
399
166
|
@params = {}
|
400
|
-
if opts.
|
167
|
+
if opts.key?(:params) && opts[:params].class == Hash
|
401
168
|
opts[:params].each do |key, val|
|
402
169
|
@params[key] = val
|
403
170
|
end
|
@@ -407,13 +174,13 @@ HTML
|
|
407
174
|
|
408
175
|
def call(env)
|
409
176
|
@env = env
|
410
|
-
|
177
|
+
unless @params.empty?
|
411
178
|
req = Rack::Request.new(@env)
|
412
179
|
@params.each do |key, val|
|
413
180
|
req.update_param(key, val)
|
414
181
|
end
|
415
182
|
end
|
416
|
-
[200, {'Content-Type' => 'text/html'}, ['']]
|
183
|
+
[200, { 'Content-Type' => 'text/html' }, ['']]
|
417
184
|
end
|
418
185
|
|
419
186
|
def [](key)
|