Permalink
Browse files

Add Rack::Config

Rack::Config is shared configuration for cooperative middleware,
pulled from the rack-config gem.
  • Loading branch information...
1 parent 4432228 commit 01533a8a88fce359d6ec07058d0ed7b13b8ed839 @jcrosby jcrosby committed Feb 1, 2009
Showing with 39 additions and 0 deletions.
  1. +1 −0 README.rdoc
  2. +16 −0 lib/rack/contrib/config.rb
  3. +22 −0 test/spec_rack_config.rb
View
@@ -27,6 +27,7 @@ interface:
a variable in Rack's environment with the offset from UTC.
* Rack::Evil - Lets the rack application return a response to the client from any place.
* Rack::Callbacks - Implements DLS for pure before/after filter like Middlewares.
+* Rack::Config - Shared configuration for cooperative middleware.
=== Use
@@ -0,0 +1,16 @@
+module Rack
+
+ # Rack::Config modifies the environment using the block given during
+ # initialization.
+ class Config
+ def initialize(app, &block)
+ @app = app
+ @block = block
+ end
+
+ def call(env)
+ @block.call(env)
+ @app.call(env)
+ end
+ end
+end
@@ -0,0 +1,22 @@
+require 'test/spec'
+require 'rack/mock'
+require 'rack/contrib/config'
+
+context "Rack::Config" do
+
+ specify "should accept a block that modifies the environment" do
+ app = Rack::Builder.new do
+ use Rack::Lint
+ use Rack::ContentLength
+ use Rack::Config do |env|
+ env['greeting'] = 'hello'
+ end
+ run lambda { |env|
+ [200, {'Content-Type' => 'text/plain'}, [env['greeting'] || '']]
+ }
+ end
+ response = Rack::MockRequest.new(app).get('/')
+ response.body.should.equal('hello')
+ end
+
+end

0 comments on commit 01533a8

Please sign in to comment.