Sha256: 223290781bf33e2b8cfff5eba1ccbfe50a9a5b69849609ef8b75707d8cfe71cc

Contents?: true

Size: 1.98 KB

Versions: 30

Compression:

Stored size: 1.98 KB

Contents

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

class UpcaseMiddleware
  def initialize(app, *args, &block)
    @app = app
    @args = args
    @block = block
  end
  def call(env)
    env['PATH_INFO'] = env['PATH_INFO'].to_s.upcase
    @app.call(env)
  end
end

context "Middleware Pipelines" do

  setup do
    Sinatra.application = nil
    @app = Sinatra.application
  end

  teardown do
    Sinatra.application = nil
  end

  specify "includes default middleware with options set" do
    @app.set_options :sessions => true, :logging => true
    @app.send(:optional_middleware).should.include([Rack::Session::Cookie, [], nil])
    @app.send(:optional_middleware).should.include([Rack::CommonLogger, [], nil])
  end

  specify "does not include default middleware with options unset" do
    @app.set_options :sessions => false, :logging => false
    @app.send(:optional_middleware).should.not.include([Rack::Session::Cookie, [], nil])
    @app.send(:optional_middleware).should.not.include([Rack::CommonLogger, [], nil])
  end

  specify "includes only optional middleware when no explicit middleware added" do
    @app.set_options :sessions => true, :logging => true
    @app.send(:middleware).should.equal @app.send(:optional_middleware)
  end

  specify "should clear middleware before reload" do
    @app.clearables.should.include(@app.send(:explicit_middleware))
  end

  specify "should add middleware with use" do
    block = Proc.new { |env| }
    @app.use UpcaseMiddleware
    @app.use UpcaseMiddleware, "foo", "bar"
    @app.use UpcaseMiddleware, "foo", "bar", &block
    @app.send(:middleware).should.include([UpcaseMiddleware, [], nil])
    @app.send(:middleware).should.include([UpcaseMiddleware, ["foo", "bar"], nil])
    @app.send(:middleware).should.include([UpcaseMiddleware, ["foo", "bar"], block])
  end

  specify "should run middleware added with use" do
    get('/foo') { "FAIL!" }
    get('/FOO') { "PASS!" }
    use UpcaseMiddleware
    get_it '/foo'
    should.be.ok
    body.should.equal "PASS!"
  end

end

Version data entries

30 entries across 30 versions & 7 rubygems

Version Path
3mix-castronaut-0.5.0.2 vendor/sinatra/test/pipeline_test.rb
Syd-sinatra-0.3.2 test/pipeline_test.rb
bmizerany-sinatra-0.3.2 test/pipeline_test.rb
masover-castronaut-0.4.4.4 vendor/sinatra/test/pipeline_test.rb
masover-castronaut-0.4.4.5 vendor/sinatra/test/pipeline_test.rb
masover-castronaut-0.5.0.1 vendor/sinatra/test/pipeline_test.rb
relevance-castronaut-0.2.0 vendor/sinatra/test/pipeline_test.rb
relevance-castronaut-0.2.5 vendor/sinatra/test/pipeline_test.rb
relevance-castronaut-0.3.0 vendor/sinatra/test/pipeline_test.rb
relevance-castronaut-0.3.5 vendor/sinatra/test/pipeline_test.rb
relevance-castronaut-0.3.6 vendor/sinatra/test/pipeline_test.rb
relevance-castronaut-0.4.1 vendor/sinatra/test/pipeline_test.rb
relevance-castronaut-0.4.2 vendor/sinatra/test/pipeline_test.rb
relevance-castronaut-0.4.3 vendor/sinatra/test/pipeline_test.rb
relevance-castronaut-0.4.4 vendor/sinatra/test/pipeline_test.rb
relevance-castronaut-0.4.5 vendor/sinatra/test/pipeline_test.rb
relevance-castronaut-0.4.6 vendor/sinatra/test/pipeline_test.rb
relevance-castronaut-0.5.0 vendor/sinatra/test/pipeline_test.rb
relevance-castronaut-0.5.1 vendor/sinatra/test/pipeline_test.rb
relevance-castronaut-0.5.2 vendor/sinatra/test/pipeline_test.rb