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:
parent
0a1737e276
commit
0c05dcda55
2 changed files with 58 additions and 47 deletions
|
@ -32,6 +32,20 @@ module HTTParty
|
||||||
@auth = {:username => u, :password => p}
|
@auth = {:username => u, :password => p}
|
||||||
end
|
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
|
def http
|
||||||
if @http.blank?
|
if @http.blank?
|
||||||
uri = URI.parse(base_uri)
|
uri = URI.parse(base_uri)
|
||||||
|
@ -43,13 +57,6 @@ module HTTParty
|
||||||
@http
|
@http
|
||||||
end
|
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={})
|
def get(path, options={})
|
||||||
send_request 'get', path, options
|
send_request 'get', path, options
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,43 +12,43 @@ end
|
||||||
|
|
||||||
describe HTTParty do
|
describe HTTParty do
|
||||||
|
|
||||||
it 'should be able to get the base_uri' do
|
describe "base uri" do
|
||||||
Foo.base_uri.should == 'http://api.foo.com/v1'
|
it "should be gettable" do
|
||||||
end
|
Foo.base_uri.should == 'http://api.foo.com/v1'
|
||||||
|
|
||||||
it 'should be able to set the base_uri' 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
|
|
||||||
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
|
end
|
||||||
|
|
||||||
it 'should not use ssl for port 80' do
|
it 'should be setable' do
|
||||||
Foo.base_uri('foobar.com')
|
Foo.base_uri('http://api.foobar.com')
|
||||||
Foo.http.use_ssl?.should == false
|
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 add https if not present for ssl requests" do
|
||||||
|
FooWithHttps.base_uri.should == 'https://api.foo.com/v1:443'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should initialize headers to empty hash" do
|
describe "headers" do
|
||||||
Foo.headers.should == {}
|
it "should default to empty hash" do
|
||||||
|
Foo.headers.should == {}
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
it "should allow updating the headers" do
|
describe "basic http authentication" do
|
||||||
init_headers = {:foo => 'bar', :baz => 'spax'}
|
it "should work" do
|
||||||
Foo.headers init_headers
|
Foo.basic_auth 'foobar', 'secret'
|
||||||
Foo.headers.should == init_headers
|
Foo.instance_variable_get("@auth").should == {:username => 'foobar', :password => 'secret'}
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should be able to set basic authentication' do
|
|
||||||
Foo.basic_auth 'foobar', 'secret'
|
|
||||||
Foo.instance_variable_get("@auth").should == {:username => 'foobar', :password => 'secret'}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "format" do
|
describe "format" do
|
||||||
|
@ -69,6 +69,17 @@ describe HTTParty do
|
||||||
end
|
end
|
||||||
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
|
describe "deriving format from path" do
|
||||||
it "should work if there is extension and extension is an allowed format" do
|
it "should work if there is extension and extension is an allowed format" do
|
||||||
%w[xml json].each do |ext|
|
%w[xml json].each do |ext|
|
||||||
|
@ -88,20 +99,13 @@ describe HTTParty do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'parsing responses' do
|
describe 'parsing responses' do
|
||||||
it 'should parse xml automatically' do
|
it 'should handle xml automatically' do
|
||||||
xml = <<EOF
|
xml = %q[<books><book><id>1234</id><name>Foo Bar!</name></book></books>]
|
||||||
<books>
|
|
||||||
<book>
|
|
||||||
<id>1234</id>
|
|
||||||
<name>Foo Bar!</name>
|
|
||||||
</book>
|
|
||||||
</books>
|
|
||||||
EOF
|
|
||||||
Foo.format :xml
|
Foo.format :xml
|
||||||
Foo.send(:parse_response, xml).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
|
Foo.send(:parse_response, xml).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should parse json automatically' do
|
it 'should handle json automatically' do
|
||||||
json = %q[{"books": {"book": {"name": "Foo Bar!", "id": "1234"}}}]
|
json = %q[{"books": {"book": {"name": "Foo Bar!", "id": "1234"}}}]
|
||||||
Foo.format :json
|
Foo.format :json
|
||||||
Foo.send(:parse_response, json).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
|
Foo.send(:parse_response, json).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
|
||||||
|
|
Loading…
Reference in a new issue