forked from lostisland/faraday
/
test.rb
147 lines (124 loc) · 3.84 KB
/
test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
module Faraday
class Adapter
# test = Faraday::Connection.new do
# use Faraday::Adapter::Test do |stub|
# stub.get '/nigiri/sake.json' do
# [200, {}, 'hi world']
# end
# end
# end
#
# resp = test.get '/nigiri/sake.json'
# resp.body # => 'hi world'
#
class Test < Faraday::Adapter
attr_accessor :stubs
def self.loaded?() false end
class Stubs
class NotFound < StandardError
end
def initialize
# {:get => [Stub, Stub]}
@stack, @consumed = {}, {}
yield self if block_given?
end
def empty?
@stack.empty?
end
def match(request_method, path, body)
return false if !@stack.key?(request_method)
stack = @stack[request_method]
consumed = (@consumed[request_method] ||= [])
if stub = matches?(stack, path, body)
consumed << stack.delete(stub)
stub
else
matches?(consumed, path, body)
end
end
def get(path, &block)
new_stub(:get, path, &block)
end
def head(path, &block)
new_stub(:head, path, &block)
end
def post(path, body=nil, &block)
new_stub(:post, path, body, &block)
end
def put(path, body=nil, &block)
new_stub(:put, path, body, &block)
end
def delete(path, &block)
new_stub(:delete, path, &block)
end
# Raises an error if any of the stubbed calls have not been made.
def verify_stubbed_calls
failed_stubs = []
@stack.each do |method, stubs|
unless stubs.size == 0
failed_stubs.concat(stubs.map {|stub|
"Expected #{method} #{stub}."
})
end
end
raise failed_stubs.join(" ") unless failed_stubs.size == 0
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
class Stub < Struct.new(:path, :params, :body, :block)
def initialize(full, body, block)
path, query = full.split('?')
params = query ?
Rack::Utils.parse_nested_query(query) :
{}
super path, params, body, block
end
def matches?(request_uri, request_body)
request_path, request_query = request_uri.split('?')
request_params = request_query ?
Rack::Utils.parse_nested_query(request_query) :
{}
request_path == path &&
params_match?(request_params) &&
(body.to_s.size.zero? || request_body == body)
end
def params_match?(request_params)
params.keys.all? do |key|
request_params[key] == params[key]
end
end
def to_s
"#{path} #{body}"
end
end
def initialize(app, stubs=nil, &block)
super(app)
@stubs = stubs || Stubs.new
configure(&block) if block
end
def configure
yield stubs
end
def call(env)
super
normalized_path = Faraday::Utils.normalize_path(env[:url])
if stub = stubs.match(env[:method], normalized_path, env[:body])
env[:params] = (query = env[:url].query) ?
Rack::Utils.parse_nested_query(query) :
{}
status, headers, body = stub.block.call(env)
save_response(env, status, body, headers)
else
raise Stubs::NotFound, "no stubbed request for #{env[:method]} #{normalized_path} #{env[:body]}"
end
@app.call(env)
end
end
end
end