require File.dirname(__FILE__) + '/helper' context "Sinatra" do setup do Sinatra.application = nil end specify "should handle result of nil" do get '/' do nil end get_it '/' should.be.ok body.should == '' end specify "handles events" do get '/:name' do 'Hello ' + params["name"] end get_it '/Blake' should.be.ok body.should.equal 'Hello Blake' end specify "handles splats" do get '/hi/*' do params["splat"].kind_of?(Array).should.equal true params["splat"].first end get_it '/hi/Blake' should.be.ok body.should.equal 'Blake' end specify "handles multiple splats" do get '/say/*/to/*' do params["splat"].join(' ') end get_it '/say/hello/to/world' should.be.ok body.should.equal 'hello world' end specify "allow empty splats" do get '/say/*/to*/*' do params["splat"].join(' ') end get_it '/say/hello/to/world' should.be.ok body.should.equal 'hello world' # second splat is empty get_it '/say/hello/tomy/world' should.be.ok body.should.equal 'hello my world' end 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 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 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 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 specify "body sets content and ends event" do Sinatra::EventContext.any_instance.expects(:foo).never get '/set_body' do stop 'Hello!' stop 'World!' foo end get_it '/set_body' should.be.ok body.should.equal 'Hello!' end specify "should set status then call helper with a var" do Sinatra::EventContext.any_instance.expects(:foo).once.with(1).returns('bah!') get '/set_body' do stop [404, [:foo, 1]] end get_it '/set_body' should.be.not_found body.should.equal 'bah!' end specify "should easily set response Content-Type" do get '/foo.html' do content_type 'text/html', :charset => 'utf-8' "

Hello, World

" end get_it '/foo.html' should.be.ok headers['Content-Type'].should.equal 'text/html;charset=utf-8' body.should.equal '

Hello, World

' get '/foo_test.xml' do content_type :xml "" end get_it '/foo_test.xml' should.be.ok headers['Content-Type'].should.equal 'application/xml' body.should.equal '' end 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' get_it '/maybe', :env => { 'HTTP_IF_MODIFIED_SINCE' => modified_at.httpdate } status.should.equal 304 body.should.equal '' end 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 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 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 # 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 # Ignore any _method parameters specified in GET requests or on the query string in POST requests. 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 end