1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Use contest instead of test/spec/mini

See <http://github.com/citrusbyte/contest> for more info. The
contest.rb file is included under the test/ directory.
This commit is contained in:
Simon Rozet 2009-03-26 16:42:13 +01:00 committed by Ryan Tomayko
parent c76a68e59f
commit ff0d068687
23 changed files with 1168 additions and 1047 deletions

View file

@ -1,142 +1,143 @@
require File.dirname(__FILE__) + '/helper'
describe 'Sinatra::Base subclasses' do
class TestApp < Sinatra::Base
get '/' do
'Hello World'
class BaseTest < Test::Unit::TestCase
describe 'Sinatra::Base subclasses' do
class TestApp < Sinatra::Base
get '/' do
'Hello World'
end
end
end
it 'include Rack::Utils' do
assert TestApp.included_modules.include?(Rack::Utils)
end
it 'processes requests with #call' do
assert TestApp.respond_to?(:call)
request = Rack::MockRequest.new(TestApp)
response = request.get('/')
assert response.ok?
assert_equal 'Hello World', response.body
end
class TestApp < Sinatra::Base
get '/state' do
body = "Foo: #{@foo}"
@foo = 'discard'
body
it 'include Rack::Utils' do
assert TestApp.included_modules.include?(Rack::Utils)
end
end
it 'does not maintain state between requests' do
request = Rack::MockRequest.new(TestApp)
2.times do
response = request.get('/state')
it 'processes requests with #call' do
assert TestApp.respond_to?(:call)
request = Rack::MockRequest.new(TestApp)
response = request.get('/')
assert response.ok?
assert_equal 'Foo: ', response.body
assert_equal 'Hello World', response.body
end
class TestApp < Sinatra::Base
get '/state' do
body = "Foo: #{@foo}"
@foo = 'discard'
body
end
end
it 'does not maintain state between requests' do
request = Rack::MockRequest.new(TestApp)
2.times do
response = request.get('/state')
assert response.ok?
assert_equal 'Foo: ', response.body
end
end
end
end
describe "Sinatra::Base as Rack middleware" do
describe "Sinatra::Base as Rack middleware" do
app = lambda { |env|
headers = {'X-Downstream' => 'true'}
headers['X-Route-Missing'] = env['sinatra.route-missing'] || ''
[210, headers, ['Hello from downstream']] }
app = lambda { |env|
headers = {'X-Downstream' => 'true'}
headers['X-Route-Missing'] = env['sinatra.route-missing'] || ''
[210, headers, ['Hello from downstream']] }
class TestMiddleware < Sinatra::Base
end
class TestMiddleware < Sinatra::Base
end
it 'creates a middleware that responds to #call with .new' do
middleware = TestMiddleware.new(app)
assert middleware.respond_to?(:call)
end
it 'exposes the downstream app' do
middleware = TestMiddleware.new(app)
assert_same app, middleware.app
end
class TestMiddleware < Sinatra::Base
def route_missing
env['sinatra.route-missing'] = '1'
super
end
get '/' do
'Hello from middleware'
end
end
it 'creates a middleware that responds to #call with .new' do
middleware = TestMiddleware.new(app)
assert middleware.respond_to?(:call)
end
request = Rack::MockRequest.new(middleware)
it 'exposes the downstream app' do
middleware = TestMiddleware.new(app)
assert_same app, middleware.app
end
class TestMiddleware < Sinatra::Base
def route_missing
env['sinatra.route-missing'] = '1'
super
it 'intercepts requests' do
response = request.get('/')
assert response.ok?
assert_equal 'Hello from middleware', response.body
end
get '/' do
'Hello from middleware'
it 'automatically forwards requests downstream when no matching route found' do
response = request.get('/missing')
assert_equal 210, response.status
assert_equal 'Hello from downstream', response.body
end
end
middleware = TestMiddleware.new(app)
request = Rack::MockRequest.new(middleware)
it 'intercepts requests' do
response = request.get('/')
assert response.ok?
assert_equal 'Hello from middleware', response.body
end
it 'automatically forwards requests downstream when no matching route found' do
response = request.get('/missing')
assert_equal 210, response.status
assert_equal 'Hello from downstream', response.body
end
it 'calls #route_missing before forwarding downstream' do
response = request.get('/missing')
assert_equal '1', response['X-Route-Missing']
end
class TestMiddleware < Sinatra::Base
get '/low-level-forward' do
app.call(env)
it 'calls #route_missing before forwarding downstream' do
response = request.get('/missing')
assert_equal '1', response['X-Route-Missing']
end
end
it 'can call the downstream app directly and return result' do
response = request.get('/low-level-forward')
assert_equal 210, response.status
assert_equal 'true', response['X-Downstream']
assert_equal 'Hello from downstream', response.body
end
class TestMiddleware < Sinatra::Base
get '/low-level-forward' do
app.call(env)
end
end
class TestMiddleware < Sinatra::Base
get '/explicit-forward' do
response['X-Middleware'] = 'true'
res = forward
assert_nil res
it 'can call the downstream app directly and return result' do
response = request.get('/low-level-forward')
assert_equal 210, response.status
assert_equal 'true', response['X-Downstream']
assert_equal ['Hello from downstream'], response.body
'Hello after explicit forward'
assert_equal 'Hello from downstream', response.body
end
end
it 'forwards the request downstream and integrates the response into the current context' do
response = request.get('/explicit-forward')
assert_equal 210, response.status
assert_equal 'true', response['X-Downstream']
assert_equal 'Hello after explicit forward', response.body
assert_equal '28', response['Content-Length']
end
app_content_length = lambda {|env|
[200, {'Content-Length' => '16'}, 'From downstream!']}
class TestMiddlewareContentLength < Sinatra::Base
get '/forward' do
res = forward
'From after explicit forward!'
class TestMiddleware < Sinatra::Base
get '/explicit-forward' do
response['X-Middleware'] = 'true'
res = forward
assert_nil res
assert_equal 210, response.status
assert_equal 'true', response['X-Downstream']
assert_equal ['Hello from downstream'], response.body
'Hello after explicit forward'
end
end
end
middleware_content_length = TestMiddlewareContentLength.new(app_content_length)
request_content_length = Rack::MockRequest.new(middleware_content_length)
it 'forwards the request downstream and integrates the response into the current context' do
response = request.get('/explicit-forward')
assert_equal 210, response.status
assert_equal 'true', response['X-Downstream']
assert_equal 'Hello after explicit forward', response.body
assert_equal '28', response['Content-Length']
end
it "sets content length for last response" do
response = request_content_length.get('/forward')
assert_equal '28', response['Content-Length']
app_content_length = lambda {|env|
[200, {'Content-Length' => '16'}, 'From downstream!']}
class TestMiddlewareContentLength < Sinatra::Base
get '/forward' do
res = forward
'From after explicit forward!'
end
end
middleware_content_length = TestMiddlewareContentLength.new(app_content_length)
request_content_length = Rack::MockRequest.new(middleware_content_length)
it "sets content length for last response" do
response = request_content_length.get('/forward')
assert_equal '28', response['Content-Length']
end
end
end

