rack_phantom 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rack_phantom.rb +9 -2
- data/lib/rack_phantom/version.rb +1 -1
- data/spec/rack_phantom_spec.rb +11 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 228af5ff03c29c2e333491e784ddbd0508ca3b6d
|
4
|
+
data.tar.gz: 31238f79e1a1b2e61c38e6df1d34670ac45e660a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec40ae7c9940e0f2682193df063406e2f5688b60b59801f3f012d4166ffe8d60bdf77137ce4885bd98ab37081dbec4d4e375bdef57bd6e383917112488763cf8
|
7
|
+
data.tar.gz: 770ed195fdaade233a87457fd6844f2d11fc63a23081f863417135bfca18a3419a009e3f2efd0d93650bb6e4696a9c8bc59bb77346f292954b26dcce996c9898
|
data/lib/rack_phantom.rb
CHANGED
@@ -21,11 +21,18 @@ module RackPhantom
|
|
21
21
|
|
22
22
|
return [status, headers, response] unless render?(headers, env)
|
23
23
|
|
24
|
-
html = Phantomjs.run(
|
25
|
-
[
|
24
|
+
html = Phantomjs.run(render_js, %['#{response.join("\n")}']).strip
|
25
|
+
headers['Content-Length'] = html.length.to_s
|
26
|
+
|
27
|
+
[status, headers, [html]]
|
26
28
|
end
|
27
29
|
|
28
30
|
private
|
31
|
+
|
32
|
+
def render_js
|
33
|
+
File.expand_path(File.join(File.dirname(__FILE__), '..', 'js', 'render.js'))
|
34
|
+
end
|
35
|
+
|
29
36
|
def render?(headers, env)
|
30
37
|
html_response?(headers) && bot?(env) && get?(env)
|
31
38
|
end
|
data/lib/rack_phantom/version.rb
CHANGED
data/spec/rack_phantom_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe RackPhantom do
|
4
4
|
|
5
5
|
let(:html) { %q{<html><head><script>document.write("Hello World");</script></head><body></body></html>} }
|
6
|
-
let(:real_app) { ->(env) { [200, {'Content-Type' => 'text/html'}, html] } }
|
6
|
+
let(:real_app) { ->(env) { [200, {'Content-Type' => 'text/html'}, [html]] } }
|
7
7
|
let(:app) { RackPhantom::Middleware.new(real_app) }
|
8
8
|
|
9
9
|
|
@@ -11,7 +11,12 @@ describe RackPhantom do
|
|
11
11
|
it 'should execute javascript' do
|
12
12
|
get '/', {}, as_bot
|
13
13
|
last_response.should be_ok
|
14
|
-
last_response.body.
|
14
|
+
last_response.body.should == evaluated_html
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should update the content length header' do
|
18
|
+
get '/', {}, as_bot
|
19
|
+
last_response.headers['Content-Length'].should == evaluated_html.length.to_s
|
15
20
|
end
|
16
21
|
|
17
22
|
it 'escapes the single quotes'
|
@@ -25,24 +30,24 @@ describe RackPhantom do
|
|
25
30
|
context 'for non bots' do
|
26
31
|
it 'should not render' do
|
27
32
|
get '/'
|
28
|
-
last_response.body.
|
33
|
+
last_response.body.should == html
|
29
34
|
end
|
30
35
|
end
|
31
36
|
|
32
37
|
context 'for POST requests' do
|
33
38
|
it 'should not render' do
|
34
39
|
post '/', {}, as_bot
|
35
|
-
last_response.body.
|
40
|
+
last_response.body.should == html
|
36
41
|
end
|
37
42
|
end
|
38
43
|
|
39
44
|
context 'for non html responses' do
|
40
45
|
let(:json) { '{}' }
|
41
|
-
let(:real_app) { ->(env) { [200, {'Content-Type' => 'application/json'}, json] } }
|
46
|
+
let(:real_app) { ->(env) { [200, {'Content-Type' => 'application/json'}, [json]] } }
|
42
47
|
|
43
48
|
it 'should not render' do
|
44
49
|
get '/'
|
45
|
-
last_response.body.
|
50
|
+
last_response.body.should == json
|
46
51
|
end
|
47
52
|
end
|
48
53
|
|