Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize usage of "Headers". #47147

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions actionpack/lib/action_dispatch/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ class Response
begin
# For `Rack::Headers` (Rack 3+):
require "rack/headers"
Header = ::Rack::Headers
Headers = ::Rack::Headers
rescue LoadError
# For `Rack::Utils::HeaderHash`:
require "rack/utils"
Header = ::Rack::Utils::HeaderHash
Headers = ::Rack::Utils::HeaderHash
end

# To be deprecated:
Header = Headers

# The request that the response is responding to.
attr_accessor :request

Expand All @@ -60,11 +63,12 @@ class Response
# headers["Content-Type"] = "application/json"
# headers["Content-Type"] # => "application/json"
#
attr_reader :header
# Also aliased as +header+ for compatibility.
attr_reader :headers

alias_method :headers, :header
alias_method :header, :headers

delegate :[], :[]=, to: :@header
delegate :[], :[]=, to: :@headers

def each(&block)
sending!
Expand Down Expand Up @@ -143,9 +147,9 @@ def each_chunk(&block)
end
end

def self.create(status = 200, header = {}, body = [], default_headers: self.default_headers)
header = merge_default_headers(header, default_headers)
new status, header, body
def self.create(status = 200, headers = {}, body = [], default_headers: self.default_headers)
headers = merge_default_headers(headers, default_headers)
new status, headers, body
end

def self.merge_default_headers(original, default)
Expand All @@ -155,13 +159,13 @@ def self.merge_default_headers(original, default)
# The underlying body, as a streamable object.
attr_reader :stream

def initialize(status = 200, header = nil, body = [])
def initialize(status = 200, headers = nil, body = [])
super()

@header = Header.new
@headers = Headers.new

header&.each do |key, value|
@header[key] = value
headers&.each do |key, value|
@headers[key] = value
end

self.body, self.status = body, status
Expand All @@ -176,10 +180,10 @@ def initialize(status = 200, header = nil, body = [])
yield self if block_given?
end

def has_header?(key); headers.key? key; end
def get_header(key); headers[key]; end
def set_header(key, v); headers[key] = v; end
def delete_header(key); headers.delete key; end
def has_header?(key); @headers.key? key; end
def get_header(key); @headers[key]; end
def set_header(key, v); @headers[key] = v; end
def delete_header(key); @headers.delete key; end

def await_commit
synchronize do
Expand Down Expand Up @@ -385,7 +389,7 @@ def abort
# status, headers, body = *response
def to_a
commit!
rack_response @status, @header.to_hash
rack_response @status, @headers.to_hash
end
alias prepare! to_a

Expand Down Expand Up @@ -452,7 +456,7 @@ def before_sending
# our last chance.
commit! unless committed?

@header.freeze
@headers.freeze
@request.commit_cookie_jar! unless committed?
end

Expand Down Expand Up @@ -506,16 +510,16 @@ def to_path

def handle_no_content!
if NO_CONTENT_CODES.include?(@status)
@header.delete CONTENT_TYPE
@header.delete "Content-Length"
@headers.delete CONTENT_TYPE
@headers.delete "Content-Length"
end
end

def rack_response(status, header)
def rack_response(status, headers)
if NO_CONTENT_CODES.include?(status)
[status, header, []]
[status, headers, []]
else
[status, header, RackBody.new(self)]
[status, headers, RackBody.new(self)]
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions actionpack/test/controller/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -854,20 +854,20 @@ def test_head_created
def test_head_created_with_application_json_content_type
post :head_created_with_application_json_content_type
assert_predicate @response.body, :blank?
assert_equal "application/json", @response.header["Content-Type"]
assert_equal "application/json", @response.headers["Content-Type"]
assert_response :created
end

def test_head_ok_with_image_png_content_type
post :head_ok_with_image_png_content_type
assert_predicate @response.body, :blank?
assert_equal "image/png", @response.header["Content-Type"]
assert_equal "image/png", @response.headers["Content-Type"]
assert_response :ok
end

def test_head_respect_string_content_type
get :head_ok_with_string_key_content_type
assert_equal "application/pdf", @response.header["Content-Type"]
assert_equal "application/pdf", @response.headers["Content-Type"]
end

def test_head_with_location_header
Expand Down Expand Up @@ -967,7 +967,7 @@ def test_head_returns_truthy_value

def test_head_default_content_type
post :head_default_content_type
assert_equal "text/html", @response.header["Content-Type"]
assert_equal "text/html", @response.headers["Content-Type"]
end
end

Expand Down
8 changes: 4 additions & 4 deletions actionpack/test/dispatch/live_response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def setup
end

def test_header_merge
header = @response.header.merge("Foo" => "Bar")
assert_kind_of(ActionController::Live::Response::Header, header)
assert_not_equal header, @response.header
headers = @response.headers.merge("Foo" => "Bar")
assert_kind_of(ActionController::Live::Response::Headers, headers)
assert_not_equal headers, @response.headers
end

def test_initialize_with_default_headers
Expand All @@ -25,7 +25,7 @@ def self.default_headers
end

headers = r.create.headers
assert_kind_of(ActionController::Live::Response::Header, headers)
assert_kind_of(ActionController::Live::Response::Headers, headers)
assert_equal "g", headers["omg"]
end

Expand Down