Remove Sinatra::Test and all test framework helper libraries

This commit is contained in:
Ryan Tomayko 2009-06-05 22:00:15 -07:00
parent f14d621874
commit c1e5852dfb
6 changed files with 0 additions and 336 deletions

View File

@ -1,128 +0,0 @@
require 'sinatra/base'
warn 'Sinatra::Test is deprecated; use Rack::Test instead.'
module Sinatra
module Test
include Rack::Utils
def self.included(base)
Sinatra::Default.set(:environment, :test)
end
attr_reader :app, :request, :response
def self.deprecate(framework)
warn <<-EOF
Warning: support for the #{framework} testing framework is deprecated and
will be dropped in Sinatra 1.0. See <http://sinatra.github.com/testing.html>
for more information.
EOF
end
def make_request(verb, path, body=nil, options={})
@app = Sinatra::Application if @app.nil? && defined?(Sinatra::Application)
fail "@app not set - cannot make request" if @app.nil?
@request = Rack::MockRequest.new(@app)
options = { :lint => true }.merge(options || {})
case
when body.respond_to?(:to_hash)
options.merge! body.delete(:env) if body.key?(:env)
options[:input] = param_string(body)
when body.respond_to?(:to_str)
options[:input] = body
when body.nil?
options[:input] = ''
else
raise ArgumentError, "body must be a Hash, String, or nil"
end
yield @request if block_given?
@response = @request.request(verb, path, rack_options(options))
end
def get(path, *args, &b) ; make_request('GET', path, *args, &b) ; end
def head(path, *args, &b) ; make_request('HEAD', path, *args, &b) ; end
def post(path, *args, &b) ; make_request('POST', path, *args, &b) ; end
def put(path, *args, &b) ; make_request('PUT', path, *args, &b) ; end
def delete(path, *args, &b) ; make_request('DELETE', path, *args, &b) ; end
def follow!
make_request 'GET', @response.location
end
def body ; @response.body ; end
def status ; @response.status ; 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
private
RACK_OPTIONS = {
:accept => 'HTTP_ACCEPT',
:agent => 'HTTP_USER_AGENT',
:host => 'HTTP_HOST',
:session => 'rack.session',
:cookies => 'HTTP_COOKIE',
:content_type => 'CONTENT_TYPE'
}
def rack_options(opts)
opts.merge(:lint => true).inject({}) do |hash,(key,val)|
key = RACK_OPTIONS[key] || key
hash[key] = val
hash
end
end
def param_string(value, prefix = nil)
case value
when Array
value.map { |v|
param_string(v, "#{prefix}[]")
} * "&"
when Hash
value.map { |k, v|
param_string(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k))
} * "&"
else
"#{prefix}=#{escape(value)}"
end
end
if defined? Sinatra::Compat
# Deprecated. Use: "get" instead of "get_it".
%w(get head post put delete).each do |verb|
eval <<-RUBY, binding, __FILE__, __LINE__
def #{verb}_it(*args, &block)
sinatra_warn "The #{verb}_it method is deprecated; use #{verb} instead."
make_request('#{verb.upcase}', *args, &block)
end
RUBY
end
end
end
class TestHarness
include Test
def initialize(app=nil)
@app = app || Sinatra::Application
@app.set(:environment, :test)
end
end
end

View File

@ -1,19 +0,0 @@
require 'bacon'
require 'sinatra/test'
Sinatra::Test.deprecate('Bacon')
Sinatra::Default.set(
:environment => :test,
:run => false,
:raise_errors => true,
:logging => false
)
module Sinatra::Test
def should
@response.should
end
end
Bacon::Context.send(:include, Sinatra::Test)

View File

@ -1,13 +0,0 @@
require 'sinatra/test'
require 'sinatra/test/unit'
require 'spec'
require 'spec/interop/test'
Sinatra::Test.deprecate('RSpec')
Sinatra::Default.set(
:environment => :test,
:run => false,
:raise_errors => true,
:logging => false
)

View File

