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:
parent
571a6dc958
commit
8408ffb25c
2 changed files with 27 additions and 11 deletions
|
@ -1,10 +1,23 @@
|
|||
require 'uri'
|
||||
|
||||
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={})
|
||||
double('Net::HTTPResponse', {to_hash: {}, body: 'response body'}.merge(opts))
|
||||
instance_double('Net::HTTPResponse', {to_hash: {}, body: 'response body'}.merge(opts))
|
||||
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)
|
||||
request ||= request_double()
|
||||
start_time = Time.now - duration
|
||||
|
@ -17,6 +30,7 @@ module Helpers
|
|||
response
|
||||
end
|
||||
|
||||
# Redirect stderr to a string for the duration of the passed block.
|
||||
def fake_stderr
|
||||
original_stderr = $stderr
|
||||
$stderr = StringIO.new
|
||||
|
@ -26,9 +40,11 @@ module Helpers
|
|||
$stderr = original_stderr
|
||||
end
|
||||
|
||||
# Create a double for RestClient::Request
|
||||
def request_double(url: 'http://example.com', method: 'get')
|
||||
double('request', url: url, uri: URI.parse(url), method: method,
|
||||
user: nil, password: nil, cookie_jar: HTTP::CookieJar.new,
|
||||
redirection_history: nil, args: {url: url, method: method})
|
||||
instance_double('RestClient::Request',
|
||||
url: url, uri: URI.parse(url), method: method, user: nil, password: nil,
|
||||
cookie_jar: HTTP::CookieJar.new, redirection_history: nil,
|
||||
args: {url: url, method: method})
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,21 +2,21 @@ require_relative '_lib'
|
|||
|
||||
describe RestClient::AbstractResponse, :include_helpers do
|
||||
|
||||
# Sample class implementing AbstractResponse used for testing.
|
||||
class MyAbstractResponse
|
||||
|
||||
include RestClient::AbstractResponse
|
||||
|
||||
attr_accessor :size
|
||||
|
||||
def initialize net_http_res, request
|
||||
@net_http_res = net_http_res
|
||||
@request = request
|
||||
def initialize(net_http_res, request)
|
||||
response_set_vars(net_http_res, request, Time.now - 1)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
before do
|
||||
@net_http_res = double('net http response')
|
||||
@net_http_res = res_double()
|
||||
@request = request_double(url: 'http://example.com', method: 'get')
|
||||
@response = MyAbstractResponse.new(@net_http_res, @request)
|
||||
end
|
||||
|
@ -92,8 +92,8 @@ describe RestClient::AbstractResponse, :include_helpers do
|
|||
it 'handles cookies when URI scheme is implicit' do
|
||||
net_http_res = double('net http response')
|
||||
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'),
|
||||
method: 'get', cookie_jar: HTTP::CookieJar.new)
|
||||
request = double('request', url: 'example.com', uri: URI.parse('http://example.com'),
|
||||
method: 'get', cookie_jar: HTTP::CookieJar.new, redirection_history: nil)
|
||||
response = MyAbstractResponse.new(net_http_res, request)
|
||||
expect(response.cookie_jar).to be_a HTTP::CookieJar
|
||||
|
||||
|
@ -135,7 +135,7 @@ describe RestClient::AbstractResponse, :include_helpers do
|
|||
end
|
||||
|
||||
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()
|
||||
@response = MyAbstractResponse.new(@net_http_res, @request)
|
||||
expect(@response).to receive(:check_max_redirects).and_return('fake-check')
|
||||
|
|
Loading…
Reference in a new issue