2013-08-06 08:44:47 -07:00
|
|
|
require 'spec_helper'
|
2010-02-10 21:31:59 +01:00
|
|
|
|
2009-01-24 14:55:18 -08:00
|
|
|
describe RestClient::Response do
|
2009-12-29 18:27:39 +01:00
|
|
|
before do
|
2013-07-27 12:09:42 +09:00
|
|
|
@net_http_res = double('net http response', :to_hash => {"Status" => ["200 OK"]}, :code => 200)
|
|
|
|
@request = double('http request', :user => nil, :password => nil)
|
2010-03-29 19:38:23 +02:00
|
|
|
@response = RestClient::Response.create('abc', @net_http_res, {})
|
2009-12-29 18:27:39 +01:00
|
|
|
end
|
2009-01-24 14:55:18 -08:00
|
|
|
|
2009-12-29 18:27:39 +01:00
|
|
|
it "behaves like string" do
|
2013-08-11 21:16:02 +01:00
|
|
|
@response.to_s.should eq 'abc'
|
|
|
|
@response.to_str.should eq 'abc'
|
|
|
|
@response.to_i.should eq 200
|
2009-12-29 18:27:39 +01:00
|
|
|
end
|
2009-01-24 14:55:18 -08:00
|
|
|
|
2009-12-29 18:27:39 +01:00
|
|
|
it "accepts nil strings and sets it to empty for the case of HEAD" do
|
2013-08-11 21:16:02 +01:00
|
|
|
RestClient::Response.create(nil, @net_http_res, {}).to_s.should eq ""
|
2009-12-29 18:27:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "test headers and raw headers" do
|
2013-08-11 21:16:02 +01:00
|
|
|
@response.raw_headers["Status"][0].should eq "200 OK"
|
|
|
|
@response.headers[:status].should eq "200 OK"
|
2009-12-29 18:27:39 +01:00
|
|
|
end
|
2010-01-22 21:04:49 +01:00
|
|
|
|
2010-01-20 17:37:54 +01:00
|
|
|
describe "cookie processing" do
|
|
|
|
it "should correctly deal with one Set-Cookie header with one cookie inside" do
|
2013-07-27 12:09:42 +09:00
|
|
|
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]})
|
2010-03-29 19:38:23 +02:00
|
|
|
response = RestClient::Response.create('abc', net_http_res, {})
|
2013-08-11 21:16:02 +01:00
|
|
|
response.headers[:set_cookie].should eq ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]
|
|
|
|
response.cookies.should eq({ "main_page" => "main_page_no_rewrite" })
|
2010-01-20 17:37:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should correctly deal with multiple cookies [multiple Set-Cookie headers]" do
|
2013-07-27 12:09:42 +09:00
|
|
|
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]})
|
2010-03-29 19:38:23 +02:00
|
|
|
response = RestClient::Response.create('abc', net_http_res, {})
|
2013-08-11 21:16:02 +01:00
|
|
|
response.headers[:set_cookie].should eq ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]
|
|
|
|
response.cookies.should eq({
|
2014-07-08 03:54:01 -07:00
|
|
|
"main_page" => "main_page_no_rewrite",
|
|
|
|
"remember_me" => "",
|
|
|
|
"user" => "somebody"
|
2013-08-11 21:16:02 +01:00
|
|
|
})
|
2010-01-20 17:37:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should correctly deal with multiple cookies [one Set-Cookie header with multiple cookies]" do
|
2013-07-27 12:09:42 +09:00
|
|
|
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT, remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT, user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]})
|
2010-03-29 19:38:23 +02:00
|
|
|
response = RestClient::Response.create('abc', net_http_res, {})
|
2013-08-11 21:16:02 +01:00
|
|
|
response.cookies.should eq({
|
2014-07-08 03:54:01 -07:00
|
|
|
"main_page" => "main_page_no_rewrite",
|
|
|
|
"remember_me" => "",
|
|
|
|
"user" => "somebody"
|
2013-08-11 21:16:02 +01:00
|
|
|
})
|
2010-01-20 17:37:54 +01:00
|
|
|
end
|
2010-01-20 16:54:54 +01:00
|
|
|
end
|
2010-01-20 17:37:54 +01:00
|
|
|
|
2010-01-22 21:04:49 +01:00
|
|
|
describe "exceptions processing" do
|
|
|
|
it "should return itself for normal codes" do
|
|
|
|
(200..206).each do |code|
|
2013-07-27 12:09:42 +09:00
|
|
|
net_http_res = double('net http response', :code => '200')
|
2010-03-29 19:38:23 +02:00
|
|
|
response = RestClient::Response.create('abc', net_http_res, {})
|
2010-05-18 20:00:09 +02:00
|
|
|
response.return! @request
|
2010-01-22 21:04:49 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should throw an exception for other codes" do
|
|
|
|
RestClient::Exceptions::EXCEPTIONS_MAP.each_key do |code|
|
2010-05-25 18:49:09 +02:00
|
|
|
unless (200..207).include? code
|
2013-07-27 12:09:42 +09:00
|
|
|
net_http_res = double('net http response', :code => code.to_i)
|
2010-03-29 19:38:23 +02:00
|
|
|
response = RestClient::Response.create('abc', net_http_res, {})
|
2010-01-27 23:07:03 +01:00
|
|
|
lambda { response.return!}.should raise_error
|
|
|
|
end
|
2010-01-22 21:04:49 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2010-02-10 21:31:59 +01:00
|
|
|
describe "redirection" do
|
2013-08-01 12:24:18 +10:00
|
|
|
|
2010-02-10 21:31:59 +01: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')
|
2013-08-11 21:16:02 +01:00
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo'
|
2010-02-10 21:31:59 +01:00
|
|
|
end
|
|
|
|
|
2010-05-18 20:00:09 +02:00
|
|
|
it "follows a redirection and keep the parameters" do
|
|
|
|
stub_request(:get, 'http://foo:bar@some/resource').with(:headers => {'Accept' => 'application/json'}).to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
|
|
|
|
stub_request(:get, 'http://foo:bar@new/resource').with(:headers => {'Accept' => 'application/json'}).to_return(:body => 'Foo')
|
2013-08-11 21:16:02 +01:00
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :user => 'foo', :password => 'bar', :headers => {:accept => :json}).body.should eq 'Foo'
|
2010-05-18 20:00:09 +02:00
|
|
|
end
|
|
|
|
|
2010-07-04 20:36:16 +02:00
|
|
|
it "follows a redirection and keep the cookies" do
|
2012-11-16 11:57:54 +00:00
|
|
|
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://new/resource', })
|
2010-07-04 20:36:16 +02:00
|
|
|
stub_request(:get, 'http://new/resource').with(:headers => {'Cookie' => 'Foo=Bar'}).to_return(:body => 'Qux')
|
2013-08-11 21:16:02 +01:00
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Qux'
|
2010-07-04 20:36:16 +02:00
|
|
|
end
|
|
|
|
|
2010-05-25 18:55:57 +02:00
|
|
|
it "doesn't follow a 301 when the request is a post" do
|
2013-07-27 12:09:42 +09:00
|
|
|
net_http_res = double('net http response', :code => 301)
|
2010-03-29 19:38:23 +02:00
|
|
|
response = RestClient::Response.create('abc', net_http_res, {:method => :post})
|
2010-05-18 20:00:09 +02:00
|
|
|
lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
|
2010-02-10 21:31:59 +01:00
|
|
|
end
|
|
|
|
|
2010-05-25 18:55:57 +02:00
|
|
|
it "doesn't follow a 302 when the request is a post" do
|
2013-07-27 12:09:42 +09:00
|
|
|
net_http_res = double('net http response', :code => 302)
|
2010-05-25 18:55:57 +02:00
|
|
|
response = RestClient::Response.create('abc', net_http_res, {:method => :post})
|
|
|
|
lambda { response.return!(@request)}.should raise_error(RestClient::Found)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't follow a 307 when the request is a post" do
|
2013-07-27 12:09:42 +09:00
|
|
|
net_http_res = double('net http response', :code => 307)
|
2010-05-25 18:55:57 +02:00
|
|
|
response = RestClient::Response.create('abc', net_http_res, {:method => :post})
|
|
|
|
lambda { response.return!(@request)}.should raise_error(RestClient::TemporaryRedirect)
|
|
|
|
end
|
|
|
|
|
2010-02-10 21:31:59 +01:00
|
|
|
it "doesn't follow a redirection when the request is a put" do
|
2013-07-27 12:09:42 +09:00
|
|
|
net_http_res = double('net http response', :code => 301)
|
2010-03-29 19:38:23 +02:00
|
|
|
response = RestClient::Response.create('abc', net_http_res, {:method => :put})
|
2010-05-18 20:00:09 +02:00
|
|
|
lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
|
2010-02-10 21:31:59 +01:00
|
|
|
end
|
|
|
|
|
2010-02-10 21:54:38 +01: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')
|
2013-08-11 21:16:02 +01:00
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :put).body.should eq 'Foo'
|
2010-02-10 21:54:38 +01:00
|
|
|
end
|
|
|
|
|
2010-02-10 21:31:59 +01: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')
|
2013-08-11 21:16:02 +01:00
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :head).body.should eq 'Foo'
|
2010-02-10 21:31:59 +01: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')
|
2013-08-11 21:16:02 +01:00
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo'
|
2010-02-10 21:31:59 +01: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')
|
2013-08-11 21:16:02 +01:00
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo'
|
2010-02-10 21:31:59 +01: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')
|
2013-08-11 21:16:02 +01:00
|
|
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo'
|
2010-02-10 21:31:59 +01:00
|
|
|
end
|
2013-08-01 12:24:18 +10: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'})
|
|
|
|
lambda { RestClient::Request.execute(:url => 'http://some/redirect-1', :method => :get) }.should raise_error(RestClient::MaxRedirectsReached)
|
|
|
|
WebMock.should have_requested(:get, 'http://some/redirect-2').times(10)
|
|
|
|
end
|
2013-08-01 12:24:18 +10: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'})
|
|
|
|
lambda { RestClient::Request.execute(:url => 'http://some/redirect-1', :method => :get, :max_redirects => 5) }.should raise_error(RestClient::MaxRedirectsReached)
|
|
|
|
WebMock.should have_requested(:get, 'http://some/redirect-2').times(5)
|
|
|
|
end
|
2010-02-10 21:31:59 +01:00
|
|
|
end
|
|
|
|
|
2010-01-22 21:04:49 +01:00
|
|
|
|
2009-01-24 14:55:18 -08:00
|
|
|
end
|