2015-03-13 21:00:51 -04:00
|
|
|
require_relative '_lib'
|
2010-02-10 15:31:59 -05:00
|
|
|
|
2015-03-13 21:00:51 -04:00
|
|
|
describe RestClient::Response, :include_helpers do
|
2009-12-29 12:27:39 -05:00
|
|
|
before do
|
2017-05-03 10:31:19 -04:00
|
|
|
@net_http_res = res_double(to_hash: {'Status' => ['200 OK']}, code: '200', body: 'abc')
|
2015-03-21 22:08:21 -04:00
|
|
|
@example_url = 'http://example.com'
|
2016-05-01 16:57:54 -04:00
|
|
|
@request = request_double(url: @example_url, method: 'get')
|
2017-05-03 10:31:19 -04:00
|
|
|
@response = response_from_res_double(@net_http_res, @request, duration: 1)
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2009-01-24 17:55:18 -05:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "behaves like string" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@response.to_s).to eq 'abc'
|
|
|
|
expect(@response.to_str).to eq 'abc'
|
2015-03-14 18:58:08 -04:00
|
|
|
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@response).to receive(:warn)
|
|
|
|
expect(@response.to_i).to eq 0
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2009-01-24 17:55:18 -05:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "accepts nil strings and sets it to empty for the case of HEAD" do
|
2017-05-03 10:31:19 -04:00
|
|
|
# TODO
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Response.create(nil, @net_http_res, @request).to_s).to eq ""
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2015-03-24 00:55:40 -04:00
|
|
|
describe 'header processing' do
|
|
|
|
it "test headers and raw headers" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@response.raw_headers["Status"][0]).to eq "200 OK"
|
|
|
|
expect(@response.headers[:status]).to eq "200 OK"
|
2015-03-24 00:55:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles multiple headers by joining with comma' do
|
2017-05-03 10:31:19 -04:00
|
|
|
net_http_res = res_double(to_hash: {'My-Header' => ['foo', 'bar']}, code: '200', body: nil)
|
|
|
|
example_url = 'http://example.com'
|
|
|
|
request = request_double(url: example_url, method: 'get')
|
|
|
|
response = response_from_res_double(net_http_res, request)
|
2015-03-24 00:55:40 -04:00
|
|
|
|
2017-05-03 10:31:19 -04:00
|
|
|
expect(response.raw_headers['My-Header']).to eq ['foo', 'bar']
|
|
|
|
expect(response.headers[:my_header]).to eq 'foo, bar'
|
2015-03-24 00:55:40 -04:00
|
|
|
end
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2010-01-22 15:04:49 -05:00
|
|
|
|
2010-01-20 11:37:54 -05:00
|
|
|
describe "cookie processing" do
|
|
|
|
it "should correctly deal with one Set-Cookie header with one cookie inside" do
|
2015-03-21 22:08:21 -04:00
|
|
|
header_val = "main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT".freeze
|
|
|
|
|
|
|
|
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => [header_val]})
|
2016-05-01 16:57:54 -04:00
|
|
|
response = RestClient::Response.create('abc', net_http_res, @request)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(response.headers[:set_cookie]).to eq [header_val]
|
|
|
|
expect(response.cookies).to eq({ "main_page" => "main_page_no_rewrite" })
|
2010-01-20 11:37:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should correctly deal with multiple cookies [multiple Set-Cookie headers]" do
|
2015-03-21 22:08:21 -04:00
|
|
|
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT", "remember_me=; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT", "user=somebody; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT"]})
|
2016-05-01 16:57:54 -04:00
|
|
|
response = RestClient::Response.create('abc', net_http_res, @request)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(response.headers[:set_cookie]).to eq ["main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT", "remember_me=; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT", "user=somebody; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT"]
|
|
|
|
expect(response.cookies).to eq({
|
2014-07-08 06:54:01 -04:00
|
|
|
"main_page" => "main_page_no_rewrite",
|
|
|
|
"remember_me" => "",
|
|
|
|
"user" => "somebody"
|
2013-08-11 16:16:02 -04:00
|
|
|
})
|
2010-01-20 11:37:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should correctly deal with multiple cookies [one Set-Cookie header with multiple cookies]" do
|
2015-03-21 22:08:21 -04:00
|
|
|
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT, remember_me=; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT, user=somebody; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT"]})
|
2016-05-01 16:57:54 -04:00
|
|
|
response = RestClient::Response.create('abc', net_http_res, @request)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(response.cookies).to eq({
|
2014-07-08 06:54:01 -04:00
|
|
|
"main_page" => "main_page_no_rewrite",
|
|
|
|
"remember_me" => "",
|
|
|
|
"user" => "somebody"
|
2013-08-11 16:16:02 -04:00
|
|
|
})
|
2010-01-20 11:37:54 -05:00
|
|
|
end
|
2010-01-20 10:54:54 -05:00
|
|
|
end
|
2010-01-20 11:37:54 -05:00
|
|
|
|
2010-01-22 15:04:49 -05:00
|
|
|
describe "exceptions processing" do
|
|
|
|
it "should return itself for normal codes" do
|
|
|
|
(200..206).each do |code|
|
2017-05-03 10:31:19 -04:00
|
|
|
net_http_res = res_double(:code => '200')
|
2016-05-01 16:57:54 -04:00
|
|
|
resp = RestClient::Response.create('abc', net_http_res, @request)
|
2015-04-15 16:03:02 -04:00
|
|
|
resp.return!
|
2010-01-22 15:04:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should throw an exception for other codes" do
|
2016-06-05 20:02:46 -04:00
|
|
|
RestClient::Exceptions::EXCEPTIONS_MAP.each_pair do |code, exc|
|
2010-05-25 12:49:09 -04:00
|
|
|
unless (200..207).include? code
|
2017-05-03 10:31:19 -04:00
|
|
|
net_http_res = res_double(:code => code.to_i)
|
2016-05-01 16:57:54 -04:00
|
|
|
resp = RestClient::Response.create('abc', net_http_res, @request)
|
2016-06-05 20:02:46 -04:00
|
|
|
allow(@request).to receive(:max_redirects).and_return(5)
|
|
|
|
expect { resp.return! }.to raise_error(exc)
|
2010-01-27 17:07:03 -05:00
|
|
|
end
|
2010-01-22 15:04:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2010-02-10 15:31:59 -05:00
|
|
|
describe "redirection" do
|
2013-07-31 22:24:18 -04:00
|
|
|
|
2010-02-10 15:31:59 -05:00
|
|
|
it "follows a redirection when the request is a get" do
|
|
|
|
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
|
|
|
|
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
|
2010-02-10 15:31:59 -05:00
|
|
|
end
|
|
|
|
|
2015-04-15 18:14:29 -04:00
|
|
|
it "keeps redirection history" do
|
|
|
|
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
|
|
|
|
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
|
|
|
|
r = RestClient::Request.execute(url: 'http://some/resource', method: :get)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(r.body).to eq 'Foo'
|
|
|
|
expect(r.history.length).to eq 1
|
|
|
|
expect(r.history.fetch(0)).to be_a(RestClient::Response)
|
|
|
|
expect(r.history.fetch(0).code).to be 301
|
2015-04-15 18:14:29 -04:00
|
|
|
end
|
|
|
|
|
2010-05-18 14:00:09 -04:00
|
|
|
it "follows a redirection and keep the parameters" do
|
2016-05-24 11:05:52 -04:00
|
|
|
stub_request(:get, 'http://some/resource').with(:headers => {'Accept' => 'application/json'}, :basic_auth => ['foo', 'bar']).to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
|
|
|
|
stub_request(:get, 'http://new/resource').with(:headers => {'Accept' => 'application/json'}, :basic_auth => ['foo', 'bar']).to_return(:body => 'Foo')
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :user => 'foo', :password => 'bar', :headers => {:accept => :json}).body).to eq 'Foo'
|
2010-05-18 14:00:09 -04:00
|
|
|
end
|
|
|
|
|
2010-07-04 14:36:16 -04:00
|
|
|
it "follows a redirection and keep the cookies" do
|
2015-03-21 22:08:21 -04:00
|
|
|
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://some/new_resource', })
|
|
|
|
stub_request(:get, 'http://some/new_resource').with(:headers => {'Cookie' => 'Foo=Bar'}).to_return(:body => 'Qux')
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Qux'
|
2015-03-21 22:08:21 -04:00
|
|
|
end
|
|
|
|
|
2016-06-05 19:03:20 -04:00
|
|
|
it 'respects cookie domains on redirect' do
|
|
|
|
stub_request(:get, 'http://some.example.com/').to_return(:body => '', :status => 301,
|
|
|
|
:headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://new.example.com/', })
|
|
|
|
stub_request(:get, 'http://new.example.com/').with(
|
|
|
|
:headers => {'Cookie' => 'passedthrough=1'}).to_return(:body => 'Qux')
|
|
|
|
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Request.execute(:url => 'http://some.example.com/', :method => :get, cookies: [HTTP::Cookie.new('passedthrough', '1', domain: 'new.example.com', path: '/')]).body).to eq 'Qux'
|
2010-07-04 14:36:16 -04:00
|
|
|
end
|
|
|
|
|
2010-05-25 12:55:57 -04:00
|
|
|
it "doesn't follow a 301 when the request is a post" do
|
2017-05-03 10:31:19 -04:00
|
|
|
net_http_res = res_double(:code => 301)
|
|
|
|
response = response_from_res_double(net_http_res, request_double(method: 'post'))
|
2016-05-01 16:57:54 -04:00
|
|
|
|
2016-06-05 19:52:16 -04:00
|
|
|
expect {
|
2015-04-15 16:03:02 -04:00
|
|
|
response.return!
|
2016-06-05 19:52:16 -04:00
|
|
|
}.to raise_error(RestClient::MovedPermanently)
|
2010-02-10 15:31:59 -05:00
|
|
|
end
|
|
|
|
|
2010-05-25 12:55:57 -04:00
|
|
|
it "doesn't follow a 302 when the request is a post" do
|
2017-05-03 10:31:19 -04:00
|
|
|
net_http_res = res_double(:code => 302)
|
|
|
|
response = response_from_res_double(net_http_res, request_double(method: 'post'))
|
|
|
|
|
2016-06-05 19:52:16 -04:00
|
|
|
expect {
|
2015-04-15 16:03:02 -04:00
|
|
|
response.return!
|
2016-06-05 19:52:16 -04:00
|
|
|
}.to raise_error(RestClient::Found)
|
2010-05-25 12:55:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't follow a 307 when the request is a post" do
|
2017-05-03 10:31:19 -04:00
|
|
|
net_http_res = res_double(:code => 307)
|
|
|
|
response = response_from_res_double(net_http_res, request_double(method: 'post'))
|
|
|
|
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(response).not_to receive(:follow_redirection)
|
|
|
|
expect {
|
2015-04-15 16:03:02 -04:00
|
|
|
response.return!
|
2016-06-05 19:52:16 -04:00
|
|
|
}.to raise_error(RestClient::TemporaryRedirect)
|
2010-05-25 12:55:57 -04:00
|
|
|
end
|
|
|
|
|
2010-02-10 15:31:59 -05:00
|
|
|
it "doesn't follow a redirection when the request is a put" do
|
2017-05-03 10:31:19 -04:00
|
|
|
net_http_res = res_double(:code => 301)
|
|
|
|
response = response_from_res_double(net_http_res, request_double(method: 'put'))
|
2016-06-05 19:52:16 -04:00
|
|
|
expect {
|
2015-04-15 16:03:02 -04:00
|
|
|
response.return!
|
2016-06-05 19:52:16 -04:00
|
|
|
}.to raise_error(RestClient::MovedPermanently)
|
2010-02-10 15:31:59 -05:00
|
|
|
end
|
|
|
|
|
2010-02-10 15:54:38 -05:00
|
|
|
it "follows a redirection when the request is a post and result is a 303" do
|
|
|
|
stub_request(:put, 'http://some/resource').to_return(:body => '', :status => 303, :headers => {'Location' => 'http://new/resource'})
|
|
|
|
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :put).body).to eq 'Foo'
|
2010-02-10 15:54:38 -05:00
|
|
|
end
|
|
|
|
|
2010-02-10 15:31:59 -05:00
|
|
|
it "follows a redirection when the request is a head" do
|
|
|
|
stub_request(:head, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
|
|
|
|
stub_request(:head, 'http://new/resource').to_return(:body => 'Foo')
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :head).body).to eq 'Foo'
|
2010-02-10 15:31:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "handles redirects with relative paths" do
|
|
|
|
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index'})
|
|
|
|
stub_request(:get, 'http://some/index').to_return(:body => 'Foo')
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
|
2010-02-10 15:31:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "handles redirects with relative path and query string" do
|
|
|
|
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index?q=1'})
|
|
|
|
stub_request(:get, 'http://some/index?q=1').to_return(:body => 'Foo')
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
|
2010-02-10 15:31:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "follow a redirection when the request is a get and the response is in the 30x range" do
|
|
|
|
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
|
|
|
|
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
|
2010-02-10 15:31:59 -05:00
|
|
|
end
|
2013-07-31 22:24:18 -04:00
|
|
|
|
2010-12-16 12:41:12 -05:00
|
|
|
it "follows no more than 10 redirections before raising error" do
|
|
|
|
stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
|
|
|
|
stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
|
2016-06-05 19:52:16 -04:00
|
|
|
expect {
|
2015-04-15 16:03:02 -04:00
|
|
|
RestClient::Request.execute(url: 'http://some/redirect-1', method: :get)
|
2016-06-05 19:52:16 -04:00
|
|
|
}.to raise_error(RestClient::MovedPermanently) { |ex|
|
|
|
|
ex.response.history.each {|r| expect(r).to be_a(RestClient::Response) }
|
|
|
|
expect(ex.response.history.length).to eq 10
|
2015-04-15 18:14:29 -04:00
|
|
|
}
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(WebMock).to have_requested(:get, 'http://some/redirect-2').times(10)
|
2010-12-16 12:41:12 -05:00
|
|
|
end
|
2013-07-31 22:24:18 -04:00
|
|
|
|
2010-12-16 12:41:12 -05:00
|
|
|
it "follows no more than max_redirects redirections, if specified" do
|
|
|
|
stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
|
|
|
|
stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
|
2016-06-05 19:52:16 -04:00
|
|
|
expect {
|
2015-04-15 16:03:02 -04:00
|
|
|
RestClient::Request.execute(url: 'http://some/redirect-1', method: :get, max_redirects: 5)
|
2016-06-05 19:52:16 -04:00
|
|
|
}.to raise_error(RestClient::MovedPermanently) { |ex|
|
|
|
|
expect(ex.response.history.length).to eq 5
|
2015-04-15 18:14:29 -04:00
|
|
|
}
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(WebMock).to have_requested(:get, 'http://some/redirect-2').times(5)
|
2010-12-16 12:41:12 -05:00
|
|
|
end
|
2016-06-05 18:39:31 -04:00
|
|
|
|
|
|
|
it "allows for manual following of redirects" do
|
|
|
|
stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/resource'})
|
|
|
|
stub_request(:get, 'http://some/resource').to_return(:body => 'Qux', :status => 200)
|
|
|
|
|
|
|
|
begin
|
|
|
|
RestClient::Request.execute(url: 'http://some/redirect-1', method: :get, max_redirects: 0)
|
|
|
|
rescue RestClient::MovedPermanently => err
|
|
|
|
resp = err.response.follow_redirection
|
|
|
|
else
|
|
|
|
raise 'notreached'
|
|
|
|
end
|
|
|
|
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(resp.code).to eq 200
|
|
|
|
expect(resp.body).to eq 'Qux'
|
2016-06-05 18:39:31 -04:00
|
|
|
end
|
2010-02-10 15:31:59 -05:00
|
|
|
end
|
|
|
|
|
2016-10-24 12:02:23 -04:00
|
|
|
describe "logging" do
|
|
|
|
it "uses the request's logger" do
|
|
|
|
stub_request(:get, 'http://some/resource').to_return(body: 'potato', status: 200)
|
|
|
|
|
|
|
|
logger = double('logger', '<<' => true)
|
|
|
|
request = RestClient::Request.new(url: 'http://some/resource', method: :get, log: logger)
|
|
|
|
|
|
|
|
expect(logger).to receive(:<<)
|
|
|
|
|
|
|
|
request.execute
|
|
|
|
end
|
|
|
|
end
|
2009-01-24 17:55:18 -05:00
|
|
|
end
|