diff --git a/actionpack/lib/action_dispatch/http/headers.rb b/actionpack/lib/action_dispatch/http/headers.rb index bc5410dc38..40053e521a 100644 --- a/actionpack/lib/action_dispatch/http/headers.rb +++ b/actionpack/lib/action_dispatch/http/headers.rb @@ -30,24 +30,27 @@ module ActionDispatch HTTP_HEADER = /\A[A-Za-z0-9-]+\z/ include Enumerable - attr_reader :env - def initialize(env = {}) # :nodoc: - @env = env + def self.from_hash(hash) + new ActionDispatch::Request.new hash + end + + def initialize(request) # :nodoc: + @req = request end # Returns the value for the given key mapped to @env. def [](key) - @env[env_name(key)] + env[env_name(key)] end # Sets the given value for the key mapped to @env. def []=(key, value) - @env[env_name(key)] = value + env[env_name(key)] = value end def key?(key) - @env.key? env_name(key) + env.key? env_name(key) end alias :include? :key? @@ -59,17 +62,17 @@ module ActionDispatch # If the code block is provided, then it will be run and # its result returned. def fetch(key, *args, &block) - @env.fetch env_name(key), *args, &block + env.fetch env_name(key), *args, &block end def each(&block) - @env.each(&block) + env.each(&block) end # Returns a new Http::Headers instance containing the contents of # headers_or_env and the original instance. def merge(headers_or_env) - headers = Http::Headers.new(env.dup) + headers = Http::Headers.new(ActionDispatch::Request.new(env.dup)) headers.merge!(headers_or_env) headers end @@ -83,7 +86,10 @@ module ActionDispatch end end + def env; @req.env; end + private + # Converts a HTTP header name to an environment variable name if it is # not contained within the headers hash. def env_name(key) diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index de28cd0998..1f480eec73 100644 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -170,7 +170,7 @@ module ActionDispatch # # request.headers["Content-Type"] # => "text/plain" def headers - @headers ||= Http::Headers.new(@env) + @headers ||= Http::Headers.new(self) end # Returns a +String+ with the last requested path including their params. diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb index 0cdc6d4e77..4dfd4f3f71 100644 --- a/actionpack/lib/action_dispatch/testing/integration.rb +++ b/actionpack/lib/action_dispatch/testing/integration.rb @@ -359,10 +359,10 @@ module ActionDispatch # this modifies the passed request_env directly if headers.present? - Http::Headers.new(request_env).merge!(headers) + Http::Headers.from_hash(request_env).merge!(headers) end if env.present? - Http::Headers.new(request_env).merge!(env) + Http::Headers.from_hash(request_env).merge!(env) end session = Rack::Test::Session.new(_mock_session) diff --git a/actionpack/test/dispatch/header_test.rb b/actionpack/test/dispatch/header_test.rb index 782c2cb11d..79600b654b 100644 --- a/actionpack/test/dispatch/header_test.rb +++ b/actionpack/test/dispatch/header_test.rb @@ -2,7 +2,7 @@ require "abstract_unit" class HeaderTest < ActiveSupport::TestCase def make_headers(hash) - ActionDispatch::Http::Headers.new hash + ActionDispatch::Http::Headers.new ActionDispatch::Request.new hash end setup do