Adds dynamic path evaluation options. Closes #585

This commit is contained in:
Michael Bleigh 2012-04-12 16:44:35 -05:00
parent ab3d87ce58
commit a682ba1f6d
2 changed files with 39 additions and 4 deletions

View File

@ -226,11 +226,19 @@ module OmniAuth
end
def on_request_path?
on_path?(request_path)
if options.request_path.respond_to?(:call)
options.request_path.call(env)
else
on_path?(request_path)
end
end
def on_callback_path?
on_path?(callback_path)
if options.callback_path.respond_to?(:call)
options.callback_path.call(env)
else
on_path?(callback_path)
end
end
def on_path?(path)
@ -350,12 +358,22 @@ module OmniAuth
options[:path_prefix] || OmniAuth.config.path_prefix
end
def custom_path(kind)
if options[kind].respond_to?(:call)
result = options[kind].call(env)
return nil unless result.is_a?(String)
result
else
options[kind]
end
end
def request_path
options[:request_path] || "#{path_prefix}/#{name}"
options[:request_path].is_a?(String) ? options[:request_path] : "#{path_prefix}/#{name}"
end
def callback_path
options[:callback_path] || "#{path_prefix}/#{name}/callback"
options[:callback_path].is_a?(String) ? options[:callback_path] : (custom_path(:request_path) || "#{path_prefix}/#{name}/callback")
end
def setup_path

View File

@ -346,6 +346,23 @@ describe OmniAuth::Strategy do
end
end
context 'dynamic paths' do
it 'should run the request phase if the custom request path evaluator is truthy' do
@options = {:request_path => lambda{|env| true}}
lambda{ strategy.call(make_env('/asoufibasfi')) }.should raise_error("Request Phase")
end
it 'should run the callback phase if the custom callback path evaluator is truthy' do
@options = {:callback_path => lambda{|env| true}}
lambda{ strategy.call(make_env('/asoufiasod')) }.should raise_error("Callback Phase")
end
it 'should provide a custom callback path if request_path evals to a string' do
strategy_instance = fresh_strategy.new(nil, :request_path => lambda{|env| "/auth/boo/callback/22" })
strategy_instance.callback_path.should == '/auth/boo/callback/22'
end
end
context 'custom paths' do
it 'should use a custom request_path if one is provided' do
@options = {:request_path => '/awesome'}