2015-03-13 21:00:51 -04:00
|
|
|
require_relative '_lib'
|
2010-05-25 14:05:41 -04:00
|
|
|
|
2017-07-05 01:40:21 -04:00
|
|
|
describe RestClient::Request, :include_helpers 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
|
2017-05-02 13:46:43 -04:00
|
|
|
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}).body).to eq 'foo'
|
2015-11-08 03:55:56 -05:00
|
|
|
|
2017-05-02 13:46:43 -04:00
|
|
|
stub_request(:get, 'http://some/resource').with(:headers => {'Accept'=>'*/*', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => :a}).body).to eq 'foo'
|
2015-11-08 03:55:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds GET params when params are present in URL' do
|
2017-05-02 13:46:43 -04:00
|
|
|
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Request.execute(:url => 'http://some/resource?a=b', :method => :get, :headers => {:foo => :bar, :params => {:c => 'd'}}).body).to eq 'foo'
|
2015-11-08 03:55:56 -05:00
|
|
|
end
|
2015-11-12 03:11:33 -05:00
|
|
|
|
|
|
|
it 'encodes nested GET params' do
|
2017-05-02 13:46:43 -04:00
|
|
|
stub_request(:get, 'http://some/resource?a[foo][]=1&a[foo][]=2&a[bar]&b=foo+bar&math=2+%2B+2+%3D%3D+4').with(:headers => {'Accept'=>'*/*',}).to_return(:body => 'foo', :status => 200)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Request.execute(url: 'http://some/resource', method: :get, headers: {
|
2015-11-12 03:11:33 -05:00
|
|
|
params: {
|
|
|
|
a: {
|
|
|
|
foo: [1,2],
|
|
|
|
bar: nil,
|
|
|
|
},
|
|
|
|
b: 'foo bar',
|
|
|
|
math: '2 + 2 == 4',
|
|
|
|
}
|
2016-06-05 19:52:16 -04:00
|
|
|
}).body).to eq 'foo'
|
2015-11-12 03:11:33 -05:00
|
|
|
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
|
2017-05-02 13:46:43 -04:00
|
|
|
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', '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)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(response_value).to 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
|
2017-07-05 01:40:21 -04:00
|
|
|
test_file = File.new(test_image_path)
|
2011-07-05 18:56:27 -04:00
|
|
|
|
2017-05-02 13:46:43 -04:00
|
|
|
stub_request(:post, 'http://some/resource').with(:headers => {'Accept'=>'*/*'}).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})
|
|
|
|
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(test_file.closed?).to be true
|
2011-07-05 18:56:27 -04:00
|
|
|
end
|
|
|
|
|
2010-05-25 14:05:41 -04:00
|
|
|
end
|