pass a request object to the headers object

This commit is contained in:
Aaron Patterson 2015-08-20 15:57:15 -07:00
parent c6cfcc6124
commit 34fa6658dd
4 changed files with 19 additions and 13 deletions

@ -30,24 +30,27 @@ class Headers
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 @@ def key?(key)
# 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
# <tt>headers_or_env</tt> 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 @@ def merge!(headers_or_env)
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)

@ -170,7 +170,7 @@ def method_symbol
#
# 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.

@ -359,10 +359,10 @@ def process(method, path, params: nil, headers: nil, env: nil, xhr: false)
# 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)

@ -2,7 +2,7 @@
class HeaderTest < ActiveSupport::TestCase
def make_headers(hash)
ActionDispatch::Http::Headers.new hash
ActionDispatch::Http::Headers.new ActionDispatch::Request.new hash
end
setup do