1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00
rest-client--rest-client/spec/resource_spec.rb
2009-12-29 18:27:39 +01:00

75 lines
3.1 KiB
Ruby

require File.dirname(__FILE__) + '/base'
describe RestClient::Resource do
before do
@resource = RestClient::Resource.new('http://some/resource', :user => 'jane', :password => 'mypass', :headers => { 'X-Something' => '1'})
end
context "Resource delegation" do
it "GET" do
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => { 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
@resource.get
end
it "POST" do
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')
@resource.post 'abc', :content_type => 'image/jpg'
end
it "PUT" do
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')
@resource.put 'abc', :content_type => 'image/jpg'
end
it "DELETE" do
RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => { 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
@resource.delete
end
it "overrides resource headers" do
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => { 'X-Something' => '2'}, :user => 'jane', :password => 'mypass')
@resource.get 'X-Something' => '2'
end
end
it "can instantiate with no user/password" do
@resource = RestClient::Resource.new('http://some/resource')
end
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
it "concatenates urls, inserting a slash when it needs one" do
@resource.concat_urls('http://example.com', 'resource').should == 'http://example.com/resource'
end
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
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
it "concatenates even non-string urls, :posts + 1 => 'posts/1'" do
@resource.concat_urls(:posts, 1).should == 'posts/1'
end
it "offers subresources via []" do
parent = RestClient::Resource.new('http://example.com')
parent['posts'].url.should == 'http://example.com/posts'
end
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
it "prints its url with to_s" do
RestClient::Resource.new('x').to_s.should == 'x'
end
end