Skip to content

Commit

Permalink
Refactoring of the test helpers and add env and app as memoize
Browse files Browse the repository at this point in the history
  • Loading branch information
Annie Caron committed Jan 28, 2016
1 parent b5179f1 commit 7b3202a
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions lib/warden/test/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,43 +38,41 @@ def logout(*scopes)
# @api public
def warden
@warden ||= begin
env = env_with_params
setup_rack(success_app).call(env)
env['warden']
end
end

private

FAILURE_APP = lambda{|e|[401, {"Content-Type" => "text/plain"}, ["You Fail!"]] }
def env
@env ||= begin
request = Rack::MockRequest.env_for(
"/?#{Rack::Utils.build_query({})}",
{ 'HTTP_VERSION' => '1.1', 'REQUEST_METHOD' => 'GET' }
)
app.call(request)

def env_with_params(path = "/", params = {}, env = {})
method = params.delete(:method) || "GET"
env = { 'HTTP_VERSION' => '1.1', 'REQUEST_METHOD' => "#{method}" }.merge(env)
Rack::MockRequest.env_for("#{path}?#{Rack::Utils.build_query(params)}", env)
end

def setup_rack(app = nil, opts = {}, &block)
app ||= block if block_given?

opts[:failure_app] ||= failure_app
opts[:default_strategies] ||= [:password]
opts[:default_serializers] ||= [:session]
blk = opts[:configurator] || proc{}

Rack::Builder.new do
use opts[:session] || Warden::Test::Helpers::Session unless opts[:nil_session]
use Warden::Manager, opts, &blk
run app
request
end
end

def failure_app
Warden::Test::Helpers::FAILURE_APP
end

def success_app
lambda{|e| [200, {"Content-Type" => "text/plain"}, ["You Win"]]}
def app
@app ||= begin
opts = {
failure_app: lambda {
[401, { 'Content-Type' => 'text/plain' }, ['You Fail!']]
},
default_strategies: :password,
default_serializers: :session
}
Rack::Builder.new do
use Warden::Test::Helpers::Session
use Warden::Manager, opts, &proc {}
run lambda { |e|
[200, { 'Content-Type' => 'text/plain' }, ['You Win']]
}
end
end
end

class Session
Expand Down

0 comments on commit 7b3202a

Please sign in to comment.