Skip to content

Commit

Permalink
Fix Rack::Utils::HeaderHash#delete: it's supposed to return the delet…
Browse files Browse the repository at this point in the history
…ed value, or nil if the key doesn't exist. [rack#54 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
FooBarWidget authored and josh committed Jun 17, 2009
1 parent e0322ed commit 07c1814
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/rack/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def context(env, app=@app)
# header when set.
class HeaderHash < Hash
def initialize(hash={})
super()
@names = {}
hash.each { |k, v| self[k] = v }
end
Expand Down Expand Up @@ -238,8 +239,9 @@ def []=(k, v)

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)
Expand Down
24 changes: 24 additions & 0 deletions test/spec_rack_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,30 @@
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
Expand Down

0 comments on commit 07c1814

Please sign in to comment.