Permalink
Browse files

fix BodyProxy#close

Signed-off-by: Konstantin Haase <konstantin.mailinglists@googlemail.com>
  • Loading branch information...
1 parent 4485088 commit 9f803f29b0f57bdc53a5c711785c04255f16728d @janx janx committed with rkh Jun 13, 2011
Showing with 21 additions and 4 deletions.
  1. +12 −4 lib/rack/body_proxy.rb
  2. +9 −0 test/spec_body_proxy.rb
View
16 lib/rack/body_proxy.rb
@@ -1,17 +1,25 @@
module Rack
class BodyProxy
def initialize(body, &block)
- @body, @block = body, block
+ @body, @block, @closed = body, block, false
end
def respond_to?(*args)
super or @body.respond_to?(*args)
end
def close
- @body.close if @body.respond_to? :close
- ensure
- @block.call
+ return if closed?
+ begin
+ @body.close if @body.respond_to? :close
+ ensure
+ @block.call
+ @closed = true
+ end
+ end
+
+ def closed?
+ @closed
end
def method_missing(*args, &block)
View
9 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 9f803f2

Please sign in to comment.