View file

@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/helper'
describe "Builder Templates" do
class BuilderTest < Test::Unit::TestCase
def builder_app(&block)
mock_app {
set :views, File.dirname(__FILE__) + '/views'

62
test/contest.rb Normal file
View file

@ -0,0 +1,62 @@
require "test/unit"
# Test::Unit loads a default test if the suite is empty, and the only
# purpose of that test is to fail. As having empty contexts is a common
# practice, we decided to overwrite TestSuite#empty? in order to
# allow them. Having a failure when no tests have been defined seems
# counter-intuitive.
class Test::Unit::TestSuite
def empty?
false
end
end
# We added setup, test and context as class methods, and the instance
# method setup now iterates on the setup blocks. Note that all setup
# blocks must be defined with the block syntax. Adding a setup instance
# method defeats the purpose of this library.
class Test::Unit::TestCase
def self.setup(&block)
setup_blocks << block
end
def setup
self.class.setup_blocks.each do |block|
instance_eval(&block)
end
end
def self.context(name, &block)
subclass = Class.new(self.superclass)
subclass.setup_blocks.unshift(*setup_blocks)
subclass.class_eval(&block)
const_set(context_name(name), subclass)
end
def self.test(name, &block)
define_method(test_name(name), &block)
end
class << self
alias_method :should, :test
alias_method :describe, :context
end
private
def self.setup_blocks
@setup_blocks ||= []
end
def self.context_name(name)
"Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}".to_sym
end
def self.test_name(name)
"test_#{sanitize_name(name).gsub(/\s+/,'_')}".to_sym
end
def self.sanitize_name(name)
name.gsub(/\W+/, ' ').strip
end
end

View file

@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/helper'
describe "ERB Templates" do
class ERBTest < Test::Unit::TestCase
def erb_app(&block)
mock_app {
set :views, File.dirname(__FILE__) + '/views'

View file

@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/helper'
describe 'Registering extensions' do
class ExtensionsTest < Test::Unit::TestCase
module FooExtensions
def foo
end

View file

@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/helper'
describe "Filters" do
class FilterTest < Test::Unit::TestCase
it "executes filters in the order defined" do
count = 0
mock_app do

View file

@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/helper'
describe "HAML Templates" do
class HAMLTest < Test::Unit::TestCase
def haml_app(&block)
mock_app {
set :views, File.dirname(__FILE__) + '/views'

View file

@ -5,10 +5,13 @@ rescue LoadError
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 'test/unit'
require 'contest'
require 'sinatra/test'
class Sinatra::Base
@ -16,11 +19,13 @@ class Sinatra::Base
include Test::Unit::Assertions
end
Sinatra::Base.set :environment, :test
class Test::Unit::TestCase
include Sinatra::Test
def setup
Sinatra::Base.set :environment, :test
class << self
alias_method :it, :test
end
# Sets up a Sinatra::Base subclass defined with the block
@ -45,38 +50,6 @@ class Test::Unit::TestCase
end
end
##
# test/spec/mini
# http://pastie.caboo.se/158871
# chris@ozmm.org
#
def describe(*args, &block)
return super unless (name = args.first.capitalize) && block
name = "#{name.gsub(/\W/, '')}Test"
Object.send :const_set, name, Class.new(Test::Unit::TestCase)
klass = Object.const_get(name)
klass.class_eval do
def self.it(name, &block)
define_method("test_#{name.gsub(/\W/,'_').downcase}", &block)
end
def self.xspecify(*args) end
def self.before(&block) define_method(:setup, &block) end
def self.after(&block) define_method(:teardown, &block) end
end
klass.class_eval &block
klass
end
def describe_option(name, &block)
klass = describe("Option #{name}", &block)
klass.before do
restore_default_options
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
klass
end
# Do not output warnings for the duration of the block.
def silence_warnings
$VERBOSE, v = nil, $VERBOSE

View file

@ -1,497 +1,499 @@
require File.dirname(__FILE__) + '/helper'
describe 'Helpers#status' do
before do
mock_app {
get '/' do
status 207
nil
end
}
end
it 'sets the response status code' do
get '/'
assert_equal 207, response.status
end
end
describe 'Helpers#body' do
it 'takes a block for defered body generation' do
mock_app {
get '/' do
body { 'Hello World' }
end
}
get '/'
assert_equal 'Hello World', body
end
it 'takes a String, Array, or other object responding to #each' do
mock_app {
get '/' do
body 'Hello World'
end
}
get '/'
assert_equal 'Hello World', body
end
end
describe 'Helpers#redirect' do
it 'uses a 302 when only a path is given' do
mock_app {
get '/' do
redirect '/foo'
fail 'redirect should halt'
end
}
get '/'
assert_equal 302, status
assert_equal '', body
assert_equal '/foo', response['Location']
end
it 'uses the code given when specified' do
mock_app {
get '/' do
redirect '/foo', 301
fail 'redirect should halt'
end
}
get '/'
assert_equal 301, status
assert_equal '', body
assert_equal '/foo', response['Location']
end
it 'redirects back to request.referer when passed back' do
mock_app {
get '/try_redirect' do
redirect back
end
}
request = Rack::MockRequest.new(@app)
response = request.get('/try_redirect', 'HTTP_REFERER' => '/foo')
assert_equal 302, response.status
assert_equal '/foo', response['Location']
end
end
describe 'Helpers#error' do
it 'sets a status code and halts' do
mock_app {
get '/' do
error 501
fail 'error should halt'
end
}
get '/'
assert_equal 501, status
assert_equal '', body
end
it 'takes an optional body' do
mock_app {
get '/' do
error 501, 'FAIL'
fail 'error should halt'
end
}
get '/'
assert_equal 501, status
assert_equal 'FAIL', body
end
it 'uses a 500 status code when first argument is a body' do
mock_app {
get '/' do
error 'FAIL'
fail 'error should halt'
end
}
get '/'
assert_equal 500, status
assert_equal 'FAIL', body
end
end
describe 'Helpers#not_found' do
it 'halts with a 404 status' do
mock_app {
get '/' do
not_found
fail 'not_found should halt'
end
}
get '/'
assert_equal 404, status
assert_equal '', body
end
end
describe 'Helpers#headers' do
it 'sets headers on the response object when given a Hash' do
mock_app {
get '/' do
headers 'X-Foo' => 'bar', 'X-Baz' => 'bling'
'kthx'
end
}
get '/'
assert ok?
assert_equal 'bar', response['X-Foo']
assert_equal 'bling', response['X-Baz']
assert_equal 'kthx', body
end
it 'returns the response headers hash when no hash provided' do
mock_app {
get '/' do
headers['X-Foo'] = 'bar'
'kthx'
end
}
get '/'
assert ok?
assert_equal 'bar', response['X-Foo']
end
end
describe 'Helpers#session' do
it 'uses the existing rack.session' do
mock_app {
get '/' do
session[:foo]
end
}
get '/', :env => { 'rack.session' => { :foo => 'bar' } }
assert_equal 'bar', body
end
it 'creates a new session when none provided' do
mock_app {
get '/' do
assert session.empty?
session[:foo] = 'bar'
'Hi'
end
}
get '/'
assert_equal 'Hi', body
end
end
describe 'Helpers#media_type' do
include Sinatra::Helpers
it "looks up media types in Rack's MIME registry" do
Rack::Mime::MIME_TYPES['.foo'] = 'application/foo'
assert_equal 'application/foo', media_type('foo')
assert_equal 'application/foo', media_type('.foo')
assert_equal 'application/foo', media_type(:foo)
end
it 'returns nil when given nil' do
assert media_type(nil).nil?
end
it 'returns nil when media type not registered' do
assert media_type(:bizzle).nil?
end
it 'returns the argument when given a media type string' do
assert_equal 'text/plain', media_type('text/plain')
end
end
describe 'Helpers#content_type' do
it 'sets the Content-Type header' do
mock_app {
get '/' do
content_type 'text/plain'
'Hello World'
end
}
get '/'
assert_equal 'text/plain', response['Content-Type']
assert_equal 'Hello World', body
end
it 'takes media type parameters (like charset=)' do
mock_app {
get '/' do
content_type 'text/html', :charset => 'utf-8'
"<h1>Hello, World</h1>"
end
}
get '/'
assert ok?
assert_equal 'text/html;charset=utf-8', response['Content-Type']
assert_equal "<h1>Hello, World</h1>", body
end
it "looks up symbols in Rack's mime types dictionary" do
Rack::Mime::MIME_TYPES['.foo'] = 'application/foo'
mock_app {
get '/foo.xml' do
content_type :foo
"I AM FOO"
end
}
get '/foo.xml'
assert ok?
assert_equal 'application/foo', response['Content-Type']
assert_equal 'I AM FOO', body
end
it 'fails when no mime type is registered for the argument provided' do
mock_app {
get '/foo.xml' do
content_type :bizzle
"I AM FOO"
end
}
assert_raise(RuntimeError) { get '/foo.xml' }
end
end
describe 'Helpers#send_file' do
before do
@file = File.dirname(__FILE__) + '/file.txt'
File.open(@file, 'wb') { |io| io.write('Hello World') }
end
after do
File.unlink @file
@file = nil
end
def send_file_app(opts={})
path = @file
mock_app {
get '/file.txt' do
send_file path, opts
end
}
end
it "sends the contents of the file" do
send_file_app
get '/file.txt'
assert ok?
assert_equal 'Hello World', body
end
it 'sets the Content-Type response header if a mime-type can be located' do
send_file_app
get '/file.txt'
assert_equal 'text/plain', response['Content-Type']
end
it 'sets the Content-Length response header' do
send_file_app
get '/file.txt'
assert_equal 'Hello World'.length.to_s, response['Content-Length']
end
it 'sets the Last-Modified response header' do
send_file_app
get '/file.txt'
assert_equal File.mtime(@file).httpdate, response['Last-Modified']
end
it "returns a 404 when not found" do
mock_app {
get '/' do
send_file 'this-file-does-not-exist.txt'
end
}
get '/'
assert not_found?
end
it "does not set the Content-Disposition header by default" do
send_file_app
get '/file.txt'
assert_nil response['Content-Disposition']
end
it "sets the Content-Disposition header when :disposition set to 'attachment'" do
send_file_app :disposition => 'attachment'
get '/file.txt'
assert_equal 'attachment; filename="file.txt"', response['Content-Disposition']
end
it "sets the Content-Disposition header when :filename provided" do
send_file_app :filename => 'foo.txt'
get '/file.txt'
assert_equal 'attachment; filename="foo.txt"', response['Content-Disposition']
end
end
describe 'Helpers#last_modified' do
before do
now = Time.now
mock_app {
get '/' do
body { 'Hello World' }
last_modified now
'Boo!'
end
}
@now = now
end
it 'sets the Last-Modified header to a valid RFC 2616 date value' do
get '/'
assert_equal @now.httpdate, response['Last-Modified']
end
it 'returns a body when conditional get misses' do
get '/'
assert_equal 200, status
assert_equal 'Boo!', body
end
it 'halts when a conditional GET matches' do
get '/', :env => { 'HTTP_IF_MODIFIED_SINCE' => @now.httpdate }
assert_equal 304, status
assert_equal '', body
end
end
describe 'Helpers#etag' do
before do
mock_app {
get '/' do
body { 'Hello World' }
etag 'FOO'
'Boo!'
end
}
end
it 'sets the ETag header' do
get '/'
assert_equal '"FOO"', response['ETag']
end
it 'returns a body when conditional get misses' do
get '/'
assert_equal 200, status
assert_equal 'Boo!', body
end
it 'halts when a conditional GET matches' do
get '/', :env => { 'HTTP_IF_NONE_MATCH' => '"FOO"' }
assert_equal 304, status
assert_equal '', body
end
it 'should handle multiple ETag values in If-None-Match header' do
get '/', :env => { 'HTTP_IF_NONE_MATCH' => '"BAR", *' }
assert_equal 304, status
assert_equal '', body
end
it 'uses a weak etag with the :weak option' do
mock_app {
get '/' do
etag 'FOO', :weak
"that's weak, dude."
end
}
get '/'
assert_equal 'W/"FOO"', response['ETag']
end
end
describe 'Helpers#back' do
it "makes redirecting back pretty" do
mock_app {
get '/foo' do
redirect back
end
}
get '/foo', {}, 'HTTP_REFERER' => 'http://github.com'
assert redirect?
assert_equal "http://github.com", response.location
end
end
module HelperOne; def one; '1'; end; end
module HelperTwo; def two; '2'; end; end
describe 'Adding new helpers' do
it 'takes a list of modules to mix into the app' do
mock_app {
helpers HelperOne, HelperTwo
get '/one' do
one
end
get '/two' do
two
end
}
get '/one'
assert_equal '1', body
get '/two'
assert_equal '2', body
end
it 'takes a block to mix into the app' do
mock_app {
helpers do
def foo
'foo'
class HelpersTest < Test::Unit::TestCase
describe 'status' do
setup do
mock_app {
get '/' do
status 207
nil
end
end
}
end
get '/' do
foo
end
}
get '/'
assert_equal 'foo', body
it 'sets the response status code' do
get '/'
assert_equal 207, response.status
end
end
it 'evaluates the block in class context so that methods can be aliased' do
mock_app {
helpers do
alias_method :h, :escape_html
end
describe 'body' do
it 'takes a block for defered body generation' do
mock_app {
get '/' do
body { 'Hello World' }
end
}
get '/' do
h('42 < 43')
end
}
get '/'
assert_equal 'Hello World', body
end
get '/'
assert ok?
assert_equal '42 &lt; 43', body
it 'takes a String, Array, or other object responding to #each' do
mock_app {
get '/' do
body 'Hello World'
end
}
get '/'
assert_equal 'Hello World', body
end
end
describe 'redirect' do
it 'uses a 302 when only a path is given' do
mock_app {
get '/' do
redirect '/foo'
fail 'redirect should halt'
end
}
get '/'
assert_equal 302, status
assert_equal '', body
assert_equal '/foo', response['Location']
end
it 'uses the code given when specified' do
mock_app {
get '/' do
redirect '/foo', 301
fail 'redirect should halt'
end
}
get '/'
assert_equal 301, status
assert_equal '', body
assert_equal '/foo', response['Location']
end
it 'redirects back to request.referer when passed back' do
mock_app {
get '/try_redirect' do
redirect back
end
}
request = Rack::MockRequest.new(@app)
response = request.get('/try_redirect', 'HTTP_REFERER' => '/foo')
assert_equal 302, response.status
assert_equal '/foo', response['Location']
end
end
describe 'error' do
it 'sets a status code and halts' do
mock_app {
get '/' do
error 501
fail 'error should halt'
end
}
get '/'
assert_equal 501, status
assert_equal '', body
end
it 'takes an optional body' do
mock_app {
get '/' do
error 501, 'FAIL'
fail 'error should halt'
end
}
get '/'
assert_equal 501, status
assert_equal 'FAIL', body
end
it 'uses a 500 status code when first argument is a body' do
mock_app {
get '/' do
error 'FAIL'
fail 'error should halt'
end
}
get '/'
assert_equal 500, status
assert_equal 'FAIL', body
end
end
describe 'not_found' do
it 'halts with a 404 status' do
mock_app {
get '/' do
not_found
fail 'not_found should halt'
end
}
get '/'
assert_equal 404, status
assert_equal '', body
end
end
describe 'headers' do
it 'sets headers on the response object when given a Hash' do
mock_app {
get '/' do
headers 'X-Foo' => 'bar', 'X-Baz' => 'bling'
'kthx'
end
}
get '/'
assert ok?
assert_equal 'bar', response['X-Foo']
assert_equal 'bling', response['X-Baz']
assert_equal 'kthx', body
end
it 'returns the response headers hash when no hash provided' do
mock_app {
get '/' do
headers['X-Foo'] = 'bar'
'kthx'
end
}
get '/'
assert ok?
assert_equal 'bar', response['X-Foo']
end
end
describe 'session' do
it 'uses the existing rack.session' do
mock_app {
get '/' do
session[:foo]
end
}
get '/', :env => { 'rack.session' => { :foo => 'bar' } }
assert_equal 'bar', body
end
it 'creates a new session when none provided' do
mock_app {
get '/' do
assert session.empty?
session[:foo] = 'bar'
'Hi'
end
}
get '/'
assert_equal 'Hi', body
end
end
describe 'media_type' do
include Sinatra::Helpers
it "looks up media types in Rack's MIME registry" do
Rack::Mime::MIME_TYPES['.foo'] = 'application/foo'
assert_equal 'application/foo', media_type('foo')
assert_equal 'application/foo', media_type('.foo')
assert_equal 'application/foo', media_type(:foo)
end
it 'returns nil when given nil' do
assert media_type(nil).nil?
end
it 'returns nil when media type not registered' do
assert media_type(:bizzle).nil?
end
it 'returns the argument when given a media type string' do
assert_equal 'text/plain', media_type('text/plain')
end
end
describe 'content_type' do
it 'sets the Content-Type header' do
mock_app {
get '/' do
content_type 'text/plain'
'Hello World'
end
}
get '/'
assert_equal 'text/plain', response['Content-Type']
assert_equal 'Hello World', body
end
it 'takes media type parameters (like charset=)' do
mock_app {
get '/' do
content_type 'text/html', :charset => 'utf-8'
"<h1>Hello, World</h1>"
end
}
get '/'
assert ok?
assert_equal 'text/html;charset=utf-8', response['Content-Type']
assert_equal "<h1>Hello, World</h1>", body
end
it "looks up symbols in Rack's mime types dictionary" do
Rack::Mime::MIME_TYPES['.foo'] = 'application/foo'
mock_app {
get '/foo.xml' do
content_type :foo
"I AM FOO"
end
}
get '/foo.xml'
assert ok?
assert_equal 'application/foo', response['Content-Type']
assert_equal 'I AM FOO', body
end
it 'fails when no mime type is registered for the argument provided' do
mock_app {
get '/foo.xml' do
content_type :bizzle
"I AM FOO"
end
}
assert_raise(RuntimeError) { get '/foo.xml' }
end
end
describe 'send_file' do
setup do
@file = File.dirname(__FILE__) + '/file.txt'
File.open(@file, 'wb') { |io| io.write('Hello World') }
end
def teardown
File.unlink @file
@file = nil
end
def send_file_app(opts={})
path = @file
mock_app {
get '/file.txt' do
send_file path, opts
end
}
end
it "sends the contents of the file" do
send_file_app
get '/file.txt'
assert ok?
assert_equal 'Hello World', body
end
it 'sets the Content-Type response header if a mime-type can be located' do
send_file_app
get '/file.txt'
assert_equal 'text/plain', response['Content-Type']
end
it 'sets the Content-Length response header' do
send_file_app
get '/file.txt'
assert_equal 'Hello World'.length.to_s, response['Content-Length']
end
it 'sets the Last-Modified response header' do
send_file_app
get '/file.txt'
assert_equal File.mtime(@file).httpdate, response['Last-Modified']
end
it "returns a 404 when not found" do
mock_app {
get '/' do
send_file 'this-file-does-not-exist.txt'
end
}
get '/'
assert not_found?
end
it "does not set the Content-Disposition header by default" do
send_file_app
get '/file.txt'
assert_nil response['Content-Disposition']
end
it "sets the Content-Disposition header when :disposition set to 'attachment'" do
send_file_app :disposition => 'attachment'
get '/file.txt'
assert_equal 'attachment; filename="file.txt"', response['Content-Disposition']
end
it "sets the Content-Disposition header when :filename provided" do
send_file_app :filename => 'foo.txt'
get '/file.txt'
assert_equal 'attachment; filename="foo.txt"', response['Content-Disposition']
end
end
describe 'last_modified' do
setup do
now = Time.now
mock_app {
get '/' do
body { 'Hello World' }
last_modified now
'Boo!'
end
}
@now = now
end
it 'sets the Last-Modified header to a valid RFC 2616 date value' do
get '/'
assert_equal @now.httpdate, response['Last-Modified']
end
it 'returns a body when conditional get misses' do
get '/'
assert_equal 200, status
assert_equal 'Boo!', body
end
it 'halts when a conditional GET matches' do
get '/', :env => { 'HTTP_IF_MODIFIED_SINCE' => @now.httpdate }
assert_equal 304, status
assert_equal '', body
end
end
describe 'etag' do
setup do
mock_app {
get '/' do
body { 'Hello World' }
etag 'FOO'
'Boo!'
end
}
end
it 'sets the ETag header' do
get '/'
assert_equal '"FOO"', response['ETag']
end
it 'returns a body when conditional get misses' do
get '/'
assert_equal 200, status
assert_equal 'Boo!', body
end
it 'halts when a conditional GET matches' do
get '/', :env => { 'HTTP_IF_NONE_MATCH' => '"FOO"' }
assert_equal 304, status
assert_equal '', body
end
it 'should handle multiple ETag values in If-None-Match header' do
get '/', :env => { 'HTTP_IF_NONE_MATCH' => '"BAR", *' }
assert_equal 304, status
assert_equal '', body
end
it 'uses a weak etag with the :weak option' do
mock_app {
get '/' do
etag 'FOO', :weak
"that's weak, dude."
end
}
get '/'
assert_equal 'W/"FOO"', response['ETag']
end
end
describe 'back' do
it "makes redirecting back pretty" do
mock_app {
get '/foo' do
redirect back
end
}
get '/foo', {}, 'HTTP_REFERER' => 'http://github.com'
assert redirect?
assert_equal "http://github.com", response.location
end
end
module HelperOne; def one; '1'; end; end
module HelperTwo; def two; '2'; end; end
describe 'Adding new helpers' do
it 'takes a list of modules to mix into the app' do
mock_app {
helpers HelperOne, HelperTwo
get '/one' do
one
end
get '/two' do
two
end
}
get '/one'
assert_equal '1', body
get '/two'
assert_equal '2', body
end
it 'takes a block to mix into the app' do
mock_app {
helpers do
def foo
'foo'
end
end
get '/' do
foo
end
}
get '/'
assert_equal 'foo', body
end
it 'evaluates the block in class context so that methods can be aliased' do
mock_app {
helpers do
alias_method :h, :escape_html
end
get '/' do
h('42 < 43')
end
}
get '/'
assert ok?
assert_equal '42 &lt; 43', body
end
end
end

View file

@ -6,155 +6,158 @@ end
class FooNotFound < Sinatra::NotFound
end
describe 'Exception Mappings' do
it 'invokes handlers registered with ::error when raised' do
mock_app {
set :raise_errors, false
error(FooError) { 'Foo!' }
get '/' do
raise FooError
end
}
get '/'
assert_equal 500, status
assert_equal 'Foo!', body
end
it 'uses the Exception handler if no matching handler found' do
mock_app {
set :raise_errors, false
error(Exception) { 'Exception!' }
get '/' do
raise FooError
end
}
get '/'
assert_equal 500, status
assert_equal 'Exception!', body
end
it "sets env['sinatra.error'] to the rescued exception" do
mock_app {
set :raise_errors, false
error(FooError) {
assert env.include?('sinatra.error')
assert env['sinatra.error'].kind_of?(FooError)
'looks good'
class MappedErrorTest < Test::Unit::TestCase
describe 'Exception Mappings' do
it 'invokes handlers registered with ::error when raised' do
mock_app {
set :raise_errors, false
error(FooError) { 'Foo!' }
get '/' do
raise FooError
end
}
get '/' do
raise FooError
end
}
get '/'
assert_equal 'looks good', body
get '/'
assert_equal 500, status
assert_equal 'Foo!', body
end
it 'uses the Exception handler if no matching handler found' do
mock_app {
set :raise_errors, false
error(Exception) { 'Exception!' }
get '/' do
raise FooError
end
}
get '/'
assert_equal 500, status
assert_equal 'Exception!', body
end
it "sets env['sinatra.error'] to the rescued exception" do
mock_app {
set :raise_errors, false
error(FooError) {
assert env.include?('sinatra.error')
assert env['sinatra.error'].kind_of?(FooError)
'looks good'
}
get '/' do
raise FooError
end
}
get '/'
assert_equal 'looks good', body
end
it 'dumps errors to rack.errors when dump_errors is enabled' do
mock_app {
set :raise_errors, false
set :dump_errors, true
get('/') { raise FooError, 'BOOM!' }
}
get '/'
assert_equal 500, status
assert @response.errors =~ /FooError - BOOM!:/
end
it "raises without calling the handler when the raise_errors options is set" do
mock_app {
set :raise_errors, true
error(FooError) { "she's not there." }
get '/' do
raise FooError
end
}
assert_raise(FooError) { get '/' }
end
it "never raises Sinatra::NotFound beyond the application" do
mock_app {
set :raise_errors, true
get '/' do
raise Sinatra::NotFound
end
}
assert_nothing_raised { get '/' }
assert_equal 404, status
end
it "cascades for subclasses of Sinatra::NotFound" do
mock_app {
set :raise_errors, true
error(FooNotFound) { "foo! not found." }
get '/' do
raise FooNotFound
end
}
assert_nothing_raised { get '/' }
assert_equal 404, status
assert_equal 'foo! not found.', body
end
it 'has a not_found method for backwards compatibility' do
mock_app {
not_found do
"Lost, are we?"
end
}
get '/test'
assert_equal 404, status
assert_equal "Lost, are we?", body
end
end
it 'dumps errors to rack.errors when dump_errors is enabled' do
mock_app {
set :raise_errors, false
set :dump_errors, true
get('/') { raise FooError, 'BOOM!' }
}
describe 'Custom Error Pages' do
it 'allows numeric status code mappings to be registered with ::error' do
mock_app {
set :raise_errors, false
error(500) { 'Foo!' }
get '/' do
[500, {}, 'Internal Foo Error']
end
}
get '/'
assert_equal 500, status
assert_equal 'Foo!', body
end
get '/'
assert_equal 500, status
assert @response.errors =~ /FooError - BOOM!:/
end
it 'allows ranges of status code mappings to be registered with :error' do
mock_app {
set :raise_errors, false
error(500..550) { "Error: #{response.status}" }
get '/' do
[507, {}, 'A very special error']
end
}
get '/'
assert_equal 507, status
assert_equal 'Error: 507', body
end
it "raises without calling the handler when the raise_errors options is set" do
mock_app {
set :raise_errors, true
error(FooError) { "she's not there." }
get '/' do
raise FooError
end
}
assert_raise(FooError) { get '/' }
end
class FooError < RuntimeError
end
it "never raises Sinatra::NotFound beyond the application" do
mock_app {
set :raise_errors, true
get '/' do
raise Sinatra::NotFound
end
}
assert_nothing_raised { get '/' }
assert_equal 404, status
end
it 'runs after exception mappings and overwrites body' do
mock_app {
set :raise_errors, false
error FooError do
response.status = 502
'from exception mapping'
end
error(500) { 'from 500 handler' }
error(502) { 'from custom error page' }
it "cascades for subclasses of Sinatra::NotFound" do
mock_app {
set :raise_errors, true
error(FooNotFound) { "foo! not found." }
get '/' do
raise FooNotFound
end
}
assert_nothing_raised { get '/' }
assert_equal 404, status
assert_equal 'foo! not found.', body
end
it 'has a not_found method for backwards compatibility' do
mock_app {
not_found do
"Lost, are we?"
end
}
get '/test'
assert_equal 404, status
assert_equal "Lost, are we?", body
end
end
describe 'Custom Error Pages' do
it 'allows numeric status code mappings to be registered with ::error' do
mock_app {
set :raise_errors, false
error(500) { 'Foo!' }
get '/' do
[500, {}, 'Internal Foo Error']
end
}
get '/'
assert_equal 500, status
assert_equal 'Foo!', body
end
it 'allows ranges of status code mappings to be registered with :error' do
mock_app {
set :raise_errors, false
error(500..550) { "Error: #{response.status}" }
get '/' do
[507, {}, 'A very special error']
end
}
get '/'
assert_equal 507, status
assert_equal 'Error: 507', body
end
class FooError < RuntimeError
end
it 'runs after exception mappings and overwrites body' do
mock_app {
set :raise_errors, false
error FooError do
response.status = 502
'from exception mapping'
end
error(500) { 'from 500 handler' }
error(502) { 'from custom error page' }
get '/' do
raise FooError
end
}
get '/'
assert_equal 502, status
assert_equal 'from custom error page', body
get '/' do
raise FooError
end
}
get '/'
assert_equal 502, status
assert_equal 'from custom error page', body
end
end
end

View file

@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/helper'
describe "Middleware" do
before do
class MiddlewareTest < Test::Unit::TestCase
setup do
@app = mock_app(Sinatra::Default) {
get '/*' do
response.headers['X-Tests'] = env['test.ran'].

View file

@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/helper'
describe 'Options' do
before do
class OptionsTest < Test::Unit::TestCase
setup do
restore_default_options
@app = Sinatra.new
end
@ -97,262 +97,342 @@ describe 'Options' do
assert_equal 200, status
assert_equal 'okay', body
end
end
describe_option 'clean_trace' do
def clean_backtrace(trace)
@base.new.send(:clean_backtrace, trace)
describe 'clean_trace' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
def clean_backtrace(trace)
@base.new.send(:clean_backtrace, trace)
end
it 'is enabled on Base' do
assert @base.clean_trace?
end
it 'is enabled on Default' do
assert @default.clean_trace?
end
it 'does nothing when disabled' do
backtrace = [
"./lib/sinatra/base.rb",
"./myapp:42",
("#{Gem.dir}/some/lib.rb" if defined?(Gem))
].compact
@base.set :clean_trace, false
assert_equal backtrace, clean_backtrace(backtrace)
end
it 'removes sinatra lib paths from backtrace when enabled' do
backtrace = [
"./lib/sinatra/base.rb",
"./lib/sinatra/compat.rb:42",
"./lib/sinatra/main.rb:55 in `foo'"
]
assert clean_backtrace(backtrace).empty?
end
it 'removes ./ prefix from backtrace paths when enabled' do
assert_equal ['myapp.rb:42'], clean_backtrace(['./myapp.rb:42'])
end
if defined?(Gem)
it 'removes gem lib paths from backtrace when enabled' do
assert clean_backtrace(["#{Gem.dir}/some/lib"]).empty?
end
end
end
it 'is enabled on Base' do
assert @base.clean_trace?
describe 'run' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'is disabled on Base' do
assert ! @base.run?
end
it 'is enabled on Default when not in test environment' do
assert @default.development?
assert @default.run?
@default.set :environment, :development
assert @default.run?
end
# TODO: it 'is enabled when $0 == app_file'
end
it 'is enabled on Default' do
assert @default.clean_trace?
describe 'raise_errors' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'is enabled on Base' do
assert @base.raise_errors?
end
it 'is enabled on Default only in test' do
@default.set(:environment, :development)
assert @default.development?
assert ! @default.raise_errors?, "disabled development"
@default.set(:environment, :production)
assert ! @default.raise_errors?
@default.set(:environment, :test)
assert @default.raise_errors?
end
end
it 'does nothing when disabled' do
backtrace = [
"./lib/sinatra/base.rb",
"./myapp:42",
("#{Gem.dir}/some/lib.rb" if defined?(Gem))
].compact
@base.set :clean_trace, false
assert_equal backtrace, clean_backtrace(backtrace)
describe 'show_exceptions' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'is enabled on Default only in development' do
@base.set(:environment, :development)
assert @base.show_exceptions?
assert @default.development?
assert @default.show_exceptions?
@default.set(:environment, :test)
assert ! @default.show_exceptions?
@base.set(:environment, :production)
assert ! @base.show_exceptions?
end
it 'returns a friendly 500' do
mock_app {
disable :raise_errors
enable :show_exceptions
get '/' do
raise StandardError
end
}
get '/'
assert_equal 500, status
assert body.include?("StandardError")
assert body.include?("<code>show_exceptions</code> option")
end
end
it 'removes sinatra lib paths from backtrace when enabled' do
backtrace = [
"./lib/sinatra/base.rb",
"./lib/sinatra/compat.rb:42",
"./lib/sinatra/main.rb:55 in `foo'"
]
assert clean_backtrace(backtrace).empty?
describe 'dump_errors' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'is disabled on Base' do
assert ! @base.dump_errors?
end
it 'is enabled on Default' do
assert @default.dump_errors?
end
it 'dumps exception with backtrace to rack.errors' do
Sinatra::Default.disable(:raise_errors)
mock_app(Sinatra::Default) {
error do
error = @env['rack.errors'].instance_variable_get(:@error)
error.rewind
error.read
end
get '/' do
raise
end
}
get '/'
assert body.include?("RuntimeError") && body.include?("options_test.rb")
end
end
it 'removes ./ prefix from backtrace paths when enabled' do
assert_equal ['myapp.rb:42'], clean_backtrace(['./myapp.rb:42'])
describe 'sessions' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'is disabled on Base' do
assert ! @base.sessions?
end
it 'is disabled on Default' do
assert ! @default.sessions?
end
# TODO: it 'uses Rack::Session::Cookie when enabled' do
end
if defined?(Gem)
it 'removes gem lib paths from backtrace when enabled' do
assert clean_backtrace(["#{Gem.dir}/some/lib"]).empty?
describe 'logging' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'is disabled on Base' do
assert ! @base.logging?
end
it 'is enabled on Default when not in test environment' do
assert @default.logging?
@default.set :environment, :test
assert ! @default.logging
end
# TODO: it 'uses Rack::CommonLogger when enabled' do
end
describe 'static' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'is disabled on Base' do
assert ! @base.static?
end
it 'is enabled on Default' do
assert @default.static?
end
# TODO: it setup static routes if public is enabled
# TODO: however, that's already tested in static_test so...
end
describe 'host' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'defaults to 0.0.0.0' do
assert_equal '0.0.0.0', @base.host
assert_equal '0.0.0.0', @default.host
end
end
describe 'port' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'defaults to 4567' do
assert_equal 4567, @base.port
assert_equal 4567, @default.port
end
end
describe 'server' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'is one of thin, mongrel, webrick' do
assert_equal %w[thin mongrel webrick], @base.server
assert_equal %w[thin mongrel webrick], @default.server
end
end
describe 'app_file' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'is nil' do
assert @base.app_file.nil?
assert @default.app_file.nil?
end
end
describe 'root' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'is nil if app_file is not set' do
assert @base.root.nil?
assert @default.root.nil?
end
it 'is equal to the expanded basename of app_file' do
@base.app_file = __FILE__
assert_equal File.expand_path(File.dirname(__FILE__)), @base.root
@default.app_file = __FILE__
assert_equal File.expand_path(File.dirname(__FILE__)), @default.root
end
end
describe 'views' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'is nil if root is not set' do
assert @base.views.nil?
assert @default.views.nil?
end
it 'is set to root joined with views/' do
@base.root = File.dirname(__FILE__)
assert_equal File.dirname(__FILE__) + "/views", @base.views
@default.root = File.dirname(__FILE__)
assert_equal File.dirname(__FILE__) + "/views", @default.views
end
end
describe 'public' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'is nil if root is not set' do
assert @base.public.nil?
assert @default.public.nil?
end
it 'is set to root joined with public/' do
@base.root = File.dirname(__FILE__)
assert_equal File.dirname(__FILE__) + "/public", @base.public
@default.root = File.dirname(__FILE__)
assert_equal File.dirname(__FILE__) + "/public", @default.public
end
end
describe 'lock' do
setup do
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
it 'is disabled by default' do
assert ! @base.lock?
end
end
end
describe_option 'run' do
it 'is disabled on Base' do
assert ! @base.run?
end
it 'is enabled on Default when not in test environment' do
assert @default.development?
assert @default.run?
@default.set :environment, :development
assert @default.run?
end
# TODO: it 'is enabled when $0 == app_file'
end
describe_option 'raise_errors' do
it 'is enabled on Base' do
assert @base.raise_errors?
end
it 'is enabled on Default only in test' do
@default.set(:environment, :development)
assert @default.development?
assert ! @default.raise_errors?, "disabled development"
@default.set(:environment, :production)
assert ! @default.raise_errors?
@default.set(:environment, :test)
assert @default.raise_errors?
end
end
describe_option 'show_exceptions' do
it 'is enabled on Default only in development' do
@base.set(:environment, :development)
assert @base.show_exceptions?
assert @default.development?
assert @default.show_exceptions?
@default.set(:environment, :test)
assert ! @default.show_exceptions?
@base.set(:environment, :production)
assert ! @base.show_exceptions?
end
it 'returns a friendly 500' do
mock_app {
disable :raise_errors
enable :show_exceptions
get '/' do
raise StandardError
end
}
get '/'
assert_equal 500, status
assert body.include?("StandardError")
assert body.include?("<code>show_exceptions</code> option")
end
end
describe_option 'dump_errors' do
it 'is disabled on Base' do
assert ! @base.dump_errors?
end
it 'is enabled on Default' do
assert @default.dump_errors?
end
it 'dumps exception with backtrace to rack.errors' do
Sinatra::Default.disable(:raise_errors)
mock_app(Sinatra::Default) {
error do
error = @env['rack.errors'].instance_variable_get(:@error)
error.rewind
error.read
end
get '/' do
raise
end
}
get '/'
assert body.include?("RuntimeError") && body.include?("options_test.rb")
end
end
describe_option 'sessions' do
it 'is disabled on Base' do
assert ! @base.sessions?
end
it 'is disabled on Default' do
assert ! @default.sessions?
end
# TODO: it 'uses Rack::Session::Cookie when enabled' do
end
describe_option 'logging' do
it 'is disabled on Base' do
assert ! @base.logging?
end
it 'is enabled on Default when not in test environment' do
assert @default.logging?
@default.set :environment, :test
assert ! @default.logging
end
# TODO: it 'uses Rack::CommonLogger when enabled' do
end
describe_option 'static' do
it 'is disabled on Base' do
assert ! @base.static?
end
it 'is enabled on Default' do
assert @default.static?
end
# TODO: it setup static routes if public is enabled
# TODO: however, that's already tested in static_test so...
end
describe_option 'host' do
it 'defaults to 0.0.0.0' do
assert_equal '0.0.0.0', @base.host
assert_equal '0.0.0.0', @default.host
end
end
describe_option 'port' do
it 'defaults to 4567' do
assert_equal 4567, @base.port
assert_equal 4567, @default.port
end
end
describe_option 'server' do
it 'is one of thin, mongrel, webrick' do
assert_equal %w[thin mongrel webrick], @base.server
assert_equal %w[thin mongrel webrick], @default.server
end
end
describe_option 'app_file' do
it 'is nil' do
assert @base.app_file.nil?
assert @default.app_file.nil?
end
end
describe_option 'root' do
it 'is nil if app_file is not set' do
assert @base.root.nil?
assert @default.root.nil?
end
it 'is equal to the expanded basename of app_file' do
@base.app_file = __FILE__
assert_equal File.expand_path(File.dirname(__FILE__)), @base.root
@default.app_file = __FILE__
assert_equal File.expand_path(File.dirname(__FILE__)), @default.root
end
end
describe_option 'views' do
it 'is nil if root is not set' do
assert @base.views.nil?
assert @default.views.nil?
end
it 'is set to root joined with views/' do
@base.root = File.dirname(__FILE__)
assert_equal File.dirname(__FILE__) + "/views", @base.views
@default.root = File.dirname(__FILE__)
assert_equal File.dirname(__FILE__) + "/views", @default.views
end
end
describe_option 'public' do
it 'is nil if root is not set' do
assert @base.public.nil?
assert @default.public.nil?
end
it 'is set to root joined with public/' do
@base.root = File.dirname(__FILE__)
assert_equal File.dirname(__FILE__) + "/public", @base.public
@default.root = File.dirname(__FILE__)
assert_equal File.dirname(__FILE__) + "/public", @default.public
end
end
describe_option 'lock' do
it 'is disabled by default' do
assert ! @base.lock?
end
end

View file

@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/helper'
describe 'Sinatra::Request' do
class RequestTest < Test::Unit::TestCase
it 'responds to #user_agent' do
request = Sinatra::Request.new({'HTTP_USER_AGENT' => 'Test'})
assert request.respond_to?(:user_agent)

View file

@ -2,8 +2,8 @@
require File.dirname(__FILE__) + '/helper'
describe 'Sinatra::Response' do
before do
class ResponseTest < Test::Unit::TestCase
setup do
@response = Sinatra::Response.new
end

View file

@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/helper'
describe 'Result Handling' do
class ResultTest < Test::Unit::TestCase
it "sets response.body when result is a String" do
mock_app {
get '/' do

View file

@ -8,9 +8,8 @@ module RouteAddedTest
end
end
describe "route_added Hook" do
before { RouteAddedTest.routes.clear }
class RouteAddedHookTest < Test::Unit::TestCase
setup { RouteAddedTest.routes.clear }
it "should be notified of an added route" do
mock_app(Class.new(Sinatra::Base)) {
@ -43,5 +42,4 @@ describe "route_added Hook" do
assert_equal [["GET", "/"], ["HEAD", "/"]],
RouteAddedTest.routes
end
end

View file

@ -5,7 +5,7 @@ def route_def(pattern)
mock_app { get(pattern) { } }
end
describe "Routing" do
class RoutingTest < Test::Unit::TestCase
%w[get put post delete].each do |verb|
it "defines #{verb.upcase} request handlers with #{verb}" do
mock_app {

View file

@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/helper'
describe "Sass Templates" do
class SassTest < Test::Unit::TestCase
def sass_app(&block)
mock_app {
set :views, File.dirname(__FILE__) + '/views'

View file

@ -14,8 +14,8 @@ class Rack::Handler::Mock
end
end
describe 'Sinatra::Base.run!' do
before do
class ServerTest < Test::Unit::TestCase
setup do
mock_app {
set :server, 'mock'
set :host, 'foo.local'
@ -24,7 +24,9 @@ describe 'Sinatra::Base.run!' do
$stdout = File.open('/dev/null', 'wb')
end
after { $stdout = STDOUT }
def teardown
$stdout = STDOUT
end
it "locates the appropriate Rack handler and calls ::run" do
@app.run!

View file

@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/helper'
describe 'Sinatra' do
class SinatraTest < Test::Unit::TestCase
it 'creates a new Sinatra::Base subclass on new' do
app =
Sinatra.new do

View file

@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/helper'
describe 'Static' do
before do
class StaticTest < Test::Unit::TestCase
setup do
mock_app {
set :static, true
set :public, File.dirname(__FILE__)

View file

@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/helper'
describe 'Templating' do
class TemplatesTest < Test::Unit::TestCase
def render_app(&block)
mock_app {
def render_test(template, data, options, locals, &block)

View file

@ -1,7 +1,7 @@
require 'yaml'
require File.dirname(__FILE__) + '/helper'
describe 'Sinatra::Test' do
class TestTest < Test::Unit::TestCase
def request
YAML.load(body)
end
@ -14,7 +14,7 @@ describe 'Sinatra::Test' do
YAML.load(request['test.params'])
end
before do
setup do
mock_app {
%w[get head post put delete].each { |verb|
send(verb, '/') do