Permalink
Browse files

Fixed FakeWeb::Response so that it preserves the original behavior of…

… Net::HTTP.

When making a request with a block that calls #read_body on the request, Net::HTTP causes the body to be set to a Net::ReadAdapter, but FakeWeb was causing the body to be set to a string.
  • Loading branch information...
1 parent 809ab3d commit fe28bfa8f9e586a3e03db2b73deca7cbae700058 @myronmarston committed Sep 8, 2010
Showing with 17 additions and 1 deletion.
  1. +5 −1 lib/fake_web/response.rb
  2. +12 −0 test/test_fake_web.rb
@@ -2,7 +2,11 @@ module FakeWeb
module Response #:nodoc:
def read_body(*args, &block)
- yield @body if block_given?
+ if block
+ old_body = @body
+ @body = Net::ReadAdapter.new(block)
+ @body << old_body
+ end
@body
end
View
@@ -252,6 +252,18 @@ def test_request_with_registered_response_yields_the_response_body_to_a_request_
assert_equal "content", body
end
+ def test_returns_net_read_adapter_from_response_body_for_real_request_with_block_and_read_body
+ FakeWeb.allow_net_connect = true
+ response = Net::HTTP.new('example.com', 80).request_get('/') { |r| r.read_body { } }
+ assert_equal Net::ReadAdapter, response.body.class
+ end
+
+ def test_returns_net_read_adapter_from_response_body_for_stubbed_request_with_block_and_read_body
+ FakeWeb.register_uri(:get, 'http://example.com/', :body => 'content')
+ response = Net::HTTP.new('example.com', 80).request_get('/') { |r| r.read_body { } }
+ assert_equal Net::ReadAdapter, response.body.class
+ end
+
def test_mock_request_with_undocumented_full_uri_argument_style
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
response = Net::HTTP.start('mock') { |query| query.get('http://mock/test_example.txt') }

0 comments on commit fe28bfa

Please sign in to comment.