2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2008-09-07 08:02:10 -04:00
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
class BeforeFilterTest < Minitest::Test
|
2008-12-13 16:06:02 -05:00
|
|
|
it "executes filters in the order defined" do
|
|
|
|
count = 0
|
|
|
|
mock_app do
|
|
|
|
get('/') { 'Hello World' }
|
2012-05-21 17:21:59 -04:00
|
|
|
before do
|
2009-01-15 07:04:10 -05:00
|
|
|
assert_equal 0, count
|
2008-12-13 16:06:02 -05:00
|
|
|
count = 1
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
|
|
|
before do
|
2009-01-15 07:04:10 -05:00
|
|
|
assert_equal 1, count
|
2008-12-13 16:06:02 -05:00
|
|
|
count = 2
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2008-09-07 08:02:10 -04:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
get '/'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 2, count
|
|
|
|
assert_equal 'Hello World', body
|
2008-09-07 08:02:10 -04:00
|
|
|
end
|
|
|
|
|
2008-12-21 21:36:14 -05:00
|
|
|
it "can modify the request" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2008-12-13 16:06:02 -05:00
|
|
|
get('/foo') { 'foo' }
|
|
|
|
get('/bar') { 'bar' }
|
|
|
|
before { request.path_info = '/bar' }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
get '/foo'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'bar', body
|
2008-09-07 08:02:10 -04:00
|
|
|
end
|
2009-01-18 07:00:55 -05:00
|
|
|
|
2009-01-18 08:37:36 -05:00
|
|
|
it "can modify instance variables available to routes" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-01-18 08:37:36 -05:00
|
|
|
before { @foo = 'bar' }
|
|
|
|
get('/foo') { @foo }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2009-01-18 08:37:36 -05:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'bar', body
|
|
|
|
end
|
|
|
|
|
2008-12-21 21:36:14 -05:00
|
|
|
it "allows redirects" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-01-18 07:00:55 -05:00
|
|
|
before { redirect '/bar' }
|
2009-01-18 08:37:36 -05:00
|
|
|
get('/foo') do
|
|
|
|
fail 'before block should have halted processing'
|
|
|
|
'ORLY?!'
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2009-01-18 07:00:55 -05:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert redirect?
|
2010-08-30 05:00:30 -04:00
|
|
|
assert_equal 'http://example.org/bar', response['Location']
|
2009-01-18 08:37:36 -05:00
|
|
|
assert_equal '', body
|
2009-01-18 07:00:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not modify the response with its return value" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-01-18 07:00:55 -05:00
|
|
|
before { 'Hello World!' }
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo') do
|
2009-01-18 07:00:55 -05:00
|
|
|
assert_equal [], response.body
|
|
|
|
'cool'
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2009-01-18 07:00:55 -05:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'cool', body
|
|
|
|
end
|
2009-01-24 23:33:06 -05:00
|
|
|
|
2009-01-25 02:41:09 -05:00
|
|
|
it "does modify the response with halt" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-01-25 02:41:09 -05:00
|
|
|
before { halt 302, 'Hi' }
|
|
|
|
get '/foo' do
|
|
|
|
"should not happen"
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2009-01-25 02:41:09 -05:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert_equal 302, response.status
|
|
|
|
assert_equal 'Hi', body
|
|
|
|
end
|
|
|
|
|
2009-01-24 23:33:06 -05:00
|
|
|
it "gives you access to params" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-01-24 23:33:06 -05:00
|
|
|
before { @foo = params['foo'] }
|
|
|
|
get('/foo') { @foo }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2009-01-24 23:33:06 -05:00
|
|
|
|
|
|
|
get '/foo?foo=cool'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'cool', body
|
|
|
|
end
|
2009-03-25 13:55:16 -04:00
|
|
|
|
2011-09-22 18:35:44 -04:00
|
|
|
it "properly unescapes parameters" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2011-09-22 18:35:44 -04:00
|
|
|
before { @foo = params['foo'] }
|
|
|
|
get('/foo') { @foo }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2011-09-22 18:35:44 -04:00
|
|
|
|
|
|
|
get '/foo?foo=bar%3Abaz%2Fbend'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'bar:baz/bend', body
|
|
|
|
end
|
|
|
|
|
2009-03-25 13:55:16 -04:00
|
|
|
it "runs filters defined in superclasses" do
|
|
|
|
base = Class.new(Sinatra::Base)
|
|
|
|
base.before { @foo = 'hello from superclass' }
|
|
|
|
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app(base) { get('/foo') { @foo } }
|
2009-03-25 13:55:16 -04:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert_equal 'hello from superclass', body
|
|
|
|
end
|
2010-01-28 11:58:19 -05:00
|
|
|
|
|
|
|
it 'does not run before filter when serving static files' do
|
|
|
|
ran_filter = false
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2010-01-28 11:58:19 -05:00
|
|
|
before { ran_filter = true }
|
|
|
|
set :static, true
|
2020-03-13 17:20:04 -04:00
|
|
|
set :public_folder, __dir__
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2010-01-28 11:58:19 -05:00
|
|
|
get "/#{File.basename(__FILE__)}"
|
|
|
|
assert ok?
|
|
|
|
assert_equal File.read(__FILE__), body
|
|
|
|
assert !ran_filter
|
|
|
|
end
|
2010-04-27 17:11:43 -04:00
|
|
|
|
|
|
|
it 'takes an optional route pattern' do
|
|
|
|
ran_filter = false
|
|
|
|
mock_app do
|
|
|
|
before("/b*") { ran_filter = true }
|
|
|
|
get('/foo') { }
|
|
|
|
get('/bar') { }
|
|
|
|
end
|
|
|
|
get '/foo'
|
|
|
|
assert !ran_filter
|
|
|
|
get '/bar'
|
|
|
|
assert ran_filter
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'generates block arguments from route pattern' do
|
|
|
|
subpath = nil
|
|
|
|
mock_app do
|
|
|
|
before("/foo/:sub") { |s| subpath = s }
|
|
|
|
get('/foo/*') { }
|
|
|
|
end
|
|
|
|
get '/foo/bar'
|
|
|
|
assert_equal subpath, 'bar'
|
|
|
|
end
|
2013-03-13 14:53:44 -04:00
|
|
|
|
|
|
|
it 'can catch exceptions in before filters and handle them properly' do
|
|
|
|
doodle = ''
|
|
|
|
mock_app do
|
|
|
|
before do
|
|
|
|
doodle += 'This begins'
|
|
|
|
raise StandardError, "before"
|
|
|
|
end
|
|
|
|
get "/" do
|
|
|
|
doodle = 'and runs'
|
|
|
|
end
|
|
|
|
error 500 do
|
|
|
|
"Error handled #{env['sinatra.error'].message}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
doodle = ''
|
|
|
|
get '/'
|
|
|
|
assert_equal 'Error handled before', body
|
|
|
|
assert_equal 'This begins', doodle
|
|
|
|
end
|
2008-09-07 08:02:10 -04:00
|
|
|
end
|
2008-12-21 21:36:14 -05:00
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
class AfterFilterTest < Minitest::Test
|
2012-05-29 15:50:05 -04:00
|
|
|
it "executes before and after filters in correct order" do
|
2008-12-21 21:36:14 -05:00
|
|
|
invoked = 0
|
|
|
|
mock_app do
|
|
|
|
before { invoked = 2 }
|
2012-05-29 15:50:05 -04:00
|
|
|
get('/') { invoked += 2; 'hello' }
|
2008-12-21 21:36:14 -05:00
|
|
|
after { invoked *= 2 }
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
|
|
|
|
assert_equal 8, invoked
|
|
|
|
end
|
|
|
|
|
|
|
|
it "executes filters in the order defined" do
|
|
|
|
count = 0
|
|
|
|
mock_app do
|
|
|
|
get('/') { 'Hello World' }
|
2012-05-21 17:21:59 -04:00
|
|
|
after do
|
2008-12-21 21:36:14 -05:00
|
|
|
assert_equal 0, count
|
|
|
|
count = 1
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
|
|
|
after do
|
2008-12-21 21:36:14 -05:00
|
|
|
assert_equal 1, count
|
|
|
|
count = 2
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-21 21:36:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 2, count
|
|
|
|
assert_equal 'Hello World', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows redirects" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2008-12-21 21:36:14 -05:00
|
|
|
get('/foo') { 'ORLY' }
|
|
|
|
after { redirect '/bar' }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-21 21:36:14 -05:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert redirect?
|
2010-08-30 05:00:30 -04:00
|
|
|
assert_equal 'http://example.org/bar', response['Location']
|
2008-12-21 21:36:14 -05:00
|
|
|
assert_equal '', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not modify the response with its return value" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2008-12-21 21:36:14 -05:00
|
|
|
get('/foo') { 'cool' }
|
|
|
|
after { 'Hello World!' }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-21 21:36:14 -05:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'cool', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does modify the response with halt" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2008-12-21 21:36:14 -05:00
|
|
|
get '/foo' do
|
|
|
|
"should not be returned"
|
|
|
|
end
|
|
|
|
after { halt 302, 'Hi' }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-21 21:36:14 -05:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert_equal 302, response.status
|
|
|
|
assert_equal 'Hi', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it "runs filters defined in superclasses" do
|
|
|
|
count = 2
|
|
|
|
base = Class.new(Sinatra::Base)
|
|
|
|
base.after { count *= 2 }
|
2011-06-04 12:00:23 -04:00
|
|
|
mock_app(base) do
|
|
|
|
get('/foo') do
|
|
|
|
count += 2
|
|
|
|
"ok"
|
|
|
|
end
|
|
|
|
end
|
2008-12-21 21:36:14 -05:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert_equal 8, count
|
|
|
|
end
|
2010-01-28 11:58:19 -05:00
|
|
|
|
2020-10-03 11:31:05 -04:00
|
|
|
it "respects content type set in superclass filter" do
|
|
|
|
base = Class.new(Sinatra::Base)
|
|
|
|
base.before { content_type :json }
|
|
|
|
mock_app(base) do
|
|
|
|
get('/foo'){ {foo: :bar}.to_json }
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert_equal 'application/json', response.headers['Content-Type']
|
|
|
|
end
|
|
|
|
|
2010-01-28 11:58:19 -05:00
|
|
|
it 'does not run after filter when serving static files' do
|
|
|
|
ran_filter = false
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2010-01-28 11:58:19 -05:00
|
|
|
after { ran_filter = true }
|
|
|
|
set :static, true
|
2020-03-13 17:20:04 -04:00
|
|
|
set :public_folder, __dir__
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2010-01-28 11:58:19 -05:00
|
|
|
get "/#{File.basename(__FILE__)}"
|
|
|
|
assert ok?
|
|
|
|
assert_equal File.read(__FILE__), body
|
|
|
|
assert !ran_filter
|
|
|
|
end
|
2010-04-27 17:11:43 -04:00
|
|
|
|
|
|
|
it 'takes an optional route pattern' do
|
|
|
|
ran_filter = false
|
|
|
|
mock_app do
|
|
|
|
after("/b*") { ran_filter = true }
|
|
|
|
get('/foo') { }
|
|
|
|
get('/bar') { }
|
|
|
|
end
|
|
|
|
get '/foo'
|
|
|
|
assert !ran_filter
|
|
|
|
get '/bar'
|
|
|
|
assert ran_filter
|
|
|
|
end
|
|
|
|
|
2013-03-14 14:20:51 -04:00
|
|
|
it 'changes to path_info from a pattern matching before filter are respected when routing' do
|
2010-11-04 15:08:24 -04:00
|
|
|
mock_app do
|
|
|
|
before('/foo') { request.path_info = '/bar' }
|
|
|
|
get('/bar') { 'blah' }
|
|
|
|
end
|
|
|
|
get '/foo'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'blah', body
|
|
|
|
end
|
|
|
|
|
2010-04-27 17:11:43 -04:00
|
|
|
it 'generates block arguments from route pattern' do
|
|
|
|
subpath = nil
|
|
|
|
mock_app do
|
|
|
|
after("/foo/:sub") { |s| subpath = s }
|
|
|
|
get('/foo/*') { }
|
|
|
|
end
|
|
|
|
get '/foo/bar'
|
|
|
|
assert_equal subpath, 'bar'
|
|
|
|
end
|
2010-11-03 08:51:52 -04:00
|
|
|
|
2010-09-03 17:19:20 -04:00
|
|
|
it 'is possible to access url params from the route param' do
|
|
|
|
ran = false
|
|
|
|
mock_app do
|
|
|
|
get('/foo/*') { }
|
|
|
|
before('/foo/:sub') do
|
|
|
|
assert_equal params[:sub], 'bar'
|
|
|
|
ran = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
get '/foo/bar'
|
|
|
|
assert ran
|
|
|
|
end
|
2010-11-03 08:51:52 -04:00
|
|
|
|
|
|
|
it 'is possible to apply host_name conditions to before filters with no path' do
|
|
|
|
ran = false
|
|
|
|
mock_app do
|
|
|
|
before(:host_name => 'example.com') { ran = true }
|
|
|
|
get('/') { 'welcome' }
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_HOST' => 'example.org' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_HOST' => 'example.com' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert ran
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is possible to apply host_name conditions to before filters with a path' do
|
|
|
|
ran = false
|
|
|
|
mock_app do
|
|
|
|
before('/foo', :host_name => 'example.com') { ran = true }
|
|
|
|
get('/') { 'welcome' }
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_HOST' => 'example.com' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_HOST' => 'example.org' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_HOST' => 'example.com' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert ran
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is possible to apply host_name conditions to after filters with no path' do
|
|
|
|
ran = false
|
|
|
|
mock_app do
|
|
|
|
after(:host_name => 'example.com') { ran = true }
|
|
|
|
get('/') { 'welcome' }
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_HOST' => 'example.org' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_HOST' => 'example.com' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert ran
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is possible to apply host_name conditions to after filters with a path' do
|
|
|
|
ran = false
|
|
|
|
mock_app do
|
|
|
|
after('/foo', :host_name => 'example.com') { ran = true }
|
|
|
|
get('/') { 'welcome' }
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_HOST' => 'example.com' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_HOST' => 'example.org' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_HOST' => 'example.com' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert ran
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is possible to apply user_agent conditions to before filters with no path' do
|
|
|
|
ran = false
|
|
|
|
mock_app do
|
|
|
|
before(:user_agent => /foo/) { ran = true }
|
|
|
|
get('/') { 'welcome' }
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_USER_AGENT' => 'bar' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_USER_AGENT' => 'foo' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert ran
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is possible to apply user_agent conditions to before filters with a path' do
|
|
|
|
ran = false
|
|
|
|
mock_app do
|
|
|
|
before('/foo', :user_agent => /foo/) { ran = true }
|
|
|
|
get('/') { 'welcome' }
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_USER_AGENT' => 'foo' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_USER_AGENT' => 'bar' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_USER_AGENT' => 'foo' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert ran
|
|
|
|
end
|
|
|
|
|
2011-10-30 16:00:12 -04:00
|
|
|
it 'can add params' do
|
|
|
|
mock_app do
|
|
|
|
before { params['foo'] = 'bar' }
|
|
|
|
get('/') { params['foo'] }
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert_body 'bar'
|
|
|
|
end
|
|
|
|
|
2019-10-26 03:56:47 -04:00
|
|
|
it 'can add params on a single path' do
|
|
|
|
mock_app do
|
|
|
|
before('/hi'){ params['foo'] = 'bar' }
|
|
|
|
get('/hi') { params['foo'] }
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/hi'
|
|
|
|
assert_body 'bar'
|
|
|
|
end
|
|
|
|
|
|
|
|
# ref: issue #1567
|
|
|
|
it 'can add params on named parameters path' do
|
|
|
|
mock_app do
|
|
|
|
before('/:id/hi'){ params['foo'] = 'bar' }
|
|
|
|
get('/:id/hi') { params['foo'] }
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/:id/hi'
|
|
|
|
assert_body 'bar'
|
|
|
|
end
|
|
|
|
|
2011-10-30 16:00:12 -04:00
|
|
|
it 'can remove params' do
|
|
|
|
mock_app do
|
|
|
|
before { params.delete('foo') }
|
|
|
|
get('/') { params['foo'].to_s }
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/?foo=bar'
|
|
|
|
assert_body ''
|
|
|
|
end
|
|
|
|
|
2010-11-03 08:51:52 -04:00
|
|
|
it 'is possible to apply user_agent conditions to after filters with no path' do
|
|
|
|
ran = false
|
|
|
|
mock_app do
|
|
|
|
after(:user_agent => /foo/) { ran = true }
|
|
|
|
get('/') { 'welcome' }
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_USER_AGENT' => 'bar' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_USER_AGENT' => 'foo' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert ran
|
|
|
|
end
|
|
|
|
|
2012-05-29 15:56:26 -04:00
|
|
|
it 'is possible to apply user_agent conditions to after filters with a path' do
|
2010-11-03 08:51:52 -04:00
|
|
|
ran = false
|
|
|
|
mock_app do
|
|
|
|
after('/foo', :user_agent => /foo/) { ran = true }
|
|
|
|
get('/') { 'welcome' }
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_USER_AGENT' => 'foo' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_USER_AGENT' => 'bar' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_USER_AGENT' => 'foo' })
|
2010-11-03 08:51:52 -04:00
|
|
|
assert ran
|
|
|
|
end
|
2012-01-02 11:07:45 -05:00
|
|
|
|
2013-07-21 15:40:50 -04:00
|
|
|
it 'only triggers provides condition if conforms with current Content-Type' do
|
2012-01-02 11:07:45 -05:00
|
|
|
mock_app do
|
|
|
|
before(:provides => :txt) { @type = 'txt' }
|
|
|
|
before(:provides => :html) { @type = 'html' }
|
|
|
|
get('/') { @type }
|
|
|
|
end
|
|
|
|
|
2013-01-28 12:43:30 -05:00
|
|
|
get('/', {}, { 'HTTP_ACCEPT' => '*/*' })
|
2012-01-02 11:07:45 -05:00
|
|
|
assert_body 'txt'
|
|
|
|
end
|
2013-03-13 14:39:12 -04:00
|
|
|
|
|
|
|
it 'can catch exceptions in after filters and handle them properly' do
|
|
|
|
doodle = ''
|
|
|
|
mock_app do
|
|
|
|
after do
|
|
|
|
doodle += ' and after'
|
|
|
|
raise StandardError, "after"
|
|
|
|
end
|
2013-03-13 14:53:44 -04:00
|
|
|
get "/foo" do
|
2013-03-13 14:39:12 -04:00
|
|
|
doodle = 'Been now'
|
|
|
|
raise StandardError, "now"
|
|
|
|
end
|
2013-03-13 14:53:44 -04:00
|
|
|
get "/" do
|
2013-03-13 14:39:12 -04:00
|
|
|
doodle = 'Been now'
|
|
|
|
end
|
|
|
|
error 500 do
|
2013-03-13 14:53:44 -04:00
|
|
|
"Error handled #{env['sinatra.error'].message}"
|
2013-03-13 14:39:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/foo'
|
2013-03-13 14:53:44 -04:00
|
|
|
assert_equal 'Error handled now', body
|
|
|
|
assert_equal 'Been now and after', doodle
|
2013-03-13 14:39:12 -04:00
|
|
|
|
|
|
|
doodle = ''
|
|
|
|
get '/'
|
2013-03-13 14:53:44 -04:00
|
|
|
assert_equal 'Error handled after', body
|
2013-03-13 14:39:12 -04:00
|
|
|
assert_equal 'Been now and after', doodle
|
|
|
|
end
|
2008-12-21 21:36:14 -05:00
|
|
|
end
|