1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/test/helper.rb
Konstantin Haase 1d676f41f8 Sets default content type according to template engine used instead of just text/html.
It does so by including a Mixin into the the returned string offering a content_type method. Therefore all of the following examples produce the expected results:

    # text/html
    get('/') do
      haml :index
    end

    # text/css
    get('/') do
      sass :index
    end

    # text/css
    get('/') do
      haml :index
      sass :index
    end

    # text/html
    get('/') do
      haml '= sass :index'
    end

It also allows setting the default content type for a template engine:

    set :builder, :content_type => :html

Tests and README adjustments (all languages) included.
2010-09-27 13:25:10 +02:00

77 lines
1.6 KiB
Ruby

ENV['RACK_ENV'] = 'test'
Encoding.default_external = "UTF-8" if defined? Encoding
begin
require 'rack'
rescue LoadError
require 'rubygems'
require 'rack'
end
testdir = File.dirname(__FILE__)
$LOAD_PATH.unshift testdir unless $LOAD_PATH.include?(testdir)
libdir = File.dirname(File.dirname(__FILE__)) + '/lib'
$LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
require 'contest'
require 'rack/test'
require 'sinatra/base'
class Sinatra::Base
# Allow assertions in request context
include Test::Unit::Assertions
end
Sinatra::Base.set :environment, :test
class Test::Unit::TestCase
include Rack::Test::Methods
class << self
alias_method :it, :test
end
alias_method :response, :last_response
setup do
Sinatra::Base.set :environment, :test
end
# 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
def app
Rack::Lint.new(@app)
end
def body
response.body.to_s
end
# 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
end