Skip to content
Browse files

Merge pull request #152 from thunderboltlabs/master

Fixed a Rack::Lock bug around failing to close the response body
  • Loading branch information...
2 parents ed795fa + f51ce1e commit 265b2492b3ce79017aef9e09c3eda46eab230e92 @bblimke committed Feb 4, 2012
Showing with 22 additions and 0 deletions.
  1. +1 −0 lib/webmock/rack_response.rb
  2. +11 −0 spec/support/my_rack_app.rb
  3. +10 −0 spec/unit/rack_response_spec.rb
View
1 lib/webmock/rack_response.rb
@@ -19,6 +19,7 @@ def evaluate(request)
def body_from_rack_response(response)
body = ""
response.each { |line| body << line }
+ response.close if response.respond_to?(:close)
return body
end
View
11 spec/support/my_rack_app.rb
@@ -21,6 +21,8 @@ def self.call(env)
[200, {}, ["Hello, #{name}"]]
when ['GET', '/non_array_response']
[200, {}, NonArrayResponse.new]
+ when ['GET', '/locked']
+ [200, {}, ["Single threaded response."]]
when ['POST', '/greet']
name = env["rack.input"].read[/name=([^&]*)/, 1] || "World"
[200, {}, ["Good to meet you, #{name}!"]]
@@ -29,3 +31,12 @@ def self.call(env)
end
end
end
+
+class MyLockedRackApp
+ MUTEX = Mutex.new
+
+ def self.call(env)
+ lock = Rack::Lock.new(MyRackApp, MUTEX)
+ lock.call(env)
+ end
+end
View
10 spec/unit/rack_response_spec.rb
@@ -3,6 +3,7 @@
describe WebMock::RackResponse do
before :each do
@rack_response = WebMock::RackResponse.new(MyRackApp)
+ @locked_rack_response = WebMock::RackResponse.new(MyLockedRackApp)
end
it "should hook up to a rack appliance" do
@@ -21,6 +22,15 @@
response.body.should include('This is not in an array!')
end
+ it "should shouldn't blow up when hitting a locked resource twice" do
+ request = WebMock::RequestSignature.new(:get, 'www.example.com/locked')
+ response = @locked_rack_response.evaluate(request)
+ response2 = @locked_rack_response.evaluate(request)
+
+ response2.body.should include('Single threaded response.')
+ response2.status.first.should == 200
+ end
+
it "should send along params" do
request = WebMock::RequestSignature.new(:get, 'www.example.com/greet?name=Johnny')

0 comments on commit 265b249

Please sign in to comment.
Something went wrong with that request. Please try again.