Sha256: e65abc88274a2ecbfd204db192c5031bc2b1e61e2d71e019d559aa9678444c09

Contents?: true

Size: 1.41 KB

Versions: 12

Compression:

Stored size: 1.41 KB

Contents

require File.dirname(__FILE__) + '/helper'

describe "Filters" do
  it "executes filters in the order defined" do
    count = 0
    mock_app do
      get('/') { 'Hello World' }
      before {
        assert_equal 0, count
        count = 1
      }
      before {
        assert_equal 1, count
        count = 2
      }
    end

    get '/'
    assert ok?
    assert_equal 2, count
    assert_equal 'Hello World', body
  end

  it "allows filters to modify the request" do
    mock_app {
      get('/foo') { 'foo' }
      get('/bar') { 'bar' }
      before { request.path_info = '/bar' }
    }

    get '/foo'
    assert ok?
    assert_equal 'bar', body
  end

  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

  it "allows redirects in filters" do
    mock_app {
      before { redirect '/bar' }
      get('/foo') do
        fail 'before block should have halted processing'
        'ORLY?!'
      end
    }

    get '/foo'
    assert redirect?
    assert_equal '/bar', response['Location']
    assert_equal '', body
  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
end

Version data entries

12 entries across 12 versions & 8 rubygems

Version Path
Syd-sinatra-0.9.0.2 test/filter_test.rb
bmizerany-sinatra-0.9.0.2 test/filter_test.rb
p8-castronaut-0.6.1.1 vendor/sinatra/test/filter_test.rb
relevance-castronaut-0.6.0 vendor/sinatra/test/filter_test.rb
relevance-castronaut-0.6.1 vendor/sinatra/test/filter_test.rb
relevance-castronaut-0.7.4 vendor/sinatra/test/filter_test.rb
relevance-castronaut-0.7.5 vendor/sinatra/test/filter_test.rb
rtomayko-sinatra-0.9.0.2 test/filter_test.rb
sinatra-sinatra-0.9.0.2 test/filter_test.rb
nbudin-castronaut-0.7.5 vendor/sinatra/test/filter_test.rb
sinatra-0.9.0.2 test/filter_test.rb
sinatra-0.9.0.3 test/filter_test.rb