Skip to content

Commit

Permalink
Made tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
bhb committed Dec 12, 2011
1 parent 5603b83 commit c41c5c3
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 46 deletions.
43 changes: 24 additions & 19 deletions lib/rack/perftools_profiler/action.rb
@@ -1,5 +1,5 @@
module Rack::PerftoolsProfiler

class Action

def initialize(env, profiler, middleware)
Expand All @@ -9,37 +9,42 @@ def initialize(env, profiler, middleware)
@profiler = profiler
@middleware = middleware
end

def act
# do nothing
end

def self.for_env(env, profiler, middleware)
request = Rack::Request.new(env)
klass =
if profiler.should_check_password? && ! request.GET.key?('profile')
CallAppDirectly
elsif !profiler.password_valid?(request.GET['profile'])
ReturnPasswordError
password = request.GET['profile']
klass =
case request.path_info
when %r{/__start__$}
password_protected(StartProfiling, profiler, password)
when %r{/__stop__$}
password_protected(StopProfiling, profiler, password)
when %r{/__data__$}
password_protected(ReturnData, profiler, password)
else
case request.path_info
when %r{/__start__$}
StartProfiling
when %r{/__stop__$}
StopProfiling
when %r{/__data__$}
ReturnData
if ProfileOnce.has_special_param?(request)
password_protected(ProfileOnce, profiler, password)
else
if ProfileOnce.has_special_param?(request)
ProfileOnce
else
CallAppDirectly
end
CallAppDirectly
end
end
klass.new(env, profiler, middleware)
end

private

def self.password_protected(klass, profiler, password)
if profiler.accepts?(password)
klass
else
ReturnPasswordError
end
end

end

end
20 changes: 14 additions & 6 deletions lib/rack/perftools_profiler/profiler.rb
Expand Up @@ -34,7 +34,7 @@ def initialize(app, options)
@mode = (options.delete(:mode) { DEFAULT_MODE }).to_sym
@bundler = options.delete(:bundler) { false }
@gemfile_dir = options.delete(:gemfile_dir) { DEFAULT_GEMFILE_DIR }
@password = options.delete(:password) { nil }
@password = options.delete(:password) { :not_set }
@mode_for_request = nil
ProfileDataAction.check_printer(@printer)
ensure_mode_is_valid(@mode)
Expand All @@ -43,7 +43,7 @@ def initialize(app, options)
require 'perftools'
raise ProfilerArgumentError, "Invalid option(s): #{options.keys.join(' ')}" unless options.empty?
end

def profile(mode = nil)
start(mode)
yield
Expand All @@ -55,20 +55,28 @@ def self.clear_data
::File.delete(PROFILING_DATA_FILE) if ::File.exists?(PROFILING_DATA_FILE)
end

def accepts?(password)
if password_protected?
password_valid?(password)
else
true
end
end

def password_valid?(password)
@password.nil? || password == @password
end

def should_check_password?
! @password.nil?
def password_protected?
@password != :not_set
end

def start(mode = nil)
ensure_mode_is_changeable(mode) if mode
PerfTools::CpuProfiler.stop
if (mode)
@mode_for_request = mode
end
end
unset_env_vars
set_env_vars
PerfTools::CpuProfiler.start(PROFILING_DATA_FILE)
Expand Down Expand Up @@ -150,7 +158,7 @@ def unset_env_vars
ENV.delete('CPUPROFILE_OBJECTS')
ENV.delete('CPUPROFILE_METHODS')
end

def profiling=(value)
pstore_transaction(false) do |store|
store[:profiling?] = value
Expand Down
75 changes: 54 additions & 21 deletions test/multiple_request_profiling_test.rb
Expand Up @@ -367,64 +367,97 @@ def profile_requests(profiled_app, requests, options = {})

context "when a profile password is required" do

should_eventually "allow the app to be called directly" do
should "call app directly on normal calls if password not provided" do
profiled_app = Rack::PerftoolsProfiler.with_profiling_off(@app, :password => 'secret')
profiled_app.call(@start_env)
status, headers, body = profiled_app.call(@root_request_env)
assert_equal 200, status
assert_equal 'text/plain', headers['Content-Type']
assert_equal 'Oh hai der', RackResponseBody.new(body).to_s
end

should "call __start__ if password is provided" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :password => "secret")
actual_password = "secret"
modified_start_env = Rack::MockRequest.env_for('/__start__', :params => "profile=#{actual_password}")
status, _, body = profiled_app.call(modified_start_env)
start_env = Rack::MockRequest.env_for('/__start__', :params => "profile=#{actual_password}")
status, _, body = profiled_app.call(start_env)
assert_equal 200, status
assert_match(/Profiling is now enabled/, RackResponseBody.new(body).to_s)
end

