wovnrb 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 844be0f5a9ee0ac6e995181311d020956f456ed7
4
- data.tar.gz: 10fc8b27efd2ea6c5ad091031916cbee54b75d00
3
+ metadata.gz: af6226de682d474cd3545ca245bdac14994375c4
4
+ data.tar.gz: 56a4592feebf0944fe01b94c91e14025e24a8ea6
5
5
  SHA512:
6
- metadata.gz: fbc82de4f3344432c508dc28b43fe727e99c8ac1b30691d90aac8c71e6a696d44dd3f06d5503dd78317ad0b3069b56d5684c7460e490cd76a2bbe1d53e43352f
7
- data.tar.gz: 056f1cc3176e6fca9fe867e57d35139eb7605bd3a6bebbecc271cafcf17a50e22d051daa5a2ed38f1f62a7ec70c201127e20b12eca5d225cf44a5ae6438a31eb
6
+ metadata.gz: 7b6a2b7aee2bd5fccbcea2e263fac158bae20e362a9fee1964a0ec810b68fbf45c8e28b61f03dc8523702c986825e6ac94c2378e05cf2cde844f3f3fe4f5ca97
7
+ data.tar.gz: fa1629f62d31efb45894a76338a0f775ac30de878ba22205266b9e4a0e6c83a5c3998cf6e76d4188f21ba19e6ca1a38b970773737ba2ccffa3a74e891512b6fc
data/lib/wovnrb.rb CHANGED
@@ -47,35 +47,42 @@ module Wovnrb
47
47
  # pass to application
48
48
  status, res_headers, body = @app.call(headers.request_out)
49
49
 
50
- if res_headers["Content-Type"] =~ /html/
51
-
52
- request = Rack::Request.new(env)
53
- unless request.params['wovn_disable'] == true
54
- @store.settings.update_dynamic_settings!(request.params)
55
- unless @store.settings['ignore_globs'].any?{|g| g.match?(headers.pathname)}
56
-
57
- # ApiData creates request for external server, but cannot use async.
58
- # Because some server not allow multi thread. (env['async.callback'] is not supported at all Server).
59
- api_data = ApiData.new(headers.redis_url, @store)
60
- values = api_data.get_data
61
- url = {
62
- :protocol => headers.protocol,
63
- :host => headers.host,
64
- :pathname => headers.pathname
65
- }
66
- body = switch_lang(body, values, url, lang, headers) unless status.to_s =~ /^1|302/
67
-
68
- content_length = 0
69
- body.each { |b| content_length += b.respond_to?(:bytesize) ? b.bytesize : 0 }
70
- res_headers["Content-Length"] = content_length.to_s
71
- end
72
- end
50
+ unless res_headers["Content-Type"] =~ /html/
51
+ return output(headers, status, res_headers, body)
52
+ end
53
+
54
+ request = Rack::Request.new(env)
73
55
 
56
+ if request.params['wovn_disable'] == true
57
+ return output(headers, status, res_headers, body)
74
58
  end
75
59
 
76
- headers.out(res_headers)
77
- [status, res_headers, body]
78
- #[status, res_headers, d.transform()]
60
+ @store.settings.update_dynamic_settings!(request.params)
61
+ if @store.settings['ignore_globs'].any?{|g| g.match?(headers.pathname)}
62
+ return output(headers, status, res_headers, body)
63
+ end
64
+
65
+ # ApiData creates request for external server, but cannot use async.
66
+ # Because some server not allow multi thread. (env['async.callback'] is not supported at all Server).
67
+ api_data = ApiData.new(headers.redis_url, @store)
68
+ values = api_data.get_data
69
+
70
+ unless have_data?(values)
71
+ return output(headers, status, res_headers, body)
72
+ end
73
+
74
+ url = {
75
+ :protocol => headers.protocol,
76
+ :host => headers.host,
77
+ :pathname => headers.pathname
78
+ }
79
+ body = switch_lang(body, values, url, lang, headers) unless status.to_s =~ /^1|302/
80
+
81
+ content_length = 0
82
+ body.each { |b| content_length += b.respond_to?(:bytesize) ? b.bytesize : 0 }
83
+ res_headers["Content-Length"] = content_length.to_s
84
+
85
+ output(headers, status, res_headers, body)
79
86
  end
80
87
 
81
88
  def switch_lang(body, values, url, lang=@store.settings['default_lang'], headers)
@@ -113,6 +120,15 @@ module Wovnrb
113
120
 
114
121
  private
115
122
 
123
+ def output(headers, status, res_headers, body)
124
+ headers.out(res_headers)
125
+ [status, res_headers, body]
126
+ end
127
+
128
+ def have_data?(values)
129
+ values.count > 0
130
+ end
131
+
116
132
  def put_back_noscripts!(output, noscripts)
117
133
  noscripts.each_with_index do |noscript, index|
