require 'test_helper' class HTTPAuthTest < Test::Unit::TestCase context "Creating new instance" do should "should take user and password" do mambanation = MambaNation::HTTPAuth.new('username', 'password') mambanation.username.should == 'username' mambanation.password.should == 'password' end should "accept options" do mambanation = MambaNation::HTTPAuth.new('username', 'password', :ssl => true) mambanation.options.should == {:ssl => true} end should "default ssl to false" do mambanation = MambaNation::HTTPAuth.new('username', 'password') mambanation.options[:ssl].should be(false) end should "use https if ssl is true" do MambaNation::HTTPAuth.expects(:base_uri).with('https://api.mambanation.com/v' + MambaNation::API_VERSION) mambanation = MambaNation::HTTPAuth.new('username', 'password', :ssl => true) end should "use http if ssl is false" do MambaNation::HTTPAuth.expects(:base_uri).with('http://api.mambanation.com/v' + MambaNation::API_VERSION) mambanation = MambaNation::HTTPAuth.new('username', 'password', :ssl => false) end should "use api version if provided" do MambaNation::HTTPAuth.expects(:base_uri).with('http://api.mambanation.com/v2') mambanation = MambaNation::HTTPAuth.new('username', 'password', {:ssl => false, :api_version => 2}) end should "not use api versioning if api_version is false " do MambaNation::HTTPAuth.expects(:base_uri).with('http://api.mambanation.com/v2') mambanation = MambaNation::HTTPAuth.new('username', 'password', {:ssl => false, :api_version => false}) end end context "Client methods" do setup do @mambanation = MambaNation::HTTPAuth.new('username', 'password') end should "not throw error when accessing response message" do stub_get('http://api.mambanation.com/users/4.json', 'user.json') response = @mambanation.get('/users/4.json') response.message.should == 'OK' end should "be able to get" do stub_get('http://username:password@api.mambanation.com/users/4.json', 'user.json') response = @mambanation.get('/users/4.json') response.should == fixture_file('user.json') end should "be able to get with headers" do @mambanation.class.expects(:get).with( '/users/4.json', { :basic_auth => {:username => 'username', :password => 'password'}, :headers => {'Foo' => 'Bar'} } ).returns(fixture_file('user.json')) @mambanation.get('/users/4.json', {'Foo' => 'Bar'}) end should "be able to post" do stub_post('http://username:password@api.mambanation.com/users/4.json', 'user.json') response = @mambanation.post('/users/4.json', :text => 'My update.') response.should == fixture_file('user.json') end should "be able to post with headers" do @mambanation.class.expects(:post).with( '/users/4.json', { :headers => {'Foo' => 'Bar'}, :body => {:text => 'My update.'}, :basic_auth => {:username => 'username', :password => 'password'} } ).returns(fixture_file('user.json')) @mambanation.post('/user/4.json', {:text => 'My update.'}, {'Foo' => 'Bar'}) end end end