2015-03-13 21:00:51 -04:00
|
|
|
require_relative '_lib'
|
2010-05-25 14:05:41 -04:00
|
|
|
|
|
|
|
describe RestClient::Request do
|
2010-05-30 03:51:59 -04:00
|
|
|
|
2015-11-08 03:55:56 -05:00
|
|
|
context 'params for GET requests' do
|
|
|
|
it "manage params for get requests" do
|
|
|
|
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
|
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}).body.should eq 'foo'
|
|
|
|
|
|
|
|
stub_request(:get, 'http://some/resource').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
|
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => :a}).body.should eq 'foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds GET params when params are present in URL' do
|
|
|
|
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
|
|
|
|
RestClient::Request.execute(:url => 'http://some/resource?a=b', :method => :get, :headers => {:foo => :bar, :params => {:c => 'd'}}).body.should eq 'foo'
|
|
|
|
end
|
2010-05-30 03:51:59 -04:00
|
|
|
|
2010-05-25 14:05:41 -04:00
|
|
|
end
|
2010-05-30 03:51:59 -04:00
|
|
|
|
2010-12-07 13:50:54 -05:00
|
|
|
it "can use a block to process response" do
|
|
|
|
response_value = nil
|
2014-07-08 06:54:01 -04:00
|
|
|
block = proc do |http_response|
|
2010-12-07 13:50:54 -05:00
|
|
|
response_value = http_response.body
|
|
|
|
end
|
2014-11-02 18:57:10 -05:00
|
|
|
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
|
2010-12-07 13:50:54 -05:00
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}, :block_response => block)
|
2013-08-11 16:16:02 -04:00
|
|
|
response_value.should eq "foo"
|
2010-12-07 13:50:54 -05:00
|
|
|
end
|
|
|
|
|
2011-07-05 18:56:27 -04:00
|
|
|
it 'closes payload if not nil' do
|
|
|
|
test_file = File.new(File.join( File.dirname(File.expand_path(__FILE__)), 'master_shake.jpg'))
|
|
|
|
|
2014-11-02 18:57:10 -05:00
|
|
|
stub_request(:post, 'http://some/resource').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate'}).to_return(:body => 'foo', :status => 200)
|
2011-07-05 18:56:27 -04:00
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :post, :payload => {:file => test_file})
|
|
|
|
|
2015-03-13 21:00:51 -04:00
|
|
|
test_file.closed?.should be true
|
2011-07-05 18:56:27 -04:00
|
|
|
end
|
|
|
|
|
2010-05-25 14:05:41 -04:00
|
|
|
end
|