118
134
  noscript_identifier = "<noscript wovn-id=\"#{index}\"></noscript>"
@@ -14,9 +14,11 @@ module Wovnrb
14
14
  # Its parameters are set by parsing env variable.
15
15
  #
16
16
  def initialize(env, settings)
17
+ request = Rack::Request.new(env)
18
+
17
19
  @env = env
18
20
  @settings = settings
19
- @protocol = @env['rack.url_scheme']
21
+ @protocol = request.scheme
20
22
  if settings['use_proxy'] && @env.has_key?('HTTP_X_FORWARDED_HOST')
21
23
  @unmasked_host = @env['HTTP_X_FORWARDED_HOST']
22
24
  else
@@ -1,3 +1,3 @@
1
1
  module Wovnrb
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.4"
3
3
  end
@@ -104,6 +104,12 @@ module Wovnrb
104
104
  assert_equal('wovn.io/dashboard', h.redis_url)
105
105
  end
106
106
 
107
+ def test_initialize_with_proto_header
108
+ env = Wovnrb.get_env('url' => 'http://page.com', 'HTTP_X_FORWARDED_PROTO' => 'https')
109
+ h = Wovnrb::Headers.new(env, Wovnrb.get_settings('query' => ['aaa']))
110
+ assert_equal('https', h.protocol)
111
+ end
112
+
107
113
  #########################
108
114
  # REDIRECT_LOCATION
109
115
  #########################
@@ -480,6 +480,18 @@ module Wovnrb
480
480
  assert(!(/html lang="zh-CHT"/ =~ swapped_body))
481
481
  end
482
482
 
483
+ def test_switch_dom_lang_when_proxy_change_protocol
484
+ lang = Lang.new('en')
485
+ env = Wovnrb.get_env('url' => 'http://page.com', 'HTTP_X_FORWARDED_PROTO' => 'https')
486
+ header = Wovnrb::Headers.new(env, Wovnrb.get_settings)
487
+ html = '<html></html>'
488
+ dom = Wovnrb.to_dom(html)
489
+ url = header.url
490
+ swapped_body = lang.switch_dom_lang(dom, Store.instance, generate_values, url, header)
491
+ assert(swapped_body.include?('hreflang="ja" href="https://page.com/ja/"'))
492
+ assert(swapped_body.include?('hreflang="en" href="https://page.com/"'))
493
+ end
494
+
483
495
  def test_switch_lang
484
496
  lang = Lang.new('ja')
485
497
  h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+ require 'test_helper'
2
3
  require 'wovnrb'
3
4
  require 'wovnrb/headers'
4
5
  require 'minitest/autorun'
@@ -119,6 +120,25 @@ class WovnrbTest < Minitest::Test
119
120
  assert_requested(stub, :times => 0)
120
121
  end
121
122
 
123
+ def test_request_to_not_published_page
124
+ settings = Wovnrb.get_settings
125
+ default_token = 'token0'
126
+ request_token = 'token1'
127
+ settings['project_token'] = default_token
128
+ url = 'wovn.io/dashboard'
129
+ stub = stub_request(:get, "#{settings['api_url']}?token=#{request_token}&url=#{url}").
130
+ to_return(:body => '{"code":423,"message":"Page is not published"}', status: 423)
131
+
132
+ app = get_app(:params => {'wovn_token' => request_token})
133
+ i = Wovnrb::Interceptor.new(app, settings)
134
+ env = Wovnrb.get_env
135
+ i.stub(:switch_lang, ->{raise 'must not called'}) do
136
+ i.call(env)
137
+ end
138
+
139
+ assert_requested(stub, :times => 1)
140
+ end
141
+
122
142
  def test_switch_lang
123
143
  i = Wovnrb::Interceptor.new(get_app)
124
144
  h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
data/test/test_helper.rb CHANGED
@@ -14,10 +14,12 @@ SimpleCov.start do
14
14
  end
15
15
 
16
16
  require 'nokogumbo'
17
+ require 'rack'
17
18
  require 'wovnrb/headers'
18
19
  require 'wovnrb/lang'
19
20
  require 'wovnrb/store'
20
21
  require 'wovnrb/html_replacers/replacer_base'
22
+ require 'wovnrb/html_replacers/input_replacer'
21
23
  require 'wovnrb/html_replacers/link_replacer'
22
24
  require 'wovnrb/html_replacers/text_replacer'
23
25
  require 'wovnrb/html_replacers/meta_replacer'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wovnrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Sandford
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-04-05 00:00:00.000000000 Z
12
+ date: 2018-06-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogumbo
@@ -412,7 +412,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
412
412
  version: '0'
413
413
  requirements: []
414
414
  rubyforge_project:
415
- rubygems_version: 2.2.0
415
+ rubygems_version: 2.5.2
416
416
  signing_key:
417
417
  specification_version: 4
418
418
  summary: Gem for WOVN.io