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/unit/resource_spec.rb

135 lines
5.7 KiB
Ruby
Raw Permalink Normal View History

require_relative '_lib'
2010-01-27 16:54:18 -05:00
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
2016-10-24 12:02:23 -04:00
expect(RestClient::Request).to receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
2009-12-29 12:27:39 -05:00
@resource.get
end
2008-03-10 20:20:57 -04:00
2010-09-16 09:59:41 -04:00
it "HEAD" do
2016-10-24 12:02:23 -04:00
expect(RestClient::Request).to receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
2010-09-16 09:59:41 -04:00
@resource.head
end
2009-12-29 12:27:39 -05:00
it "POST" do
2016-10-24 12:02:23 -04:00
expect(RestClient::Request).to receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
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
2016-10-24 12:02:23 -04:00
expect(RestClient::Request).to receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
2009-12-29 12:27:39 -05:00
@resource.put 'abc', :content_type => 'image/jpg'
end
2008-03-10 20:20:57 -04:00
it "PATCH" do
2016-10-24 12:02:23 -04:00
expect(RestClient::Request).to receive(:execute).with(:method => :patch, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
@resource.patch 'abc', :content_type => 'image/jpg'
end
2009-12-29 12:27:39 -05:00
it "DELETE" do
2016-10-24 12:02:23 -04:00
expect(RestClient::Request).to receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
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
2016-10-24 12:02:23 -04:00
expect(RestClient::Request).to receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '2'}, :user => 'jane', :password => 'mypass', :log => nil)
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')
expect(@resource.user).to eq 'user'
expect(@resource.password).to eq 'pass'
2009-12-29 12:27:39 -05:00
end
2009-12-29 12:27:39 -05:00
it "concatenates urls, inserting a slash when it needs one" do
expect(@resource.concat_urls('http://example.com', 'resource')).to eq 'http://example.com/resource'
2009-12-29 12:27:39 -05:00
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
expect(@resource.concat_urls('http://example.com/', 'resource')).to eq 'http://example.com/resource'
2009-12-29 12:27:39 -05:00
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
expect(@resource.concat_urls('http://example.com', '/resource')).to eq 'http://example.com/resource'
2009-12-29 12:27:39 -05:00
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
expect(@resource.concat_urls(:posts, 1)).to eq 'posts/1'
2009-12-29 12:27:39 -05:00
end
2009-12-29 12:27:39 -05:00
it "offers subresources via []" do
parent = RestClient::Resource.new('http://example.com')
expect(parent['posts'].url).to eq 'http://example.com/posts'
2009-12-29 12:27:39 -05:00
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')
expect(parent['posts'].user).to eq 'user'
expect(parent['posts'].password).to eq 'password'
2009-12-29 12:27:39 -05:00
end
2008-11-01 20:23:38 -04:00
2010-07-03 07:30:58 -04:00
it "passes a given block to subresources" do
block = proc {|r| r}
2010-07-03 07:30:58 -04:00
parent = RestClient::Resource.new('http://example.com', &block)
expect(parent['posts'].block).to eq block
2010-07-03 07:30:58 -04:00
end
it "the block should be overrideable" do
block1 = proc {|r| r}
block2 = proc {|r| }
2010-07-03 07:30:58 -04:00
parent = RestClient::Resource.new('http://example.com', &block1)
# parent['posts', &block2].block.should eq block2 # ruby 1.9 syntax
expect(parent.send(:[], 'posts', &block2).block).to eq block2
expect(parent.send(:[], 'posts', &block2).block).not_to eq block1
2010-07-03 07:30:58 -04:00
end
# Test fails on jruby 9.1.[0-5].* due to
# https://github.com/jruby/jruby/issues/4217
it "the block should be overrideable in ruby 1.9 syntax",
:unless => (RUBY_ENGINE == 'jruby' && JRUBY_VERSION =~ /\A9\.1\.[0-5]\./) \
do
block1 = proc {|r| r}
block2 = ->(r) {}
parent = RestClient::Resource.new('http://example.com', &block1)
expect(parent['posts', &block2].block).to eq block2
expect(parent['posts', &block2].block).not_to eq block1
2010-07-03 07:30:58 -04:00
end
2009-12-29 12:27:39 -05:00
it "prints its url with to_s" do
expect(RestClient::Resource.new('x').to_s).to eq 'x'
2009-12-29 12:27:39 -05:00
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' }
expect(resource.get).to eq 'foo'
2010-01-27 16:54:18 -05:00
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')
expect(resource.get { |response, request| 'foo' }).to eq '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' }
expect(resource.get { |response, request| 'bar' }).to eq 'bar'
2010-01-27 16:54:18 -05:00
end
end
2008-03-10 20:20:57 -04:00
end