Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Have MockRequest call close on the body rather than MockResponse.

That way close is called automatically when testing just with
vanilla Rack, but not called twice when using other testing libs
like rack-test.

Related to #191.
  • Loading branch information...
commit 9cc14bd95e4b5cdc39596c7da746aef685fc99df 1 parent fb3532e
@rkh rkh authored
Showing with 20 additions and 8 deletions.
  1. +4 −2 lib/rack/mock.rb
  2. +16 −6 test/spec_mock.rb
View
6 lib/rack/mock.rb
@@ -68,7 +68,10 @@ def request(method="GET", uri="", opts={})
end
errors = env["rack.errors"]
- MockResponse.new(*(app.call(env) + [errors]))
+ status, headers, body = app.call(env)
+ MockResponse.new(status, headers, body, errors)
+ ensure
+ body.close if body.respond_to?(:close)
end
# Return the Rack environment used for a request to +uri+.
@@ -154,7 +157,6 @@ def initialize(status, headers, body, errors=StringIO.new(""))
@body_string = nil
super(body, status, headers)
- body.close if body.respond_to?(:close)
end
def =~(other)
View
22 test/spec_mock.rb
@@ -1,5 +1,6 @@
require 'yaml'
require 'rack/mock'
+require 'stringio'
app = lambda { |env|
req = Rack::Request.new(env)
@@ -183,6 +184,15 @@
get("https://bla.example.org:9292/meh/foo?bar", :lint => true)
}.should.not.raise(Rack::Lint::LintError)
end
+
+ should "call close on the original body object" do
+ called = false
+ body = Rack::BodyProxy.new(['hi']) { called = true }
+ capp = proc { |e| [200, {'Content-Type' => 'text/plain '}, body] }
+ called.should.equal false
+ Rack::MockRequest.new(capp).get('/', :lint => true)
+ called.should.equal true
+ end
end
describe Rack::MockResponse do
@@ -233,12 +243,12 @@
res.errors.should.include "foo"
end
- should "call close on the original body object" do
- called = false
- body = Rack::BodyProxy.new(['hi']) { called = true }
- called.should.equal false
- Rack::MockResponse.new(200, {}, body)
- called.should.equal true
+ should "allow calling body.close afterwards" do
+ # this is exactly what rack-test does
+ body = StringIO.new("hi")
+ res = Rack::MockResponse.new(200, {}, body)
+ body.close if body.respond_to?(:close)
+ res.body.should == 'hi'
end
should "optionally make Rack errors fatal" do
Please sign in to comment.
Something went wrong with that request. Please try again.