/
middleware_spec.rb
284 lines (240 loc) · 9.46 KB
/
middleware_spec.rb
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# frozen_string_literal: true
require 'rack/test'
require 'zlib'
describe Rack::MiniProfiler do
include Rack::Test::Methods
before(:each) { Rack::MiniProfiler.reset_config }
def do_get(params = {})
get '/html', params, 'HTTP_ACCEPT_ENCODING' => 'gzip, compress'
end
def decompressed_response
Zlib::GzipReader.new(StringIO.new(last_response.body)).read
end
describe '/rack-mini-profiler/requests page' do
def app
Rack::Builder.new do
use Rack::MiniProfiler
run lambda { |_env| [200, { 'Content-Type' => 'text/html' }, [+'<html><body><h1>Hi</h1></body></html>']] }
end
end
it 'is an empty page' do
get '/rack-mini-profiler/requests', {}, 'HTTP_ACCEPT_ENCODING' => 'gzip, compress'
expect(last_response.body).to include('<title>Rack::MiniProfiler Requests</title>')
expect(last_response.body).to match('<body>\n </body>')
end
end
describe '?pp=help page' do
def app
Rack::Builder.new do
use Rack::MiniProfiler
run lambda { |_env| [200, { 'Content-Type' => 'text/html' }, [+'<html><body><h1>Hi</h1></body></html>']] }
end
end
it 'shows commands' do
do_get(pp: 'help')
expect(last_response.body).to include('<title>Rack::MiniProfiler Help</title>')
expect(last_response.body).to include("help")
expect(last_response.body).to include("env")
expect(last_response.body).to include("skip")
expect(last_response.body).to include("no-backtrace")
expect(last_response.body).to include("normal-backtrace")
expect(last_response.body).to include("full-backtrace")
expect(last_response.body).to include("disable")
expect(last_response.body).to include("enable")
expect(last_response.body).to include("profile-gc")
expect(last_response.body).to include("profile-memory")
expect(last_response.body).to include("flamegraph")
expect(last_response.body).to include("async-flamegraph")
expect(last_response.body).to include("flamegraph&flamegraph_sample_rate=1")
expect(last_response.body).to include("flamegraph&flamegraph_mode=cpu")
expect(last_response.body).to include("flamegraph&flamegraph_ignore_gc=true")
expect(last_response.body).to include("flamegraph_embed")
expect(last_response.body).to include("trace-exceptions")
expect(last_response.body).to include("analyze-memory")
end
end
shared_examples 'should not affect a skipped requests' do
it 'should not affect a skipped requests' do
do_get(pp: 'skip')
expect(last_response.headers).to include('Content-Encoding')
expect(last_response.headers['Content-Encoding']).to eq('gzip')
end
end
describe 'when enable_advanced_debugging_tools is false' do
def app
Rack::Builder.new do
use Rack::MiniProfiler
run lambda { |_env| [200, { 'Content-Type' => 'text/html' }, [+'<html><body><h1>Hi</h1></body></html>']] }
end
end
it 'advanced tools are disabled' do
%w{env analyze-memory profile-gc profile-memory}.each do |p|
do_get(pp: p)
expect(last_response.body).to eq(Rack::MiniProfiler.advanced_tools_message)
end
end
end
describe 'when enable_advanced_debugging_tools is true' do
def app
Rack::Builder.new do
use Rack::MiniProfiler
run(
lambda do |_env|
[
201,
{ 'Content-Type' => 'text/html', 'X-CUSTOM' => "1" },
[+'<html><body><h1>Hi</h1></body></html>'],
]
end
)
end
end
before(:each) { Rack::MiniProfiler.config.enable_advanced_debugging_tools = true }
describe 'with analyze-memory query' do
it 'should return ObjectSpace statistics' do
do_get(pp: 'analyze-memory')
expect(last_response.body).to include('Largest strings:')
end
end
describe 'with profile-memory query' do
it 'should return memory_profiler error message' do
do_get(pp: 'profile-memory')
expect(last_response.body).to eq(
'Please install the memory_profiler gem and require it: add gem \'memory_profiler\' to your Gemfile'
)
expect(last_response.headers['Content-Type']).to eq('text/plain; charset=utf-8')
expect(last_response.headers['X-CUSTOM']).to eq('1')
expect(last_response.status).to eq(500)
end
end
describe 'with flamegraph query' do
it 'should return stackprof error message' do
do_get(pp: 'flamegraph')
expect(last_response.body).to eq(
'Please install the stackprof gem and require it: add gem \'stackprof\' to your Gemfile'
)
expect(last_response.headers['Content-Type']).to eq('text/plain; charset=utf-8')
expect(last_response.headers['X-CUSTOM']).to eq('1')
expect(last_response.status).to eq(201)
end
end
describe 'with async-flamegraph query' do
it 'should return stackprof error message' do
do_get(pp: 'async-flamegraph')
expect(last_response.body).to eq(
'Please install the stackprof gem and require it: add gem \'stackprof\' to your Gemfile'
)
expect(last_response.headers['Content-Type']).to eq('text/plain; charset=utf-8')
expect(last_response.headers['X-CUSTOM']).to eq('1')
expect(last_response.status).to eq(201)
end
end
end
describe 'with Rack::MiniProfiler before Rack::Deflater' do
def app
Rack::Builder.new do
use Rack::MiniProfiler
use Rack::Deflater
run lambda { |_env| [200, { 'Content-Type' => 'text/html' }, [+'<html><body><h1>Hi</h1></body></html>']] }
end
end
describe 'with suppress_encoding true' do
before { Rack::MiniProfiler.config.suppress_encoding = true }
it 'should inject script and *not* compress' do
do_get
expect(last_response.body).to include('/mini-profiler-resources/includes.js')
expect(last_response.headers).not_to include('Content-Encoding')
end
include_examples 'should not affect a skipped requests'
end
describe 'with suppress_encoding false' do
before { Rack::MiniProfiler.config.suppress_encoding = false }
it 'should *not* inject script but should compress' do
do_get
expect(decompressed_response).not_to include('/mini-profiler-resources/includes.js')
expect(last_response.headers['Content-Encoding']).to eq('gzip')
end
include_examples 'should not affect a skipped requests'
end
end
describe 'with Rack::Deflater before Rack::MiniProfiler' do
def app
Rack::Builder.new do
use Rack::Deflater
use Rack::MiniProfiler
run lambda { |_env| [200, { 'Content-Type' => 'text/html' }, [+'<html><body><h1>Hi</h1></body></html>']] }
end
end
describe 'with suppress_encoding true' do
before { Rack::MiniProfiler.config.suppress_encoding = true }
it 'should inject script and compress' do
do_get
expect(decompressed_response).to include('/mini-profiler-resources/includes.js')
expect(last_response.headers['Content-Encoding']).to eq('gzip')
end
include_examples 'should not affect a skipped requests'
end
describe 'with suppress_encoding false' do
before { Rack::MiniProfiler.config.suppress_encoding = false }
it 'should inject script and compress' do
do_get
expect(decompressed_response).to include('/mini-profiler-resources/includes.js')
expect(last_response.headers['Content-Encoding']).to eq('gzip')
end
include_examples 'should not affect a skipped requests'
end
end
context 'when using a different profile parameter' do
def app
Rack::Builder.new do
use Rack::MiniProfiler
run lambda { |_env| [200, { 'Content-Type' => 'text/html' }, [+'<html><body><h1>Hi</h1></body></html>']] }
end
end
def with_profile_parameter(param)
old_param = Rack::MiniProfiler.config.profile_parameter
Rack::MiniProfiler.config.profile_parameter = param
yield
ensure
Rack::MiniProfiler.config.profile_parameter = old_param
end
it 'show help page' do
with_profile_parameter('profile') do
do_get(profile: 'help')
expect(last_response.body).to include('This is the help menu')
end
end
end
context "with CSP nonce" do
def app
Rack::Builder.new do
use Rack::MiniProfiler
run lambda { |env|
env["action_dispatch.content_security_policy_nonce"] = "railsnonce"
[200, { 'Content-Type' => 'text/html' }, [+'<html><body><h1>Hello world</h1></body></html>']]
}
end
end
it 'uses Rails value when available' do
do_get
expect(last_response.body).to include("nonce=\"railsnonce\"")
end
it 'uses configured string when available' do
Rack::MiniProfiler.config.content_security_policy_nonce = "configurednonce"
do_get
expect(last_response.body).to include("nonce=\"configurednonce\"")
end
it 'calls configured block when available' do
proc_arguments = nil
Rack::MiniProfiler.config.content_security_policy_nonce = Proc.new do |env, response_headers|
proc_arguments = [env, response_headers]
"dynamicnonce"
end
do_get
expect(last_response.body).to include("nonce=\"dynamicnonce\"")
(env, response_headers) = proc_arguments
expect(env["REQUEST_METHOD"]).to eq("GET")
expect(response_headers["Content-Type"]).to eq("text/html")
end
end
end