2016-05-15 22:44:52 -04:00
|
|
|
require 'uri'
|
|
|
|
|
2015-03-13 21:00:51 -04:00
|
|
|
module Helpers
|
2017-05-03 10:47:59 -04:00
|
|
|
|
|
|
|
# @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
|
2017-05-03 10:31:19 -04:00
|
|
|
def res_double(opts={})
|
2017-05-03 10:47:59 -04:00
|
|
|
instance_double('Net::HTTPResponse', {to_hash: {}, body: 'response body'}.merge(opts))
|
2017-05-03 10:31:19 -04:00
|
|
|
end
|
|
|
|
|
2017-05-03 10:47:59 -04:00
|
|
|
# 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]
|
|
|
|
#
|
2017-05-03 10:31:19 -04:00
|
|
|
def response_from_res_double(net_http_res_double, request=nil, duration: 1)
|
|
|
|
request ||= request_double()
|
|
|
|
start_time = Time.now - duration
|
|
|
|
|
|
|
|
response = RestClient::Response.create(net_http_res_double.body, net_http_res_double, request, start_time)
|
|
|
|
|
|
|
|
# mock duration to ensure it gets the value we expect
|
|
|
|
allow(response).to receive(:duration).and_return(duration)
|
|
|
|
|
|
|
|
response
|
2015-03-13 21:00:51 -04:00
|
|
|
end
|
2015-03-14 18:18:36 -04:00
|
|
|
|
2017-05-03 10:47:59 -04:00
|
|
|
# Redirect stderr to a string for the duration of the passed block.
|
2015-03-14 18:18:36 -04:00
|
|
|
def fake_stderr
|
|
|
|
original_stderr = $stderr
|
|
|
|
$stderr = StringIO.new
|
|
|
|
yield
|
|
|
|
$stderr.string
|
|
|
|
ensure
|
|
|
|
$stderr = original_stderr
|
|
|
|
end
|
2016-05-01 16:57:54 -04:00
|
|
|
|
2017-05-03 10:47:59 -04:00
|
|
|
# Create a double for RestClient::Request
|
2016-05-01 16:57:54 -04:00
|
|
|
def request_double(url: 'http://example.com', method: 'get')
|
2017-05-03 10:47:59 -04:00
|
|
|
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})
|
2016-05-01 16:57:54 -04:00
|
|
|
end
|
2017-07-05 01:40:21 -04:00
|
|
|
|
|
|
|
def test_image_path
|
|
|
|
File.dirname(__FILE__) + "/ISS.jpg"
|
|
|
|
end
|
2015-03-13 21:00:51 -04:00
|
|
|
end
|