FIX: sloppy. run setup! for tests!

This commit is contained in:
Blake Mizerany 2008-02-24 18:30:04 -08:00
parent 5a61c3be45
commit 59199dcc13
2 changed files with 53 additions and 58 deletions

View File

@ -34,7 +34,11 @@ module Sinatra
Result = Struct.new(:block, :params, :status) unless defined?(Result)
def application
@app ||= Application.new
unless @app
@app = Application.new
Sinatra::Environment.setup!
end
@app
end
def application=(app)
@ -780,7 +784,6 @@ end
at_exit do
raise $! if $!
if Sinatra.application.options.run
Sinatra::Environment.setup!
Sinatra.run
end
end

View File

@ -1,16 +1,24 @@
require File.dirname(__FILE__) + '/helper'
class TesterWithEach
def each
yield 'foo'
yield 'bar'
yield 'baz'
end
end
context "Looking up a request" do
setup do
@app = Sinatra::Application.new
Sinatra.application = nil
end
specify "returns what's at the end" do
block = Proc.new { 'Hello' }
@app.define_event(:get, '/', &block)
result = @app.lookup(
get '/', &block
result = Sinatra.application.lookup(
'REQUEST_METHOD' => 'GET',
'PATH_INFO' => '/'
)
@ -21,9 +29,9 @@ context "Looking up a request" do
specify "takes params in path" do
block = Proc.new { 'Hello' }
@app.define_event(:get, '/:foo', &block)
get '/:foo', &block
result = @app.lookup(
result = Sinatra.application.lookup(
'REQUEST_METHOD' => 'GET',
'PATH_INFO' => '/bar'
)
@ -38,60 +46,48 @@ end
context "An app returns" do
setup do
@app = Sinatra::Application.new
Sinatra.application = nil
end
specify "404 if no events found" do
request = Rack::MockRequest.new(@app)
result = request.get('/')
result.should.be.not_found
result.body.should.equal '<h1>Not Found</h1>'
get_it '/'
should.be.not_found
body.should.equal '<h1>Not Found</h1>'
end
specify "200 if success" do
@app.define_event(:get, '/') do
get '/' do
'Hello World'
end
request = Rack::MockRequest.new(@app)
result = request.get('/')
result.should.be.ok
result.body.should.equal 'Hello World'
get_it '/'
should.be.ok
body.should.equal 'Hello World'
end
specify "an objects result from each if it has it" do
class TesterWithEach
def each
yield 'foo'
yield 'bar'
yield 'baz'
end
end
@app.define_event(:get, '/') do
get '/' do
TesterWithEach.new
end
request = Rack::MockRequest.new(@app)
result = request.get('/')
result.should.be.ok
result.body.should.equal 'foobarbaz'
get_it '/'
should.be.ok
body.should.equal 'foobarbaz'
end
specify "the body set if set before the last" do
@app.define_event(:get, '/') do
get '/' do
body 'Blake'
'Mizerany'
end
request = Rack::MockRequest.new(@app)
result = request.get('/')
result.should.be.ok
result.body.should.equal 'Blake'
get_it '/'
should.be.ok
body.should.equal 'Blake'
end
end
@ -99,51 +95,47 @@ end
context "Events in an app" do
setup do
@app = Sinatra::Application.new
Sinatra.application = nil
end
specify "evaluate in a clean context" do
Sinatra::EventContext.class_eval do
helpers do
def foo
'foo'
end
end
@app.define_event(:get, '/foo') do
get '/foo' do
foo
end
request = Rack::MockRequest.new(@app)
result = request.get('/foo')
result.should.be.ok
result.body.should.equal 'foo'
get_it '/foo'
should.be.ok
body.should.equal 'foo'
end
specify "get access to request, response, and params" do
@app.define_event(:get, '/:foo') do
get '/:foo' do
params[:foo] + params[:bar]
end
request = Rack::MockRequest.new(@app)
result = request.get('/foo?bar=baz')
result.should.be.ok
result.body.should.equal 'foobaz'
get_it '/foo?bar=baz'
should.be.ok
body.should.equal 'foobaz'
end
specify "can filters by agent" do
@app.define_event(:get, '/', :agent => /Windows/) do
get '/', :agent => /Windows/ do
request.env['HTTP_USER_AGENT']
end
request = Rack::MockRequest.new(@app)
result = request.get('/', :agent => 'Windows')
result.should.be.ok
result.body.should.equal 'Windows'
get_it '/', :agent => 'Windows'
should.be.ok
body.should.equal 'Windows'
request = Rack::MockRequest.new(@app)
result = request.get('/', :agent => 'Mac')
result.should.not.be.ok
get_it '/', :agent => 'Mac'
should.not.be.ok
end