mirror of
https://github.com/lostisland/faraday.git
synced 2025-08-30 00:03:09 -04:00
MockRequest test adapter, connection error.
This commit is contained in:
parent
6656d37569
commit
0a7ce75588
@ -9,10 +9,12 @@ module Faraday
|
||||
|
||||
autoload :Connection, 'faraday/connection'
|
||||
autoload :Response, 'faraday/response'
|
||||
autoload :Error, 'faraday/error'
|
||||
|
||||
module Adapter
|
||||
autoload :NetHttp, 'faraday/adapter/net_http'
|
||||
autoload :Typhoeus, 'faraday/adapter/typhoeus'
|
||||
autoload :NetHttp, 'faraday/adapter/net_http'
|
||||
autoload :Typhoeus, 'faraday/adapter/typhoeus'
|
||||
autoload :MockRequest, 'faraday/adapter/mock_request'
|
||||
end
|
||||
end
|
||||
|
||||
@ -32,4 +34,4 @@ class Object
|
||||
yield self
|
||||
self
|
||||
end unless Object.respond_to?(:tap)
|
||||
end
|
||||
end
|
||||
|
62
lib/faraday/adapter/mock_request.rb
Normal file
62
lib/faraday/adapter/mock_request.rb
Normal file
@ -0,0 +1,62 @@
|
||||
module Faraday
|
||||
module Adapter
|
||||
module MockRequest
|
||||
include Faraday::Error # ConnectionFailed
|
||||
|
||||
class Stubs
|
||||
def initialize
|
||||
# {:get => [Stub, Stub]}
|
||||
@stack = {}
|
||||
yield self if block_given?
|
||||
end
|
||||
|
||||
def empty?
|
||||
@stack.empty?
|
||||
end
|
||||
|
||||
def match(request_method, path, request_headers)
|
||||
return false if !@stack.key?(request_method)
|
||||
@stack[request_method].detect { |stub| stub.matches?(path, request_headers) }
|
||||
end
|
||||
|
||||
def get(path, request_headers = {}, &block)
|
||||
(@stack[:get] ||= []) << new_stub(path, request_headers, block)
|
||||
end
|
||||
|
||||
def new_stub(path, request_headers, block)
|
||||
status, response_headers, body = block.call
|
||||
Stub.new(path, request_headers, status, response_headers, body)
|
||||
end
|
||||
end
|
||||
|
||||
class Stub < Struct.new(:path, :request_headers, :status, :response_headers, :body)
|
||||
def matches?(request_path, headers)
|
||||
return false if request_path != path
|
||||
return true if request_headers.empty?
|
||||
request_headers.each do |key, value|
|
||||
return true if headers[key] == value
|
||||
end
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def initialize &block
|
||||
super nil
|
||||
@stubs = Stubs.new
|
||||
yield @stubs
|
||||
end
|
||||
|
||||
def _get(uri, headers)
|
||||
raise ConnectionFailed, "no stubbed requests" if @stubs.empty?
|
||||
if stub = @stubs.match(:get, uri.path, headers)
|
||||
response_class.new do |resp|
|
||||
resp.headers = stub.response_headers
|
||||
resp.process stub.body
|
||||
end
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -12,6 +12,8 @@ module Faraday
|
||||
resp.headers[key] = value
|
||||
end
|
||||
end
|
||||
rescue Errno::ECONNREFUSED
|
||||
raise Faraday::Error::ConnectionFailed, "connection refused"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -33,9 +33,12 @@ module Faraday
|
||||
@parallel_manager.queue(req)
|
||||
if !is_async then run_parallel_requests end
|
||||
end
|
||||
rescue Errno::ECONNREFUSED
|
||||
raise Faraday::Error::ConnectionFailed, "connection refused"
|
||||
end
|
||||
|
||||
def parse_response_headers(header_string)
|
||||
return {} if !header_string || header_string.empty?
|
||||
Hash[*header_string.split(/\r\n/).
|
||||
tap { |a| a.shift }. # drop the HTTP status line
|
||||
map! { |h| h.split(/:\s+/,2) }. # split key and value
|
||||
|
@ -58,4 +58,4 @@ module Faraday
|
||||
end.join("&")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
5
lib/faraday/error.rb
Normal file
5
lib/faraday/error.rb
Normal file
@ -0,0 +1,5 @@
|
||||
module Faraday
|
||||
module Error
|
||||
class ConnectionFailed < StandardError; end
|
||||
end
|
||||
end
|
@ -10,61 +10,7 @@ module Faraday
|
||||
LIVE_SERVER = 'http://localhost:4567'
|
||||
|
||||
class TestConnection < Faraday::Connection
|
||||
class Stubs
|
||||
def initialize
|
||||
# {:get => [Stub, Stub]}
|
||||
@stack = {}
|
||||
yield self if block_given?
|
||||
end
|
||||
|
||||
def match(request_method, path, request_headers)
|
||||
return false if !@stack.key?(request_method)
|
||||
@stack[request_method].detect { |stub| stub.matches?(path, request_headers) }
|
||||
end
|
||||
|
||||
def get(path, request_headers = {}, &block)
|
||||
(@stack[:get] ||= []) << new_stub(path, request_headers, block)
|
||||
end
|
||||
|
||||
def new_stub(path, request_headers, block)
|
||||
status, response_headers, body = block.call
|
||||
Stub.new(path, request_headers, status, response_headers, body)
|
||||
end
|
||||
end
|
||||
|
||||
class Stub < Struct.new(:path, :request_headers, :status, :response_headers, :body)
|
||||
def matches?(request_path, headers)
|
||||
return false if request_path != path
|
||||
return true if request_headers.empty?
|
||||
request_headers.each do |key, value|
|
||||
return true if headers[key] == value
|
||||
end
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :stub
|
||||
|
||||
# TestConnection.new do |expect|
|
||||
# expect.get("/foo/bar", 'Content-Type' => 'application/json') { [200, {'content-type' => 'application/json'}, %(['a','b','c'])] }
|
||||
# end
|
||||
def initialize(url = nil)
|
||||
super(url)
|
||||
@stub = Stubs.new do |stubs|
|
||||
yield stubs if block_given?
|
||||
end
|
||||
end
|
||||
|
||||
def _get(uri, headers)
|
||||
if stub = @stub.match(:get, uri.path, headers)
|
||||
response_class.new do |resp|
|
||||
resp.headers = stub.response_headers
|
||||
resp.process stub.body
|
||||
end
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
include Faraday::Adapter::MockRequest
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user