Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow blocks when specifying Rack middleware to use #98

Merged
merged 2 commits into from Mar 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/lotus/action/rack.rb
Expand Up @@ -94,8 +94,8 @@ def rack_builder
# end
# end
# end
def use(middleware, *args)
rack_builder.use middleware, *args
def use(middleware, *args, &block)
rack_builder.use middleware, *args, &block
end
end

Expand Down
23 changes: 23 additions & 0 deletions test/fixtures.rb
Expand Up @@ -170,6 +170,18 @@ def call(params)
end
end

class ZMiddleware
def initialize(app, &message)
@app = app
@message = message
end

def call(env)
code, headers, body = @app.call(env)
[code, headers.merge!('Z-Middleware' => @message.call), body]
end
end

class YMiddleware
def initialize(app)
@app = app
Expand Down Expand Up @@ -210,6 +222,17 @@ def call(params)
self.body = 'Hello from UseAction::Show'
end
end

class Edit
include Lotus::Action
use ZMiddleware do
'OK'
end

def call(params)
self.body = 'Hello from UseAction::Edit'
end
end
end

module NoUseAction
Expand Down
9 changes: 9 additions & 0 deletions test/integration/use_test.rb
Expand Up @@ -15,6 +15,7 @@ def response
router = Lotus::Router.new do
get '/', to: 'use_action#index'
get '/show', to: 'use_action#show'
get '/edit', to: 'use_action#edit'
end

UseActionApplication = Rack::Builder.new do
Expand All @@ -34,6 +35,14 @@ def response
response.headers.fetch('Y-Middleware').must_equal 'OK'
response.headers['X-Middleware'].must_be_nil
response.body.must_equal 'Hello from UseAction::Show'

get '/edit'

response.status.must_equal 200
response.headers.fetch('Z-Middleware').must_equal 'OK'
response.headers['X-Middleware'].must_be_nil
response.headers['Y-Middleware'].must_be_nil
response.body.must_equal 'Hello from UseAction::Edit'
end
end

Expand Down