2009-01-13 12:53:53 -05:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
2008-09-07 08:02:10 -04:00
|
|
|
|
2008-12-21 21:36:14 -05:00
|
|
|
class BeforeFilterTest < Test::Unit::TestCase
|
2008-12-13 16:06:02 -05:00
|
|
|
it "executes filters in the order defined" do
|
|
|
|
count = 0
|
|
|
|
mock_app do
|
|
|
|
get('/') { 'Hello World' }
|
|
|
|
before {
|
2009-01-15 07:04:10 -05:00
|
|
|
assert_equal 0, count
|
2008-12-13 16:06:02 -05:00
|
|
|
count = 1
|
|
|
|
}
|
|
|
|
before {
|
2009-01-15 07:04:10 -05:00
|
|
|
assert_equal 1, count
|
2008-12-13 16:06:02 -05:00
|
|
|
count = 2
|
|
|
|
}
|
|
|
|
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
|
2008-12-13 16:06:02 -05:00
|
|
|
mock_app {
|
|
|
|
get('/foo') { 'foo' }
|
|
|
|
get('/bar') { 'bar' }
|
|
|
|
before { request.path_info = '/bar' }
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
mock_app {
|
|
|
|
before { @foo = 'bar' }
|
|
|
|
get('/foo') { @foo }
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'bar', body
|
|
|
|
end
|
|
|
|
|
2008-12-21 21:36:14 -05:00
|
|
|
it "allows redirects" do
|
2009-01-18 07:00:55 -05:00
|
|
|
mock_app {
|
|
|
|
before { redirect '/bar' }
|
2009-01-18 08:37:36 -05:00
|
|
|
get('/foo') do
|
|
|
|
fail 'before block should have halted processing'
|
|
|
|
'ORLY?!'
|
|
|
|
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
|
|
|
|
mock_app {
|
|
|
|
before { 'Hello World!' }
|
|
|
|
get '/foo' do
|
|
|
|
assert_equal [], response.body
|
|
|
|
'cool'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
mock_app {
|
|
|
|
before { halt 302, 'Hi' }
|
|
|
|
get '/foo' do
|
|
|
|
"should not happen"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
mock_app {
|
|
|
|
before { @foo = params['foo'] }
|
|
|
|
get('/foo') { @foo }
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo?foo=cool'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'cool', 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' }
|
|
|
|
|
|
|
|
mock_app(base) {
|
|
|
|
get('/foo') { @foo }
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
mock_app {
|
|
|
|
before { ran_filter = true }
|
|
|
|
set :static, true
|
|
|
|
set :public, File.dirname(__FILE__)
|
|
|
|
}
|
|
|
|
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
|
2008-09-07 08:02:10 -04:00
|
|
|
end
|
2008-12-21 21:36:14 -05:00
|
|
|
|
|
|
|
class AfterFilterTest < Test::Unit::TestCase
|
|
|
|
it "executes filters in the order defined" do
|
|
|
|
invoked = 0
|
|
|
|
mock_app do
|
|
|
|
before { invoked = 2 }
|
|
|
|
get('/') { invoked += 2 }
|
|
|
|
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' }
|
|
|
|
after {
|
|
|
|
assert_equal 0, count
|
|
|
|
count = 1
|
|
|
|
}
|
|
|
|
after {
|
|
|
|
assert_equal 1, count
|
|
|
|
count = 2
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 2, count
|
|
|
|
assert_equal 'Hello World', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows redirects" do
|
|
|
|
mock_app {
|
|
|
|
get('/foo') { 'ORLY' }
|
|
|
|
after { redirect '/bar' }
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
mock_app {
|
|
|
|
get('/foo') { 'cool' }
|
|
|
|
after { 'Hello World!' }
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'cool', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does modify the response with halt" do
|
|
|
|
mock_app {
|
|
|
|
get '/foo' do
|
|
|
|
"should not be returned"
|
|
|
|
end
|
|
|
|
after { halt 302, 'Hi' }
|
|
|
|
}
|
|
|
|
|
|
|
|
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 }
|
|
|
|
mock_app(base) {
|
|
|
|
get('/foo') { count += 2 }
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert_equal 8, count
|
|
|
|
end
|
2010-01-28 11:58:19 -05:00
|
|
|
|
|
|
|
it 'does not run after filter when serving static files' do
|
|
|
|
ran_filter = false
|
|
|
|
mock_app {
|
|
|
|
after { ran_filter = true }
|
|
|
|
set :static, true
|
|
|
|
set :public, File.dirname(__FILE__)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
|
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-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
|
2008-12-21 21:36:14 -05:00
|
|
|
end
|