2007-11-28 05:55:19 -05:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
|
|
|
|
|
|
|
context "Sinatra" do
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 05:55:19 -05:00
|
|
|
setup do
|
|
|
|
Sinatra.application = nil
|
|
|
|
end
|
2008-04-14 16:34:10 -04:00
|
|
|
|
|
|
|
specify "should put all DSL methods on (main)" do
|
|
|
|
object = Object.new
|
2009-01-26 00:37:38 -05:00
|
|
|
methods = %w[get put post head delete configure template helpers set]
|
|
|
|
methods.each do |method|
|
2009-01-30 20:49:47 -05:00
|
|
|
object.private_methods.map { |m| m.to_sym }.should.include(method.to_sym)
|
2008-04-14 16:34:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-02-19 22:51:39 -05:00
|
|
|
specify "should handle result of nil" do
|
|
|
|
get '/' do
|
|
|
|
nil
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-02-19 22:51:39 -05:00
|
|
|
get_it '/'
|
|
|
|
should.be.ok
|
|
|
|
body.should == ''
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 05:55:19 -05:00
|
|
|
specify "handles events" do
|
|
|
|
get '/:name' do
|
2008-03-31 21:48:06 -04:00
|
|
|
'Hello ' + params["name"]
|
2007-11-28 05:55:19 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 05:55:19 -05:00
|
|
|
get_it '/Blake'
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 05:55:19 -05:00
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'Hello Blake'
|
|
|
|
end
|
2008-04-13 05:17:39 -04:00
|
|
|
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-24 22:24:31 -04:00
|
|
|
specify "handles splats" do
|
|
|
|
get '/hi/*' do
|
|
|
|
params["splat"].kind_of?(Array).should.equal true
|
|
|
|
params["splat"].first
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-24 22:24:31 -04:00
|
|
|
get_it '/hi/Blake'
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-24 22:24:31 -04:00
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'Blake'
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "handles multiple splats" do
|
|
|
|
get '/say/*/to/*' do
|
|
|
|
params["splat"].join(' ')
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-24 22:24:31 -04:00
|
|
|
get_it '/say/hello/to/world'
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-24 22:24:31 -04:00
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'hello world'
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "allow empty splats" do
|
|
|
|
get '/say/*/to*/*' do
|
|
|
|
params["splat"].join(' ')
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-24 22:24:31 -04:00
|
|
|
get_it '/say/hello/to/world'
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-24 22:24:31 -04:00
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'hello world' # second splat is empty
|
|
|
|
|
|
|
|
get_it '/say/hello/tomy/world'
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-24 22:24:31 -04:00
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'hello my world'
|
|
|
|
end
|
|
|
|
|
2008-04-13 05:17:39 -04:00
|
|
|
specify "gives access to underlying response header Hash" do
|
|
|
|
get '/' do
|
|
|
|
header['X-Test'] = 'Is this thing on?'
|
|
|
|
headers 'X-Test2' => 'Foo', 'X-Test3' => 'Bar'
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
|
|
|
get_it '/'
|
|
|
|
should.be.ok
|
|
|
|
headers.should.include 'X-Test'
|
|
|
|
headers['X-Test'].should.equal 'Is this thing on?'
|
|
|
|
headers.should.include 'X-Test3'
|
|
|
|
headers['X-Test3'].should.equal 'Bar'
|
|
|
|
end
|
|
|
|
|
2007-11-28 05:55:19 -05:00
|
|
|
specify "follows redirects" do
|
|
|
|
get '/' do
|
|
|
|
redirect '/blake'
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 05:55:19 -05:00
|
|
|
get '/blake' do
|
|
|
|
'Mizerany'
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 05:55:19 -05:00
|
|
|
get_it '/'
|
|
|
|
should.be.redirection
|
|
|
|
body.should.equal ''
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 05:55:19 -05:00
|
|
|
follow!
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'Mizerany'
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-02-21 02:46:29 -05:00
|
|
|
specify "renders a body with a redirect" do
|
2008-12-13 16:06:02 -05:00
|
|
|
helpers do
|
|
|
|
def foo ; 'blah' ; end
|
|
|
|
end
|
2008-02-21 02:46:29 -05:00
|
|
|
get "/" do
|
|
|
|
redirect 'foo', :foo
|
|
|
|
end
|
|
|
|
get_it '/'
|
|
|
|
should.be.redirection
|
|
|
|
headers['Location'].should.equal 'foo'
|
|
|
|
body.should.equal 'blah'
|
|
|
|
end
|
2008-04-13 05:17:39 -04:00
|
|
|
|
|
|
|
specify "redirects permanently with 301 status code" do
|
|
|
|
get "/" do
|
|
|
|
redirect 'foo', 301
|
|
|
|
end
|
|
|
|
get_it '/'
|
|
|
|
should.be.redirection
|
|
|
|
headers['Location'].should.equal 'foo'
|
|
|
|
status.should.equal 301
|
|
|
|
body.should.be.empty
|
|
|
|
end
|
|
|
|
|
2008-09-07 09:23:02 -04:00
|
|
|
specify "stop sets content and ends event" do
|
2007-11-28 16:25:14 -05:00
|
|
|
get '/set_body' do
|
2007-11-29 00:28:59 -05:00
|
|
|
stop 'Hello!'
|
|
|
|
stop 'World!'
|
2008-12-13 16:06:02 -05:00
|
|
|
fail 'stop should have halted'
|
2007-11-28 16:25:14 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 16:25:14 -05:00
|
|
|
get_it '/set_body'
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 16:25:14 -05:00
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'Hello!'
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 16:25:14 -05:00
|
|
|
end
|
2008-03-10 03:31:19 -04:00
|
|
|
|
2008-04-13 05:32:23 -04:00
|
|
|
specify "should easily set response Content-Type" do
|
|
|
|
get '/foo.html' do
|
|
|
|
content_type 'text/html', :charset => 'utf-8'
|
|
|
|
"<h1>Hello, World</h1>"
|
|
|
|
end
|
|
|
|
|
|
|
|
get_it '/foo.html'
|
|
|
|
should.be.ok
|
|
|
|
headers['Content-Type'].should.equal 'text/html;charset=utf-8'
|
|
|
|
body.should.equal '<h1>Hello, World</h1>'
|
|
|
|
|
2008-04-15 22:14:31 -04:00
|
|
|
get '/foo_test.xml' do
|
2008-04-13 05:32:23 -04:00
|
|
|
content_type :xml
|
|
|
|
"<feed></feed>"
|
|
|
|
end
|
|
|
|
|
2008-04-15 22:14:31 -04:00
|
|
|
get_it '/foo_test.xml'
|
2008-04-13 05:32:23 -04:00
|
|
|
should.be.ok
|
|
|
|
headers['Content-Type'].should.equal 'application/xml'
|
|
|
|
body.should.equal '<feed></feed>'
|
|
|
|
end
|
|
|
|
|
2008-03-31 09:10:40 -04:00
|
|
|
specify "supports conditional GETs with last_modified" do
|
|
|
|
modified_at = Time.now
|
|
|
|
get '/maybe' do
|
|
|
|
last_modified modified_at
|
|
|
|
'response body, maybe'
|
|
|
|
end
|
|
|
|
|
|
|
|
get_it '/maybe'
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'response body, maybe'
|
|
|
|
|
2008-04-15 22:17:05 -04:00
|
|
|
get_it '/maybe', :env => { 'HTTP_IF_MODIFIED_SINCE' => modified_at.httpdate }
|
2008-03-31 09:10:40 -04:00
|
|
|
status.should.equal 304
|
|
|
|
body.should.equal ''
|
|
|
|
end
|
|
|
|
|
2008-04-13 05:07:15 -04:00
|
|
|
specify "supports conditional GETs with entity_tag" do
|
|
|
|
get '/strong' do
|
|
|
|
entity_tag 'FOO'
|
|
|
|
'foo response'
|
|
|
|
end
|
|
|
|
|
|
|
|
get_it '/strong'
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'foo response'
|
|
|
|
|
|
|
|
get_it '/strong', {},
|
|
|
|
'HTTP_IF_NONE_MATCH' => '"BAR"'
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'foo response'
|
|
|
|
|
|
|
|
get_it '/strong', {},
|
|
|
|
'HTTP_IF_NONE_MATCH' => '"FOO"'
|
|
|
|
status.should.equal 304
|
|
|
|
body.should.equal ''
|
|
|
|
|
|
|
|
get_it '/strong', {},
|
|
|
|
'HTTP_IF_NONE_MATCH' => '"BAR", *'
|
|
|
|
status.should.equal 304
|
|
|
|
body.should.equal ''
|
|
|
|
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-08-31 03:39:26 -04:00
|
|
|
|
2008-09-07 09:23:02 -04:00
|
|
|
specify "supports PUT" do
|
2008-03-04 19:30:06 -05:00
|
|
|
put '/' do
|
|
|
|
'puted'
|
|
|
|
end
|
2008-09-07 09:23:02 -04:00
|
|
|
put_it '/'
|
2008-03-04 19:30:06 -05:00
|
|
|
assert_equal 'puted', body
|
|
|
|
end
|
|
|
|
|
2008-09-07 09:23:02 -04:00
|
|
|
specify "rewrites POSTs with _method param to PUT" do
|
2008-03-04 19:30:06 -05:00
|
|
|
put '/' do
|
|
|
|
'puted'
|
|
|
|
end
|
2008-09-07 09:23:02 -04:00
|
|
|
post_it '/', :_method => 'PUT'
|
2008-03-04 19:30:06 -05:00
|
|
|
assert_equal 'puted', body
|
|
|
|
end
|
2008-03-12 10:23:09 -04:00
|
|
|
|
2008-09-07 09:23:02 -04:00
|
|
|
specify "rewrites POSTs with lowercase _method param to PUT" do
|
2008-03-12 10:23:09 -04:00
|
|
|
put '/' do
|
|
|
|
'puted'
|
|
|
|
end
|
|
|
|
post_it '/', :_method => 'put'
|
|
|
|
body.should.equal 'puted'
|
|
|
|
end
|
|
|
|
|
2008-09-07 09:23:02 -04:00
|
|
|
specify "does not rewrite GETs with _method param to PUT" do
|
2008-03-12 10:23:09 -04:00
|
|
|
get '/' do
|
|
|
|
'getted'
|
|
|
|
end
|
|
|
|
get_it '/', :_method => 'put'
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'getted'
|
|
|
|
end
|
|
|
|
|
2008-09-07 09:23:02 -04:00
|
|
|
specify "ignores _method query string parameter on non-POST requests" do
|
2008-03-12 10:23:09 -04:00
|
|
|
post '/' do
|
|
|
|
'posted'
|
|
|
|
end
|
|
|
|
put '/' do
|
|
|
|
'booo'
|
|
|
|
end
|
|
|
|
post_it "/?_method=PUT"
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'posted'
|
|
|
|
end
|
|
|
|
|
2008-09-07 09:23:02 -04:00
|
|
|
specify "does not read body if content type is not url encoded" do
|
|
|
|
post '/foo.xml' do
|
|
|
|
request.env['CONTENT_TYPE'].should.be == 'application/xml'
|
|
|
|
request.content_type.should.be == 'application/xml'
|
|
|
|
request.body.read
|
|
|
|
end
|
|
|
|
|
|
|
|
post_it '/foo.xml', '<foo></foo>', :content_type => 'application/xml'
|
|
|
|
@response.should.be.ok
|
|
|
|
@response.body.should.be == '<foo></foo>'
|
|
|
|
end
|
|
|
|
|
2007-11-28 05:55:19 -05:00
|
|
|
end
|