2007-11-28 05:55:19 -05:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
|
|
|
|
|
|
|
context "Sinatra" do
|
|
|
|
|
|
|
|
setup do
|
|
|
|
Sinatra.application = nil
|
|
|
|
end
|
|
|
|
|
2008-02-19 22:51:39 -05:00
|
|
|
specify "should handle result of nil" do
|
|
|
|
get '/' do
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
get_it '/'
|
|
|
|
should.be.ok
|
|
|
|
body.should == ''
|
|
|
|
end
|
|
|
|
|
2007-11-28 05:55:19 -05:00
|
|
|
specify "handles events" do
|
|
|
|
get '/:name' do
|
|
|
|
'Hello ' + params[:name]
|
|
|
|
end
|
|
|
|
|
|
|
|
get_it '/Blake'
|
|
|
|
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'Hello Blake'
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "follows redirects" do
|
|
|
|
get '/' do
|
|
|
|
redirect '/blake'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/blake' do
|
|
|
|
'Mizerany'
|
|
|
|
end
|
|
|
|
|
|
|
|
get_it '/'
|
|
|
|
should.be.redirection
|
|
|
|
body.should.equal ''
|
|
|
|
|
|
|
|
follow!
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'Mizerany'
|
|
|
|
end
|
|
|
|
|
2008-02-21 02:46:29 -05:00
|
|
|
specify "renders a body with a redirect" do
|
|
|
|
Sinatra::EventContext.any_instance.expects(:foo).returns('blah')
|
|
|
|
get "/" do
|
|
|
|
redirect 'foo', :foo
|
|
|
|
end
|
|
|
|
get_it '/'
|
|
|
|
should.be.redirection
|
|
|
|
headers['Location'].should.equal 'foo'
|
|
|
|
body.should.equal 'blah'
|
|
|
|
end
|
|
|
|
|
2007-11-28 16:25:14 -05:00
|
|
|
specify "body sets content and ends event" do
|
|
|
|
|
|
|
|
Sinatra::EventContext.any_instance.expects(:foo).never
|
|
|
|
|
|
|
|
get '/set_body' do
|
2007-11-29 00:28:59 -05:00
|
|
|
stop 'Hello!'
|
|
|
|
stop 'World!'
|
2007-11-28 16:25:14 -05:00
|
|
|
foo
|
|
|
|
end
|
|
|
|
|
|
|
|
get_it '/set_body'
|
|
|
|
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'Hello!'
|
|
|
|
|
|
|
|
end
|
2008-03-10 03:31:19 -04:00
|
|
|
|
|
|
|
specify "delegates HEAD requests to GET handlers" do
|
|
|
|
get '/invisible' do
|
|
|
|
"I am invisible to the world"
|
|
|
|
end
|
|
|
|
|
|
|
|
head_it '/invisible'
|
|
|
|
should.be.ok
|
|
|
|
body.should.not.equal "I am invisible to the world"
|
|
|
|
body.should.equal ''
|
|
|
|
end
|
|
|
|
|
2008-03-04 19:30:06 -05:00
|
|
|
|
|
|
|
specify "put'n with POST" do
|
|
|
|
put '/' do
|
|
|
|
'puted'
|
|
|
|
end
|
|
|
|
post_it '/', :_method => 'PUT'
|
|
|
|
assert_equal 'puted', body
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "put'n wth PUT" do
|
|
|
|
put '/' do
|
|
|
|
'puted'
|
|
|
|
end
|
|
|
|
put_it '/'
|
|
|
|
assert_equal 'puted', body
|
|
|
|
end
|
2008-03-12 10:23:09 -04:00
|
|
|
|
|
|
|
# Some Ajax libraries downcase the _method parameter value. Make
|
|
|
|
# sure we can handle that.
|
|
|
|
specify "put'n with POST and lowercase _method param" do
|
|
|
|
put '/' do
|
|
|
|
'puted'
|
|
|
|
end
|
|
|
|
post_it '/', :_method => 'put'
|
|
|
|
body.should.equal 'puted'
|
|
|
|
end
|
|
|
|
|
2008-03-10 03:31:19 -04:00
|
|
|
# Ignore any _method parameters specified in GET requests or on the query string in POST requests.
|
2008-03-12 10:23:09 -04:00
|
|
|
specify "not put'n with GET" do
|
|
|
|
get '/' do
|
|
|
|
'getted'
|
|
|
|
end
|
|
|
|
get_it '/', :_method => 'put'
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'getted'
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "_method query string parameter ignored on POST" do
|
|
|
|
post '/' do
|
|
|
|
'posted'
|
|
|
|
end
|
|
|
|
put '/' do
|
|
|
|
'booo'
|
|
|
|
end
|
|
|
|
post_it "/?_method=PUT"
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'posted'
|
|
|
|
end
|
|
|
|
|
2007-11-28 05:55:19 -05:00
|
|
|
end
|