Sha256: 40912d49aa0233e9b2d1a6a01cbb7cc76c6de829b2981d77ac9a6d10a8088c8f

Contents?: true

Size: 1.61 KB

Versions: 20

Compression:

Stored size: 1.61 KB

Contents

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

class MiddlewareTest < Test::Unit::TestCase
  setup do
    @app = mock_app(Sinatra::Default) {
      get '/*' do
        response.headers['X-Tests'] = env['test.ran'].
          map { |n| n.split('::').last }.
          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

  it "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

  it "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

  it "works when app is used as middleware" do
    @app.use UpcaseMiddleware
    @app = @app.new
    get '/Foo'
    assert_equal "/FOO", body
    assert_equal "UpcaseMiddleware", response['X-Tests']
  end
end

Version data entries

20 entries across 20 versions & 5 rubygems

Version Path
adamwiggins-sinatra-0.10.1 test/middleware_test.rb
sinatra-sinatra-0.10.0 test/middleware_test.rb
sinatra-sinatra-0.10.1 test/middleware_test.rb
sinatra-sinatra-0.9.1.3 test/middleware_test.rb
sinatra-sinatra-0.9.2 test/middleware_test.rb
akamai_bookmarklet-0.1.2 vendor/gems/ruby/1.8/gems/sinatra-0.9.4/test/middleware_test.rb
middleman-0.13.1 lib/middleman/vendor/gems/ruby/1.9.1/gems/sinatra-0.9.4/test/middleware_test.rb
akamai_bookmarklet-0.1.1 vendor/gems/ruby/1.8/gems/sinatra-0.9.4/test/middleware_test.rb
akamai_bookmarklet-0.1.0 vendor/gems/ruby/1.8/gems/sinatra-0.9.4/test/middleware_test.rb
middleman-0.12.2 lib/middleman/vendor/gems/gems/sinatra-0.9.4/test/middleware_test.rb
middleman-0.12.1 lib/middleman/vendor/gems/gems/sinatra-0.9.4/test/middleware_test.rb
middleman-0.12.0.pre3 lib/middleman/vendor/gems/gems/sinatra-0.9.4/test/middleware_test.rb
middleman-0.12.0.pre2 lib/middleman/vendor/gems/gems/sinatra-0.9.4/test/middleware_test.rb
middleman-0.12.0.pre lib/middleman/vendor/gems/gems/sinatra-0.9.4/test/middleware_test.rb
middleman-0.10.17 vendor/gems/gems/sinatra-0.9.4/test/middleware_test.rb
middleman-0.10.16 vendor/gems/gems/sinatra-0.9.4/test/middleware_test.rb
middleman-0.10.15 vendor/gems/gems/sinatra-0.9.4/test/middleware_test.rb
middleman-0.10.14 vendor/gems/gems/sinatra-0.9.4/test/middleware_test.rb
sinatra-0.9.4 test/middleware_test.rb
sinatra-0.9.2 test/middleware_test.rb