1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Added some argument errors and specs.

This commit is contained in:
John Nunemaker 2008-07-28 13:32:35 -04:00
parent 36f24a417d
commit a8ff3e3639
2 changed files with 26 additions and 5 deletions

View file

@ -89,13 +89,14 @@ module HTTParty
# body => string for raw post data
# headers => hash of headers to send request with
def send_request(method, path, options={})
raise ArgumentError, 'only get, post, put and delete methods are supported' unless %w[get post put delete].include?(method.to_s)
raise ArgumentError, ':query must be a hash' if options[:query] && !options[:query].is_a?(Hash)
raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
# we always want path that begins with /
path = path.starts_with?('/') ? path : "/#{path}"
@format = format_from_path(path) unless @format
path = path.starts_with?('/') ? path : "/#{path}"
@format ||= format_from_path(path)
uri = URI.parse("#{base_uri}#{path}")
params = default_params
params.merge!(options[:query] || {})
uri.query = params.to_query
uri.query = default_params.merge(options[:query] || {}).to_query
klass = Net::HTTP.const_get method.to_s.downcase.capitalize
request = klass.new(uri.request_uri)
request.body = options[:body] unless options[:body].blank?

View file

@ -123,4 +123,24 @@ describe HTTParty do
Foo.send(:parse_response, json).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
end
end
describe "sending requests" do
it "should not work with request method other than get, post, put, delete" do
lambda do
Foo.send(:send_request, 'foo', '/foo')
end.should raise_error(ArgumentError)
end
it 'should require that :query is a hash if present' do
lambda do
Foo.send(:send_request, 'get', '/foo', :query => 'string')
end.should raise_error(ArgumentError)
end
it 'should require that :headers is a hash if present' do
lambda do
Foo.send(:send_request, 'get', '/foo', :headers => 'string')
end.should raise_error(ArgumentError)
end
end
end