diff --git a/lib/httparty.rb b/lib/httparty.rb index d5aee4d..0fc8309 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -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 diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index 3274434..29a0796 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -12,43 +12,43 @@ end describe HTTParty do - it 'should be able to get the base_uri' do - Foo.base_uri.should == 'http://api.foo.com/v1' - end - - 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 + describe "base uri" do + it "should be gettable" do + Foo.base_uri.should == 'http://api.foo.com/v1' end - it 'should not use ssl for port 80' do - Foo.base_uri('foobar.com') - Foo.http.use_ssl?.should == false + 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 add https if not present for ssl requests" do + FooWithHttps.base_uri.should == 'https://api.foo.com/v1:443' end end - it "should initialize headers to empty hash" do - Foo.headers.should == {} + describe "headers" do + 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 - it "should allow updating the headers" do - init_headers = {:foo => 'bar', :baz => 'spax'} - Foo.headers init_headers - Foo.headers.should == init_headers - 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'} + 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 @@ -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 = < - - 1234 - Foo Bar! - - -EOF + it 'should handle xml automatically' do + xml = %q[1234Foo Bar!] 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!'}}}