summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Williams <samuel.williams@oriontransfer.co.nz>2020-02-05 13:19:46 +1300
committerSamuel Williams <samuel.williams@oriontransfer.co.nz>2020-02-05 13:35:25 +1300
commitd4bb19862643f7ec8d47d5a4fc716aff8af63e69 (patch)
tree40dd1251cb60fac6e8299b44c39dbd02d95ff8bc
parent0e8abf01a968ea9e6724bf0f585788ba78ae7826 (diff)
downloadrack-d4bb19862643f7ec8d47d5a4fc716aff8af63e69.tar.gz
Handle case where headers are frozen, and add specs.
-rw-r--r--lib/rack/utils.rb2
-rw-r--r--test/spec_utils.rb29
2 files changed, 30 insertions, 1 deletions
diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb
index e1a44686..0fef215d 100644
--- a/lib/rack/utils.rb
+++ b/lib/rack/utils.rb
@@ -406,7 +406,7 @@ module Rack
# @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)
diff --git a/test/spec_utils.rb b/test/spec_utils.rb
index fb77cd76..e25070bc 100644
--- a/test/spec_utils.rb
+++ b/test/spec_utils.rb
@@ -727,6 +727,35 @@ describe Rack::Utils::HeaderHash do
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