diff --git a/spec/integration/httpbin_spec.rb b/spec/integration/httpbin_spec.rb new file mode 100644 index 0000000..4d4cf4c --- /dev/null +++ b/spec/integration/httpbin_spec.rb @@ -0,0 +1,62 @@ +require_relative '_lib' +require 'json' + +describe RestClient::Request do + before(:all) do + WebMock.disable! + end + + after(:all) do + WebMock.enable! + end + + def httpbin(suffix='') + url = ENV.fetch('HTTPBIN_URL', 'https://httpbin.org/') + 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) + data['user-agent'].should match(/rest-client/) + 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| + ex.http_code.should eq 302 + ex.response.cookies['foo'].should eq 'bar' + } + end + + it 'passes along cookies through 302' do + data = execute_httpbin_json('cookies/set?foo=bar', method: :get) + data.should have_key('cookies') + data['cookies']['foo'].should eq 'bar' + 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| + ex.http_code.should eq 302 + ex.response.cookies['foo'].should eq '"bar:baz"' + } + end + end +end