prerender_rails 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ec81d9470f347151a442c94c7ed1a39102d4bdde
4
- data.tar.gz: 37567e7f03e9a2b163eb1d0a14e163ecee8530f0
3
+ metadata.gz: 480c6d3189a06dc2343b3339fac868bb03ae5d45
4
+ data.tar.gz: 283c51d78e87ba863f53b2a0be089db9ba0e0f90
5
5
  SHA512:
6
- metadata.gz: e49da14a9f06eb1bceeb7ff0303d567b9418c4ffaa1e2e0b04796cb0c000541766c712864981c71517b1197ba266ce56c1872b9c030d83c09c4ac19767094312
7
- data.tar.gz: 41ac300c1f8e7e1ee0c7b801ca6cf746893aec55f1b8a49baba070d9e994277ec9e5b687d775e228bfa2316570870c852c65b1c6a2046dd35cb98ce1527da72a
6
+ metadata.gz: 9b0b9b470a46328f7ab4c7e18c47b34523e4b23a1c76416c1ff2417434d8faaf198d562f8fa7ea40361076a8049a854ab8b48655733b20382edce87112792d58
7
+ data.tar.gz: d6a0e6b6a9cffbb781106e6182545fe7dfa3298f3a3460c6c6c8e3c37c830f87dfd31313499a182b669291f51700794cde207069571cc513b32975e4092a721b
@@ -122,7 +122,14 @@ module Rack
122
122
  end
123
123
 
124
124
  def build_api_url(env)
125
- url = Rack::Request.new(env).url
125
+ new_env = env
126
+ if env["CF-VISITOR"]
127
+ match = /"scheme":"(http|https)"/.match(env['CF-VISITOR'])
128
+ new_env["HTTPS"] = true and new_env["rack.url_scheme"] = "https" and new_env["SERVER_PORT"] = 443 if (match && match[1] == "https")
129
+ new_env["HTTPS"] = false and new_env["rack.url_scheme"] = "http" and new_env["SERVER_PORT"] = 80 if (match && match[1] == "http")
130
+ end
131
+
132
+ url = Rack::Request.new(new_env).url
126
133
  prerender_url = get_prerender_service_url()
127
134
  forward_slash = prerender_url[-1, 1] == '/' ? '' : '/'
128
135
  "#{prerender_url}#{forward_slash}#{url}"
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "prerender_rails"
5
- spec.version = "0.1.7"
5
+ spec.version = "0.1.8"
6
6
  spec.authors = ["Todd Hooper"]
7
7
  spec.email = ["todd@collectiveip.com"]
8
8
  spec.description = %q{Rails middleware to prerender your javascript heavy pages on the fly by a phantomjs service}
@@ -20,7 +20,7 @@ describe Rack::Prerender do
20
20
 
21
21
  assert_equal response[2].body, ["<html></html>"]
22
22
  assert_equal response[2].status, 301
23
- assert_equal response[2].headers, { 'location' => 'http://google.com', 'Content-Length' => '13'}
23
+ assert_equal( { 'location' => 'http://google.com', 'Content-Length' => '13'}, response[2].headers )
24
24
  end
25
25
 
26
26
  it "should return a prerendered reponse if user is a bot by checking for _escaped_fragment_" do
@@ -28,42 +28,42 @@ describe Rack::Prerender do
28
28
  stub_request(:get, @prerender.build_api_url(request)).with(:headers => { 'User-Agent' => user }).to_return(:body => "<html></html>")
29
29
  response = Rack::Prerender.new(@app).call(request)
30
30
 
31
- assert_equal response[2].body, ["<html></html>"]
31
+ assert_equal ["<html></html>"], response[2].body
32
32
  end
33
33
 
34
34
  it "should continue to app routes if the url is a bad url with _escaped_fragment_" do
35
35
  request = Rack::MockRequest.env_for "/path?query=string?_escaped_fragment_=", "HTTP_USER_AGENT" => user
36
36
  response = Rack::Prerender.new(@app).call(request)
37
37
 
38
- assert_equal response[2], ""
38
+ assert_equal "", response[2]
39
39
  end
40
40
 
41
41
  it "should continue to app routes if the request is not a GET" do
42
42
  request = Rack::MockRequest.env_for "/path?_escaped_fragment_=", { "HTTP_USER_AGENT" => user, "REQUEST_METHOD" => "POST" }
43
43
  response = Rack::Prerender.new(@app).call(request)
44
44
 
45
- assert_equal response[2], ""
45
+ assert_equal "", response[2]
46
46
  end
47
47
 
48
48
  it "should continue to app routes if user is not a bot by checking agent string" do
49
49
  request = Rack::MockRequest.env_for "/", "HTTP_USER_AGENT" => user
50
50
  response = Rack::Prerender.new(@app).call(request)
51
51
 
52
- assert_equal response[2], ""
52
+ assert_equal "", response[2]
53
53
  end
54
54
 
55
55
  it "should continue to app routes if user is a bot, but the bot is requesting a resource file" do
56
56
  request = Rack::MockRequest.env_for "/main.js?anyQueryParam=true", "HTTP_USER_AGENT" => bot
57
57
  response = Rack::Prerender.new(@app).call(request)
58
58
 
59
- assert_equal response[2], ""
59
+ assert_equal "", response[2]
60
60
  end
61
61
 
62
62
  it "should continue to app routes if the url is not part of the regex specific whitelist" do
