summary refs log tree commit
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2009-12-11 15:40:08 -0600
committerJoshua Peek <josh@joshpeek.com>2009-12-11 15:40:08 -0600
commita1534a5d7b48812e1e67d7ff02ef53b70e3ea492 (patch)
treeb6f956e06a7d1822c5dbc2c77709ddd984d172a8
parent5c4bd17d791d97310667acf6b458456aa1f8af0d (diff)
downloadrack-a1534a5d7b48812e1e67d7ff02ef53b70e3ea492.tar.gz
Import Config by jcrosby (Jon Crosby) into core
-rw-r--r--lib/rack.rb1
-rw-r--r--lib/rack/config.rb15
-rw-r--r--test/spec_rack_config.rb24
3 files changed, 40 insertions, 0 deletions
diff --git a/lib/rack.rb b/lib/rack.rb
index 25438afe..b2aed6a5 100644
--- a/lib/rack.rb
+++ b/lib/rack.rb
@@ -28,6 +28,7 @@ module Rack
   autoload :Chunked, "rack/chunked"
   autoload :CommonLogger, "rack/commonlogger"
   autoload :ConditionalGet, "rack/conditionalget"
+  autoload :Config, "rack/config"
   autoload :ContentLength, "rack/content_length"
   autoload :ContentType, "rack/content_type"
   autoload :File, "rack/file"
diff --git a/lib/rack/config.rb b/lib/rack/config.rb
new file mode 100644
index 00000000..c6d446c0
--- /dev/null
+++ b/lib/rack/config.rb
@@ -0,0 +1,15 @@
+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
diff --git a/test/spec_rack_config.rb b/test/spec_rack_config.rb
new file mode 100644
index 00000000..a508ea4b
--- /dev/null
+++ b/test/spec_rack_config.rb
@@ -0,0 +1,24 @@
+require 'test/spec'
+require 'rack/mock'
+require 'rack/builder'
+require 'rack/content_length'
+require 'rack/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