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
|
2011-05-01 07:51:11 -04:00
|
|
|
alias_method :section, :context
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.example(desc = nil, &block)
|
|
|
|
@example_count = 0 unless instance_variable_defined? :@example_count
|
|
|
|
@example_count += 1
|
|
|
|
it(desc || "Example #{@example_count}", &block)
|
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)
|
2011-10-10 22:50:59 -04:00
|
|
|
if value.respond_to? :to_str
|
|
|
|
assert_equal value.lstrip.gsub(/\s*\n\s*/, ""), body.lstrip.gsub(/\s*\n\s*/, "")
|
|
|
|
else
|
|
|
|
assert_match value, body
|
|
|
|
end
|
2010-12-23 06:11:57 -05:00
|
|
|
end
|
|
|
|
|
2011-09-17 17:27:26 -04:00
|
|
|
def assert_status(expected)
|
|
|
|
assert_equal Integer(expected), Integer(status)
|
|
|
|
end
|
|
|
|
|
2011-04-15 05:14:23 -04:00
|
|
|
def assert_like(a,b)
|
2011-05-11 16:53:03 -04:00
|
|
|
pattern = /id=['"][^"']*["']|\s+/
|
2011-04-15 05:14:23 -04:00
|
|
|
assert_equal a.strip.gsub(pattern, ""), b.strip.gsub(pattern, "")
|
|
|
|
end
|
|
|
|
|
2011-04-13 08:53:40 -04:00
|
|
|
def assert_include(str, substr)
|
|
|
|
assert str.include?(substr), "expected #{str.inspect} to include #{substr.inspect}"
|
|
|
|
end
|
|
|
|
|
2011-05-01 07:51:11 -04:00
|
|
|
def options(uri, params = {}, env = {}, &block)
|
|
|
|
request(uri, env.merge(:method => "OPTIONS", :params => params), &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def patch(uri, params = {}, env = {}, &block)
|
|
|
|
request(uri, env.merge(:method => "PATCH", :params => params), &block)
|
|
|
|
end
|
|
|
|
|
2013-03-09 12:09:56 -05:00
|
|
|
def link(uri, params = {}, env = {}, &block)
|
|
|
|
request(uri, env.merge(:method => "LINK", :params => params), &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unlink(uri, params = {}, env = {}, &block)
|
|
|
|
request(uri, env.merge(:method => "UNLINK", :params => params), &block)
|
|
|
|
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
|
2011-04-15 05:14:23 -04:00
|
|
|
rescue Rack::Test::Error
|
|
|
|
super
|
2009-03-28 11:28:18 -04:00
|
|
|
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
|