forked from railslove/rack-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker_spec.rb
More file actions
108 lines (93 loc) · 3.42 KB
/
tracker_spec.rb
File metadata and controls
108 lines (93 loc) · 3.42 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
class DummyHandler < Rack::Tracker::Handler
def render
Tilt.new( File.join( File.dirname(__FILE__), '../fixtures/dummy.erb') ).render(self)
end
def dummy_alert
env['tracker']['dummy']
end
end
class BodyHandler < DummyHandler
self.position = :body
end
RSpec.describe Rack::Tracker do
def app
Rack::Builder.new do
use Rack::Session::Cookie, secret: "FOO"
use Rack::Tracker do
handler DummyHandler, { foo: 'head' }
handler BodyHandler, { foo: 'body' }
end
run lambda {|env|
request = Rack::Request.new(env)
case request.path
when '/' then
[200, {'Content-Type' => 'application/html'}, ['<head>Hello world</head>']]
when '/body' then
[200, {'Content-Type' => 'application/html'}, ['<body>bob here</body>']]
when '/body-head' then
[200, {'Content-Type' => 'application/html'}, ['<head></head><body></body>']]
when '/test.xml' then
[200, {'Content-Type' => 'application/xml'}, ['Xml here']]
when '/redirect' then
[302, {'Content-Type' => 'application/html', 'Location' => '/'}, ['<body>redirection</body>']]
when '/moved' then
[301, {'Content-Type' => 'application/html', 'Location' => '/redirect'}, ['<body>redirection</body>']]
else
[404, 'Nothing here']
end
}
end
end
subject { app }
describe 'when head is present' do
it 'injects the handler code' do
get '/'
expect(last_response.body).to include("alert('this is a dummy class');")
end
it 'will pass options to the Handler' do
get '/'
expect(last_response.body).to include("console.log('head');")
expect(last_response.body).to_not include("console.log('body');")
end
it 'injects custom variables that was directly assigned' do
get '/', {}, {'tracker' => { 'dummy' => 'foo bar'}}
expect(last_response.body).to include("alert('foo bar');")
end
it 'injects custom variables that lives in the session' do
get '/', {}, {'rack.session' => {'tracker' => { 'dummy' => 'bar foo'}}}
expect(last_response.body).to include("alert('bar foo');")
end
end
describe 'when body is present' do
it 'will not inject the body handler code' do
get '/body'
expect(last_response.body).to include("console.log('body');")
expect(last_response.body).to_not include("console.log('head');")
end
end
describe 'when head and body is present' do
it 'will pass options to the Handler' do
get '/body-head'
expect(last_response.body).to include("console.log('head');")
expect(last_response.body).to include("console.log('body');")
end
end
describe 'when a redirect' do
it 'will keep the tracker attributes and show them on the new location' do
get '/redirect', {}, { 'tracker' => { 'dummy' => 'Keep this!' } }
follow_redirect!
expect(last_response.body).to include("alert('Keep this!');")
end
it 'will keep the tracker attributes over multiple redirects' do
get '/moved', {}, { 'tracker' => { 'dummy' => 'Keep this twice!' } }
follow_redirect!
expect(last_response.body).to include("alert('Keep this twice!');")
end
end
describe 'when not html' do
it 'will not inject the handler code' do
get '/test.xml'
expect(last_response.body).to_not include("alert('this is a dummy class');")
end
end
end