63
63
  request = Rack::MockRequest.env_for "/saved/search/blah", "HTTP_USER_AGENT" => bot
64
64
  response = Rack::Prerender.new(@app, whitelist: ['^/search', '/help']).call(request)
65
65
 
66
- assert_equal response[2], ""
66
+ assert_equal "", response[2]
67
67
  end
68
68
 
69
69
  it "should return a prerendered response if the url is part of the regex specific whitelist" do
@@ -71,14 +71,14 @@ describe Rack::Prerender do
71
71
  stub_request(:get, @prerender.build_api_url(request)).to_return(:body => "<html></html>")
72
72
  response = Rack::Prerender.new(@app, whitelist: ['^/search.*page', '/help']).call(request)
73
73
 
74
- assert_equal response[2].body, ["<html></html>"]
74
+ assert_equal ["<html></html>"], response[2].body
75
75
  end
76
76
 
77
77
  it "should continue to app routes if the url is part of the regex specific blacklist" do
78
78
  request = Rack::MockRequest.env_for "/search/things/123/page", "HTTP_USER_AGENT" => bot
79
79
  response = Rack::Prerender.new(@app, blacklist: ['^/search', '/help']).call(request)
80
80
 
81
- assert_equal response[2], ""
81
+ assert_equal "", response[2]
82
82
  end
83
83
 
84
84
  it "should return a prerendered response if the url is not part of the regex specific blacklist" do
@@ -86,14 +86,14 @@ describe Rack::Prerender do
86
86
  stub_request(:get, @prerender.build_api_url(request)).to_return(:body => "<html></html>")
87
87
  response = Rack::Prerender.new(@app, blacklist: ['^/search', '/help']).call(request)
88
88
 
89
- assert_equal response[2].body, ["<html></html>"]
89
+ assert_equal ["<html></html>"], response[2].body
90
90
  end
91
91
 
92
92
  it "should continue to app routes if the referer is part of the regex specific blacklist" do
93
93
  request = Rack::MockRequest.env_for "/api/results", "HTTP_USER_AGENT" => bot, "HTTP_REFERER" => '/search'
94
94
  response = Rack::Prerender.new(@app, blacklist: ['^/search', '/help']).call(request)
95
95
 
96
- assert_equal response[2], ""
96
+ assert_equal "", response[2]
97
97
  end
98
98
 
99
99
  it "should return a prerendered response if the referer is not part of the regex specific blacklist" do
@@ -101,27 +101,34 @@ describe Rack::Prerender do
101
101
  stub_request(:get, @prerender.build_api_url(request)).to_return(:body => "<html></html>")
102
102
  response = Rack::Prerender.new(@app, blacklist: ['^/search', '/help']).call(request)
103
103
 
104
- assert_equal response[2].body, ["<html></html>"]
104
+ assert_equal ["<html></html>"], response[2].body
105
105
  end
106
106
 
107
107
  describe '#buildApiUrl' do
108
108
  it "should build the correct api url with the default url" do
109
109
  request = Rack::MockRequest.env_for "https://google.com/search?q=javascript"
110
110
  ENV['PRERENDER_SERVICE_URL'] = nil
111
- assert_equal @prerender.build_api_url(request), 'http://prerender.herokuapp.com/https://google.com/search?q=javascript'
111
+ assert_equal 'http://prerender.herokuapp.com/https://google.com/search?q=javascript', @prerender.build_api_url(request)
112
112
  end
113
113
 
114
114
  it "should build the correct api url with an environment variable url" do
115
115
  ENV['PRERENDER_SERVICE_URL'] = 'http://prerenderurl.com'
116
116
  request = Rack::MockRequest.env_for "https://google.com/search?q=javascript"
117
- assert_equal @prerender.build_api_url(request), 'http://prerenderurl.com/https://google.com/search?q=javascript'
117
+ assert_equal 'http://prerenderurl.com/https://google.com/search?q=javascript', @prerender.build_api_url(request)
118
118
  ENV['PRERENDER_SERVICE_URL'] = nil
119
119
  end
120
120
 
121
121
  it "should build the correct api url with an initialization variable url" do
122
122
  @prerender = Rack::Prerender.new(@app, prerender_service_url: 'http://prerenderurl.com')
123
123
  request = Rack::MockRequest.env_for "https://google.com/search?q=javascript"
124
- assert_equal @prerender.build_api_url(request), 'http://prerenderurl.com/https://google.com/search?q=javascript'
124
+ assert_equal 'http://prerenderurl.com/https://google.com/search?q=javascript', @prerender.build_api_url(request)
125
+ end
126
+
127
+ # Check CF-Visitor header in order to Work behind CloudFlare with Flexible SSL (https://support.cloudflare.com/hc/en-us/articles/200170536)
128
+ it "should build the correct api url for the Cloudflare Flexible SSL support" do
129
+ request = Rack::MockRequest.env_for "http://google.com/search?q=javascript", { 'CF-VISITOR' => '"scheme":"https"'}
130
+ ENV['PRERENDER_SERVICE_URL'] = nil
131
+ assert_equal 'http://prerender.herokuapp.com/https://google.com/search?q=javascript', @prerender.build_api_url(request)
125
132
  end
126
133
  end
127
134
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prerender_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Todd Hooper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-10 00:00:00.000000000 Z
11
+ date: 2013-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack