From a1534a5d7b48812e1e67d7ff02ef53b70e3ea492 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Fri, 11 Dec 2009 15:40:08 -0600 Subject: [PATCH] Import Config by jcrosby (Jon Crosby) into core --- lib/rack.rb | 1 + lib/rack/config.rb | 15 +++++++++++++++ test/spec_rack_config.rb | 24 ++++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 lib/rack/config.rb create mode 100644 test/spec_rack_config.rb diff --git a/lib/rack.rb b/lib/rack.rb index 25438afeb..b2aed6a52 100644 --- a/lib/rack.rb +++ b/lib/rack.rb @@ -28,6 +28,7 @@ def self.release 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 000000000..c6d446c0c --- /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 000000000..a508ea4be --- /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