Permalink
Browse files

Merge pull request #162 from godfat/fix-catching-on-request

Make it able to throw in Warden::Manager.on_request
  • Loading branch information...
2 parents dead31f + 207de2a commit a4b197e0b28e7b576b0745b0f6aeaed8dbb774a4 @jsmestad jsmestad committed Nov 28, 2017
Showing with 24 additions and 1 deletion.
  1. +1 −0 lib/warden/manager.rb
  2. +5 −0 lib/warden/proxy.rb
  3. +1 −1 lib/warden/test/mock.rb
  4. +17 −0 spec/warden/hooks_spec.rb
View
@@ -32,6 +32,7 @@ def call(env) # :nodoc:
env['warden'] = Proxy.new(env, self)
result = catch(:warden) do
+ env['warden'].on_request
@app.call(env)
end
View
@@ -29,6 +29,11 @@ def initialize(env, manager) #:nodoc:
@env, @users, @winning_strategies, @locked = env, {}, {}, false
@manager, @config = manager, manager.config.dup
@strategies = Hash.new { |h,k| h[k] = {} }
+ end
+
+ # Run the on_request callbacks
+ # :api: private
+ def on_request
manager._run_callbacks(:on_request, self)
end
View
@@ -37,7 +37,7 @@ def env
def app
@app ||= begin
opts = {
- failure_app: lambda {
+ failure_app: lambda { |_e|
[401, { 'Content-Type' => 'text/plain' }, ['You Fail!']]
},
default_strategies: :password,
View
@@ -368,5 +368,22 @@
setup_rack(app).call(env)
expect(env['warden.spec.order']).to eq([1,2,3])
end
+
+ it "should have the proxy on env in on_request" do
+ warden = nil
+ RAM.on_request{|proxy| warden = proxy.env['warden']}
+ app = lambda{|e| valid_response}
+ env = env_with_params
+ setup_rack(app).call(env)
+ expect(warden).to eq(env['warden'])
+ end
+
+ it "should be able to throw in on_request" do
+ RAM.on_request{|proxy| throw :warden}
+ app = lambda{|e| valid_response}
+ env = env_with_params
+ result = setup_rack(app).call(env)
+ expect(result.first).to eq(401)
+ end
end
end

0 comments on commit a4b197e

Please sign in to comment.