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
|
|
|
|
it "should be gettable" do
|
|
|
|
Foo.base_uri.should == 'http://api.foo.com/v1'
|
2008-07-27 11:52:18 -04:00
|
|
|
end
|
|
|
|
|
2008-07-28 12:08:21 -04:00
|
|
|
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'
|
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:08:21 -04:00
|
|
|
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
|
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
|
|
|
|
Foo.instance_variable_get("@format").should == 'xml'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow json" do
|
|
|
|
Foo.format :json
|
|
|
|
Foo.instance_variable_get("@format").should == 'json'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not allow funky format' do
|
|
|
|
lambda do
|
|
|
|
Foo.format :foobar
|
|
|
|
end.should raise_error(HTTParty::UnsupportedFormat)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-28 12:08:21 -04:00
|
|
|
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
|
|
|
|
|
2008-07-28 11:56:58 -04:00
|
|
|
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|
|
|
|
|
Foo.send(:format_from_path, "/foo/bar.#{ext}").should == ext
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should NOT work if there is extension but extention is not allow format" do
|
|
|
|
Foo.send(:format_from_path, '/foo/bar.php').should == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should NOT work if there is no extension' do
|
|
|
|
['', '.'].each do |ext|
|
|
|
|
Foo.send(:format_from_path, "/foo/bar#{ext}").should == nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'parsing responses' do
|
2008-07-28 12:08:21 -04:00
|
|
|
it 'should handle xml automatically' do
|
|
|
|
xml = %q[<books><book><id>1234</id><name>Foo Bar!</name></book></books>]
|
2008-07-28 11:56:58 -04:00
|
|
|
Foo.format :xml
|
|
|
|
Foo.send(:parse_response, xml).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
|
|
|
|
end
|
|
|
|
|
2008-07-28 12:08:21 -04:00
|
|
|
it 'should handle json automatically' do
|
2008-07-28 11:56:58 -04:00
|
|
|
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!'}}}
|
|
|
|
end
|
|
|
|
end
|
2008-07-27 11:52:18 -04:00
|
|
|
end
|