2010-07-03 06:41:59 -04:00
|
|
|
require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
|
2008-03-10 20:20:57 -04:00
|
|
|
|
2010-01-27 16:54:18 -05:00
|
|
|
require 'webmock/rspec'
|
|
|
|
include WebMock
|
|
|
|
|
2008-03-10 20:20:57 -04:00
|
|
|
describe RestClient::Resource do
|
2009-12-29 12:27:39 -05:00
|
|
|
before do
|
2010-05-25 12:43:18 -04:00
|
|
|
@resource = RestClient::Resource.new('http://some/resource', :user => 'jane', :password => 'mypass', :headers => {'X-Something' => '1'})
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2008-03-10 20:20:57 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
context "Resource delegation" do
|
|
|
|
it "GET" do
|
2010-05-25 12:43:18 -04:00
|
|
|
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
2009-12-29 12:27:39 -05:00
|
|
|
@resource.get
|
|
|
|
end
|
2008-03-10 20:20:57 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "POST" do
|
2010-05-25 12:43:18 -04:00
|
|
|
RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
2009-12-29 12:27:39 -05:00
|
|
|
@resource.post 'abc', :content_type => 'image/jpg'
|
|
|
|
end
|
2008-03-10 20:20:57 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "PUT" do
|
2010-05-25 12:43:18 -04:00
|
|
|
RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
2009-12-29 12:27:39 -05:00
|
|
|
@resource.put 'abc', :content_type => 'image/jpg'
|
|
|
|
end
|
2008-03-10 20:20:57 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "DELETE" do
|
2010-05-25 12:43:18 -04:00
|
|
|
RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
2009-12-29 12:27:39 -05:00
|
|
|
@resource.delete
|
|
|
|
end
|
2008-10-13 21:30:44 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "overrides resource headers" do
|
2010-05-25 12:43:18 -04:00
|
|
|
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '2'}, :user => 'jane', :password => 'mypass')
|
2009-12-29 12:27:39 -05:00
|
|
|
@resource.get 'X-Something' => '2'
|
|
|
|
end
|
|
|
|
end
|
2008-03-10 21:30:14 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "can instantiate with no user/password" do
|
|
|
|
@resource = RestClient::Resource.new('http://some/resource')
|
|
|
|
end
|
2008-06-20 23:18:32 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "is backwards compatible with previous constructor" do
|
|
|
|
@resource = RestClient::Resource.new('http://some/resource', 'user', 'pass')
|
|
|
|
@resource.user.should == 'user'
|
|
|
|
@resource.password.should == 'pass'
|
|
|
|
end
|
2008-10-13 20:44:04 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "concatenates urls, inserting a slash when it needs one" do
|
|
|
|
@resource.concat_urls('http://example.com', 'resource').should == 'http://example.com/resource'
|
|
|
|
end
|
2008-06-20 23:18:32 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "concatenates urls, using no slash if the first url ends with a slash" do
|
|
|
|
@resource.concat_urls('http://example.com/', 'resource').should == 'http://example.com/resource'
|
|
|
|
end
|
2008-06-20 23:18:32 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "concatenates urls, using no slash if the second url starts with a slash" do
|
|
|
|
@resource.concat_urls('http://example.com', '/resource').should == 'http://example.com/resource'
|
|
|
|
end
|
2008-06-20 23:18:32 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "concatenates even non-string urls, :posts + 1 => 'posts/1'" do
|
|
|
|
@resource.concat_urls(:posts, 1).should == 'posts/1'
|
|
|
|
end
|
2008-06-20 23:31:17 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "offers subresources via []" do
|
|
|
|
parent = RestClient::Resource.new('http://example.com')
|
|
|
|
parent['posts'].url.should == 'http://example.com/posts'
|
|
|
|
end
|
2008-10-13 20:44:04 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "transports options to subresources" do
|
|
|
|
parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password')
|
|
|
|
parent['posts'].user.should == 'user'
|
|
|
|
parent['posts'].password.should == 'password'
|
|
|
|
end
|
2008-11-01 20:23:38 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "prints its url with to_s" do
|
|
|
|
RestClient::Resource.new('x').to_s.should == 'x'
|
|
|
|
end
|
2010-01-27 16:54:18 -05:00
|
|
|
|
|
|
|
describe 'block' do
|
|
|
|
it 'can use block when creating the resource' do
|
|
|
|
stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
|
2010-05-25 12:43:18 -04:00
|
|
|
resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
|
2010-01-27 16:54:18 -05:00
|
|
|
resource.get.should == 'foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can use block when executing the resource' do
|
|
|
|
stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
|
|
|
|
resource = RestClient::Resource.new('www.example.com')
|
2010-05-25 12:43:18 -04:00
|
|
|
resource.get { |response, request| 'foo' }.should == 'foo'
|
2010-01-27 16:54:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'execution block override resource block' do
|
|
|
|
stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
|
2010-05-25 12:43:18 -04:00
|
|
|
resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
|
|
|
|
resource.get { |response, request| 'bar' }.should == 'bar'
|
2010-01-27 16:54:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2008-03-10 20:20:57 -04:00
|
|
|
end
|