Skip to content

Commit

Permalink
Added support for Net::HTTP::Post#body_stream (fixed compatibility wi…
Browse files Browse the repository at this point in the history
…th new versions of RestClient)
  • Loading branch information
Bartosz Blimke committed Jan 31, 2010
1 parent e4d5d7b commit 689bd29
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/webmock/http_lib_adapters/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ def request_with_webmock(request, body = nil, &block)

headers.reject! {|k,v| k =~ /[Aa]uthorization/ && v =~ /^Basic / } #we added it to url userinfo

request.set_body_internal body
if request.body_stream
body = request.body_stream.read
request.body_stream = nil
end

if body != nil && body.respond_to?(:read)
request.set_body_internal body.read
else
request.set_body_internal body
end

request_signature = WebMock::RequestSignature.new(method, uri, :body => request.body, :headers => headers)

Expand Down
8 changes: 8 additions & 0 deletions spec/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
req.body = "my_params"
Net::HTTP.start("www.example.com") { |http| http.request(req)}.body.should == "abc"
end

it "should handle Net::HTTP::Post#body_stream" do
stub_http_request(:post, "www.example.com").with(:body => "my_params").to_return(:body => "abc")
req = Net::HTTP::Post.new("/")
req.body_stream = StringIO.new("my_params")
Net::HTTP.start("www.example.com") { |http| http.request(req)}.body.should == "abc"
end

it "should behave like Net::HTTP and raise error if both request body and body argument are set" do
stub_http_request(:post, "www.example.com").with(:body => "my_params").to_return(:body => "abc")
Expand All @@ -39,4 +46,5 @@
}.should raise_error("both of body argument and HTTPRequest#body set")
end


end

0 comments on commit 689bd29

Please sign in to comment.