/
mini_profiler_spec.rb
635 lines (539 loc) · 21.7 KB
/
mini_profiler_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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# frozen_string_literal: true
require 'rack/test'
describe Rack::MiniProfiler do
include Rack::Test::Methods
def app
@app ||= Rack::Builder.new {
use Rack::MiniProfiler
map '/path2/a' do
run lambda { |env| [200, { 'Content-Type' => 'text/html' }, +'<h1>path1</h1>'] }
end
map '/path1/a' do
run lambda { |env| [200, { 'Content-Type' => 'text/html' }, +'<h1>path2</h1>'] }
end
map '/cached-resource' do
run lambda { |env|
ims = env['HTTP_IF_MODIFIED_SINCE'] || ""
if ims.size > 0
[304, { 'Content-Type' => 'application/json' }, '']
else
[200, { 'Content-Type' => 'application/json', 'Cache-Control' => 'original-cache-control' }, '{"name": "Ryan"}']
end
}
end
map '/post' do
run lambda { |env| [302, { 'Content-Type' => 'text/html' }, +'<h1>POST</h1>'] }
end
map '/html' do
run lambda { |env| [200, { 'Content-Type' => 'text/html' }, +"<html><BODY><h1>Hi</h1></BODY>\n \t</html>"] }
end
map '/explicitly-allowed-html' do
run lambda { |env|
Rack::MiniProfiler.authorize_request
[200, { 'Content-Type' => 'text/html' }, +"<html><BODY><h1>Hi</h1></BODY>\n \t</html>"]
}
end
map '/implicitbody' do
run lambda { |env| [200, { 'Content-Type' => 'text/html' }, +"<html><h1>Hi</h1></html>"] }
end
map '/implicitbodyhtml' do
run lambda { |env| [200, { 'Content-Type' => 'text/html' }, +"<h1>Hi</h1>"] }
end
map '/db' do
run lambda { |env|
::Rack::MiniProfiler.record_sql("I want to be, in a db", 10)
[200, { 'Content-Type' => 'text/html' }, +'<h1>Hi+db</h1>']
}
end
map '/3ms' do
run lambda { |env|
sleep(0.003)
[200, { 'Content-Type' => 'text/html' }, +'<h1>Hi</h1>']
}
end
map '/explicitly-allowed' do
run lambda { |env|
Rack::MiniProfiler.authorize_request
[200, { 'Content-Type' => 'text/html' }, +'<h1>path1</h1>']
}
end
map '/rails_engine' do
run lambda { |env|
env['SCRIPT_NAME'] = '/rails_engine' # Rails engines do that
[200, { 'Content-Type' => 'text/html' }, +'<html><h1>Hi</h1></html>']
}
end
map '/under_passenger' do
run lambda { |env|
[200, { 'Content-Type' => 'text/html' }, +'<html><h1>and I ride and I ride</h1></html>']
}
end
map '/create' do
run lambda { |env|
[201, { 'Content-Type' => 'text/html' }, +'<html><h1>success</h1></html>']
}
end
map '/notallowed' do
run lambda { |env|
[403, { 'Content-Type' => 'text/html' }, +'<html><h1>you are not allowed here</h1></html>']
}
end
map '/whoopsie-daisy' do
run lambda { |env|
[500, { 'Content-Type' => 'text/html' }, +'<html><h1>whoopsie daisy</h1></html>']
}
end
map '/test-snapshots-custom-fields' do
run lambda { |env|
qp = Rack::Utils.parse_nested_query(env['QUERY_STRING'])
qp.each { |k, v| Rack::MiniProfiler.add_snapshot_custom_field(k, v) }
[200, { 'Content-Type' => 'text/html' }, +'<html><h1>custom fields</h1></html>']
}
end
}.to_app
end
before do
Rack::MiniProfiler.reset_config
end
describe 'with a valid request' do
before do
get '/html'
end
it 'returns 200' do
expect(last_response).to be_ok
end
it 'has the X-MiniProfiler-Ids header' do
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(true)
end
it 'has only one X-MiniProfiler-Ids header' do
h = last_response.headers['X-MiniProfiler-Ids']
ids = h.split(",")
expect(ids.count).to eq(1)
end
it 'has the JS in the body' do
expect(last_response.body.include?('/mini-profiler-resources/includes.js')).to be(true)
end
it 'has a functioning share link' do
h = last_response.headers['X-MiniProfiler-Ids']
id = h.split(",")[0]
get "/mini-profiler-resources/results?id=#{id}"
expect(last_response).to be_ok
end
it 'avoids xss attacks' do
h = last_response.headers['X-MiniProfiler-Ids']
_id = h.split(",")[0]
get "/mini-profiler-resources/results?id=%22%3E%3Cqss%3E"
expect(last_response).not_to be_ok
expect(last_response.body).not_to match(/<qss>/)
expect(last_response.body).to match(/<qss>/)
end
end
it 'works with async-flamegraph' do
pid = fork do # Avoid polluting main process with stackprof
require 'stackprof'
# Should store flamegraph for ?pp=async-flamegraph
get '/html?pp=async-flamegraph'
expect(last_response).to be_ok
id = last_response.headers['X-MiniProfiler-Ids'].split(",")[0]
get "/mini-profiler-resources/flamegraph?id=#{id}"
expect(last_response).to be_ok
expect(last_response.body).to include("var graph = {")
# Should store flamegraph based on REFERER
get '/html', nil, { "HTTP_REFERER" => "/origin?pp=async-flamegraph" }
expect(last_response).to be_ok
id = last_response.headers['X-MiniProfiler-Ids'].split(",")[0]
get "/mini-profiler-resources/flamegraph?id=#{id}"
expect(last_response).to be_ok
expect(last_response.body).to include("var graph = {")
# Should not store/return flamegraph for regular requests
get '/html'
expect(last_response).to be_ok
id = last_response.headers['X-MiniProfiler-Ids'].split(",")[0]
get "/mini-profiler-resources/flamegraph?id=#{id}"
expect(last_response.status).to eq(404)
end
Process.wait(pid)
expect($?.exitstatus).to eq(0)
end
describe 'with an implicit body tag' do
before do
get '/implicitbody'
end
it 'has the JS in the body' do
expect(last_response.body.include?('/mini-profiler-resources/includes.js')).to be(true)
end
end
describe 'with implicit body and html tags' do
before do
get '/implicitbodyhtml'
end
it 'does not include the JS in the body' do
expect(last_response.body.include?('/mini-profiler-resources/includes.js')).to be(false)
end
end
describe 'with a SCRIPT_NAME' do
before do
get '/html', nil, 'SCRIPT_NAME' => '/test'
end
it 'has the JS in the body with the correct path' do
expect(last_response.body.include?('/test/mini-profiler-resources/includes.js')).to be(true)
end
end
describe 'within a rails engine' do
before do
get '/rails_engine'
end
it 'include the correct JS in the body' do
expect(last_response.body.include?('/rails_engine/mini-profiler-resources/includes.js')).not_to be(true)
expect(last_response.body.include?('src="/mini-profiler-resources/includes.js')).to be(true)
end
end
describe 'under passenger' do
before do
ENV['PASSENGER_BASE_URI'] = '/under_passenger'
end
after do
ENV['PASSENGER_BASE_URI'] = nil
end
it 'include the correct JS in the body' do
get '/under_passenger'
expect(last_response.body.include?('src="/under_passenger/mini-profiler-resources/includes.js')).to be(true)
end
end
describe 'configuration' do
it "should remove caching headers by default" do
get '/cached-resource'
expect(last_response.headers['X-MiniProfiler-Original-Cache-Control']).to eq('original-cache-control')
expect(last_response.headers['Cache-Control']).to include('no-store')
end
it "should not store the original cache header if not set" do
get '/html'
expect(last_response.headers).to_not have_key('X-MiniProfiler-Original-Cache-Control')
end
it "should strip if-modified-since on the way in" do
old_time = 1409326086
get '/cached-resource', {}, 'HTTP_IF_MODIFIED_SINCE' => old_time
expect(last_response.status).to equal(200)
end
describe 'with hotwire turbo drive support enabled' do
before do
Rack::MiniProfiler.config.enable_hotwire_turbo_drive_support = true
end
it 'should define data-turbo-permanent as true' do
get '/html'
expect(last_response.body).to include('data-turbo-permanent="true"')
end
end
describe 'with caching re-enabled' do
before :each do
Rack::MiniProfiler.config.disable_caching = false
end
it "should strip if-modified-since on the way in" do
old_time = 1409326086
get '/cached-resource', {}, 'HTTP_IF_MODIFIED_SINCE' => old_time
expect(last_response.status).to equal(304)
end
it "should be able to re-enable caching" do
get '/cached-resource'
expect(last_response.headers['X-MiniProfiler-Original-Cache-Control']).to eq('original-cache-control')
expect(last_response.headers['Cache-Control']).not_to include('no-store')
end
end
it "doesn't add MiniProfiler if the callback fails" do
Rack::MiniProfiler.config.pre_authorize_cb = lambda { |env| false }
get '/html'
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(false)
end
it "skips paths listed" do
Rack::MiniProfiler.config.skip_paths = ['/path/', '/path2/']
get '/path2/a'
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(false)
get '/path1/a'
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(true)
end
it "skip_paths can contain regular expressions" do
Rack::MiniProfiler.config.skip_paths = [/path[^1]/]
get '/path2/a'
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(false)
get '/path1/a'
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(true)
end
it 'disables default functionality' do
Rack::MiniProfiler.config.enabled = false
get '/html'
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(false)
end
end
def load_prof(response)
id = response.headers['X-MiniProfiler-Ids']
id = id.split(",")[0]
Rack::MiniProfiler.config.storage_instance.load(id)
end
describe 'special options' do
it "omits db backtrace if requested" do
get '/db?pp=no-backtrace'
prof = load_prof(last_response)
stack = prof[:root][:sql_timings][0][:stack_trace_snippet]
expect(stack).to be_nil
end
it 'disables functionality if requested' do
get '/html?pp=disable'
expect(last_response.body).not_to include('/mini-profiler-resources/includes.js')
end
context 'when disabled' do
before(:each) do
get '/html?pp=disable'
get '/html'
expect(last_response.body).not_to include('/mini-profiler-resources/includes.js')
end
it 're-enables functionality if requested' do
get '/html?pp=enable'
expect(last_response.body).to include('/mini-profiler-resources/includes.js')
end
it "does not re-enable functionality if not explicitly allowed" do
Rack::MiniProfiler.config.authorization_mode = :allow_authorized
get '/html?pp=enable'
get '/html?pp=enable'
expect(last_response.body).not_to include('/mini-profiler-resources/includes.js')
end
it "re-enabled functionality if explicitly allowed" do
Rack::MiniProfiler.config.authorization_mode = :allow_authorized
get '/explicitly-allowed-html?pp=enable'
get '/explicitly-allowed-html?pp=enable'
expect(last_response.body).to include('/mini-profiler-resources/includes.js')
end
end
describe 'env dump' do
it 'works when advanced tools are enabled' do
Rack::MiniProfiler.config.enable_advanced_debugging_tools = true
get '/html?pp=env'
expect(last_response.body).to include('QUERY_STRING')
expect(last_response.body).to include('CONTENT_LENGTH')
end
end
end
describe 'POST followed by GET' do
it "should end up with 2 ids" do
post '/post'
get '/html'
ids = last_response.headers['X-MiniProfiler-Ids']
expect(ids.split(",").length).to eq(2)
end
end
describe 'authorization mode :allow_authorized' do
before do
Rack::MiniProfiler.config.authorization_mode = :allow_authorized
end
it "should ban requests that are not explicitly allowed" do
get '/html'
expect(last_response.headers['X-MiniProfiler-Ids']).to be_nil
end
it "should allow requests that are explicitly allowed" do
get '/explicitly-allowed'
# second time will ensure cookie is set
# first time around there is no cookie, so no profiling
get '/explicitly-allowed'
expect(last_response.headers['X-MiniProfiler-Ids']).not_to be_nil
end
end
describe 'gc profiler' do
it "should return a report" do
get '/html?pp=profile-gc'
expect(last_response.header['Content-Type']).to eq('text/plain')
end
end
describe 'error handling when storage_instance fails to save' do
it "should recover gracefully" do
Rack::MiniProfiler.config.pre_authorize_cb = lambda { |env| true }
allow_any_instance_of(Rack::MiniProfiler::MemoryStore).to receive(:save) { raise "This error" }
expect(Rack::MiniProfiler.config.storage_failure).to receive(:call)
get '/html'
end
end
describe 'when profiler is disabled by default' do
before(:each) do
Rack::MiniProfiler.config.enabled = false
get '/html'
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(false)
end
it 'functionality can be re-enabled' do
get '/html?pp=enable'
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(true)
get '/html'
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(true)
end
end
context 'snapshots sampling' do
before(:each) do
Rack::MiniProfiler.config.tap do |c|
c.authorization_mode = :allow_authorized
c.snapshot_every_n_requests = 1
end
end
after(:each) do
Rack::MiniProfiler.reset_config
end
it 'does not take snapshots of paths in skip_paths' do
config = Rack::MiniProfiler.config
config.skip_paths = ['/path2/a']
get '/path2/a'
expect(Rack::MiniProfiler.config.storage_instance.snapshot_groups_overview).to eq([])
end
it 'takes snapshots of requests that fail the pre_authorize_cb check' do
Rack::MiniProfiler.config.pre_authorize_cb = lambda { |env| false }
get '/path2/a'
data = Rack::MiniProfiler.config.storage_instance.snapshot_groups_overview
expect(data.size).to eq(1)
expect(last_response.body.include?('/mini-profiler-resources/includes.js')).to be(false)
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(false)
expect(data[0][:name]).to eq("GET /path2/a")
end
it 'takes snapshots of requests that do not have valid token in cookie' do
Rack::MiniProfiler.config.pre_authorize_cb = lambda { |env| true }
get '/path2/a'
data = Rack::MiniProfiler.config.storage_instance.snapshot_groups_overview
expect(last_response.body.include?('/mini-profiler-resources/includes.js')).to be(false)
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(false)
expect(data.size).to eq(1)
end
it 'does not take snapshots of requests that have valid token in cookie' do
Rack::MiniProfiler.config.pre_authorize_cb = lambda { |env| true }
get '/explicitly-allowed-html'
cookies = last_response.set_cookie_header
get '/path2/a', nil, { cookie: cookies } # no snapshot here
data = Rack::MiniProfiler.config.storage_instance.snapshot_groups_overview
expect(data.size).to eq(1)
expect(data[0][:name]).to eq("GET /explicitly-allowed-html")
end
it 'respects snapshot_every_n_requests config' do
Rack::MiniProfiler.config.snapshot_every_n_requests = 2
get '/path2/a'
get '/path2/a'
get '/path2/a'
get '/path2/a'
store = Rack::MiniProfiler.config.storage_instance
groups = store.snapshot_groups_overview
expect(groups.size).to eq(1)
group_name = "GET /path2/a"
expect(groups[0][:name]).to eq(group_name)
expect(store.find_snapshots_group(group_name).size).to eq(2)
end
it 'does not take snapshots for non-2xx requests' do
Rack::MiniProfiler.config.snapshot_every_n_requests = 1
get '/path/that/doesnot/exist' # 404
get '/post' # 302
get '/notallowed' # 403
get '/whoopsie-daisy' # 500
post '/create' # 201
groups = Rack::MiniProfiler.config.storage_instance.snapshot_groups_overview
expect(groups.size).to eq(1)
expect(groups[0][:name]).to eq("POST /create")
end
it 'custom fields are reset between requests' do
get '/test-snapshots-custom-fields?field1=value1&field2=value2'
store = Rack::MiniProfiler.config.storage_instance
group_name = "GET /test-snapshots-custom-fields"
id1 = store.find_snapshots_group(group_name).first[:id]
snapshot1 = store.load_snapshot(id1)
get '/test-snapshots-custom-fields?field3=value3&field4=value4'
id2 = store.find_snapshots_group(group_name).find { |s| s[:id] != id1 }[:id]
snapshot2 = store.load_snapshot(id2)
expect(snapshot1[:custom_fields]).to eq(
{ "field1" => "value1", "field2" => "value2" }
)
expect(snapshot2[:custom_fields]).to eq(
{ "field3" => "value3", "field4" => "value4" }
)
end
end
context 'snapshots page' do
it 'allows only authorized users to access it' do
base_url = Rack::MiniProfiler.config.base_url_path
Rack::MiniProfiler.config.authorization_mode = :allow_authorized
get "#{base_url}snapshots"
expect(last_response.status).to eq(404)
expect(last_response.body).to eq("Not Found: /mini-profiler-resources/snapshots")
get '/explicitly-allowed-html'
cookies = last_response.set_cookie_header
get "#{base_url}snapshots", nil, { cookie: cookies }
expect(last_response.status).to eq(200)
expect(last_response.body).to include('id="snapshots-data"')
end
it 'without group_name param it sends groups list in response' do
base_url = Rack::MiniProfiler.config.base_url_path
# initial request to initialize storage_instance
get "#{base_url}snapshots"
store = Rack::MiniProfiler.config.storage_instance
struct = Rack::MiniProfiler::TimerStruct::Page.new({
'PATH_INFO' => '/some/path/here',
'REQUEST_METHOD' => 'POST'
})
struct[:root].record_time(1342.314242)
store.push_snapshot(struct, Rack::MiniProfiler.config)
get "#{base_url}snapshots"
expect(last_response.body).to include('id="snapshots-data"')
expect(last_response.body).to include("/some/path/here")
expect(last_response.body).to include("POST /some/path/here")
expect(last_response.body).to include("1342.314242")
end
it 'with group_name params it sends a list of snapshots of the given group' do
base_url = Rack::MiniProfiler.config.base_url_path
# initial request to initialize storage_instance
get "#{base_url}snapshots"
store = Rack::MiniProfiler.config.storage_instance
struct1 = Rack::MiniProfiler::TimerStruct::Page.new({
'PATH_INFO' => '/some/path/here',
'REQUEST_METHOD' => 'POST'
})
struct1[:root].record_time(1342.314242)
struct2 = Rack::MiniProfiler::TimerStruct::Page.new({
'PATH_INFO' => '/another/path/here',
'REQUEST_METHOD' => 'DELETE'
})
struct2[:root].record_time(8342.08342)
struct3 = Rack::MiniProfiler::TimerStruct::Page.new({
'PATH_INFO' => '/another/path/here',
'REQUEST_METHOD' => 'DELETE'
})
struct3[:root].record_time(3084.803185)
store.push_snapshot(struct1, Rack::MiniProfiler.config)
store.push_snapshot(struct2, Rack::MiniProfiler.config)
store.push_snapshot(struct3, Rack::MiniProfiler.config)
qs = Rack::Utils.build_query({ group_name: "DELETE /another/path/here" })
get "#{base_url}snapshots?#{qs}"
expect(last_response.body).to include('id="snapshots-data"')
expect(last_response.body).to include(struct2[:id])
expect(last_response.body).to include(struct3[:id])
expect(last_response.body).to include("DELETE /another/path/here")
expect(last_response.body).to include("3084.803185")
expect(last_response.body).to include("8342.08342")
expect(last_response.body).not_to include(struct1[:id])
expect(last_response.body).not_to include("1342.314242")
end
it 'individual snapshot can be viewed' do
base_url = Rack::MiniProfiler.config.base_url_path
# initial request to initialize storage_instance
get "#{base_url}snapshots"
store = Rack::MiniProfiler.config.storage_instance
struct = Rack::MiniProfiler::TimerStruct::Page.new({
'PATH_INFO' => '/some/path/here',
'REQUEST_METHOD' => 'POST'
})
struct[:root].record_time(1342.314242)
store.push_snapshot(struct, Rack::MiniProfiler.config)
get "#{base_url}results?id=#{struct[:id]}&snapshot=true"
expect(last_response.status).to eq(200)
expect(last_response.body).to include(struct[:id])
expect(last_response.body).to include("1342.314242")
end
it 'when snapshot is not found a 404 response is given' do
base_url = Rack::MiniProfiler.config.base_url_path
get "#{base_url}results?id=nonsenseidhere&snapshot=true"
expect(last_response.status).to eq(404)
expect(last_response.body).to eq("Snapshot with id 'nonsenseidhere' not found")
get "#{base_url}results?id=%22%3E%3Cqss%3E&snapshot=true"
expect(last_response.status).to eq(404)
expect(last_response.body).to eq("Snapshot with id '"><qss>' not found"), "id should be escaped to prevent XSS"
end
end
end