1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
|
require File.expand_path('../helper', __FILE__)
class BeforeFilterTest < Test::Unit::TestCase
it "executes filters in the order defined" do
count = 0
mock_app do
get('/') { 'Hello World' }
before do
assert_equal 0, count
count = 1
end
before do
assert_equal 1, count
count = 2
end
end
get '/'
assert ok?
assert_equal 2, count
assert_equal 'Hello World', body
end
it "can modify the request" do
mock_app do
get('/foo') { 'foo' }
get('/bar') { 'bar' }
before { request.path_info = '/bar' }
end
get '/foo'
assert ok?
assert_equal 'bar', body
end
it "can modify instance variables available to routes" do
mock_app do
before { @foo = 'bar' }
get('/foo') { @foo }
end
get '/foo'
assert ok?
assert_equal 'bar', body
end
it "allows redirects" do
mock_app do
before { redirect '/bar' }
get('/foo') do
fail 'before block should have halted processing'
'ORLY?!'
end
end
get '/foo'
assert redirect?
assert_equal 'http://example.org/bar', response['Location']
assert_equal '', body
end
it "does not modify the response with its return value" do
mock_app do
before { 'Hello World!' }
get('/foo') do
assert_equal [], response.body
'cool'
end
end
get '/foo'
assert ok?
assert_equal 'cool', body
end
it "does modify the response with halt" do
mock_app do
before { halt 302, 'Hi' }
get '/foo' do
"should not happen"
end
end
get '/foo'
assert_equal 302, response.status
assert_equal 'Hi', body
end
it "gives you access to params" do
mock_app do
before { @foo = params['foo'] }
get('/foo') { @foo }
end
get '/foo?foo=cool'
assert ok?
assert_equal 'cool', body
end
it "properly unescapes parameters" do
mock_app do
before { @foo = params['foo'] }
get('/foo') { @foo }
end
get '/foo?foo=bar%3Abaz%2Fbend'
assert ok?
assert_equal 'bar:baz/bend', body
end
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
it 'does not run before filter when serving static files' do
ran_filter = false
mock_app do
before { ran_filter = true }
set :static, true
set :public_folder, File.dirname(__FILE__)
end
get "/#{File.basename(__FILE__)}"
assert ok?
assert_equal File.read(__FILE__), body
assert !ran_filter
end
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
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
end
class AfterFilterTest < Test::Unit::TestCase
it "executes before and after filters in correct order" do
invoked = 0
mock_app do
before { invoked = 2 }
get('/') { invoked += 2; 'hello' }
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 do
assert_equal 0, count
count = 1
end
after do
assert_equal 1, count
count = 2
end
end
get '/'
assert ok?
assert_equal 2, count
assert_equal 'Hello World', body
end
it "allows redirects" do
mock_app do
get('/foo') { 'ORLY' }
after { redirect '/bar' }
end
get '/foo'
assert redirect?
assert_equal 'http://example.org/bar', response['Location']
assert_equal '', body
end
it "does not modify the response with its return value" do
mock_app do
get('/foo') { 'cool' }
after { 'Hello World!' }
end
get '/foo'
assert ok?
assert_equal 'cool', body
end
it "does modify the response with halt" do
mock_app do
get '/foo' do
"should not be returned"
end
after { halt 302, 'Hi' }
end
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) do
get('/foo') do
count += 2
"ok"
end
end
get '/foo'
assert_equal 8, count
end
it 'does not run after filter when serving static files' do
ran_filter = false
mock_app do
after { ran_filter = true }
set :static, true
set :public_folder, File.dirname(__FILE__)
end
get "/#{File.basename(__FILE__)}"
assert ok?
assert_equal File.read(__FILE__), body
assert !ran_filter
end
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 'changes to path_info from a pattern matching before filter are respected when routing' do
mock_app do
before('/foo') { request.path_info = '/bar' }
get('/bar') { 'blah' }
end
get '/foo'
assert ok?
assert_equal 'blah', body
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
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
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
get('/', {}, { 'HTTP_HOST' => 'example.org' })
assert !ran
get('/', {}, { 'HTTP_HOST' => 'example.com' })
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
get('/', {}, { 'HTTP_HOST' => 'example.com' })
assert !ran
get('/foo', {}, { 'HTTP_HOST' => 'example.org' })
assert !ran
get('/foo', {}, { 'HTTP_HOST' => 'example.com' })
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
get('/', {}, { 'HTTP_HOST' => 'example.org' })
assert !ran
get('/', {}, { 'HTTP_HOST' => 'example.com' })
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
get('/', {}, { 'HTTP_HOST' => 'example.com' })
assert !ran
get('/foo', {}, { 'HTTP_HOST' => 'example.org' })
assert !ran
get('/foo', {}, { 'HTTP_HOST' => 'example.com' })
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
get('/', {}, { 'HTTP_USER_AGENT' => 'bar' })
assert !ran
get('/', {}, { 'HTTP_USER_AGENT' => 'foo' })
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
get('/', {}, { 'HTTP_USER_AGENT' => 'foo' })
assert !ran
get('/foo', {}, { 'HTTP_USER_AGENT' => 'bar' })
assert !ran
get('/foo', {}, { 'HTTP_USER_AGENT' => 'foo' })
assert ran
end
it 'can add params' do
mock_app do
before { params['foo'] = 'bar' }
get('/') { params['foo'] }
end
get '/'
assert_body 'bar'
end
it 'can remove params' do
mock_app do
before { params.delete('foo') }
get('/') { params['foo'].to_s }
end
get '/?foo=bar'
assert_body ''
end
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
get('/', {}, { 'HTTP_USER_AGENT' => 'bar' })
assert !ran
get('/', {}, { 'HTTP_USER_AGENT' => 'foo' })
assert ran
end
it 'is possible to apply user_agent conditions to after filters with a path' do
ran = false
mock_app do
after('/foo', :user_agent => /foo/) { ran = true }
get('/') { 'welcome' }
end
get('/', {}, { 'HTTP_USER_AGENT' => 'foo' })
assert !ran
get('/foo', {}, { 'HTTP_USER_AGENT' => 'bar' })
assert !ran
get('/foo', {}, { 'HTTP_USER_AGENT' => 'foo' })
assert ran
end
it 'only triggers provides condition if conforms with current Content-Type' do
mock_app do
before(:provides => :txt) { @type = 'txt' }
before(:provides => :html) { @type = 'html' }
get('/') { @type }
end
get('/', {}, { 'HTTP_ACCEPT' => '*/*' })
assert_body 'txt'
end
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
get "/foo" do
doodle = 'Been now'
raise StandardError, "now"
end
get "/" do
doodle = 'Been now'
end
error 500 do
"Error handled #{env['sinatra.error'].message}"
end
end
get '/foo'
assert_equal 'Error handled now', body
assert_equal 'Been now and after', doodle
doodle = ''
get '/'
assert_equal 'Error handled after', body
assert_equal 'Been now and after', doodle
end
end
|