-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathprerender_rails.rb
More file actions
224 lines (154 loc) · 10.1 KB
/
prerender_rails.rb
File metadata and controls
224 lines (154 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
require_relative '../test_helper'
describe Rack::Prerender do
bot = 'Baiduspider+(+http://www.baidu.com/search/spider.htm)'
user = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36'
before :each do
@app = lambda do |params|
[200, {}, ""]
end
@prerender = Rack::Prerender.new(@app)
end
it "should return a prerendered response for a crawler with the returned status code and headers" do
request = Rack::MockRequest.env_for "/", "HTTP_USER_AGENT" => bot
stub_request(:get, @prerender.build_api_url(request)).with(:headers => { 'User-Agent' => bot }).to_return(:body => "<html></html>", :status => 301, :headers => { 'Location' => 'http://google.com'})
response = Rack::Prerender.new(@app).call(request)
assert_equal response[2].body, ["<html></html>"]
assert_equal response[2].status, 301
assert_equal( { 'location' => 'http://google.com', 'Content-Length' => '13'}, response[2].headers )
end
it "should return a prerendered reponse if user is a bot by checking for _escaped_fragment_" do
request = Rack::MockRequest.env_for "/path?_escaped_fragment_=", "HTTP_USER_AGENT" => user
stub_request(:get, @prerender.build_api_url(request)).with(:headers => { 'User-Agent' => user }).to_return(:body => "<html></html>")
response = Rack::Prerender.new(@app).call(request)
assert_equal ["<html></html>"], response[2].body
end
it "should continue to app routes if the url is a bad url with _escaped_fragment_" do
request = Rack::MockRequest.env_for "/path?query=string?_escaped_fragment_=", "HTTP_USER_AGENT" => user
response = Rack::Prerender.new(@app).call(request)
assert_equal "", response[2]
end
it "should continue to app routes if the request is not a GET" do
request = Rack::MockRequest.env_for "/path?_escaped_fragment_=", { "HTTP_USER_AGENT" => user, "REQUEST_METHOD" => "POST" }
response = Rack::Prerender.new(@app).call(request)
assert_equal "", response[2]
end
it "should continue to app routes if user is not a bot by checking agent string" do
request = Rack::MockRequest.env_for "/", "HTTP_USER_AGENT" => user
response = Rack::Prerender.new(@app).call(request)
assert_equal "", response[2]
end
it "should continue to app routes if contains X-Prerender header" do
request = Rack::MockRequest.env_for "/path?_escaped_fragment_=", "HTTP_USER_AGENT" => user, "HTTP_X_PRERENDER" => "1"
response = Rack::Prerender.new(@app).call(request)
assert_equal "", response[2]
end
it "should continue to app routes if user is a bot, but the bot is requesting a resource file" do
request = Rack::MockRequest.env_for "/main.js?anyQueryParam=true", "HTTP_USER_AGENT" => bot
response = Rack::Prerender.new(@app).call(request)
assert_equal "", response[2]
end
it "should continue to app routes if the url is not part of the regex specific whitelist" do
request = Rack::MockRequest.env_for "/saved/search/blah?_escaped_fragment_=", "HTTP_USER_AGENT" => bot
response = Rack::Prerender.new(@app, whitelist: ['^/search', '/help']).call(request)
assert_equal "", response[2]
end
it "should set use_ssl to true for https prerender_service_url" do
@prerender = Rack::Prerender.new(@app, prerender_service_url: 'https://service.prerender.io/')
request = Rack::MockRequest.env_for "/search/things/123/page?_escaped_fragment_=", "HTTP_USER_AGENT" => bot
stub_request(:get, @prerender.build_api_url(request)).to_return(:body => "<html></html>")
response = @prerender.call(request)
assert_equal ["<html></html>"], response[2].body
end
it "should return a prerendered response if the url is part of the regex specific whitelist" do
request = Rack::MockRequest.env_for "/search/things/123/page?_escaped_fragment_=", "HTTP_USER_AGENT" => bot
stub_request(:get, @prerender.build_api_url(request)).to_return(:body => "<html></html>")
response = Rack::Prerender.new(@app, whitelist: ['^/search.*page', '/help']).call(request)
assert_equal ["<html></html>"], response[2].body
end
it "should continue to app routes if the url is part of the regex specific blacklist" do
request = Rack::MockRequest.env_for "/search/things/123/page", "HTTP_USER_AGENT" => bot
response = Rack::Prerender.new(@app, blacklist: ['^/search', '/help']).call(request)
assert_equal "", response[2]
end
it "should continue to app routes if the hashbang url is part of the regex specific blacklist" do
request = Rack::MockRequest.env_for "?_escaped_fragment_=/search/things/123/page", "HTTP_USER_AGENT" => bot
response = Rack::Prerender.new(@app, blacklist: ['/search', '/help']).call(request)
assert_equal "", response[2]
end
it "should return a prerendered response if the url is not part of the regex specific blacklist" do
request = Rack::MockRequest.env_for "/profile/search/blah", "HTTP_USER_AGENT" => bot
stub_request(:get, @prerender.build_api_url(request)).to_return(:body => "<html></html>")
response = Rack::Prerender.new(@app, blacklist: ['^/search', '/help']).call(request)
assert_equal ["<html></html>"], response[2].body
end
it "should continue to app routes if the referer is part of the regex specific blacklist" do
request = Rack::MockRequest.env_for "/api/results", "HTTP_USER_AGENT" => bot, "HTTP_REFERER" => '/search'
response = Rack::Prerender.new(@app, blacklist: ['^/search', '/help']).call(request)
assert_equal "", response[2]
end
it "should return a prerendered response if the referer is not part of the regex specific blacklist" do
request = Rack::MockRequest.env_for "/api/results", "HTTP_USER_AGENT" => bot, "HTTP_REFERER" => '/profile/search'
stub_request(:get, @prerender.build_api_url(request)).to_return(:body => "<html></html>")
response = Rack::Prerender.new(@app, blacklist: ['^/search', '/help']).call(request)
assert_equal ["<html></html>"], response[2].body
end
it "should return a prerendered response if a string is returned from before_render" do
request = Rack::MockRequest.env_for "/", "HTTP_USER_AGENT" => bot
response = Rack::Prerender.new(@app, before_render: Proc.new do |env| '<html>cached</html>' end).call(request)
assert_equal ["<html>cached</html>"], response[2].body
end
it "should return a prerendered response if a response is returned from before_render" do
request = Rack::MockRequest.env_for "/", "HTTP_USER_AGENT" => bot
response = Rack::Prerender.new(@app, before_render: Proc.new do |env| Rack::Response.new('<html>cached2</html>', 200, { 'test' => 'test2Header'}) end).call(request)
assert_equal ["<html>cached2</html>"], response[2].body
assert_equal response[2].status, 200
assert_equal( { 'test' => 'test2Header', "Content-Length"=>"20"}, response[2].headers )
end
describe '#buildApiUrl' do
it "should build the correct api url with the default url" do
request = Rack::MockRequest.env_for "https://google.com/search?q=javascript"
ENV['PRERENDER_SERVICE_URL'] = nil
assert_equal 'http://service.prerender.io/https://google.com/search?q=javascript', @prerender.build_api_url(request)
end
it "should build the correct api url with an environment variable url" do
ENV['PRERENDER_SERVICE_URL'] = 'http://prerenderurl.com'
request = Rack::MockRequest.env_for "https://google.com/search?q=javascript"
assert_equal 'http://prerenderurl.com/https://google.com/search?q=javascript', @prerender.build_api_url(request)
ENV['PRERENDER_SERVICE_URL'] = nil
end
it "should build the correct api url with an initialization variable url" do
@prerender = Rack::Prerender.new(@app, prerender_service_url: 'http://prerenderurl.com')
request = Rack::MockRequest.env_for "https://google.com/search?q=javascript"
assert_equal 'http://prerenderurl.com/https://google.com/search?q=javascript', @prerender.build_api_url(request)
end
it "should build the correct https api url with an initialization variable url" do
@prerender = Rack::Prerender.new(@app, prerender_service_url: 'https://prerenderurl.com')
request = Rack::MockRequest.env_for "https://google.com/search?q=javascript"
assert_equal 'https://prerenderurl.com/https://google.com/search?q=javascript', @prerender.build_api_url(request)
end
# Check CF-Visitor header in order to Work behind CloudFlare with Flexible SSL (https://support.cloudflare.com/hc/en-us/articles/200170536)
it "should build the correct api url for the Cloudflare Flexible SSL support" do
request = Rack::MockRequest.env_for "http://google.com/search?q=javascript", { 'CF-VISITOR' => '"scheme":"https"'}
ENV['PRERENDER_SERVICE_URL'] = nil
assert_equal 'http://service.prerender.io/https://google.com/search?q=javascript', @prerender.build_api_url(request)
end
# Check X-Forwarded-Proto because Heroku SSL Support terminates at the load balancer
it "should build the correct api url for the Heroku SSL Addon support with single value" do
request = Rack::MockRequest.env_for "http://google.com/search?q=javascript", { 'HTTP_X_FORWARDED_PROTO' => 'https'}
ENV['PRERENDER_SERVICE_URL'] = nil
assert_equal 'http://service.prerender.io/https://google.com/search?q=javascript', @prerender.build_api_url(request)
end
# Check X-Forwarded-Proto because Heroku SSL Support terminates at the load balancer
it "should build the correct api url for the Heroku SSL Addon support with double value" do
request = Rack::MockRequest.env_for "http://google.com/search?q=javascript", { 'HTTP_X_FORWARDED_PROTO' => 'https,http'}
ENV['PRERENDER_SERVICE_URL'] = nil
assert_equal 'http://service.prerender.io/https://google.com/search?q=javascript', @prerender.build_api_url(request)
end
it "should build the correct api url for the protocol option" do
@prerender = Rack::Prerender.new(@app, protocol: 'https')
request = Rack::MockRequest.env_for "http://google.com/search?q=javascript"
ENV['PRERENDER_SERVICE_URL'] = nil
assert_equal 'http://service.prerender.io/https://google.com/search?q=javascript', @prerender.build_api_url(request)
end
end
end