@ -1,11 +0,0 @@
require 'test/spec'
require 'sinatra/test'
require 'sinatra/test/unit'
Sinatra::Test.deprecate('test/spec')
module Sinatra::Test
def should
@response.should
end
end

View File

@ -1,13 +0,0 @@
require 'sinatra/test'
require 'test/unit'
Sinatra::Test.deprecate('test/unit')
Test::Unit::TestCase.send :include, Sinatra::Test
Sinatra::Default.set(
:environment => :test,
:run => false,
:raise_errors => true,
:logging => false
)

View File

@ -1,152 +0,0 @@
require File.dirname(__FILE__) + '/helper'
require 'yaml'
# silence deprecation warning when requiring sinatra/test
$VERBOSE, v = nil, $VERBOSE
require 'sinatra/test'
$VERBOSE = v
class TestTest < Test::Unit::TestCase
include Sinatra::Test
def request
YAML.load(body)
end
def request_body
request['test.body']
end
def request_params
YAML.load(request['test.params'])
end
setup do
mock_app {
%w[get head post put delete].each { |verb|
send(verb, '/') do
redirect '/redirected' if params[:redirect]
env.update('test.body' => request.body.read)
env.update('test.params' => params.to_yaml)
env.to_yaml
end
}
get '/redirected' do
"you've been redirected"
end
}
end
it 'allows GET/HEAD/POST/PUT/DELETE' do
get '/'
assert_equal('GET', request['REQUEST_METHOD'])
post '/'
assert_equal('POST', request['REQUEST_METHOD'])
put '/'
assert_equal('PUT', request['REQUEST_METHOD'])
delete '/'
assert_equal('DELETE', request['REQUEST_METHOD'])
head '/'
assert response.headers['Content-Length'].to_i > 0
assert_equal('', body)
end
it 'allows to specify a body' do
post '/', '42'
assert_equal '42', request_body
end
it 'allows to specify params' do
get '/', :foo => 'bar'
assert_equal 'bar', request_params['foo']
end
it 'supports nested params' do
get '/', :foo => { :x => 'y', :chunky => 'bacon' }
assert_equal "y", request_params['foo']['x']
assert_equal "bacon", request_params['foo']['chunky']
end
it 'provides easy access to response status and body' do
get '/'
assert_equal 200, status
assert body =~ /^---/
end
it 'delegates methods to @response' do
get '/'
assert ok?
end
it 'follows redirect' do
get '/', :redirect => true
follow!
assert_equal "you've been redirected", body
end
it 'provides sugar for common HTTP headers' do
get '/', :env => { :accept => 'text/plain' }
assert_equal 'text/plain', request['HTTP_ACCEPT']
get '/', :env => { :agent => 'TATFT' }
assert_equal 'TATFT', request['HTTP_USER_AGENT']
get '/', :env => { :host => '1.2.3.4' }
assert_equal '1.2.3.4', request['HTTP_HOST']
get '/', :env => { :session => {'foo' => 'bar'} }
assert_equal({'foo' => 'bar'}, request['rack.session'])
get '/', :env => { :cookies => 'foo' }
assert_equal 'foo', request['HTTP_COOKIE']
get '/', :env => { :content_type => 'text/plain' }
assert_equal 'text/plain', request['CONTENT_TYPE']
end
it 'allow to test session easily' do
app = mock_app(Sinatra::Default) {
get '/' do
session['foo'] = 'bar'
200
end
post '/' do
assert_equal 'bar', session['foo']
session['foo'] || "blah"
end
}
browser = Sinatra::TestHarness.new(app)
browser.get '/'
browser.post '/', {}, :session => { 'foo' => 'bar' }
assert_equal 'bar', browser.response.body
end
it 'yields the request object to the block before invoking the application' do
called = false
get '/' do |req|
called = true
assert req.kind_of?(Rack::MockRequest)
end
assert called
end
it 'sets the environment to :test on include' do
Sinatra::Default.set(:environment, :production)
Class.new { include Sinatra::Test }
assert_equal :test, Sinatra::Default.environment
end
def test_TestHarness
session = Sinatra::TestHarness.new(@app)
response = session.get('/')
assert_equal 200, response.status
end
end