2011-03-24 07:45:47 +00:00
|
|
|
require 'sinatra/base'
|
|
|
|
require 'rack'
|
2017-01-13 22:53:54 +00:00
|
|
|
begin
|
|
|
|
require 'rack/test'
|
|
|
|
rescue LoadError
|
|
|
|
abort 'Add rack-test to your Gemfile to use this module!'
|
|
|
|
end
|
2011-03-24 07:45:47 +00:00
|
|
|
require 'forwardable'
|
|
|
|
|
|
|
|
module Sinatra
|
|
|
|
Base.set :environment, :test
|
|
|
|
|
2017-05-16 00:20:50 +00:00
|
|
|
# Helper methods to ease testing your Sinatra application. Partly extracted
|
2017-09-09 12:55:46 +00:00
|
|
|
# from Sinatra. Testing framework agnostic.
|
2011-03-24 07:45:47 +00:00
|
|
|
module TestHelpers
|
|
|
|
include Rack::Test::Methods
|
|
|
|
extend Forwardable
|
2011-03-25 23:13:26 +00:00
|
|
|
attr_accessor :settings
|
2011-03-24 07:45:47 +00:00
|
|
|
|
2017-09-09 12:55:46 +00:00
|
|
|
# @!group Instance Methods delegated to last_response
|
|
|
|
|
|
|
|
# @!method body
|
|
|
|
#
|
|
|
|
# Body of last_response
|
|
|
|
#
|
|
|
|
# @see http://www.rubydoc.info/github/rack/rack/master/Rack/Response#body-instance_method
|
|
|
|
# @return [String] body of the last response
|
|
|
|
|
|
|
|
# @!method headers
|
|
|
|
#
|
|
|
|
# Headers of last_response
|
|
|
|
#
|
|
|
|
# @return [Hash] hash of the last response
|
|
|
|
|
|
|
|
# @!method status
|
|
|
|
#
|
|
|
|
# HTTP status of last_response
|
|
|
|
#
|
|
|
|
# @return [Integer] HTTP status of the last response
|
|
|
|
|
|
|
|
# @!method errors
|
|
|
|
#
|
|
|
|
# Errors of last_response
|
|
|
|
#
|
|
|
|
# @return [Array] errors of the last response
|
2011-03-24 07:45:47 +00:00
|
|
|
def_delegators :last_response, :body, :headers, :status, :errors
|
2017-09-09 12:55:46 +00:00
|
|
|
# @!endgroup
|
|
|
|
|
|
|
|
# @!group Class Methods delegated to app
|
|
|
|
|
|
|
|
# @!method configure(*envs) {|_self| ... }
|
|
|
|
# @!scope class
|
|
|
|
# @yieldparam _self [Sinatra::Base] the object that the method was called on
|
|
|
|
#
|
|
|
|
# Set configuration options for Sinatra and/or the app. Allows scoping of
|
|
|
|
# settings for certain environments.
|
|
|
|
|
|
|
|
# @!method set(option, value = (not_set = true), ignore_setter = false, &block)
|
|
|
|
# @!scope class
|
|
|
|
# Sets an option to the given value. If the value is a proc, the proc will
|
|
|
|
# be called every time the option is accessed.
|
|
|
|
# @raise [ArgumentError]
|
|
|
|
|
|
|
|
# @!method enable(*opts)
|
|
|
|
# @!scope class
|
|
|
|
#
|
|
|
|
# Same as calling `set :option, true` for each of the given options.
|
|
|
|
|
|
|
|
# @!method disable(*opts)
|
|
|
|
# @!scope class
|
|
|
|
#
|
|
|
|
# Same as calling `set :option, false` for each of the given options.
|
|
|
|
|
|
|
|
# @!method use(middleware, *args, &block)
|
|
|
|
# @!scope class
|
|
|
|
# Use the specified Rack middleware
|
|
|
|
|
|
|
|
# @!method helpers(*extensions, &block)
|
|
|
|
# @!scope class
|
|
|
|
#
|
|
|
|
# Makes the methods defined in the block and in the Modules given in
|
|
|
|
# `extensions` available to the handlers and templates.
|
|
|
|
|
|
|
|
# @!method register(*extensions, &block)
|
|
|
|
# @!scope class
|
|
|
|
# Register an extension. Alternatively take a block from which an
|
|
|
|
# extension will be created and registered on the fly.
|
|
|
|
|
2011-03-24 07:45:47 +00:00
|
|
|
def_delegators :app, :configure, :set, :enable, :disable, :use, :helpers, :register
|
2017-09-09 12:55:46 +00:00
|
|
|
# @!endgroup
|
|
|
|
|
|
|
|
# @!group Instance Methods delegated to current_session
|
|
|
|
|
|
|
|
# @!method env_for(uri = "", opts = {})
|
|
|
|
#
|
|
|
|
# Return the Rack environment used for a request to `uri`.
|
|
|
|
#
|
|
|
|
# @return [Hash]
|
2011-03-24 07:45:47 +00:00
|
|
|
def_delegators :current_session, :env_for
|
2017-09-09 12:55:46 +00:00
|
|
|
# @!endgroup
|
|
|
|
|
|
|
|
# @!group Instance Methods delegated to rack_mock_session
|
|
|
|
# @!method cookie_jar
|
|
|
|
#
|
|
|
|
# Returns a {http://www.rubydoc.info/github/rack-test/rack-test/Rack/Test/CookieJar Rack::Test::CookieJar}.
|
|
|
|
#
|
|
|
|
# @return [Rack::Test::CookieJar]
|
2011-09-05 01:13:40 +00:00
|
|
|
def_delegators :rack_mock_session, :cookie_jar
|
2011-03-24 07:45:47 +00:00
|
|
|
|
2017-09-09 12:55:46 +00:00
|
|
|
# @!endgroup
|
|
|
|
|
|
|
|
# Instantiate and configure a mock Sinatra app.
|
|
|
|
#
|
|
|
|
# Takes a `base` app class, or defaults to Sinatra::Base, and instantiates
|
|
|
|
# an app instance. Any given code in `block` is `class_eval`'d on this new
|
|
|
|
# instance before the instance is returned.
|
|
|
|
#
|
|
|
|
# @param base [Sinatra::Base] App base class
|
|
|
|
#
|
|
|
|
# @return [Sinatra] Configured mocked app
|
2011-03-24 07:45:47 +00:00
|
|
|
def mock_app(base = Sinatra::Base, &block)
|
2011-03-25 23:13:26 +00:00
|
|
|
inner = nil
|
|
|
|
@app = Sinatra.new(base) do
|
|
|
|
inner = self
|
|
|
|
class_eval(&block)
|
|
|
|
end
|
|
|
|
@settings = inner
|
|
|
|
app
|
2011-03-24 07:45:47 +00:00
|
|
|
end
|
|
|
|
|
2017-09-09 12:55:46 +00:00
|
|
|
# Replaces the configured app.
|
|
|
|
#
|
|
|
|
# @param base [Sinatra::Base] a configured app
|
2011-03-24 07:45:47 +00:00
|
|
|
def app=(base)
|
|
|
|
@app = base
|
|
|
|
end
|
|
|
|
|
|
|
|
alias set_app app=
|
|
|
|
|
2017-09-09 12:55:46 +00:00
|
|
|
# Returns a Rack::Lint-wrapped Sinatra app.
|
|
|
|
#
|
|
|
|
# If no app has been configured, a new subclass of Sinatra::Base will be
|
|
|
|
# used and stored.
|
|
|
|
#
|
|
|
|
# (Rack::Lint validates your application and the requests and
|
|
|
|
# responses according to the Rack spec.)
|
|
|
|
#
|
|
|
|
# @return [Sinatra::Base]
|
2011-03-24 07:45:47 +00:00
|
|
|
def app
|
|
|
|
@app ||= Class.new Sinatra::Base
|
|
|
|
Rack::Lint.new @app
|
|
|
|
end
|
|
|
|
|
|
|
|
unless method_defined? :options
|
2017-09-09 12:55:46 +00:00
|
|
|
# Processes an OPTIONS request in the context of the current session.
|
|
|
|
#
|
|
|
|
# @param uri [String]
|
|
|
|
# @param params [Hash]
|
|
|
|
# @param env [Hash]
|
2011-03-24 07:45:47 +00:00
|
|
|
def options(uri, params = {}, env = {}, &block)
|
|
|
|
env = env_for(uri, env.merge(:method => "OPTIONS", :params => params))
|
|
|
|
current_session.send(:process_request, uri, env, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless method_defined? :patch
|
2017-09-09 12:55:46 +00:00
|
|
|
# Processes a PATCH request in the context of the current session.
|
|
|
|
#
|
|
|
|
# @param uri [String]
|
|
|
|
# @param params [Hash]
|
|
|
|
# @param env [Hash]
|
2011-03-24 07:45:47 +00:00
|
|
|
def patch(uri, params = {}, env = {}, &block)
|
|
|
|
env = env_for(uri, env.merge(:method => "PATCH", :params => params))
|
|
|
|
current_session.send(:process_request, uri, env, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-09 12:55:46 +00:00
|
|
|
# @return [Boolean]
|
2011-03-24 07:45:47 +00:00
|
|
|
def last_request?
|
|
|
|
last_request
|
|
|
|
true
|
|
|
|
rescue Rack::Test::Error
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-09-09 12:55:46 +00:00
|
|
|
# @raise [Rack::Test:Error] If sessions are not enabled for app
|
|
|
|
# @return [Hash] Session of last request, or the empty Hash
|
2011-03-24 07:45:47 +00:00
|
|
|
def session
|
|
|
|
return {} unless last_request?
|
2013-01-15 00:42:09 +00:00
|
|
|
raise Rack::Test::Error, "session not enabled for app" unless last_env["rack.session"] or app.session?
|
2011-03-24 07:45:47 +00:00
|
|
|
last_request.session
|
|
|
|
end
|
|
|
|
|
2017-09-09 12:55:46 +00:00
|
|
|
# @return The env of the last request
|
2011-03-24 07:45:47 +00:00
|
|
|
def last_env
|
|
|
|
last_request.env
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|