sinatra/test/application_test.rb

176 lines
2.9 KiB
Ruby
Raw Normal View History

2007-11-28 05:55:05 +00:00
require File.dirname(__FILE__) + '/helper'
require 'uri'
2008-02-25 02:30:04 +00:00
class TesterWithEach
def each
yield 'foo'
yield 'bar'
yield 'baz'
end
end
context "Looking up a request" do
2007-11-28 05:55:05 +00:00
setup do
2008-02-25 02:30:04 +00:00
Sinatra.application = nil
2007-11-28 05:55:05 +00:00
end
specify "returns what's at the end" do
block = Proc.new { 'Hello' }
2008-02-25 02:30:04 +00:00
get '/', &block
result = Sinatra.application.lookup(
2008-03-05 00:30:06 +00:00
Rack::Request.new(
'REQUEST_METHOD' => 'GET',
'PATH_INFO' => '/'
)
2007-11-28 05:55:05 +00:00
)
result.should.not.be.nil
result.block.should.be block
2007-11-28 05:55:05 +00:00
end
specify "takes params in path" do
block = Proc.new { 'Hello' }
2008-02-25 02:30:04 +00:00
get '/:foo', &block
2007-11-28 05:55:05 +00:00
2008-02-25 02:30:04 +00:00
result = Sinatra.application.lookup(
2008-03-05 00:30:06 +00:00
Rack::Request.new(
'REQUEST_METHOD' => 'GET',
'PATH_INFO' => '/bar'
)
2007-11-28 05:55:05 +00:00
)
result.should.not.be.nil
result.block.should.be block
result.params.should.equal "foo" => 'bar'
2007-11-28 05:55:05 +00:00
end
2007-11-28 05:55:05 +00:00
end
2007-11-28 06:52:52 +00:00
2007-11-28 08:10:12 +00:00
context "An app returns" do
2007-11-28 06:52:52 +00:00
setup do
2008-02-25 02:30:04 +00:00
Sinatra.application = nil
2007-11-28 06:52:52 +00:00
end
2007-11-28 08:10:12 +00:00
2007-11-28 06:52:52 +00:00
specify "404 if no events found" do
request = Rack::MockRequest.new(@app)
2008-02-25 02:30:04 +00:00
get_it '/'
should.be.not_found
body.should.equal '<h1>Not Found</h1>'
2007-11-28 06:52:52 +00:00
end
specify "200 if success" do
2008-02-25 02:30:04 +00:00
get '/' do
2007-11-28 06:52:52 +00:00
'Hello World'
end
2008-02-25 02:30:04 +00:00
get_it '/'
should.be.ok
body.should.equal 'Hello World'
2007-11-28 06:52:52 +00:00
end
2007-11-28 08:10:12 +00:00
specify "an objects result from each if it has it" do
2008-02-25 02:30:04 +00:00
get '/' do
2007-11-28 08:10:12 +00:00
TesterWithEach.new
end
2008-02-25 02:30:04 +00:00
get_it '/'
should.be.ok
body.should.equal 'foobarbaz'
2007-11-28 08:10:12 +00:00
end
specify "the body set if set before the last" do
2008-02-25 02:30:04 +00:00
get '/' do
2007-11-28 21:25:14 +00:00
body 'Blake'
2007-11-28 08:10:12 +00:00
'Mizerany'
end
2008-02-25 02:30:04 +00:00
get_it '/'
should.be.ok
body.should.equal 'Blake'
2007-11-28 08:10:12 +00:00
end
end
context "Events in an app" do
setup do
2008-02-25 02:30:04 +00:00
Sinatra.application = nil
2007-11-28 08:10:12 +00:00
end
specify "evaluate in a clean context" do
2008-02-25 02:30:04 +00:00
helpers do
2007-11-28 07:24:13 +00:00
def foo
'foo'
end
end
2008-02-25 02:30:04 +00:00
get '/foo' do
2007-11-28 07:24:13 +00:00
foo
end
2008-02-25 02:30:04 +00:00
get_it '/foo'
should.be.ok
body.should.equal 'foo'
2007-11-28 07:24:13 +00:00
end
2007-11-28 08:10:12 +00:00
specify "get access to request, response, and params" do
2008-02-25 02:30:04 +00:00
get '/:foo' do
params["foo"] + params["bar"]
2007-11-28 07:47:48 +00:00
end
2008-02-25 02:30:04 +00:00
get_it '/foo?bar=baz'
should.be.ok
body.should.equal 'foobaz'
2007-11-28 07:47:48 +00:00
end
2008-01-12 01:01:00 +00:00
specify "can filters by agent" do
2008-02-25 02:30:04 +00:00
get '/', :agent => /Windows/ do
2008-01-12 01:01:00 +00:00
request.env['HTTP_USER_AGENT']
end
get_it '/', :env => { :agent => 'Windows' }
2008-02-25 02:30:04 +00:00
should.be.ok
body.should.equal 'Windows'
2008-01-12 01:01:00 +00:00
2008-02-25 02:30:04 +00:00
get_it '/', :agent => 'Mac'
should.not.be.ok
2008-01-12 01:01:00 +00:00
end
2008-03-25 01:04:51 +00:00
specify "can filters by agent" do
get '/', :agent => /Windows (NT)/ do
params[:agent].first
end
get_it '/', :env => { :agent => 'Windows NT' }
2008-03-25 01:04:51 +00:00
body.should.equal 'NT'
end
2008-01-12 01:01:00 +00:00
specify "can deal with spaces in paths" do
path = '/path with spaces'
get path do
"Look ma, a path with spaces!"
end
get_it URI.encode(path)
body.should.equal "Look ma, a path with spaces!"
end
2007-11-28 06:52:52 +00:00
end
2007-11-28 08:10:12 +00:00