Skip to content

Commit

Permalink
Handle case where headers are frozen, and add specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Feb 5, 2020
1 parent 0e8abf0 commit d4bb198
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rack/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def context(env, app = @app)
# @api private
class HeaderHash < Hash # :nodoc:
def self.[](headers)
if headers.is_a?(HeaderHash)
if headers.is_a?(HeaderHash) && !headers.frozen?
return headers
else
return self.new(headers)
Expand Down
29 changes: 29 additions & 0 deletions test/spec_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,35 @@ def initialize(*)
h['foo'].must_be_nil
h.wont_include 'foo'
end

it "uses memoized header hash" do
env = {}
headers = Rack::Utils::HeaderHash.new({ 'content-type' => "text/plain", "content-length" => "3" })

app = lambda do |env|
[200, headers, []]
end

app = Rack::ContentLength.new(app)

response = app.call(env)
assert_same response[1], headers
end

it "duplicates header hash" do
env = {}
headers = Rack::Utils::HeaderHash.new({ 'content-type' => "text/plain", "content-length" => "3" })
headers.freeze

app = lambda do |env|
[200, headers, []]
end

app = Rack::ContentLength.new(app)

response = app.call(env)
refute_same response[1], headers
end
end

describe Rack::Utils::Context do
Expand Down

0 comments on commit d4bb198

Please sign in to comment.