2015-04-15 15:37:29 -07:00
|
|
|
require_relative '_lib'
|
|
|
|
require 'json'
|
|
|
|
|
|
|
|
describe RestClient::Request do
|
|
|
|
before(:all) do
|
|
|
|
WebMock.disable!
|
|
|
|
end
|
|
|
|
|
|
|
|
after(:all) do
|
|
|
|
WebMock.enable!
|
|
|
|
end
|
|
|
|
|
2015-06-09 18:00:32 -07:00
|
|
|
def default_httpbin_url
|
|
|
|
# add a hack to work around java/jruby bug
|
|
|
|
# java.lang.RuntimeException: Could not generate DH keypair with backtrace
|
|
|
|
if ENV['TRAVIS_RUBY_VERSION'] == 'jruby-19mode'
|
|
|
|
'http://httpbin.org/'
|
|
|
|
else
|
|
|
|
'https://httpbin.org/'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-15 15:37:29 -07:00
|
|
|
def httpbin(suffix='')
|
2015-06-09 18:00:32 -07:00
|
|
|
url = ENV.fetch('HTTPBIN_URL', default_httpbin_url)
|
2015-04-15 15:37:29 -07:00
|
|
|
unless url.end_with?('/')
|
|
|
|
url += '/'
|
|
|
|
end
|
|
|
|
|
|
|
|
url + suffix
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_httpbin(suffix, opts={})
|
|
|
|
opts = {url: httpbin(suffix)}.merge(opts)
|
|
|
|
RestClient::Request.execute(opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_httpbin_json(suffix, opts={})
|
|
|
|
JSON.parse(execute_httpbin(suffix, opts))
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.execute' do
|
|
|
|
it 'sends a user agent' do
|
|
|
|
data = execute_httpbin_json('user-agent', method: :get)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(data['user-agent']).to match(/rest-client/)
|
2015-04-15 15:37:29 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'receives cookies on 302' do
|
|
|
|
expect {
|
|
|
|
execute_httpbin('cookies/set?foo=bar', method: :get, max_redirects: 0)
|
|
|
|
}.to raise_error(RestClient::Found) { |ex|
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(ex.http_code).to eq 302
|
|
|
|
expect(ex.response.cookies['foo']).to eq 'bar'
|
2015-04-15 15:37:29 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes along cookies through 302' do
|
|
|
|
data = execute_httpbin_json('cookies/set?foo=bar', method: :get)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(data).to have_key('cookies')
|
|
|
|
expect(data['cookies']['foo']).to eq 'bar'
|
2015-04-15 15:37:29 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles quote wrapped cookies' do
|
|
|
|
expect {
|
|
|
|
execute_httpbin('cookies/set?foo=' + CGI.escape('"bar:baz"'),
|
|
|
|
method: :get, max_redirects: 0)
|
|
|
|
}.to raise_error(RestClient::Found) { |ex|
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(ex.http_code).to eq 302
|
|
|
|
expect(ex.response.cookies['foo']).to eq '"bar:baz"'
|
2015-04-15 15:37:29 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|