Skip to content

Commit

Permalink
fix BodyProxy#close
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Haase <konstantin.mailinglists@googlemail.com>
  • Loading branch information
Jan Xie authored and rkh committed Jun 16, 2011
1 parent 069a89d commit 81a2a75
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/rack/body_proxy.rb
@@ -1,17 +1,25 @@
module Rack module Rack
class BodyProxy class BodyProxy
def initialize(body, &block) def initialize(body, &block)
@body, @block = body, block @body, @block, @closed = body, block, false
end end


def respond_to?(*args) def respond_to?(*args)
super or @body.respond_to?(*args) super or @body.respond_to?(*args)
end end


def close def close
@body.close if @body.respond_to? :close return if closed?
ensure begin
@block.call @body.close if @body.respond_to? :close
ensure
@block.call
@closed = true
end
end

def closed?
@closed
end end


def method_missing(*args, &block) def method_missing(*args, &block)
Expand Down
9 changes: 9 additions & 0 deletions test/spec_body_proxy.rb
@@ -0,0 +1,9 @@
describe Rack::BodyProxy do
should 'not close more than one time' do
count = 0
proxy = Rack::BodyProxy.new([]) { count += 1 }
proxy.close
proxy.close
count.should == 1
end
end

0 comments on commit 81a2a75

Please sign in to comment.