Skip to content

Commit

Permalink
Improved handling exceptions in Patron adapter, to reflect error hand…
Browse files Browse the repository at this point in the history
…ling in Patron.
  • Loading branch information
Bartosz Blimke committed Feb 17, 2010
1 parent d8c6d7d commit 863ef8c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
16 changes: 10 additions & 6 deletions lib/webmock/http_lib_adapters/patron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ def build_request_signature(req)
uri.user = req.username
uri.password = req.password

if req.file_name && [:put, :post].include?(req.action)
if !File.exist?(req.file_name) || !File.readable?(req.file_name)
raise ArgumentError.new("Unable to open specified file.")
if [:put, :post].include?(req.action)
if req.file_name
if !File.exist?(req.file_name) || !File.readable?(req.file_name)
raise ArgumentError.new("Unable to open specified file.")
end
request_body = File.read(req.file_name)
elsif req.upload_data
request_body = req.upload_data
else
raise ArgumentError.new("Must provide either data or a filename when doing a PUT or POST")
end
request_body = File.read(req.file_name)
else
request_body = req.upload_data
end

request_signature = WebMock::RequestSignature.new(
Expand Down
9 changes: 9 additions & 0 deletions spec/patron_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@

end

describe "handling errors same way as patron" do
it "should raise error if put request has neither upload_data nor file_name" do
stub_http_request(:post, "www.example.com")
lambda {
@sess.post("/", nil)
}.should raise_error(ArgumentError, "Must provide either data or a filename when doing a PUT or POST")
end
end

it "should work with WebDAV copy request" do
stub_http_request(:copy, "www.example.com/abc").with(:headers => {'Destination' => "/def"})
@sess.copy("/abc", "/def")
Expand Down
30 changes: 15 additions & 15 deletions spec/webmock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ class MyException < StandardError; end;
stub_http_request(:get, "www.example.com")
http_request(:get, "http://www.example.com/").status.should == "200"
lambda {
http_request(:post, "http://www.example.com/")
http_request(:delete, "http://www.example.com/")
}.should fail_with(client_specific_request_string(
"Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/")
"Real HTTP connections are disabled. Unregistered request: DELETE http://www.example.com/")
)
end

Expand Down Expand Up @@ -335,7 +335,7 @@ class MyException < StandardError; end;

it "should return evaluated response headers" do
stub_http_request(:post, "www.example.com").to_return(:headers => lambda { |request| request.headers })
http_request(:post, "http://www.example.com/", :headers => {'A' => 'B'}).headers['A'].should == 'B'
http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'B'}).headers['A'].should == 'B'
end

end
Expand Down Expand Up @@ -578,7 +578,7 @@ class MyException < StandardError; end;

it "should fail if request was executed with different method" do
lambda {
http_request(:post, "http://www.example.com/")
http_request(:post, "http://www.example.com/", :body => "abc")
request(:get, "http://www.example.com").should have_been_made
}.should fail_with("The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times")
end
Expand Down Expand Up @@ -671,8 +671,8 @@ class MyException < StandardError; end;

it "should succeed if request was executed with the same body" do
lambda {
http_request(:get, "http://www.example.com/", :body => "abc")
request(:get, "www.example.com").with(:body => "abc").should have_been_made
http_request(:post, "http://www.example.com/", :body => "abc")
request(:post, "www.example.com").with(:body => "abc").should have_been_made
}.should_not raise_error
end

Expand All @@ -686,8 +686,8 @@ class MyException < StandardError; end;

it "should succeed if request was executed with the same body" do
lambda {
http_request(:get, "http://www.example.com/", :body => "abc")
request(:get, "www.example.com").with(:body => /^abc$/).should have_been_made
http_request(:post, "http://www.example.com/", :body => "abc")
request(:post, "www.example.com").with(:body => /^abc$/).should have_been_made
}.should_not raise_error
end

Expand Down Expand Up @@ -768,7 +768,7 @@ class MyException < StandardError; end;

it "should fail if request was executed and block evaluated to false" do
lambda {
http_request(:post, "http://www.example.com/")
http_request(:post, "http://www.example.com/", :body => "abc")
request(:post, "www.example.com").with { |req| req.body == "wadus" }.should have_been_made
}.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times")
end
Expand Down Expand Up @@ -835,8 +835,8 @@ class MyException < StandardError; end;

it "should verify expected requests occured" do
lambda {
http_request(:get, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
WebMock.should have_requested(:get, "http://www.example.com").with(:body => "abc", :headers => {'A' => 'a'}).once
http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
WebMock.should have_requested(:post, "http://www.example.com").with(:body => "abc", :headers => {'A' => 'a'}).once
}.should_not raise_error
end

Expand All @@ -856,7 +856,7 @@ class MyException < StandardError; end;

it "should fail if request was executed and block evaluated to false" do
lambda {
http_request(:post, "http://www.example.com/")
http_request(:post, "http://www.example.com/", :body => "abc")
WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
}.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times")
end
Expand All @@ -883,8 +883,8 @@ class MyException < StandardError; end;

it "should verify expected requests occured" do
lambda {
http_request(:get, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
assert_requested(:get, "http://www.example.com", :body => "abc", :headers => {'A' => 'a'})
http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
assert_requested(:post, "http://www.example.com", :body => "abc", :headers => {'A' => 'a'})
}.should_not raise_error
end

Expand All @@ -911,7 +911,7 @@ class MyException < StandardError; end;

it "should verify if request was executed and block evaluated to false" do
lambda {
http_request(:post, "http://www.example.com/")
http_request(:post, "http://www.example.com/", :body => "abc")
assert_requested(:post, "www.example.com") { |req| req.body == "wadus" }
}.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times")
end
Expand Down

0 comments on commit 863ef8c

Please sign in to comment.