2011-05-11 09:44:02 +02:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2008-09-07 05:02:10 -07:00
|
|
|
|
2015-01-11 01:00:47 +05:30
|
|
|
class BeforeFilterTest < Minitest::Test
|
2008-12-13 13:06:02 -08: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 04:04:10 -08:00
|
|
|
assert_equal 0, count
|
2008-12-13 13:06:02 -08:00
|
|
|
count = 1
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
|
|
|
before do
|
2009-01-15 04:04:10 -08:00
|
|
|
assert_equal 1, count
|
2008-12-13 13:06:02 -08:00
|
|
|
count = 2
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-13 13:06:02 -08:00
|
|
|
end
|
2008-09-07 05:02:10 -07:00
|
|
|
|
2008-12-13 13:06:02 -08:00
|
|
|
get '/'
|
2009-01-14 14:00:26 -08:00
|
|
|
assert ok?
|
|
|
|
assert_equal 2, count
|
|
|
|
assert_equal 'Hello World', body
|
2008-09-07 05:02:10 -07:00
|
|
|
end
|
|
|
|
|
2008-12-21 18:36:14 -08:00
|
|
|
it "can modify the request" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2008-12-13 13:06:02 -08:00
|
|
|
get('/foo') { 'foo' }
|
|
|
|
get('/bar') { 'bar' }
|
|
|
|
before { request.path_info = '/bar' }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-13 13:06:02 -08:00
|
|
|
|
|
|
|
get '/foo'
|
2009-01-14 14:00:26 -08:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'bar', body
|
2008-09-07 05:02:10 -07:00
|
|
|
end
|
2009-01-18 04:00:55 -08:00
|
|
|
|
2009-01-18 05:37:36 -08:00
|
|
|
it "can modify instance variables available to routes" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-01-18 05:37:36 -08:00
|
|
|
before { @foo = 'bar' }
|
|
|
|
get('/foo') { @foo }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2009-01-18 05:37:36 -08:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'bar', body
|
|
|
|
end
|
|
|
|
|
2008-12-21 18:36:14 -08:00
|
|
|
it "allows redirects" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-01-18 04:00:55 -08:00
|
|
|
before { redirect '/bar' }
|
2009-01-18 05:37:36 -08: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 04:00:55 -08:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert redirect?
|
2010-08-30 11:00:30 +02:00
|
|
|
assert_equal 'http://example.org/bar', response['Location']
|
2009-01-18 05:37:36 -08:00
|
|
|
assert_equal '', body
|
2009-01-18 04:00:55 -08: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 04:00:55 -08:00
|
|
|
before { 'Hello World!' }
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo') do
|
2009-01-18 04:00:55 -08:00
|
|
|
assert_equal [], response.body
|
|
|
|
'cool'
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2009-01-18 04:00:55 -08:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'cool', body
|
|
|
|
end
|
2009-01-24 20:33:06 -08:00
|
|
|
|
2009-01-24 23:41:09 -08:00
|
|
|
it "does modify the response with halt" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-01-24 23:41:09 -08:00
|
|
|
before { halt 302, 'Hi' }
|
|
|
|
get '/foo' do
|
|
|
|
"should not happen"
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2009-01-24 23:41:09 -08:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert_equal 302, response.status
|
|
|
|
assert_equal 'Hi', body
|
|
|
|
end
|
|
|
|
|
2009-01-24 20:33:06 -08:00
|
|
|
it "gives you access to params" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-01-24 20:33:06 -08:00
|
|
|
before { @foo = params['foo'] }
|
|
|
|
get('/foo') { @foo }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2009-01-24 20:33:06 -08:00
|
|
|
|
|
|
|
get '/foo?foo=cool'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'cool', body
|
|
|
|
end
|
2009-03-25 10:55:16 -07:00
|
|
|
|
2011-09-22 15:35:44 -07:00
|
|
|
it "properly unescapes parameters" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2011-09-22 15:35:44 -07:00
|
|
|
before { @foo = params['foo'] }
|
|
|
|
get('/foo') { @foo }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2011-09-22 15:35:44 -07:00
|
|
|
|
|
|
|
get '/foo?foo=bar%3Abaz%2Fbend'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'bar:baz/bend', body
|
|
|
|
end
|
|
|
|
|
2009-03-25 10:55:16 -07: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 10:55:16 -07:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert_equal 'hello from superclass', body
|
|
|
|
end
|
2010-01-28 08:58:19 -08: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 08:58:19 -08:00
|
|
|
before { ran_filter = true }
|
|
|
|
set :static, true
|
2020-03-13 22:20:04 +01:00
|
|
|
set :public_folder, __dir__
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2010-01-28 08:58:19 -08:00
|
|
|
get "/#{File.basename(__FILE__)}"
|
|
|
|
assert ok?
|
|
|
|
assert_equal File.read(__FILE__), body
|
|
|
|
assert !ran_filter
|
|
|
|
end
|
2010-04-27 23:11:43 +02: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 11:53:44 -07: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 05:02:10 -07:00
|
|
|
end
|
2008-12-21 18:36:14 -08:00
|
|
|
|
2015-01-11 01:00:47 +05:30
|
|
|
class AfterFilterTest < Minitest::Test
|
2012-05-29 20:50:05 +01:00
|
|
|
it "executes before and after filters in correct order" do
|
2008-12-21 18:36:14 -08:00
|
|
|
invoked = 0
|
|
|
|
mock_app do
|
|
|
|
before { invoked = 2 }
|
2012-05-29 20:50:05 +01:00
|
|
|
get('/') { invoked += 2; 'hello' }
|
2008-12-21 18:36:14 -08: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 18:36:14 -08:00
|
|
|
assert_equal 0, count
|
|
|
|
count = 1
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
|
|
|
after do
|
2008-12-21 18:36:14 -08:00
|
|
|
assert_equal 1, count
|
|
|
|
count = 2
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-21 18:36:14 -08: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 18:36:14 -08:00
|
|
|
get('/foo') { 'ORLY' }
|
|
|
|
after { redirect '/bar' }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-21 18:36:14 -08:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert redirect?
|
2010-08-30 11:00:30 +02:00
|
|
|
assert_equal 'http://example.org/bar', response['Location']
|
2008-12-21 18:36:14 -08: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 18:36:14 -08:00
|
|
|
get('/foo') { 'cool' }
|
|
|
|
after { 'Hello World!' }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-21 18:36:14 -08: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 18:36:14 -08: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 18:36:14 -08: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 18:00:23 +02:00
|
|
|
mock_app(base) do
|
|
|
|
get('/foo') do
|
|
|
|
count += 2
|
|
|
|
"ok"
|
|
|
|
end
|
|
|
|
end
|
2008-12-21 18:36:14 -08:00
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert_equal 8, count
|
|
|
|
end
|
2010-01-28 08:58:19 -08: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 08:58:19 -08:00
|
|
|
after { ran_filter = true }
|
|
|
|
set :static, true
|
2020-03-13 22:20:04 +01:00
|
|
|
set :public_folder, __dir__
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2010-01-28 08:58:19 -08:00
|
|
|
get "/#{File.basename(__FILE__)}"
|
|
|
|
assert ok?
|
|
|
|
assert_equal File.read(__FILE__), body
|
|
|
|
assert !ran_filter
|
|
|
|
end
|
2010-04-27 23:11:43 +02: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 23:50:51 +05:30
|
|
|
it 'changes to path_info from a pattern matching before filter are respected when routing' do
|
2010-11-04 20:08:24 +01: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 23:11:43 +02: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 13:51:52 +01:00
|
|
|
|
2010-09-03 23:19:20 +02: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 13:51:52 +01: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 13:51:52 +01:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_HOST' => 'example.com' })
|
2010-11-03 13:51:52 +01: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 13:51:52 +01:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_HOST' => 'example.org' })
|
2010-11-03 13:51:52 +01:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_HOST' => 'example.com' })
|
2010-11-03 13:51:52 +01: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 13:51:52 +01:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_HOST' => 'example.com' })
|
2010-11-03 13:51:52 +01: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 13:51:52 +01:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_HOST' => 'example.org' })
|
2010-11-03 13:51:52 +01:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_HOST' => 'example.com' })
|
2010-11-03 13:51:52 +01: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 13:51:52 +01:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_USER_AGENT' => 'foo' })
|
2010-11-03 13:51:52 +01: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 13:51:52 +01:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_USER_AGENT' => 'bar' })
|
2010-11-03 13:51:52 +01:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_USER_AGENT' => 'foo' })
|
2010-11-03 13:51:52 +01:00
|
|
|
assert ran
|
|
|
|
end
|
|
|
|
|
2011-10-30 13:00:12 -07: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 09:56:47 +02: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 13:00:12 -07: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 13:51:52 +01: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 13:51:52 +01:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', {}, { 'HTTP_USER_AGENT' => 'foo' })
|
2010-11-03 13:51:52 +01:00
|
|
|
assert ran
|
|
|
|
end
|
|
|
|
|
2012-05-29 20:56:26 +01:00
|
|
|
it 'is possible to apply user_agent conditions to after filters with a path' do
|
2010-11-03 13:51:52 +01: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 13:51:52 +01:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_USER_AGENT' => 'bar' })
|
2010-11-03 13:51:52 +01:00
|
|
|
assert !ran
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/foo', {}, { 'HTTP_USER_AGENT' => 'foo' })
|
2010-11-03 13:51:52 +01:00
|
|
|
assert ran
|
|
|
|
end
|
2012-01-02 17:07:45 +01:00
|
|
|
|
2013-07-22 01:10:50 +05:30
|
|
|
it 'only triggers provides condition if conforms with current Content-Type' do
|
2012-01-02 17:07:45 +01:00
|
|
|
mock_app do
|
|
|
|
before(:provides => :txt) { @type = 'txt' }
|
|
|
|
before(:provides => :html) { @type = 'html' }
|
|
|
|
get('/') { @type }
|
|
|
|
end
|
|
|
|
|
2013-01-28 09:43:30 -08:00
|
|
|
get('/', {}, { 'HTTP_ACCEPT' => '*/*' })
|
2012-01-02 17:07:45 +01:00
|
|
|
assert_body 'txt'
|
|
|
|
end
|
2013-03-13 11:39:12 -07: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 11:53:44 -07:00
|
|
|
get "/foo" do
|
2013-03-13 11:39:12 -07:00
|
|
|
doodle = 'Been now'
|
|
|
|
raise StandardError, "now"
|
|
|
|
end
|
2013-03-13 11:53:44 -07:00
|
|
|
get "/" do
|
2013-03-13 11:39:12 -07:00
|
|
|
doodle = 'Been now'
|
|
|
|
end
|
|
|
|
error 500 do
|
2013-03-13 11:53:44 -07:00
|
|
|
"Error handled #{env['sinatra.error'].message}"
|
2013-03-13 11:39:12 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/foo'
|
2013-03-13 11:53:44 -07:00
|
|
|
assert_equal 'Error handled now', body
|
|
|
|
assert_equal 'Been now and after', doodle
|
2013-03-13 11:39:12 -07:00
|
|
|
|
|
|
|
doodle = ''
|
|
|
|
get '/'
|
2013-03-13 11:53:44 -07:00
|
|
|
assert_equal 'Error handled after', body
|
2013-03-13 11:39:12 -07:00
|
|
|
assert_equal 'Been now and after', doodle
|
|
|
|
end
|
2008-12-21 18:36:14 -08:00
|
|
|
end
|