mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-08 00:03:41 -04:00
Stubs in test mode can be called more than one time.
This commit is contained in:
parent
5bd3b864a6
commit
5918164275
@ -19,7 +19,7 @@ module Faraday
|
|||||||
class Stubs
|
class Stubs
|
||||||
def initialize
|
def initialize
|
||||||
# {:get => [Stub, Stub]}
|
# {:get => [Stub, Stub]}
|
||||||
@stack = {}
|
@stack, @consumed = {}, {}
|
||||||
yield self if block_given?
|
yield self if block_given?
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -29,8 +29,15 @@ module Faraday
|
|||||||
|
|
||||||
def match(request_method, path, body)
|
def match(request_method, path, body)
|
||||||
return false if !@stack.key?(request_method)
|
return false if !@stack.key?(request_method)
|
||||||
stub = @stack[request_method].detect { |stub| stub.matches?(path, body) }
|
stack = @stack[request_method]
|
||||||
@stack[request_method].delete stub
|
consumed = (@consumed[request_method] ||= [])
|
||||||
|
|
||||||
|
if stub = matches?(stack, path, body)
|
||||||
|
consumed << stack.delete(stub)
|
||||||
|
stub
|
||||||
|
else
|
||||||
|
matches?(consumed, path, body)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(path, &block)
|
def get(path, &block)
|
||||||
@ -53,10 +60,6 @@ module Faraday
|
|||||||
new_stub(:delete, path, &block)
|
new_stub(:delete, path, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def new_stub(request_method, path, body=nil, &block)
|
|
||||||
(@stack[request_method] ||= []) << Stub.new(path, body, block)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Raises an error if any of the stubbed calls have not been made.
|
# Raises an error if any of the stubbed calls have not been made.
|
||||||
def verify_stubbed_calls
|
def verify_stubbed_calls
|
||||||
failed_stubs = []
|
failed_stubs = []
|
||||||
@ -69,6 +72,16 @@ module Faraday
|
|||||||
end
|
end
|
||||||
raise failed_stubs.join(" ") unless failed_stubs.size == 0
|
raise failed_stubs.join(" ") unless failed_stubs.size == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def new_stub(request_method, path, body=nil, &block)
|
||||||
|
(@stack[request_method] ||= []) << Stub.new(path, body, block)
|
||||||
|
end
|
||||||
|
|
||||||
|
def matches?(stack, path, body)
|
||||||
|
stack.detect { |stub| stub.matches?(path, body) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Stub < Struct.new(:path, :body, :block)
|
class Stub < Struct.new(:path, :body, :block)
|
||||||
|
@ -22,5 +22,16 @@ module Adapters
|
|||||||
def test_middleware_with_simple_path_sets_body
|
def test_middleware_with_simple_path_sets_body
|
||||||
assert_equal 'hello', @resp.body
|
assert_equal 'hello', @resp.body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_middleware_can_be_called_several_times
|
||||||
|
assert_equal 'hello', @conn.get("/hello").body
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_middleware_allow_different_outcomes_for_the_same_request
|
||||||
|
@stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
|
||||||
|
@stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'world'] }
|
||||||
|
assert_equal 'hello', @conn.get("/hello").body
|
||||||
|
assert_equal 'world', @conn.get("/hello").body
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user