summary refs log tree commit
diff options
context:
space:
mode:
authorHongli Lai (Phusion) <hongli@phusion.nl>2009-06-17 17:20:40 -0500
committerJoshua Peek <josh@joshpeek.com>2009-06-17 17:20:40 -0500
commit07c18145496fe1833b20d31856f70a2bc50db18b (patch)
treef0c187c02eb8e1edea9579ae65f3f719ecc0e510
parente0322ed04b419e88a4790f63eb402cdce0f6a528 (diff)
downloadrack-07c18145496fe1833b20d31856f70a2bc50db18b.tar.gz
Fix Rack::Utils::HeaderHash#delete: it's supposed to return the deleted value, or nil if the key doesn't exist. [#54 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
-rw-r--r--lib/rack/utils.rb4
-rw-r--r--test/spec_rack_utils.rb24
2 files changed, 27 insertions, 1 deletions
diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb
index 0899c106..34f43ce2 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 36b4e7b4..20abff53 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