1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00

Helpers: switch to verifying instance_double.

This commit is contained in:
Andy Brody 2017-05-03 10:47:59 -04:00
parent 571a6dc958
commit 8408ffb25c
2 changed files with 27 additions and 11 deletions

View file

@ -1,10 +1,23 @@
require 'uri' require 'uri'
module Helpers module Helpers
# @param [Hash] opts A hash of methods, passed directly to the double
# definition. Use this to stub other required methods.
#
# @return double for Net::HTTPResponse
def res_double(opts={}) def res_double(opts={})
double('Net::HTTPResponse', {to_hash: {}, body: 'response body'}.merge(opts)) instance_double('Net::HTTPResponse', {to_hash: {}, body: 'response body'}.merge(opts))
end end
# Given a Net::HTTPResponse or double and a Request or double, create a
# RestClient::Response object.
#
# @param net_http_res_double an rspec double for Net::HTTPResponse
# @param request A RestClient::Request or rspec double
#
# @return [RestClient::Response]
#
def response_from_res_double(net_http_res_double, request=nil, duration: 1) def response_from_res_double(net_http_res_double, request=nil, duration: 1)
request ||= request_double() request ||= request_double()
start_time = Time.now - duration start_time = Time.now - duration
@ -17,6 +30,7 @@ module Helpers
response response
end end
# Redirect stderr to a string for the duration of the passed block.
def fake_stderr def fake_stderr
original_stderr = $stderr original_stderr = $stderr
$stderr = StringIO.new $stderr = StringIO.new
@ -26,9 +40,11 @@ module Helpers
$stderr = original_stderr $stderr = original_stderr
end end
# Create a double for RestClient::Request
def request_double(url: 'http://example.com', method: 'get') def request_double(url: 'http://example.com', method: 'get')
double('request', url: url, uri: URI.parse(url), method: method, instance_double('RestClient::Request',
user: nil, password: nil, cookie_jar: HTTP::CookieJar.new, url: url, uri: URI.parse(url), method: method, user: nil, password: nil,
redirection_history: nil, args: {url: url, method: method}) cookie_jar: HTTP::CookieJar.new, redirection_history: nil,
args: {url: url, method: method})
end end
end end

View file

@ -2,21 +2,21 @@ require_relative '_lib'
describe RestClient::AbstractResponse, :include_helpers do describe RestClient::AbstractResponse, :include_helpers do
# Sample class implementing AbstractResponse used for testing.
class MyAbstractResponse class MyAbstractResponse
include RestClient::AbstractResponse include RestClient::AbstractResponse
attr_accessor :size attr_accessor :size
def initialize net_http_res, request def initialize(net_http_res, request)
@net_http_res = net_http_res response_set_vars(net_http_res, request, Time.now - 1)
@request = request
end end
end end
before do before do
@net_http_res = double('net http response') @net_http_res = res_double()
@request = request_double(url: 'http://example.com', method: 'get') @request = request_double(url: 'http://example.com', method: 'get')
@response = MyAbstractResponse.new(@net_http_res, @request) @response = MyAbstractResponse.new(@net_http_res, @request)
end end
@ -92,8 +92,8 @@ describe RestClient::AbstractResponse, :include_helpers do
it 'handles cookies when URI scheme is implicit' do it 'handles cookies when URI scheme is implicit' do
net_http_res = double('net http response') net_http_res = double('net http response')
expect(net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/']) expect(net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
request = double(url: 'example.com', uri: URI.parse('http://example.com'), request = double('request', url: 'example.com', uri: URI.parse('http://example.com'),
method: 'get', cookie_jar: HTTP::CookieJar.new) method: 'get', cookie_jar: HTTP::CookieJar.new, redirection_history: nil)
response = MyAbstractResponse.new(net_http_res, request) response = MyAbstractResponse.new(net_http_res, request)
expect(response.cookie_jar).to be_a HTTP::CookieJar expect(response.cookie_jar).to be_a HTTP::CookieJar
@ -135,7 +135,7 @@ describe RestClient::AbstractResponse, :include_helpers do
end end
it "should gracefully handle 302 redirect with no location header" do it "should gracefully handle 302 redirect with no location header" do
@net_http_res = res_double(code: 302, location: nil) @net_http_res = res_double(code: 302)
@request = request_double() @request = request_double()
@response = MyAbstractResponse.new(@net_http_res, @request) @response = MyAbstractResponse.new(@net_http_res, @request)
expect(@response).to receive(:check_max_redirects).and_return('fake-check') expect(@response).to receive(:check_max_redirects).and_return('fake-check')