2007-11-28 00:55:05 -05:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
|
|
|
|
2007-11-28 01:31:31 -05:00
|
|
|
context "Looking up a request" do
|
2007-11-28 00:55:05 -05:00
|
|
|
|
|
|
|
setup do
|
|
|
|
@app = Sinatra::Application.new
|
|
|
|
end
|
|
|
|
|
2007-11-28 01:31:31 -05:00
|
|
|
specify "returns what's at the end" do
|
|
|
|
block = Proc.new { 'Hello' }
|
|
|
|
@app.define_event(:get, '/', &block)
|
2007-11-28 00:55:05 -05:00
|
|
|
|
|
|
|
result = @app.lookup(
|
|
|
|
'REQUEST_METHOD' => 'GET',
|
|
|
|
'PATH_INFO' => '/'
|
|
|
|
)
|
|
|
|
|
|
|
|
result.should.not.be.nil
|
2007-11-28 01:31:31 -05:00
|
|
|
result.block.should.be block
|
2007-11-28 00:55:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
specify "takes params in path" do
|
2007-11-28 01:31:31 -05:00
|
|
|
block = Proc.new { 'Hello' }
|
|
|
|
@app.define_event(:get, '/:foo', &block)
|
2007-11-28 00:55:05 -05:00
|
|
|
|
|
|
|
result = @app.lookup(
|
|
|
|
'REQUEST_METHOD' => 'GET',
|
|
|
|
'PATH_INFO' => '/bar'
|
|
|
|
)
|
|
|
|
|
|
|
|
result.should.not.be.nil
|
2007-11-28 01:31:31 -05:00
|
|
|
result.block.should.be block
|
2007-11-28 00:55:05 -05:00
|
|
|
result.params.should.equal :foo => 'bar'
|
|
|
|
end
|
2007-11-28 01:31:31 -05:00
|
|
|
|
2007-11-28 00:55:05 -05:00
|
|
|
end
|
2007-11-28 01:52:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
context "Calling an app" do
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@app = Sinatra::Application.new
|
|
|
|
end
|
|
|
|
|
|
|
|
# - 404 if no events found
|
|
|
|
|
|
|
|
specify "404 if no events found" do
|
|
|
|
request = Rack::MockRequest.new(@app)
|
|
|
|
result = request.get('/')
|
|
|
|
result.should.be.not_found
|
|
|
|
result.body.should.equal 'Not Found'
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "200 if success" do
|
|
|
|
@app.define_event(:get, '/') do
|
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
|
|
|
|
request = Rack::MockRequest.new(@app)
|
|
|
|
result = request.get('/')
|
|
|
|
result.should.be.ok
|
|
|
|
result.body.should.equal 'Hello World'
|
|
|
|
end
|
|
|
|
|
2007-11-28 02:24:13 -05:00
|
|
|
specify "evaluates events in a clean context" do
|
|
|
|
Sinatra::EventContext.class_eval do
|
|
|
|
def foo
|
|
|
|
'foo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@app.define_event(:get, '/foo') do
|
|
|
|
foo
|
|
|
|
end
|
|
|
|
|
|
|
|
request = Rack::MockRequest.new(@app)
|
|
|
|
result = request.get('/foo')
|
|
|
|
result.should.be.ok
|
|
|
|
result.body.should.equal 'foo'
|
|
|
|
end
|
|
|
|
|
2007-11-28 02:47:48 -05:00
|
|
|
specify "gives the event access to request, response, and params" do
|
|
|
|
@app.define_event(: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'
|
|
|
|
end
|
|
|
|
|
2007-11-28 01:52:52 -05:00
|
|
|
end
|