2015-03-13 21:00:51 -04:00
|
|
|
require_relative '_lib'
|
2009-03-16 18:28:20 -04:00
|
|
|
|
2016-05-01 16:57:54 -04:00
|
|
|
describe RestClient::AbstractResponse, :include_helpers do
|
2010-03-29 13:38:23 -04:00
|
|
|
|
2017-05-03 10:47:59 -04:00
|
|
|
# Sample class implementing AbstractResponse used for testing.
|
2010-03-29 13:38:23 -04:00
|
|
|
class MyAbstractResponse
|
|
|
|
|
|
|
|
include RestClient::AbstractResponse
|
|
|
|
|
2010-04-23 13:40:49 -04:00
|
|
|
attr_accessor :size
|
|
|
|
|
2017-05-03 10:47:59 -04:00
|
|
|
def initialize(net_http_res, request)
|
|
|
|
response_set_vars(net_http_res, request, Time.now - 1)
|
2010-03-29 13:38:23 -04:00
|
|
|
end
|
2010-04-23 13:40:49 -04:00
|
|
|
|
2010-03-29 13:38:23 -04:00
|
|
|
end
|
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
before do
|
2017-05-03 10:47:59 -04:00
|
|
|
@net_http_res = res_double()
|
2016-05-15 22:44:52 -04:00
|
|
|
@request = request_double(url: 'http://example.com', method: 'get')
|
2016-05-01 16:57:54 -04:00
|
|
|
@response = MyAbstractResponse.new(@net_http_res, @request)
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fetches the numeric response code" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@net_http_res).to receive(:code).and_return('200')
|
|
|
|
expect(@response.code).to eq 200
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2010-04-23 13:40:49 -04:00
|
|
|
it "has a nice description" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@net_http_res).to receive(:to_hash).and_return({'Content-Type' => ['application/pdf']})
|
|
|
|
expect(@net_http_res).to receive(:code).and_return('200')
|
|
|
|
expect(@response.description).to eq "200 OK | application/pdf bytes\n"
|
2010-04-23 13:40:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-24 00:55:40 -04:00
|
|
|
describe '.beautify_headers' do
|
|
|
|
it "beautifies the headers by turning the keys to symbols" do
|
|
|
|
h = RestClient::AbstractResponse.beautify_headers('content-type' => [ 'x' ])
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(h.keys.first).to eq :content_type
|
2015-03-24 00:55:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "beautifies the headers by turning the values to strings instead of one-element arrays" do
|
|
|
|
h = RestClient::AbstractResponse.beautify_headers('x' => [ 'text/html' ] )
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(h.values.first).to eq 'text/html'
|
2015-03-24 00:55:40 -04:00
|
|
|
end
|
2009-12-29 12:27:39 -05:00
|
|
|
|
2015-03-24 00:55:40 -04:00
|
|
|
it 'joins multiple header values by comma' do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::AbstractResponse.beautify_headers(
|
2015-03-24 00:55:40 -04:00
|
|
|
{'My-Header' => ['one', 'two']}
|
2016-06-05 19:52:16 -04:00
|
|
|
)).to eq({:my_header => 'one, two'})
|
2015-03-24 00:55:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'leaves set-cookie headers as array' do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::AbstractResponse.beautify_headers(
|
2015-03-24 00:55:40 -04:00
|
|
|
{'Set-Cookie' => ['cookie1=foo', 'cookie2=bar']}
|
2016-06-05 19:52:16 -04:00
|
|
|
)).to eq({:set_cookie => ['cookie1=foo', 'cookie2=bar']})
|
2015-03-24 00:55:40 -04:00
|
|
|
end
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fetches the headers" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@net_http_res).to receive(:to_hash).and_return('content-type' => [ 'text/html' ])
|
|
|
|
expect(@response.headers).to eq({ :content_type => 'text/html' })
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "extracts cookies from response headers" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
|
|
|
|
expect(@response.cookies).to eq({ 'session_id' => '1' })
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2010-04-30 14:31:24 -04:00
|
|
|
it "extract strange cookies" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/'])
|
|
|
|
expect(@response.headers).to eq({:set_cookie => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/']})
|
|
|
|
expect(@response.cookies).to eq({ 'session_id' => 'ZJ/HQVH6YE+rVkTpn0zvTQ==' })
|
2010-06-03 15:18:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't escape cookies" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca; path=/'])
|
|
|
|
expect(@response.cookies).to eq({ 'session_id' => 'BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca' })
|
2010-04-30 14:31:24 -04:00
|
|
|
end
|
|
|
|
|
2016-05-15 23:04:56 -04:00
|
|
|
describe '.cookie_jar' do
|
|
|
|
it 'extracts cookies into cookie jar' do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
|
|
|
|
expect(@response.cookie_jar).to be_a HTTP::CookieJar
|
2016-05-15 23:04:56 -04:00
|
|
|
|
|
|
|
cookie = @response.cookie_jar.cookies.first
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(cookie.domain).to eq 'example.com'
|
|
|
|
expect(cookie.name).to eq 'session_id'
|
|
|
|
expect(cookie.value).to eq '1'
|
|
|
|
expect(cookie.path).to eq '/'
|
2016-05-15 23:04:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles cookies when URI scheme is implicit' do
|
|
|
|
net_http_res = double('net http response')
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
|
2017-05-03 10:47:59 -04:00
|
|
|
request = double('request', url: 'example.com', uri: URI.parse('http://example.com'),
|
|
|
|
method: 'get', cookie_jar: HTTP::CookieJar.new, redirection_history: nil)
|
2016-05-15 23:04:56 -04:00
|
|
|
response = MyAbstractResponse.new(net_http_res, request)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(response.cookie_jar).to be_a HTTP::CookieJar
|
2016-05-15 23:04:56 -04:00
|
|
|
|
|
|
|
cookie = response.cookie_jar.cookies.first
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(cookie.domain).to eq 'example.com'
|
|
|
|
expect(cookie.name).to eq 'session_id'
|
|
|
|
expect(cookie.value).to eq '1'
|
|
|
|
expect(cookie.path).to eq '/'
|
2016-05-15 23:04:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "can access the net http result directly" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@response.net_http_res).to eq @net_http_res
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2013-07-31 22:24:18 -04:00
|
|
|
|
2010-12-10 18:11:57 -05:00
|
|
|
describe "#return!" do
|
|
|
|
it "should return the response itself on 200-codes" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@net_http_res).to receive(:code).and_return('200')
|
|
|
|
expect(@response.return!).to be_equal(@response)
|
2010-12-10 18:11:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise RequestFailed on unknown codes" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@net_http_res).to receive(:code).and_return('1000')
|
|
|
|
expect { @response.return! }.to raise_error RestClient::RequestFailed
|
2010-12-10 18:11:57 -05:00
|
|
|
end
|
2013-07-31 22:24:18 -04:00
|
|
|
|
2010-12-10 18:11:57 -05:00
|
|
|
it "should raise an error on a redirection after non-GET/HEAD requests" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@net_http_res).to receive(:code).and_return('301')
|
|
|
|
expect(@request).to receive(:method).and_return('put')
|
|
|
|
expect(@response).not_to receive(:follow_redirection)
|
|
|
|
expect { @response.return! }.to raise_error RestClient::RequestFailed
|
2010-12-10 18:11:57 -05:00
|
|
|
end
|
2016-05-01 16:57:54 -04:00
|
|
|
|
|
|
|
it "should follow 302 redirect" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@net_http_res).to receive(:code).and_return('302')
|
|
|
|
expect(@response).to receive(:check_max_redirects).and_return('fake-check')
|
|
|
|
expect(@response).to receive(:follow_redirection).and_return('fake-redirection')
|
|
|
|
expect(@response.return!).to eq 'fake-redirection'
|
2016-05-01 16:57:54 -04:00
|
|
|
end
|
2016-05-01 17:07:41 -04:00
|
|
|
|
|
|
|
it "should gracefully handle 302 redirect with no location header" do
|
2017-05-03 10:47:59 -04:00
|
|
|
@net_http_res = res_double(code: 302)
|
2016-05-15 23:04:56 -04:00
|
|
|
@request = request_double()
|
|
|
|
@response = MyAbstractResponse.new(@net_http_res, @request)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(@response).to receive(:check_max_redirects).and_return('fake-check')
|
|
|
|
expect { @response.return! }.to raise_error RestClient::Found
|
2016-05-01 17:07:41 -04:00
|
|
|
end
|
2010-12-10 18:11:57 -05:00
|
|
|
end
|
2009-03-16 18:28:20 -04:00
|
|
|
end
|