From 698eecc291d4598689eddd6907802fc4d92bf50c Mon Sep 17 00:00:00 2001 From: Hongli Lai (Phusion) Date: Wed, 17 Jun 2009 23:46:03 +0200 Subject: [PATCH] Fix Rack::Utils::HeaderHash#delete: it's supposed to return the deleted value, or nil if the key doesn't exist. --- lib/rack/utils.rb | 4 +++- test/spec_rack_utils.rb | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletions(-) diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb index 0899c10..34f43ce 100644 --- a/lib/rack/utils.rb +++ b/lib/rack/utils.rb @@ -211,6 +211,7 @@ module Rack # header when set. class HeaderHash < Hash def initialize(hash={}) + super() @names = {} hash.each { |k, v| self[k] = v } end @@ -238,8 +239,9 @@ module Rack def delete(k) canonical = k.downcase - super @names.delete(canonical) + result = super @names.delete(canonical) @names.delete_if { |name,| name.downcase == canonical } + result end def include?(k) diff --git a/test/spec_rack_utils.rb b/test/spec_rack_utils.rb index 36b4e7b..20abff5 100644 --- a/test/spec_rack_utils.rb +++ b/test/spec_rack_utils.rb @@ -235,6 +235,30 @@ context "Rack::Utils::HeaderHash" do h.replace(j) h["foo"].should.equal "bar" end + + specify "should be able to delete the given key case-sensitively" do + h = Rack::Utils::HeaderHash.new("foo" => "bar") + h.delete("foo") + h["foo"].should.be.nil + h["FOO"].should.be.nil + end + + specify "should be able to delete the given key case-insensitively" do + h = Rack::Utils::HeaderHash.new("foo" => "bar") + h.delete("FOO") + h["foo"].should.be.nil + h["FOO"].should.be.nil + end + + specify "should return the deleted value when #delete is called on an existing key" do + h = Rack::Utils::HeaderHash.new("foo" => "bar") + h.delete("Foo").should.equal("bar") + end + + specify "should return nil when #delete is called on a non-existant key" do + h = Rack::Utils::HeaderHash.new("foo" => "bar") + h.delete("Hello").should.be.nil + end end context "Rack::Utils::Context" do -- 1.6.0.5