test/routing_test.rb in sinatra-sinatra-0.9.0.4 vs test/routing_test.rb in sinatra-sinatra-0.9.0.5
- old
+ new
@@ -482,6 +482,172 @@
assert ok?
assert_equal type, body
assert_equal type, response.headers['Content-Type']
end
end
+
+ it 'degrades gracefully when optional accept header is not provided' do
+ mock_app {
+ get '/', :provides => :xml do
+ request.env['HTTP_ACCEPT']
+ end
+ get '/' do
+ 'default'
+ end
+ }
+ get '/'
+ assert ok?
+ assert_equal 'default', body
+ end
+
+ it 'passes a single url param as block parameters when one param is specified' do
+ mock_app {
+ get '/:foo' do |foo|
+ assert_equal 'bar', foo
+ end
+ }
+
+ get '/bar'
+ assert ok?
+ end
+
+ it 'passes multiple params as block parameters when many are specified' do
+ mock_app {
+ get '/:foo/:bar/:baz' do |foo, bar, baz|
+ assert_equal 'abc', foo
+ assert_equal 'def', bar
+ assert_equal 'ghi', baz
+ end
+ }
+
+ get '/abc/def/ghi'
+ assert ok?
+ end
+
+ it 'passes regular expression captures as block parameters' do
+ mock_app {
+ get(/^\/fo(.*)\/ba(.*)/) do |foo, bar|
+ assert_equal 'orooomma', foo
+ assert_equal 'f', bar
+ 'looks good'
+ end
+ }
+
+ get '/foorooomma/baf'
+ assert ok?
+ assert_equal 'looks good', body
+ end
+
+ it "supports mixing multiple splat params like /*/foo/*/* as block parameters" do
+ mock_app {
+ get '/*/foo/*/*' do |foo, bar, baz|
+ assert_equal 'bar', foo
+ assert_equal 'bling', bar
+ assert_equal 'baz/boom', baz
+ 'looks good'
+ end
+ }
+
+ get '/bar/foo/bling/baz/boom'
+ assert ok?
+ assert_equal 'looks good', body
+ end
+
+ it 'raises an ArgumentError with block arity > 1 and too many values' do
+ mock_app {
+ get '/:foo/:bar/:baz' do |foo, bar|
+ 'quux'
+ end
+ }
+
+ assert_raise(ArgumentError) { get '/a/b/c' }
+ end
+
+ it 'raises an ArgumentError with block param arity > 1 and too few values' do
+ mock_app {
+ get '/:foo/:bar' do |foo, bar, baz|
+ 'quux'
+ end
+ }
+
+ assert_raise(ArgumentError) { get '/a/b' }
+ end
+
+ it 'succeeds if no block parameters are specified' do
+ mock_app {
+ get '/:foo/:bar' do
+ 'quux'
+ end
+ }
+
+ get '/a/b'
+ assert ok?
+ assert_equal 'quux', body
+ end
+
+ it 'passes all params with block param arity -1 (splat args)' do
+ mock_app {
+ get '/:foo/:bar' do |*args|
+ args.join
+ end
+ }
+
+ get '/a/b'
+ assert ok?
+ assert_equal 'ab', body
+ end
+
+ # NOTE Block params behaves differently under 1.8 and 1.9. Under 1.8, block
+ # param arity is lax: declaring a mismatched number of block params results
+ # in a warning. Under 1.9, block param arity is strict: mismatched block
+ # arity raises an ArgumentError.
+
+ if RUBY_VERSION >= '1.9'
+
+ it 'raises an ArgumentError with block param arity 1 and no values' do
+ mock_app {
+ get '/foo' do |foo|
+ 'quux'
+ end
+ }
+
+ assert_raise(ArgumentError) { get '/foo' }
+ end
+
+ it 'raises an ArgumentError with block param arity 1 and too many values' do
+ mock_app {
+ get '/:foo/:bar/:baz' do |foo|
+ 'quux'
+ end
+ }
+
+ assert_raise(ArgumentError) { get '/a/b/c' }
+ end
+
+ else
+
+ it 'does not raise an ArgumentError with block param arity 1 and no values' do
+ mock_app {
+ get '/foo' do |foo|
+ 'quux'
+ end
+ }
+
+ silence_warnings { get '/foo' }
+ assert ok?
+ assert_equal 'quux', body
+ end
+
+ it 'does not raise an ArgumentError with block param arity 1 and too many values' do
+ mock_app {
+ get '/:foo/:bar/:baz' do |foo|
+ 'quux'
+ end
+ }
+
+ silence_warnings { get '/a/b/c' }
+ assert ok?
+ assert_equal 'quux', body
+ end
+
+ end
end