spec/lib/hyperion/requestor_spec.rb in hyperion_http-0.5.0 vs spec/lib/hyperion/requestor_spec.rb in hyperion_http-0.6.0
- old
+ new
@@ -17,20 +17,28 @@
describe Hyperion::Requestor do
include Hyperion::Requestor
def arrange(method, response)
- @route = RestRoute.new(method, 'http://indigo.com/things',
- ResponseDescriptor.new('thing', 1, :json),
- PayloadDescriptor.new(:json))
-
+ create_route(method)
fake_route(@route) do |request|
@request = request
response
end
end
+ def arrange_timeout(method)
+ create_route(method)
+ fake_route(@route) { sleep 2 }
+ end
+
+ def create_route(method)
+ @route = RestRoute.new(method, 'http://indigo.com/things',
+ ResponseDescriptor.new('thing', 1, :json),
+ PayloadDescriptor.new(:json))
+ end
+
def assert_result(expected_result)
expect(@result).to eql expected_result
end
it 'makes requests' do
@@ -40,18 +48,25 @@
end
it 'makes requests with additional headers' do
headers = {'X-my-header' => 'value'}
arrange(:get, {'a' => 'b'})
- expect(Hyperion).to receive(:request).with(@route, nil, headers)
+ expect(Hyperion).to receive(:request).with(@route, body: nil, additional_headers: headers, timeout: 0)
@result = request(@route, headers: headers)
end
it 'makes requests with payload bodies' do
arrange(:put, {'a' => 'b'})
@result = request(@route, body: {'the' => 'body'})
assert_result({'a' => 'b'})
expect(@request.body).to eql({'the' => 'body'})
+ end
+
+ it 'makes requests with a timeout' do
+ arrange_timeout(:get)
+ error = 'oops: time out'
+ handlers = {HyperionStatus::TIMED_OUT => proc { raise error }}
+ expect { request(@route, timeout: 1, also_handle: handlers) }.to raise_error error
end
it 'renders the response with the render proc' do
arrange(:get, {'x' => 1, 'y' => 2})
@result = request(@route, render: OpenStruct.method(:new))