Skip to content

Commit

Permalink
Allow HeaderHash#fetch to respect case insensitivity
Browse files Browse the repository at this point in the history
It just makes good sense in general, but it specifically makes the
proximate fix for rack#1629 a lot cleaner.
  • Loading branch information
mpalmer committed Mar 31, 2020
1 parent 026537f commit 4aeda5c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/rack/utils.rb
Expand Up @@ -474,6 +474,14 @@ def include?(k)
alias_method :member?, :include?
alias_method :key?, :include?

def fetch(*args)
begin
super(args.first)
rescue KeyError
super(@names[args.first.downcase], *args[1..-1])
end
end

def merge!(other)
other.each { |k, v| self[k] = v }
self
Expand Down
18 changes: 18 additions & 0 deletions test/spec_utils.rb
Expand Up @@ -639,6 +639,24 @@ def initialize(*)
h.wont_include 'ETag'
end

it "fetches values via case-insensitive keys" do
h = Rack::Utils::HeaderHash.new("Content-MD5" => "d5ff4e2a0 ...")
v = h.fetch("content-MD5", "nope")
v.must_equal "d5ff4e2a0 ..."
end

it "fetches values via case-insensitive keys without defaults" do
h = Rack::Utils::HeaderHash.new("Content-MD5" => "d5ff4e2a0 ...")
v = h.fetch("content-MD5")
v.must_equal "d5ff4e2a0 ..."
end

it "correctly raises an exception on fetch for a non-existent key" do
h = Rack::Utils::HeaderHash.new("Content-MD5" => "d5ff4e2a0 ...")

-> { h.fetch("Set-Cookie") }.must_raise(KeyError)
end

it "create deep HeaderHash copy on dup" do
h1 = Rack::Utils::HeaderHash.new("Content-MD5" => "d5ff4e2a0 ...")
h2 = h1.dup
Expand Down

0 comments on commit 4aeda5c

Please sign in to comment.