2015-03-13 18:00:51 -07:00
|
|
|
require_relative '_lib'
|
2010-05-25 20:05:41 +02:00
|
|
|
|
|
|
|
describe RestClient::Request do
|
2010-05-30 09:51:59 +02:00
|
|
|
|
2015-11-08 00:55:56 -08: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
|
2015-11-12 00:11:33 -08:00
|
|
|
|
|
|
|
it 'encodes nested GET params' do
|
|
|
|
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'=>'*/*', 'Accept-Encoding'=>'gzip, deflate'}).to_return(:body => 'foo', :status => 200)
|
|
|
|
RestClient::Request.execute(url: 'http://some/resource', method: :get, headers: {
|
|
|
|
params: {
|
|
|
|
a: {
|
|
|
|
foo: [1,2],
|
|
|
|
bar: nil,
|
|
|
|
},
|
|
|
|
b: 'foo bar',
|
|
|
|
math: '2 + 2 == 4',
|
|
|
|
}
|
|
|
|
}).body.should eq 'foo'
|
|
|
|
end
|
2010-05-30 09:51:59 +02:00
|
|
|
|
2010-05-25 20:05:41 +02:00
|
|
|
end
|
2010-05-30 09:51:59 +02:00
|
|
|
|
2010-12-07 19:50:54 +01:00
|
|
|
it "can use a block to process response" do
|
|
|
|
response_value = nil
|
2014-07-08 03:54:01 -07:00
|
|
|
block = proc do |http_response|
|
2010-12-07 19:50:54 +01:00
|
|
|
response_value = http_response.body
|
|
|
|
end
|
2014-11-02 15:57:10 -08: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 19:50:54 +01:00
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}, :block_response => block)
|
2013-08-11 21:16:02 +01:00
|
|
|
response_value.should eq "foo"
|
2010-12-07 19:50:54 +01:00
|
|
|
end
|
|
|
|
|
2011-07-05 15:56:27 -07: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 15:57:10 -08:00
|
|
|
stub_request(:post, 'http://some/resource').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate'}).to_return(:body => 'foo', :status => 200)
|
2011-07-05 15:56:27 -07:00
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :post, :payload => {:file => test_file})
|
|
|
|
|
2015-03-13 18:00:51 -07:00
|
|
|
test_file.closed?.should be true
|
2011-07-05 15:56:27 -07:00
|
|
|
end
|
|
|
|
|
2010-05-25 20:05:41 +02:00
|
|
|
end
|