mirror of
https://github.com/lostisland/faraday.git
synced 2025-11-27 00:04:03 -05:00
Add support for headers to the test adapter
This commit is contained in:
parent
55c2418031
commit
f9b8fe8546
@ -28,46 +28,46 @@ module Faraday
|
|||||||
@stack.empty?
|
@stack.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
def match(request_method, path, body)
|
def match(request_method, path, headers, body)
|
||||||
return false if !@stack.key?(request_method)
|
return false if !@stack.key?(request_method)
|
||||||
stack = @stack[request_method]
|
stack = @stack[request_method]
|
||||||
consumed = (@consumed[request_method] ||= [])
|
consumed = (@consumed[request_method] ||= [])
|
||||||
path = normalize_path(path)
|
path = normalize_path(path)
|
||||||
|
|
||||||
if stub = matches?(stack, path, body)
|
if stub = matches?(stack, path, headers, body)
|
||||||
consumed << stack.delete(stub)
|
consumed << stack.delete(stub)
|
||||||
stub
|
stub
|
||||||
else
|
else
|
||||||
matches?(consumed, path, body)
|
matches?(consumed, path, headers, body)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(path, &block)
|
def get(path, headers = {}, &block)
|
||||||
new_stub(:get, path, &block)
|
new_stub(:get, path, headers, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def head(path, &block)
|
def head(path, headers = {}, &block)
|
||||||
new_stub(:head, path, &block)
|
new_stub(:head, path, headers, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def post(path, body=nil, &block)
|
def post(path, body=nil, headers = {}, &block)
|
||||||
new_stub(:post, path, body, &block)
|
new_stub(:post, path, headers, body, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def put(path, body=nil, &block)
|
def put(path, body=nil, headers = {}, &block)
|
||||||
new_stub(:put, path, body, &block)
|
new_stub(:put, path, headers, body, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def patch(path, body=nil, &block)
|
def patch(path, body=nil, headers = {}, &block)
|
||||||
new_stub(:patch, path, body, &block)
|
new_stub(:patch, path, headers, body, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete(path, &block)
|
def delete(path, headers = {}, &block)
|
||||||
new_stub(:delete, path, &block)
|
new_stub(:delete, path, headers, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def options(path, &block)
|
def options(path, headers = {}, &block)
|
||||||
new_stub(:options, path, &block)
|
new_stub(:options, path, headers, &block)
|
||||||
end
|
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.
|
||||||
@ -85,12 +85,12 @@ module Faraday
|
|||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def new_stub(request_method, path, body=nil, &block)
|
def new_stub(request_method, path, headers = {}, body=nil, &block)
|
||||||
(@stack[request_method] ||= []) << Stub.new(normalize_path(path), body, block)
|
(@stack[request_method] ||= []) << Stub.new(normalize_path(path), headers, body, block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches?(stack, path, body)
|
def matches?(stack, path, headers, body)
|
||||||
stack.detect { |stub| stub.matches?(path, body) }
|
stack.detect { |stub| stub.matches?(path, headers, body) }
|
||||||
end
|
end
|
||||||
|
|
||||||
# ensure leading + trailing slash
|
# ensure leading + trailing slash
|
||||||
@ -102,23 +102,24 @@ module Faraday
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Stub < Struct.new(:path, :params, :body, :block)
|
class Stub < Struct.new(:path, :params, :headers, :body, :block)
|
||||||
def initialize(full, body, block)
|
def initialize(full, headers, body, block)
|
||||||
path, query = full.split('?')
|
path, query = full.split('?')
|
||||||
params = query ?
|
params = query ?
|
||||||
Faraday::Utils.parse_nested_query(query) :
|
Faraday::Utils.parse_nested_query(query) :
|
||||||
{}
|
{}
|
||||||
super path, params, body, block
|
super path, params, headers, body, block
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches?(request_uri, request_body)
|
def matches?(request_uri, request_headers, request_body)
|
||||||
request_path, request_query = request_uri.split('?')
|
request_path, request_query = request_uri.split('?')
|
||||||
request_params = request_query ?
|
request_params = request_query ?
|
||||||
Faraday::Utils.parse_nested_query(request_query) :
|
Faraday::Utils.parse_nested_query(request_query) :
|
||||||
{}
|
{}
|
||||||
request_path == path &&
|
request_path == path &&
|
||||||
params_match?(request_params) &&
|
params_match?(request_params) &&
|
||||||
(body.to_s.size.zero? || request_body == body)
|
(body.to_s.size.zero? || request_body == body) &&
|
||||||
|
headers_match?(request_headers)
|
||||||
end
|
end
|
||||||
|
|
||||||
def params_match?(request_params)
|
def params_match?(request_params)
|
||||||
@ -127,6 +128,12 @@ module Faraday
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def headers_match?(request_headers)
|
||||||
|
headers.keys.all? do |key|
|
||||||
|
request_headers[key] == headers[key]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
"#{path} #{body}"
|
"#{path} #{body}"
|
||||||
end
|
end
|
||||||
@ -146,7 +153,7 @@ module Faraday
|
|||||||
super
|
super
|
||||||
normalized_path = Faraday::Utils.normalize_path(env[:url])
|
normalized_path = Faraday::Utils.normalize_path(env[:url])
|
||||||
|
|
||||||
if stub = stubs.match(env[:method], normalized_path, env[:body])
|
if stub = stubs.match(env[:method], normalized_path, env.request_headers, env[:body])
|
||||||
env[:params] = (query = env[:url].query) ?
|
env[:params] = (query = env[:url].query) ?
|
||||||
Faraday::Utils.parse_nested_query(query) :
|
Faraday::Utils.parse_nested_query(query) :
|
||||||
{}
|
{}
|
||||||
|
|||||||
@ -41,6 +41,13 @@ module Adapters
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_middleware_with_http_headers
|
||||||
|
@stubs.get('/yo', { 'X-HELLO' => 'hello' }) { [200, {}, 'a'] }
|
||||||
|
@stubs.get('/yo') { [200, {}, 'b'] }
|
||||||
|
assert_equal 'a', @conn.get('/yo') { |env| env.headers['X-HELLO'] = 'hello' }.body
|
||||||
|
assert_equal 'b', @conn.get('/yo').body
|
||||||
|
end
|
||||||
|
|
||||||
def test_middleware_allow_different_outcomes_for_the_same_request
|
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'}, 'hello'] }
|
||||||
@stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'world'] }
|
@stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'world'] }
|
||||||
@ -66,5 +73,12 @@ module Adapters
|
|||||||
@conn.get('/invalid'){ [200, {}, []] }
|
@conn.get('/invalid'){ [200, {}, []] }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_raises_an_error_if_no_stub_is_found_for_request_without_this_header
|
||||||
|
@stubs.get('/yo', { 'X-HELLO' => 'hello' }) { [200, {}, 'a'] }
|
||||||
|
assert_raises Faraday::Adapter::Test::Stubs::NotFound do
|
||||||
|
@conn.get('/yo')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user