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

Added #default_params method. Tweaked specs a bit.

This commit is contained in:
John Nunemaker 2008-07-28 12:08:21 -04:00
parent 0a1737e276
commit 0c05dcda55
2 changed files with 58 additions and 47 deletions

View file

@ -32,6 +32,20 @@ module HTTParty
@auth = {:username => u, :password => p}
end
def default_params(h)
raise ArgumentError, 'Headers must be a hash' unless h.is_a?(Hash)
@default_params ||= {}
return @default_params if h.blank?
@default_params.merge!(h)
end
def headers(h={})
raise ArgumentError, 'Headers must be a hash' unless h.is_a?(Hash)
@headers ||= {}
return @headers if h.blank?
@headers.merge!(h)
end
def http
if @http.blank?
uri = URI.parse(base_uri)
@ -43,13 +57,6 @@ module HTTParty
@http
end
def headers(h={})
raise ArgumentError, 'Headers must be a hash' unless h.is_a?(Hash)
@headers ||= {}
return @headers if h.blank?
@headers.merge!(h)
end
def get(path, options={})
send_request 'get', path, options
end

View file

@ -12,44 +12,44 @@ end
describe HTTParty do
it 'should be able to get the base_uri' do
describe "base uri" do
it "should be gettable" do
Foo.base_uri.should == 'http://api.foo.com/v1'
end
it 'should be able to set the base_uri' do
it 'should be setable' do
Foo.base_uri('http://api.foobar.com')
Foo.base_uri.should == 'http://api.foobar.com'
end
it "should add http if not present for non ssl requests" do
Foo.base_uri('api.foobar.com')
Foo.base_uri.should == 'http://api.foobar.com'
end
it 'should set scheme to https if port 443' do
it "should add https if not present for ssl requests" do
FooWithHttps.base_uri.should == 'https://api.foo.com/v1:443'
end
describe 'http' do
it "should use ssl for port 443" do
FooWithHttps.http.use_ssl?.should == true
end
it 'should not use ssl for port 80' do
Foo.base_uri('foobar.com')
Foo.http.use_ssl?.should == false
end
end
it "should initialize headers to empty hash" do
describe "headers" do
it "should default to empty hash" do
Foo.headers.should == {}
end
it "should allow updating the headers" do
it "should be able to be updated" do
init_headers = {:foo => 'bar', :baz => 'spax'}
Foo.headers init_headers
Foo.headers.should == init_headers
end
end
it 'should be able to set basic authentication' do
describe "basic http authentication" do
it "should work" do
Foo.basic_auth 'foobar', 'secret'
Foo.instance_variable_get("@auth").should == {:username => 'foobar', :password => 'secret'}
end
end
describe "format" do
it "should allow xml" do
@ -69,6 +69,17 @@ describe HTTParty do
end
end
describe 'http' do
it "should use ssl for port 443" do
FooWithHttps.http.use_ssl?.should == true
end
it 'should not use ssl for port 80' do
Foo.base_uri('foobar.com')
Foo.http.use_ssl?.should == false
end
end
describe "deriving format from path" do
it "should work if there is extension and extension is an allowed format" do
%w[xml json].each do |ext|
@ -88,20 +99,13 @@ describe HTTParty do
end
describe 'parsing responses' do
it 'should parse xml automatically' do
xml = <<EOF
<books>
<book>
<id>1234</id>
<name>Foo Bar!</name>
</book>
</books>
EOF
it 'should handle xml automatically' do
xml = %q[<books><book><id>1234</id><name>Foo Bar!</name></book></books>]
Foo.format :xml
Foo.send(:parse_response, xml).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
end
it 'should parse json automatically' do
it 'should handle json automatically' do
json = %q[{"books": {"book": {"name": "Foo Bar!", "id": "1234"}}}]
Foo.format :json
Foo.send(:parse_response, json).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}