1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/compat/app_test.rb

301 lines
6 KiB
Ruby
Raw Normal View History

2007-11-28 05:55:19 -05:00
require File.dirname(__FILE__) + '/helper'
context "Sinatra" do
2007-11-28 05:55:19 -05:00
setup do
Sinatra.application = nil
end
specify "should put all DSL methods on (main)" do
object = Object.new
Sinatra::Application::FORWARD_METHODS.each do |method|
object.private_methods.should.include(method)
end
end
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"]
2007-11-28 05:55:19 -05:00
end
2007-11-28 05:55:19 -05:00
get_it '/Blake'
2007-11-28 05:55:19 -05:00
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
2007-11-28 05:55:19 -05:00
specify "follows redirects" do
get '/' do
redirect '/blake'
end
2007-11-28 05:55:19 -05:00
get '/blake' do
'Mizerany'
end
2007-11-28 05:55:19 -05:00
get_it '/'
should.be.redirection
body.should.equal ''
2007-11-28 05:55:19 -05:00
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
I knew I shoulda taken that left turn at Hoboken This is a fairly large reworking of Sinatra's innards. Although most of the internal implementation has been modified, it provides the same basic feature set and is meant to be compatible with Sinatra 0.3.2. * The Event and EventContext classes have been removed. Sinatra applications are now defined within the class context of a Sinatra::Base subclass; each request is processed within a new instance. * Sinatra::Base can be used as a base class for multiple Rack applications within a single process and can be used as Rack middleware. * The routing and result type processing implementation has been simplified and enhanced a bit. There's a new route conditions system for things like :agent/:host matching and a request level #pass method has been added to allow an event handler to exit immediately, passing control to the next matching route. * Regular expressions may now be used in route patterns. Captures are available as an array from "params[:captures]". * The #body helper method now takes a block. The block is not evaluated until an attempt is made to read the body. * Options are now dynamically generated class attributes on the Sinatra::Base subclass (instead of OpenStruct); options are inherited by subclasses and may be overridden up the inheritance hierarchy. The Base.set manages all option related stuff. * The application file (app_file) detection heuristics are bit more sane now. This fixes some bugs with reloading and public/views directory detection. All thin / passenger issues of these type should be better now. * Error mappings are now split into to distinct layers: exception mappings and custom error pages. Exception mappings are registered with 'error(Exception)' and are run only when the app raises an exception. Custom error pages are registered with error(status_code) and are run any time the response has the status code specified. It's also possible to register an error page for a range of status codes: 'error(500..599)'. * The spec and unit testing extensions have been modified to take advantage of the ability to have multiple Sinatra applications. The Sinatra::Test module must be included within the TestCase in order to take advantage of these methods (unless the 'sinatra/compat' library has been required). * Rebuilt specs from scratch for better coverage and organization. Sinatra 3.2 unit tests have been retained under ./compat to ensure a baseline level of compatibility with previous versions; use the 'rake compat' task to run these. A large number of existing Sinatra idioms have been deprecated but continue to be supported through the 'sinatra/compat' library. * The "set_option" and "set_options" methods have been deprecated due to redundancy; use "set". * The "env" option (Sinatra::Base.env) has been renamed to "environment" and deprecated because it's too easy to confuse with the request-level Rack environment Hash (Sinatra::Base#env). * The request level "stop" method has been renamed "halt" and deprecated. This is for consistency with `throw :halt`. * The request level "entity_tag" method has been renamed "etag" and deprecated. Both versions were previously supported. * The request level "headers" method has been deprecated. Use response['Header-Name'] to access and modify response headers. * Sinatra.application is deprecated. Use Sinatra::Application instead. * Setting Sinatra.application = nil to reset an application is deprecated. You shouldn't have to reset objects anymore. * The Sinatra.default_options Hash is deprecated. Modifying this object now results in "set(key, value)" invocations on the Sinatra::Base subclass. * The "body.to_result" convention has been deprecated. * The ServerError exception has been deprecated. Any Exception is now considered a ServerError.
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
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 "stop sets content and ends event" do
2007-11-28 16:25:14 -05:00
get '/set_body' do
stop 'Hello!'
stop 'World!'
I knew I shoulda taken that left turn at Hoboken This is a fairly large reworking of Sinatra's innards. Although most of the internal implementation has been modified, it provides the same basic feature set and is meant to be compatible with Sinatra 0.3.2. * The Event and EventContext classes have been removed. Sinatra applications are now defined within the class context of a Sinatra::Base subclass; each request is processed within a new instance. * Sinatra::Base can be used as a base class for multiple Rack applications within a single process and can be used as Rack middleware. * The routing and result type processing implementation has been simplified and enhanced a bit. There's a new route conditions system for things like :agent/:host matching and a request level #pass method has been added to allow an event handler to exit immediately, passing control to the next matching route. * Regular expressions may now be used in route patterns. Captures are available as an array from "params[:captures]". * The #body helper method now takes a block. The block is not evaluated until an attempt is made to read the body. * Options are now dynamically generated class attributes on the Sinatra::Base subclass (instead of OpenStruct); options are inherited by subclasses and may be overridden up the inheritance hierarchy. The Base.set manages all option related stuff. * The application file (app_file) detection heuristics are bit more sane now. This fixes some bugs with reloading and public/views directory detection. All thin / passenger issues of these type should be better now. * Error mappings are now split into to distinct layers: exception mappings and custom error pages. Exception mappings are registered with 'error(Exception)' and are run only when the app raises an exception. Custom error pages are registered with error(status_code) and are run any time the response has the status code specified. It's also possible to register an error page for a range of status codes: 'error(500..599)'. * The spec and unit testing extensions have been modified to take advantage of the ability to have multiple Sinatra applications. The Sinatra::Test module must be included within the TestCase in order to take advantage of these methods (unless the 'sinatra/compat' library has been required). * Rebuilt specs from scratch for better coverage and organization. Sinatra 3.2 unit tests have been retained under ./compat to ensure a baseline level of compatibility with previous versions; use the 'rake compat' task to run these. A large number of existing Sinatra idioms have been deprecated but continue to be supported through the 'sinatra/compat' library. * The "set_option" and "set_options" methods have been deprecated due to redundancy; use "set". * The "env" option (Sinatra::Base.env) has been renamed to "environment" and deprecated because it's too easy to confuse with the request-level Rack environment Hash (Sinatra::Base#env). * The request level "stop" method has been renamed "halt" and deprecated. This is for consistency with `throw :halt`. * The request level "entity_tag" method has been renamed "etag" and deprecated. Both versions were previously supported. * The request level "headers" method has been deprecated. Use response['Header-Name'] to access and modify response headers. * Sinatra.application is deprecated. Use Sinatra::Application instead. * Setting Sinatra.application = nil to reset an application is deprecated. You shouldn't have to reset objects anymore. * The Sinatra.default_options Hash is deprecated. Modifying this object now results in "set(key, value)" invocations on the Sinatra::Base subclass. * The "body.to_result" convention has been deprecated. * The ServerError exception has been deprecated. Any Exception is now considered a ServerError.
2008-12-13 16:06:02 -05:00
fail 'stop should have halted'
2007-11-28 16:25:14 -05:00
end
2007-11-28 16:25:14 -05:00
get_it '/set_body'
2007-11-28 16:25:14 -05:00
should.be.ok
body.should.equal 'Hello!'
2007-11-28 16:25:14 -05:00
end
I knew I shoulda taken that left turn at Hoboken This is a fairly large reworking of Sinatra's innards. Although most of the internal implementation has been modified, it provides the same basic feature set and is meant to be compatible with Sinatra 0.3.2. * The Event and EventContext classes have been removed. Sinatra applications are now defined within the class context of a Sinatra::Base subclass; each request is processed within a new instance. * Sinatra::Base can be used as a base class for multiple Rack applications within a single process and can be used as Rack middleware. * The routing and result type processing implementation has been simplified and enhanced a bit. There's a new route conditions system for things like :agent/:host matching and a request level #pass method has been added to allow an event handler to exit immediately, passing control to the next matching route. * Regular expressions may now be used in route patterns. Captures are available as an array from "params[:captures]". * The #body helper method now takes a block. The block is not evaluated until an attempt is made to read the body. * Options are now dynamically generated class attributes on the Sinatra::Base subclass (instead of OpenStruct); options are inherited by subclasses and may be overridden up the inheritance hierarchy. The Base.set manages all option related stuff. * The application file (app_file) detection heuristics are bit more sane now. This fixes some bugs with reloading and public/views directory detection. All thin / passenger issues of these type should be better now. * Error mappings are now split into to distinct layers: exception mappings and custom error pages. Exception mappings are registered with 'error(Exception)' and are run only when the app raises an exception. Custom error pages are registered with error(status_code) and are run any time the response has the status code specified. It's also possible to register an error page for a range of status codes: 'error(500..599)'. * The spec and unit testing extensions have been modified to take advantage of the ability to have multiple Sinatra applications. The Sinatra::Test module must be included within the TestCase in order to take advantage of these methods (unless the 'sinatra/compat' library has been required). * Rebuilt specs from scratch for better coverage and organization. Sinatra 3.2 unit tests have been retained under ./compat to ensure a baseline level of compatibility with previous versions; use the 'rake compat' task to run these. A large number of existing Sinatra idioms have been deprecated but continue to be supported through the 'sinatra/compat' library. * The "set_option" and "set_options" methods have been deprecated due to redundancy; use "set". * The "env" option (Sinatra::Base.env) has been renamed to "environment" and deprecated because it's too easy to confuse with the request-level Rack environment Hash (Sinatra::Base#env). * The request level "stop" method has been renamed "halt" and deprecated. This is for consistency with `throw :halt`. * The request level "entity_tag" method has been renamed "etag" and deprecated. Both versions were previously supported. * The request level "headers" method has been deprecated. Use response['Header-Name'] to access and modify response headers. * Sinatra.application is deprecated. Use Sinatra::Application instead. * Setting Sinatra.application = nil to reset an application is deprecated. You shouldn't have to reset objects anymore. * The Sinatra.default_options Hash is deprecated. Modifying this object now results in "set(key, value)" invocations on the Sinatra::Base subclass. * The "body.to_result" convention has been deprecated. * The ServerError exception has been deprecated. Any Exception is now considered a ServerError.
2008-12-13 16:06:02 -05:00
# Deprecated. WTF was going on here? What's the 1 in [:foo, 1] do?
xspecify "should set status then call helper with a var" do
helpers do
def foo
'bah!'
end
end
2008-03-24 20:35:42 -04:00
get '/set_body' do
stop [404, [:foo, 1]]
end
2008-03-24 20:35:42 -04:00
get_it '/set_body'
2008-03-24 20:35:42 -04:00
should.be.not_found
body.should.equal 'bah!'
2008-03-24 20:35:42 -04:00
end
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>'
get '/foo_test.xml' do
content_type :xml
"<feed></feed>"
end
get_it '/foo_test.xml'
should.be.ok
headers['Content-Type'].should.equal 'application/xml'
body.should.equal '<feed></feed>'
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'
2008-04-15 22:17:05 -04:00
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 "supports PUT" do
2008-03-04 19:30:06 -05:00
put '/' do
'puted'
end
put_it '/'
2008-03-04 19:30:06 -05:00
assert_equal 'puted', body
end
specify "rewrites POSTs with _method param to PUT" do
2008-03-04 19:30:06 -05:00
put '/' do
'puted'
end
post_it '/', :_method => 'PUT'
2008-03-04 19:30:06 -05:00
assert_equal 'puted', body
end
specify "rewrites POSTs with lowercase _method param to PUT" do
put '/' do
'puted'
end
post_it '/', :_method => 'put'
body.should.equal 'puted'
end
specify "does not rewrite GETs with _method param to PUT" do
get '/' do
'getted'
end
get_it '/', :_method => 'put'
should.be.ok
body.should.equal 'getted'
end
specify "ignores _method query string parameter on non-POST requests" do
post '/' do
'posted'
end
put '/' do
'booo'
end
post_it "/?_method=PUT"
should.be.ok
body.should.equal 'posted'
end
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