2008-07-27 11:52:18 -04:00
|
|
|
require File.join(File.dirname(__FILE__), 'spec_helper')
|
|
|
|
|
2008-07-28 10:49:53 -04:00
|
|
|
describe HTTParty do
|
2009-01-28 23:41:01 -05:00
|
|
|
before(:each) do
|
|
|
|
@klass = Class.new
|
|
|
|
@klass.instance_eval { include HTTParty }
|
|
|
|
end
|
|
|
|
|
2008-07-28 12:08:21 -04:00
|
|
|
describe "base uri" do
|
2009-01-28 23:41:01 -05:00
|
|
|
before(:each) do
|
|
|
|
@klass.base_uri('api.foo.com/v1')
|
2008-11-08 13:59:57 -05:00
|
|
|
end
|
|
|
|
|
2008-11-08 11:18:25 -05:00
|
|
|
it "should have reader" do
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.base_uri.should == 'http://api.foo.com/v1'
|
2008-07-27 11:52:18 -04:00
|
|
|
end
|
|
|
|
|
2008-11-08 11:18:25 -05:00
|
|
|
it 'should have writer' do
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.base_uri('http://api.foobar.com')
|
|
|
|
@klass.base_uri.should == 'http://api.foobar.com'
|
2008-07-28 12:08:21 -04:00
|
|
|
end
|
2008-07-27 11:52:18 -04:00
|
|
|
end
|
|
|
|
|
2008-12-06 23:41:28 -05:00
|
|
|
describe "#normalize_base_uri" do
|
|
|
|
it "should add http if not present for non ssl requests" do
|
|
|
|
uri = HTTParty.normalize_base_uri('api.foobar.com')
|
|
|
|
uri.should == 'http://api.foobar.com'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should add https if not present for ssl requests" do
|
|
|
|
uri = HTTParty.normalize_base_uri('api.foo.com/v1:443')
|
|
|
|
uri.should == 'https://api.foo.com/v1:443'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not remove https for ssl requests" do
|
|
|
|
uri = HTTParty.normalize_base_uri('https://api.foo.com/v1:443')
|
|
|
|
uri.should == 'https://api.foo.com/v1:443'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-28 12:08:21 -04:00
|
|
|
describe "headers" do
|
|
|
|
it "should default to empty hash" do
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.headers.should == {}
|
2008-07-28 12:08:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able to be updated" do
|
|
|
|
init_headers = {:foo => 'bar', :baz => 'spax'}
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.headers init_headers
|
|
|
|
@klass.headers.should == init_headers
|
2008-07-28 12:08:21 -04:00
|
|
|
end
|
2008-07-28 11:56:58 -04:00
|
|
|
end
|
|
|
|
|
2008-07-28 12:40:40 -04:00
|
|
|
describe "default params" do
|
|
|
|
it "should default to empty hash" do
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.default_params.should == {}
|
2008-07-28 12:40:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able to be updated" do
|
|
|
|
new_defaults = {:foo => 'bar', :baz => 'spax'}
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.default_params new_defaults
|
|
|
|
@klass.default_params.should == new_defaults
|
2008-07-28 12:40:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-28 12:08:21 -04:00
|
|
|
describe "basic http authentication" do
|
|
|
|
it "should work" do
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.basic_auth 'foobar', 'secret'
|
|
|
|
@klass.default_options[:basic_auth].should == {:username => 'foobar', :password => 'secret'}
|
2008-07-28 12:08:21 -04:00
|
|
|
end
|
2008-07-27 11:52:18 -04:00
|
|
|
end
|
2008-07-28 10:55:35 -04:00
|
|
|
|
2008-07-28 11:56:58 -04:00
|
|
|
describe "format" do
|
|
|
|
it "should allow xml" do
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.format :xml
|
|
|
|
@klass.default_options[:format].should == :xml
|
2008-07-28 11:56:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow json" do
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.format :json
|
|
|
|
@klass.default_options[:format].should == :json
|
2008-07-28 11:56:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not allow funky format' do
|
|
|
|
lambda do
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.format :foobar
|
2008-07-28 11:56:58 -04:00
|
|
|
end.should raise_error(HTTParty::UnsupportedFormat)
|
|
|
|
end
|
|
|
|
end
|
2008-08-27 16:38:19 -04:00
|
|
|
|
2008-11-08 13:59:57 -05:00
|
|
|
describe "with explicit override of automatic redirect handling" do
|
2008-08-27 16:38:19 -04:00
|
|
|
|
2008-11-08 13:59:57 -05:00
|
|
|
it "should fail with redirected GET" do
|
|
|
|
lambda do
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.get('/foo', :no_follow => true)
|
2008-11-08 13:59:57 -05:00
|
|
|
end.should raise_error(HTTParty::RedirectionTooDeep)
|
2008-08-27 16:38:19 -04:00
|
|
|
end
|
2008-09-19 19:31:06 -04:00
|
|
|
|
2008-11-08 13:59:57 -05:00
|
|
|
it "should fail with redirected POST" do
|
|
|
|
lambda do
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.post('/foo', :no_follow => true)
|
2008-11-08 13:59:57 -05:00
|
|
|
end.should raise_error(HTTParty::RedirectionTooDeep)
|
|
|
|
end
|
2008-09-19 19:31:06 -04:00
|
|
|
|
2008-11-08 13:59:57 -05:00
|
|
|
it "should fail with redirected DELETE" do
|
|
|
|
lambda do
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.delete('/foo', :no_follow => true)
|
2008-11-08 13:59:57 -05:00
|
|
|
end.should raise_error(HTTParty::RedirectionTooDeep)
|
|
|
|
end
|
2008-09-19 19:31:06 -04:00
|
|
|
|
2008-11-08 13:59:57 -05:00
|
|
|
it "should fail with redirected PUT" do
|
|
|
|
lambda do
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.put('/foo', :no_follow => true)
|
2008-11-08 13:59:57 -05:00
|
|
|
end.should raise_error(HTTParty::RedirectionTooDeep)
|
2008-09-19 19:31:06 -04:00
|
|
|
end
|
2008-07-28 13:32:35 -04:00
|
|
|
end
|
2008-11-30 23:58:06 -05:00
|
|
|
|
|
|
|
describe "with multiple class definitions" do
|
2009-01-28 23:41:01 -05:00
|
|
|
before(:each) do
|
|
|
|
@klass.instance_eval do
|
|
|
|
base_uri "http://first.com"
|
|
|
|
default_params :one => 1
|
|
|
|
end
|
|
|
|
|
|
|
|
@additional_klass = Class.new
|
|
|
|
@additional_klass.instance_eval do
|
|
|
|
include HTTParty
|
|
|
|
base_uri "http://second.com"
|
|
|
|
default_params :two => 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-30 23:58:06 -05:00
|
|
|
it "should not run over each others options" do
|
2009-01-28 23:41:01 -05:00
|
|
|
@klass.default_options.should == { :base_uri => 'http://first.com', :default_params => { :one => 1 } }
|
|
|
|
@additional_klass.default_options.should == { :base_uri => 'http://second.com', :default_params => { :two => 2 } }
|
2008-11-30 23:58:06 -05:00
|
|
|
end
|
|
|
|
end
|
2008-12-06 19:41:23 -05:00
|
|
|
|
|
|
|
describe "#get" do
|
|
|
|
it "should be able to get html" do
|
|
|
|
stub_http_response_with('google.html')
|
|
|
|
HTTParty.get('http://www.google.com').should == file_fixture('google.html')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able parse response type json automatically" do
|
|
|
|
stub_http_response_with('twitter.json')
|
|
|
|
tweets = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
|
|
|
|
tweets.size.should == 20
|
|
|
|
tweets.first['user'].should == {
|
|
|
|
"name" => "Pyk",
|
|
|
|
"url" => nil,
|
|
|
|
"id" => "7694602",
|
|
|
|
"description" => nil,
|
|
|
|
"protected" => false,
|
|
|
|
"screen_name" => "Pyk",
|
|
|
|
"followers_count" => 1,
|
|
|
|
"location" => "Opera Plaza, California",
|
|
|
|
"profile_image_url" => "http://static.twitter.com/images/default_profile_normal.png"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able parse response type xml automatically" do
|
|
|
|
stub_http_response_with('twitter.xml')
|
|
|
|
tweets = HTTParty.get('http://twitter.com/statuses/public_timeline.xml')
|
|
|
|
tweets['statuses'].size.should == 20
|
|
|
|
tweets['statuses'].first['user'].should == {
|
|
|
|
"name" => "Magic 8 Bot",
|
|
|
|
"url" => nil,
|
|
|
|
"id" => "17656026",
|
|
|
|
"description" => "ask me a question",
|
|
|
|
"protected" => "false",
|
|
|
|
"screen_name" => "magic8bot",
|
|
|
|
"followers_count" => "90",
|
|
|
|
"profile_image_url" => "http://s3.amazonaws.com/twitter_production/profile_images/65565851/8ball_large_normal.jpg",
|
|
|
|
"location" => nil
|
|
|
|
}
|
|
|
|
end
|
2009-01-28 12:55:25 -05:00
|
|
|
|
|
|
|
it "should not get undefined method add_node for nil class for the following xml" do
|
|
|
|
stub_http_response_with('undefined_method_add_node_for_nil.xml')
|
|
|
|
result = HTTParty.get('http://foobar.com')
|
|
|
|
result.should == {"Entities"=>{"href"=>"https://s3-sandbox.parature.com/api/v1/5578/5633/Account", "results"=>"0", "total"=>"0", "page_size"=>"25", "page"=>"1"}}
|
|
|
|
end
|
2009-01-28 13:17:39 -05:00
|
|
|
|
|
|
|
it "should parse empty response fine" do
|
|
|
|
stub_http_response_with('empty.xml')
|
|
|
|
result = HTTParty.get('http://foobar.com')
|
|
|
|
result.should == nil
|
|
|
|
end
|
2008-12-06 19:41:23 -05:00
|
|
|
end
|
2008-11-08 12:05:59 -05:00
|
|
|
end
|