Sha256: 9d858a617e41aa3593e1d857200768bfce13b660702aa1303efb9703aaee385f

Contents?: true

Size: 1.62 KB

Versions: 17

Compression:

Stored size: 1.62 KB

Contents

require File.expand_path('../helper', __FILE__)

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

17 entries across 17 versions & 4 rubygems

Version Path
sinatra-acd-1.4.5 test/middleware_test.rb
sinatra-1.4.5 test/middleware_test.rb
mango-0.8.0 vendor/bundler/ruby/2.1.0/gems/sinatra-1.4.4/test/middleware_test.rb
mango-0.7.1 vendor/bundler/ruby/2.0.0/gems/sinatra-1.4.4/test/middleware_test.rb
mango-0.7.0 vendor/bundler/ruby/2.0.0/gems/sinatra-1.4.4/test/middleware_test.rb
sinatra-1.4.4 test/middleware_test.rb
sinatra-1.4.3 test/middleware_test.rb
sinatra-1.4.2 test/middleware_test.rb
sinatra-1.4.1 test/middleware_test.rb
sinatra-1.4.0 test/middleware_test.rb
sinatra-1.4.0.d test/middleware_test.rb
sinatra-1.4.0.c test/middleware_test.rb
sinatra-1.4.0.b test/middleware_test.rb
sinatra-1.4.0.a test/middleware_test.rb
devcenter-0.0.3 vendor/sinatra/test/middleware_test.rb
devcenter-0.0.2 vendor/sinatra/test/middleware_test.rb
devcenter-0.0.1 vendor/sinatra/test/middleware_test.rb