-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:lotus/controller
- Loading branch information
Showing
6 changed files
with
160 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Copyright (c) 2014 Luca Guidi | ||
Copyright © 2014-2015 Luca Guidi | ||
|
||
MIT License | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
module Lotus | ||
module Action | ||
module Rack | ||
module Callable | ||
# Callable module for actions. With this module, actions with middlewares | ||
# will be able to work with rack builder. | ||
# | ||
# @param env [Hash] the full Rack env or the params. This value may vary, | ||
# see the examples below. | ||
# | ||
# @since x.x.x | ||
# | ||
# @see Lotus::Action::Rack::ClassMethods#rack_builder | ||
# @see Lotus::Action::Rack::ClassMethods#use | ||
# | ||
# @example | ||
# require 'lotus/controller' | ||
# | ||
# class MyMiddleware | ||
# def initialize(app) | ||
# @app = app | ||
# end | ||
# | ||
# def call(env) | ||
# #... | ||
# end | ||
# end | ||
# | ||
# class Show | ||
# include Lotus::Action | ||
# use MyMiddleware | ||
# | ||
# def call(params) | ||
# # ... | ||
# puts params # => { id: 23 } extracted from Rack env | ||
# end | ||
# end | ||
# | ||
# Show.respond_to?(:call) # => true | ||
def call(env) | ||
rack_builder.call(env) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,59 @@ | ||
require 'test_helper' | ||
require 'rack/test' | ||
|
||
describe 'Rack middleware integration' do | ||
include Rack::Test::Methods | ||
|
||
def response | ||
last_response | ||
end | ||
|
||
describe '.use' do | ||
let(:app) { UseActionApplication } | ||
|
||
it 'uses the specified Rack middleware' do | ||
response = Rack::MockRequest.new(UseAction.new).get('/') | ||
status, headers, _ = response | ||
router = Lotus::Router.new do | ||
get '/', to: 'use_action#index' | ||
get '/show', to: 'use_action#show' | ||
end | ||
|
||
UseActionApplication = Rack::Builder.new do | ||
run router | ||
end.to_app | ||
|
||
get '/' | ||
|
||
response.status.must_equal 200 | ||
response.headers.fetch('X-Middleware').must_equal 'OK' | ||
response.headers['Y-Middleware'].must_be_nil | ||
response.body.must_equal 'Hello from UseAction::Index' | ||
|
||
get '/show' | ||
|
||
response.status.must_equal 200 | ||
response.headers.fetch('Y-Middleware').must_equal 'OK' | ||
response.headers['X-Middleware'].must_be_nil | ||
response.body.must_equal 'Hello from UseAction::Show' | ||
end | ||
end | ||
|
||
describe 'not using .use' do | ||
let(:app) { NoUseActionApplication } | ||
|
||
it "action doens't use a middleware" do | ||
router = Lotus::Router.new do | ||
get '/', to: 'no_use_action#index' | ||
end | ||
|
||
NoUseActionApplication = Rack::Builder.new do | ||
run router | ||
end.to_app | ||
|
||
get '/' | ||
|
||
status.must_equal 200 | ||
headers['X-Middleware'].must_equal 'OK' | ||
response.status.must_equal 200 | ||
response.headers['X-Middleware'].must_be_nil | ||
response.body.must_equal 'Hello from NoUseAction::Index' | ||
end | ||
end | ||
end |