2013-08-06 11:44:47 -04:00
|
|
|
require 'spec_helper'
|
2010-05-25 14:05:41 -04:00
|
|
|
|
|
|
|
describe RestClient::Request do
|
2010-05-30 03:51:59 -04:00
|
|
|
|
2010-05-25 14:05:41 -04:00
|
|
|
it "manage params for get requests" do
|
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)
|
2013-08-11 16:16:02 -04:00
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}).body.should eq 'foo'
|
2010-05-30 03:51:59 -04:00
|
|
|
|
2014-11-02 18:57:10 -05:00
|
|
|
stub_request(:get, 'http://some/resource').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar', 'params' => 'a'}).to_return(:body => 'foo', :status => 200)
|
2013-08-11 16:16:02 -04:00
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => :a}).body.should eq 'foo'
|
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})
|
|
|
|
|
2013-08-01 15:02:15 -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
|