Skip to content

Commit

Permalink
Add titles to middleware pages
Browse files Browse the repository at this point in the history
Right now the pages you visit that route to the middleware don't have
HTML titles or valid markup. This fixes that.
  • Loading branch information
gmcgibbon authored and nateberkopec committed Apr 5, 2023
1 parent f5abf2d commit c13e925
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 29 deletions.
72 changes: 43 additions & 29 deletions lib/mini_profiler/profiler.rb
Expand Up @@ -382,9 +382,13 @@ def call(env)
end
elsif path == '/rack-mini-profiler/requests'
blank_page_html = <<~HTML
<!DOCTYPE html>
<html>
<head></head>
<body></body>
<head>
<title>Rack::MiniProfiler Requests</title>
</head>
<body>
</body>
</html>
HTML

Expand Down Expand Up @@ -650,33 +654,39 @@ def make_link(postfix, env)

def help(client_settings, env)
headers = { 'Content-Type' => 'text/html' }
body = "<html><body>
<pre style='line-height: 30px; font-size: 16px;'>
This is the help menu of the <a href='#{Rack::MiniProfiler::SOURCE_CODE_URI}'>rack-mini-profiler</a> gem, append the following to your query string for more options:
#{make_link "help", env} : display this screen
#{make_link "env", env} : display the rack environment
#{make_link "skip", env} : skip mini profiler for this request
#{make_link "no-backtrace", env} #{"(*) " if client_settings.backtrace_none?}: don't collect stack traces from all the SQL executed (sticky, use pp=normal-backtrace to enable)
#{make_link "normal-backtrace", env} #{"(*) " if client_settings.backtrace_default?}: collect stack traces from all the SQL executed and filter normally
#{make_link "full-backtrace", env} #{"(*) " if client_settings.backtrace_full?}: enable full backtraces for SQL executed (use pp=normal-backtrace to disable)
#{make_link "disable", env} : disable profiling for this session
#{make_link "enable", env} : enable profiling for this session (if previously disabled)
#{make_link "profile-gc", env} : perform gc profiling on this request, analyzes ObjectSpace generated by request
#{make_link "profile-memory", env} : requires the memory_profiler gem, new location based report
#{make_link "flamegraph", env} : a graph representing sampled activity (requires the stackprof gem).
#{make_link "async-flamegraph", env} : store flamegraph data for this page and all its AJAX requests. Flamegraph links will be available in the mini-profiler UI (requires the stackprof gem).
#{make_link "flamegraph&flamegraph_sample_rate=1", env}: creates a flamegraph with the specified sample rate (in ms). Overrides value set in config
#{make_link "flamegraph&flamegraph_mode=cpu", env}: creates a flamegraph with the specified mode (one of cpu, wall, object, or custom). Overrides value set in config
#{make_link "flamegraph_embed", env} : a graph representing sampled activity (requires the stackprof gem), embedded resources for use on an intranet.
#{make_link "trace-exceptions", env} : will return all the spots where your application raises exceptions
#{make_link "analyze-memory", env} : will perform basic memory analysis of heap
</pre>
</body>
</html>
"
html = <<~HTML
<!DOCTYPE html>
<html>
<head>
<title>Rack::MiniProfiler Help</title>
</head>
<body>
<pre style='line-height: 30px; font-size: 16px'>
This is the help menu of the <a href='#{Rack::MiniProfiler::SOURCE_CODE_URI}'>rack-mini-profiler</a> gem, append the following to your query string for more options:
#{make_link "help", env} : display this screen
#{make_link "env", env} : display the rack environment
#{make_link "skip", env} : skip mini profiler for this request
#{make_link "no-backtrace", env} #{"(*) " if client_settings.backtrace_none?}: don't collect stack traces from all the SQL executed (sticky, use pp=normal-backtrace to enable)
#{make_link "normal-backtrace", env} #{"(*) " if client_settings.backtrace_default?}: collect stack traces from all the SQL executed and filter normally
#{make_link "full-backtrace", env} #{"(*) " if client_settings.backtrace_full?}: enable full backtraces for SQL executed (use pp=normal-backtrace to disable)
#{make_link "disable", env} : disable profiling for this session
#{make_link "enable", env} : enable profiling for this session (if previously disabled)
#{make_link "profile-gc", env} : perform gc profiling on this request, analyzes ObjectSpace generated by request
#{make_link "profile-memory", env} : requires the memory_profiler gem, new location based report
#{make_link "flamegraph", env} : a graph representing sampled activity (requires the stackprof gem).
#{make_link "async-flamegraph", env} : store flamegraph data for this page and all its AJAX requests. Flamegraph links will be available in the mini-profiler UI (requires the stackprof gem).
#{make_link "flamegraph&flamegraph_sample_rate=1", env}: creates a flamegraph with the specified sample rate (in ms). Overrides value set in config
#{make_link "flamegraph&flamegraph_mode=cpu", env}: creates a flamegraph with the specified mode (one of cpu, wall, object, or custom). Overrides value set in config
#{make_link "flamegraph_embed", env} : a graph representing sampled activity (requires the stackprof gem), embedded resources for use on an intranet.
#{make_link "trace-exceptions", env} : will return all the spots where your application raises exceptions
#{make_link "analyze-memory", env} : will perform basic memory analysis of heap
</pre>
</body>
</html>
HTML

[200, headers, [body]]
[200, headers, [html]]
end

def flamegraph(graph, path)
Expand All @@ -685,6 +695,7 @@ def flamegraph(graph, path)
<!DOCTYPE html>
<html>
<head>
<title>Rack::MiniProfiler Flamegraph</title>
<style>
body { margin: 0; height: 100vh; }
#speedscope-iframe { width: 100%; height: 100%; border: none; }
Expand Down Expand Up @@ -828,8 +839,11 @@ def handle_snapshots_request(env)
response = Rack::Response.new([], status, headers)

response.write <<~HTML
<!DOCTYPE html>
<html>
<head></head>
<head>
<title>Rack::MiniProfiler Snapshots</title>
</head>
<body class="mp-snapshots">
HTML
response.write(data_html)
Expand Down
46 changes: 46 additions & 0 deletions spec/integration/middleware_spec.rb
Expand Up @@ -16,6 +16,52 @@ 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 <script .*></script>\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_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')
Expand Down
2 changes: 2 additions & 0 deletions spec/integration/mini_profiler_spec.rb
Expand Up @@ -159,6 +159,7 @@ def app
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("<title>Rack::MiniProfiler Flamegraph</title>")
expect(last_response.body).to include("var graph = {")

# Should not store/return flamegraph for regular requests
Expand Down Expand Up @@ -605,6 +606,7 @@ def load_prof(response)

qs = Rack::Utils.build_query({ group_name: "DELETE /another/path/here" })
get "#{base_url}snapshots?#{qs}"
expect(last_response.body).to include("<title>Rack::MiniProfiler Snapshots</title>")
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])
Expand Down

0 comments on commit c13e925

Please sign in to comment.