should "call __stop__ if password is provided" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :password => "secret")
actual_password = "secret"
modified_stop_env = Rack::MockRequest.env_for('/__stop__', :params => "profile=#{actual_password}")
status, _, body = profiled_app.call(modified_stop_env)
stop_env = Rack::MockRequest.env_for('/__stop__', :params => "profile=#{actual_password}")
status, _, body = profiled_app.call(stop_env)
assert_equal 200, status
assert_match(/Profiling is now disabled/, RackResponseBody.new(body).to_s)
end

should_eventually "call __data__ if password is provided" do
should "call __data__ if password is provided" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :password => "secret")
actual_password = "secret"
modified_start_env = Rack::MockRequest.env_for('/__start__', :params => "profile=#{actual_password}")
modified_stop_env = Rack::MockRequest.env_for('/__stop__', :params => "profile=#{actual_password}")
modified_data_env = Rack::MockRequest.env_for('/__data__', :params => "profile=#{actual_password}")
profiled_app.call(modified_start_env)
profiled_app.call(Rack::MockRequest.env_for("/"))
profiled_app.call(modified_stop_env)
status, _, body = profiled_app.call(modified_data_env)
start_env = Rack::MockRequest.env_for('/__start__', :params => "profile=#{actual_password}")
stop_env = Rack::MockRequest.env_for('/__stop__', :params => "profile=#{actual_password}")
data_env = Rack::MockRequest.env_for('/__data__', :params => "profile=#{actual_password}")
profiled_app.call(start_env)
profiled_app.call(@root_request_env)
profiled_app.call(stop_env)
status, _, body = profiled_app.call(data_env)
assert_equal 200, status
assert_match(/Profiling is now disabled/, RackResponseBody.new(body).to_s)
assert_match(/Total: \d+ samples/, RackResponseBody.new(body).to_s)
end

should "error on __start__ if password does not match" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :password => "secret")
actual_password = "foobar"
modified_start_env = Rack::MockRequest.env_for('/__start__', :params => "profile=#{actual_password}")
status, _, body = profiled_app.call(modified_start_env)
start_env = Rack::MockRequest.env_for('/__start__', :params => "profile=#{actual_password}")
status, _, body = profiled_app.call(start_env)
assert_equal 401, status
assert_match(/Profiling is password-protected/, RackResponseBody.new(body).to_s)
end

should "error on __stop__ if password does not match" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :password => "secret")
actual_password = "foobar"
modified_stop_env = Rack::MockRequest.env_for('/__stop__', :params => "profile={actual_password}")
status, _, body = profiled_app.call(modified_stop_env)
stop_env = Rack::MockRequest.env_for('/__stop__', :params => "profile={actual_password}")
status, _, body = profiled_app.call(stop_env)
assert_equal 401, status
assert_match(/Profiling is password-protected/, RackResponseBody.new(body).to_s)
end

should "error on __data__ if password does not match" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :password => "secret")
actual_password = "foobar"
modified_data_env = Rack::MockRequest.env_for('/__data__', :params => "profile={actual_password}")
status, _, body = profiled_app.call(modified_data_env)
data_env = Rack::MockRequest.env_for('/__data__', :params => "profile={actual_password}")
status, _, body = profiled_app.call(data_env)
assert_equal 401, status
assert_match(/Profiling is password-protected/, RackResponseBody.new(body).to_s)
end

should "error on __start__ if password is missing" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :password => "secret")
actual_password = "foobar"
start_env = Rack::MockRequest.env_for('/__start__')
status, _, body = profiled_app.call(start_env)
assert_equal 401, status
assert_match(/Profiling is password-protected/, RackResponseBody.new(body).to_s)
end

should "error on __stop__ if password is missing" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :password => "secret")
actual_password = "foobar"
stop_env = Rack::MockRequest.env_for('/__stop__')
status, _, body = profiled_app.call(stop_env)
assert_equal 401, status
assert_match(/Profiling is password-protected/, RackResponseBody.new(body).to_s)
end

should "error on __data__ if password is missing" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :password => "secret")
actual_password = "foobar"
data_env = Rack::MockRequest.env_for('/__data__')
status, _, body = profiled_app.call(data_env)
assert_equal 401, status
assert_match(/Profiling is password-protected/, RackResponseBody.new(body).to_s)
end
Expand Down

0 comments on commit c41c5c3

Please sign in to comment.