2008-07-27 11:52:18 -04:00
|
|
|
require File.join(File.dirname(__FILE__), 'spec_helper')
|
|
|
|
|
|
|
|
class Foo
|
2008-07-28 10:49:53 -04:00
|
|
|
include HTTParty
|
2008-07-27 11:52:18 -04:00
|
|
|
base_uri 'api.foo.com/v1'
|
|
|
|
end
|
|
|
|
|
|
|
|
class FooWithHttps
|
2008-07-28 10:49:53 -04:00
|
|
|
include HTTParty
|
2008-07-27 11:52:18 -04:00
|
|
|
base_uri 'api.foo.com/v1:443'
|
|
|
|
end
|
|
|
|
|
2008-07-28 10:49:53 -04:00
|
|
|
describe HTTParty do
|
2008-07-27 11:52:18 -04:00
|
|
|
|
2008-07-28 12:08:21 -04:00
|
|
|
describe "base uri" do
|
2008-11-08 13:59:57 -05:00
|
|
|
before do
|
|
|
|
Foo.base_uri('api.foo.com/v1')
|
|
|
|
end
|
|
|
|
|
2008-11-08 11:18:25 -05:00
|
|
|
it "should have reader" do
|
2008-07-28 12:08:21 -04:00
|
|
|
Foo.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
|
2008-07-28 12:08:21 -04:00
|
|
|
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
|
2008-11-08 11:18:25 -05:00
|
|
|
Foo.base_uri('api.foo.com/v1:443')
|
|
|
|
Foo.base_uri.should == 'https://api.foo.com/v1:443'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not remove https for ssl requests" do
|
|
|
|
Foo.base_uri('https://api.foo.com/v1:443')
|
|
|
|
Foo.base_uri.should == 'https://api.foo.com/v1:443'
|
2008-07-27 11:52:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-28 12:08:21 -04:00
|
|
|
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
|
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
|
|
|
|
Foo.default_params.should == {}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able to be updated" do
|
|
|
|
new_defaults = {:foo => 'bar', :baz => 'spax'}
|
|
|
|
Foo.default_params new_defaults
|
|
|
|
Foo.default_params.should == new_defaults
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-28 12:08:21 -04:00
|
|
|
describe "basic http authentication" do
|
|
|
|
it "should work" do
|
|
|
|
Foo.basic_auth 'foobar', 'secret'
|
2008-11-08 13:59:57 -05:00
|
|
|
Foo.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
|
|
|
|
Foo.format :xml
|
2008-11-08 13:59:57 -05:00
|
|
|
Foo.default_options[:format].should == :xml
|
2008-07-28 11:56:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow json" do
|
|
|
|
Foo.format :json
|
2008-11-08 13:59:57 -05:00
|
|
|
Foo.default_options[:format].should == :json
|
2008-07-28 11:56:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not allow funky format' do
|
|
|
|
lambda do
|
|
|
|
Foo.format :foobar
|
|
|
|
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
|
|
|
|
Foo.get('/foo', :no_follow => true)
|
|
|
|
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
|
|
|
|
Foo.post('/foo', :no_follow => true)
|
|
|
|
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
|
|
|
|
Foo.delete('/foo', :no_follow => true)
|
|
|
|
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
|
|
|
|
Foo.put('/foo', :no_follow => true)
|
|
|
|
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-08 12:05:59 -05:00
|
|
|
end
|