-
-
Save elvuel/1308529 to your computer and use it in GitHub Desktop.
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
module Rack | |
class Callbacks < ::Array | |
DUMMY_APP = proc {|env| } | |
def initialize(app, &block) | |
@app = app | |
instance_eval(&block) if block_given? | |
end | |
def use(middleware, *args, &block) | |
if block_given? | |
push middleware.new(DUMMY_APP, *args, &block) | |
else | |
push middleware.new(DUMMY_APP, *args) | |
end | |
end | |
def call(env) | |
each {|c| c.call(env) } | |
@app.call(env) | |
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
require 'rack/mock' | |
require 'rack/contrib/callbacks' | |
class Flame | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
env['flame'] = 'F Lifo..' | |
@app.call(env) | |
end | |
end | |
class Pacify | |
def initialize(app, with) | |
@app = app | |
@with = with | |
end | |
def call(env) | |
env['peace'] = @with | |
@app.call(env) | |
end | |
end | |
context "Rack::Callbacks" do | |
specify "supports blocks on use" do | |
app = Rack::Builder.new do | |
use Rack::Callbacks do | |
use Flame | |
use Pacify, "with love" | |
end | |
run lambda { |env| [200, {}, env['flame'] + env['peace']] } | |
end.to_app | |
response = Rack::MockRequest.new(app).get("/") | |
response.body.to_s.should.equal 'F Lifo..with love' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment