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

100 lines
4 KiB
Ruby
Raw Normal View History

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
@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
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
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
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
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
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
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
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
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
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)
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')
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)
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