Sha256: 4d2002e064986a7c8f72c5d6eff04567783b6562ef3de96ba136ee024f2609c7

Contents?: true

Size: 1.35 KB

Versions: 23

Compression:

Stored size: 1.35 KB

Contents

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

describe "Middleware" do
  before do
    @app = mock_app(Sinatra::Default) {
      get '/*' do
        response.headers['X-Tests'] = env['test.ran'].join(', ')
        env['PATH_INFO']
      end
    }
  end

  class MockMiddleware < Struct.new(:app)
    def call(env)
      (env['test.ran'] ||= []) << self.class.to_s
      app.call(env)
    end
  end

  class UpcaseMiddleware < MockMiddleware
    def call(env)
      env['PATH_INFO'] = env['PATH_INFO'].upcase
      super
    end
  end

  it "is added with Sinatra::Application.use" do
    @app.use UpcaseMiddleware
    get '/hello-world'
    assert ok?
    assert_equal '/HELLO-WORLD', body
  end

  class DowncaseMiddleware < MockMiddleware
    def call(env)
      env['PATH_INFO'] = env['PATH_INFO'].downcase
      super
    end
  end

  specify "runs in the order defined" do
    @app.use UpcaseMiddleware
    @app.use DowncaseMiddleware
    get '/Foo'
    assert_equal "/foo", body
    assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
  end

  specify "resets the prebuilt pipeline when new middleware is added" do
    @app.use UpcaseMiddleware
    get '/Foo'
    assert_equal "/FOO", body
    @app.use DowncaseMiddleware
    get '/Foo'
    assert_equal '/foo', body
    assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
  end
end

Version data entries

23 entries across 23 versions & 8 rubygems

Version Path
Syd-sinatra-0.9.0.2 test/middleware_test.rb
bmizerany-sinatra-0.8.10 test/middleware_test.rb
bmizerany-sinatra-0.8.9 test/middleware_test.rb
bmizerany-sinatra-0.9.0.2 test/middleware_test.rb
p8-castronaut-0.6.1.1 vendor/sinatra/test/middleware_test.rb
relevance-castronaut-0.6.0 vendor/sinatra/test/middleware_test.rb
relevance-castronaut-0.6.1 vendor/sinatra/test/middleware_test.rb
relevance-castronaut-0.7.4 vendor/sinatra/test/middleware_test.rb
relevance-castronaut-0.7.5 vendor/sinatra/test/middleware_test.rb
rtomayko-sinatra-0.8.10 test/middleware_test.rb
rtomayko-sinatra-0.9.0.2 test/middleware_test.rb
sinatra-sinatra-0.8.10 test/middleware_test.rb
sinatra-sinatra-0.8.9 test/middleware_test.rb
sinatra-sinatra-0.9.0.1 test/middleware_test.rb
sinatra-sinatra-0.9.0.2 test/middleware_test.rb
sinatra-sinatra-0.9.0 test/middleware_test.rb
nbudin-castronaut-0.7.5 vendor/sinatra/test/middleware_test.rb
sinatra-0.9.0.1 test/middleware_test.rb
sinatra-0.9.0.5 test/middleware_test.rb
sinatra-0.9.0.2 test/middleware_test.rb