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

Use Rack::Test instead of Sinatra::Test for tests

This commit is contained in:
Simon Rozet 2009-03-28 16:28:18 +01:00 committed by Ryan Tomayko
parent 7030e25066
commit 2fa9fd81bb
5 changed files with 60 additions and 48 deletions

View file

@ -14,7 +14,8 @@ libdir = File.dirname(File.dirname(__FILE__)) + '/lib'
$LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
require 'contest'
require 'sinatra/test'
require 'rack/test'
require 'sinatra/base'
class Sinatra::Base
# Allow assertions in request context
@ -24,24 +25,52 @@ end
Sinatra::Base.set :environment, :test
class Test::Unit::TestCase
include Sinatra::Test
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
end
# Do not output warnings for the duration of the block.
def silence_warnings
$VERBOSE, v = nil, $VERBOSE
yield
ensure
$VERBOSE = v
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

View file

@ -178,7 +178,7 @@ class HelpersTest < Test::Unit::TestCase
end
}
get '/', :env => { 'rack.session' => { :foo => 'bar' } }
get '/', {}, { 'rack.session' => { :foo => 'bar' } }
assert_equal 'bar', body
end
@ -373,7 +373,7 @@ class HelpersTest < Test::Unit::TestCase
end
it 'halts when a conditional GET matches' do
get '/', :env => { 'HTTP_IF_MODIFIED_SINCE' => @now.httpdate }
get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => @now.httpdate }
assert_equal 304, status
assert_equal '', body
end
@ -402,13 +402,13 @@ class HelpersTest < Test::Unit::TestCase
end
it 'halts when a conditional GET matches' do
get '/', :env => { 'HTTP_IF_NONE_MATCH' => '"FOO"' }
get '/', {}, { '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", *' }
get '/', {}, { 'HTTP_IF_NONE_MATCH' => '"BAR", *' }
assert_equal 304, status
assert_equal '', body
end

View file

@ -51,18 +51,6 @@ class MappedErrorTest < Test::Unit::TestCase
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

View file

@ -258,17 +258,7 @@ class RoutingTest < Test::Unit::TestCase
end
it "supports deeply nested params" do
input = {
'browser[chrome][engine][name]' => 'V8',
'browser[chrome][engine][version]' => '1.0',
'browser[firefox][engine][name]' => 'spidermonkey',
'browser[firefox][engine][version]' => '1.7.0',
'emacs[map][goto-line]' => 'M-g g',
'emacs[version]' => '22.3.1',
'paste[name]' => 'hello world',
'paste[syntax]' => 'ruby'
}
expected = {
expected_params = {
"emacs" => {
"map" => { "goto-line" => "M-g g" },
"version" => "22.3.1"
@ -281,11 +271,11 @@ class RoutingTest < Test::Unit::TestCase
}
mock_app {
get '/foo' do
assert_equal expected, params
assert_equal expected_params, params
'looks good'
end
}
get "/foo?#{build_query(input)}"
get '/foo', expected_params
assert ok?
assert_equal 'looks good', body
end
@ -368,8 +358,9 @@ class RoutingTest < Test::Unit::TestCase
end
it 'raises a TypeError when pattern is not a String or Regexp' do
@app = mock_app
assert_raise(TypeError) { @app.get(42){} }
assert_raise(TypeError) {
mock_app { get(42){} }
}
end
it "returns response immediately on halt" do
@ -494,7 +485,7 @@ class RoutingTest < Test::Unit::TestCase
get '/foo'
assert not_found?
get '/foo', :env => { 'HTTP_HOST' => 'example.com' }
get '/foo', {}, { 'HTTP_HOST' => 'example.com' }
assert_equal 200, status
assert_equal 'Hello World', body
end
@ -509,7 +500,7 @@ class RoutingTest < Test::Unit::TestCase
get '/foo'
assert not_found?
get '/foo', :env => { 'HTTP_USER_AGENT' => 'Foo Bar' }
get '/foo', {}, { 'HTTP_USER_AGENT' => 'Foo Bar' }
assert_equal 200, status
assert_equal 'Hello World', body
end
@ -521,7 +512,7 @@ class RoutingTest < Test::Unit::TestCase
'Hello ' + params[:agent].first
end
}
get '/foo', :env => { 'HTTP_USER_AGENT' => 'Foo Bar' }
get '/foo', {}, { 'HTTP_USER_AGENT' => 'Foo Bar' }
assert_equal 200, status
assert_equal 'Hello Bar', body
end
@ -533,12 +524,12 @@ class RoutingTest < Test::Unit::TestCase
end
}
get '/', :env => { :accept => 'application/xml' }
get '/', {}, { 'HTTP_ACCEPT' => 'application/xml' }
assert ok?
assert_equal 'application/xml', body
assert_equal 'application/xml', response.headers['Content-Type']
get '/', :env => { :accept => 'text/html' }
get '/', {}, { :accept => 'text/html' }
assert !ok?
end
@ -552,7 +543,7 @@ class RoutingTest < Test::Unit::TestCase
}
types.each do |type|
get '/', :env => { :accept => type }
get '/', {}, { 'HTTP_ACCEPT' => type }
assert ok?
assert_equal type, body
assert_equal type, response.headers['Content-Type']

View file

@ -1,7 +1,11 @@
require 'yaml'
require File.dirname(__FILE__) + '/helper'
require 'yaml'
require 'sinatra/test'
class TestTest < Test::Unit::TestCase
include Sinatra::Test
def request
YAML.load(body)
end
@ -45,8 +49,8 @@ class TestTest < Test::Unit::TestCase
assert_equal('DELETE', request['REQUEST_METHOD'])
head '/'
assert_equal('', response.body)
assert response.headers['Content-Length'].to_i > 0
assert_equal('', body)
end
it 'allows to specify a body' do