2009-04-25 12:17:11 -04:00
|
|
|
ENV['RACK_ENV'] = 'test'
|
2010-09-19 11:57:48 -04:00
|
|
|
Encoding.default_external = "UTF-8" if defined? Encoding
|
2009-04-25 12:17:11 -04:00
|
|
|
|
2011-02-21 05:54:26 -05:00
|
|
|
RUBY_ENGINE = 'ruby' unless defined? RUBY_ENGINE
|
|
|
|
|
2009-01-20 15:24:36 -05:00
|
|
|
begin
|
|
|
|
require 'rack'
|
|
|
|
rescue LoadError
|
|
|
|
require 'rubygems'
|
|
|
|
require 'rack'
|
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
testdir = File.dirname(__FILE__)
|
|
|
|
$LOAD_PATH.unshift testdir unless $LOAD_PATH.include?(testdir)
|
|
|
|
|
2009-01-20 15:24:36 -05:00
|
|
|
libdir = File.dirname(File.dirname(__FILE__)) + '/lib'
|
|
|
|
$LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
require 'contest'
|
2009-03-28 11:28:18 -04:00
|
|
|
require 'rack/test'
|
|
|
|
require 'sinatra/base'
|
2009-02-14 14:15:50 -05:00
|
|
|
|
|
|
|
class Sinatra::Base
|
|
|
|
# Allow assertions in request context
|
|
|
|
include Test::Unit::Assertions
|
|
|
|
end
|
|
|
|
|
2011-03-13 04:18:31 -04:00
|
|
|
class Rack::Builder
|
|
|
|
def include?(middleware)
|
|
|
|
@ins.any? { |m| p m ; middleware === m }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
Sinatra::Base.set :environment, :test
|
|
|
|
|
2009-02-14 14:15:50 -05:00
|
|
|
class Test::Unit::TestCase
|
2009-03-28 11:28:18 -04:00
|
|
|
include Rack::Test::Methods
|
2009-01-13 12:53:53 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class << self
|
|
|
|
alias_method :it, :test
|
2009-01-23 05:37:43 -05:00
|
|
|
end
|
|
|
|
|
2009-03-28 11:28:18 -04:00
|
|
|
alias_method :response, :last_response
|
|
|
|
|
|
|
|
setup do
|
|
|
|
Sinatra::Base.set :environment, :test
|
|
|
|
end
|
|
|
|
|
2009-01-13 12:53:53 -05:00
|
|
|
# Sets up a Sinatra::Base subclass defined with the block
|
|
|
|
# given. Used in setup or individual spec methods to establish
|
|
|
|
# the application.
|
|
|
|
def mock_app(base=Sinatra::Base, &block)
|
|
|
|
@app = Sinatra.new(base, &block)
|
|
|
|
end
|
2009-01-15 07:04:10 -05:00
|
|
|
|
2009-03-28 11:28:18 -04:00
|
|
|
def app
|
|
|
|
Rack::Lint.new(@app)
|
|
|
|
end
|
|
|
|
|
|
|
|
def body
|
|
|
|
response.body.to_s
|
|
|
|
end
|
|
|
|
|
2010-12-23 06:11:57 -05:00
|
|
|
def assert_body(value)
|
2010-12-24 10:46:18 -05:00
|
|
|
assert_equal value.lstrip.gsub(/\s*\n\s*/, ""), body.lstrip.gsub(/\s*\n\s*/, "")
|
2010-12-23 06:11:57 -05:00
|
|
|
end
|
|
|
|
|
2009-03-28 11:28:18 -04:00
|
|
|
# Delegate other missing methods to response.
|
|
|
|
def method_missing(name, *args, &block)
|
|
|
|
if response && response.respond_to?(name)
|
|
|
|
response.send(name, *args, &block)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Also check response since we delegate there.
|
|
|
|
def respond_to?(symbol, include_private=false)
|
|
|
|
super || (response && response.respond_to?(symbol, include_private))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Do not output warnings for the duration of the block.
|
|
|
|
def silence_warnings
|
|
|
|
$VERBOSE, v = nil, $VERBOSE
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
$VERBOSE = v
|
|
|
|
end
|
2009-01-28 15:27:32 -05:00
|
|
|
end
|