Skip to content

Commit

Permalink
Merge pull request #913 from 97jaz/origin-mock-request
Browse files Browse the repository at this point in the history
Updates mock request phase to use options.origin correctly
  • Loading branch information
tmilewski committed Oct 2, 2017
2 parents e9978e3 + 2536355 commit ef7f7c2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
10 changes: 6 additions & 4 deletions lib/omniauth/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,12 @@ def mock_request_call

session['omniauth.params'] = request.GET
OmniAuth.config.before_request_phase.call(env) if OmniAuth.config.before_request_phase
if request.params['origin']
session['omniauth.origin'] = request.params['origin']
elsif env['HTTP_REFERER'] && !env['HTTP_REFERER'].match(/#{request_path}$/)
session['omniauth.origin'] = env['HTTP_REFERER']
if options.origin_param
if request.params[options.origin_param]
session['omniauth.origin'] = request.params[options.origin_param]
elsif env['HTTP_REFERER'] && !env['HTTP_REFERER'].match(/#{request_path}$/)
session['omniauth.origin'] = env['HTTP_REFERER']
end
end

redirect(callback_url)
Expand Down
34 changes: 27 additions & 7 deletions spec/omniauth/strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -689,14 +689,34 @@ def make_env(path = '/auth/test', props = {})
expect(strategy.env['omniauth.error.type']).to eq(:invalid_credentials)
end

it 'sets omniauth.origin on the request phase' do
strategy.call(make_env('/auth/test', 'HTTP_REFERER' => 'http://example.com/origin'))
expect(strategy.env['rack.session']['omniauth.origin']).to eq('http://example.com/origin')
end
context 'omniauth.origin' do
context 'disabled' do
it 'does not set omniauth.origin' do
@options = {:origin_param => false}
strategy.call(make_env('/auth/test', 'HTTP_REFERER' => 'http://example.com/origin'))
expect(strategy.env['rack.session']['omniauth.origin']).to be_nil
end
end

context 'default flow' do
it 'sets omniauth.origin to the HTTP_REFERER on the request phase by default' do
strategy.call(make_env('/auth/test', 'HTTP_REFERER' => 'http://example.com/origin'))
expect(strategy.env['rack.session']['omniauth.origin']).to eq('http://example.com/origin')
end

it 'sets omniauth.origin from the params if provided' do
strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'origin=/foo'))
expect(strategy.env['rack.session']['omniauth.origin']).to eq('/foo')
it 'sets omniauth.origin from the params if provided' do
strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'origin=/foo'))
expect(strategy.env['rack.session']['omniauth.origin']).to eq('/foo')
end
end

context 'custom' do
it 'sets omniauth.origin from a custom param' do
@options = {:origin_param => 'return'}
strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'return=/foo'))
expect(strategy.env['rack.session']['omniauth.origin']).to eq('/foo')
end
end
end

it 'turns omniauth.origin into an env variable on the callback phase' do
Expand Down

0 comments on commit ef7f7c2

Please sign in to comment.