@@ -15,27 +15,11 @@ module ClientStubs
15
15
16
16
# @api private
17
17
def setup_stubbing
18
- @stubs = { }
19
- @stub_mutex = Mutex . new
20
18
if Hash === @config . stub_responses
21
19
@config . stub_responses . each do |operation_name , stubs |
22
20
apply_stubs ( operation_name , Array === stubs ? stubs : [ stubs ] )
23
21
end
24
22
end
25
-
26
- # When a client is stubbed allow the user to access the requests made
27
- requests = @api_requests = [ ]
28
- requests_mutex = @requests_mutex = Mutex . new
29
- self . handle do |context |
30
- requests_mutex . synchronize do
31
- requests << {
32
- operation_name : context . operation_name ,
33
- params : context . params ,
34
- context : context
35
- }
36
- end
37
- @handler . call ( context )
38
- end
39
23
end
40
24
41
25
# Configures what data / errors should be returned from the named operation
@@ -175,7 +159,7 @@ def setup_stubbing
175
159
# on a client that has not enabled response stubbing via
176
160
# `:stub_responses => true`.
177
161
def stub_responses ( operation_name , *stubs )
178
- if config . stub_responses
162
+ if @ config. stub_responses
179
163
apply_stubs ( operation_name , stubs . flatten )
180
164
else
181
165
msg = 'stubbing is not enabled; enable stubbing in the constructor ' \
@@ -194,12 +178,12 @@ def stub_responses(operation_name, *stubs)
194
178
# @raise [NotImplementedError] Raises `NotImplementedError` when the client
195
179
# is not stubbed.
196
180
def api_requests ( options = { } )
197
- if config . stub_responses
198
- @requests_mutex . synchronize do
181
+ if @ config. stub_responses
182
+ @config . api_requests_mutex . synchronize do
199
183
if options [ :exclude_presign ]
200
- @api_requests . reject { |req | req [ :context ] [ :presigned_url ] }
184
+ @config . api_requests . reject { |req | req [ :context ] [ :presigned_url ] }
201
185
else
202
- @api_requests
186
+ @config . api_requests
203
187
end
204
188
end
205
189
else
@@ -228,54 +212,44 @@ def api_requests(options = {})
228
212
# @return [Structure] Returns a stubbed response data structure. The
229
213
# actual class returned will depend on the given `operation_name`.
230
214
def stub_data ( operation_name , data = { } )
231
- Stubbing ::StubData . new ( config . api . operation ( operation_name ) ) . stub ( data )
215
+ Stubbing ::StubData . new ( @ config. api . operation ( operation_name ) ) . stub ( data )
232
216
end
233
217
234
218
# @api private
235
219
def next_stub ( context )
236
220
operation_name = context . operation_name . to_sym
237
- stub = @stub_mutex . synchronize do
238
- stubs = @stubs [ operation_name ] || [ ]
221
+ stub = @config . stubs_mutex . synchronize do
222
+ stubs = @config . stubs [ operation_name ] || [ ]
239
223
case stubs . length
240
- when 0 then default_stub ( operation_name )
224
+ when 0 then stub_data ( operation_name )
241
225
when 1 then stubs . first
242
226
else stubs . shift
243
227
end
244
228
end
245
- Proc === stub ? convert_stub ( operation_name , stub . call ( context ) ) : stub
229
+ stub = convert_stub ( operation_name , stub , context )
230
+ stub [ :mutex ] = Mutex . new
231
+ stub
246
232
end
247
233
248
234
private
249
235
250
- def default_stub ( operation_name )
251
- stub = stub_data ( operation_name )
252
- http_response_stub ( operation_name , stub )
236
+ def apply_stubs ( operation_name , stubs )
237
+ @config . stubs_mutex . synchronize do
238
+ @config . stubs [ operation_name . to_sym ] = stubs
239
+ end
253
240
end
254
241
255
242
# This method converts the given stub data and converts it to a
256
243
# HTTP response (when possible). This enables the response stubbing
257
244
# plugin to provide a HTTP response that triggers all normal events
258
245
# during response handling.
259
- def apply_stubs ( operation_name , stubs )
260
- @stub_mutex . synchronize do
261
- @stubs [ operation_name . to_sym ] = stubs . map do |stub |
262
- convert_stub ( operation_name , stub )
263
- end
264
- end
265
- end
266
-
267
- def convert_stub ( operation_name , stub )
268
- stub = case stub
269
- when Proc then stub
246
+ def convert_stub ( operation_name , stub , context )
247
+ case stub
248
+ when Proc then convert_stub ( operation_name , stub . call ( context ) , context )
270
249
when Exception , Class then { error : stub }
271
250
when String then service_error_stub ( stub )
272
- when Hash then http_response_stub ( operation_name , stub )
273
- else { data : stub }
274
- end
275
- if Hash === stub
276
- stub [ :mutex ] = Mutex . new
251
+ else http_response_stub ( operation_name , stub )
277
252
end
278
- stub
279
253
end
280
254
281
255
def service_error_stub ( error_code )
@@ -299,14 +273,14 @@ def hash_to_http_resp(data)
299
273
end
300
274
301
275
def data_to_http_resp ( operation_name , data )
302
- api = config . api
276
+ api = @ config. api
303
277
operation = api . operation ( operation_name )
304
278
ParamValidator . new ( operation . output , input : false ) . validate! ( data )
305
279
protocol_helper . stub_data ( api , operation , data )
306
280
end
307
281
308
282
def protocol_helper
309
- case config . api . metadata [ 'protocol' ]
283
+ case @ config. api . metadata [ 'protocol' ]
310
284
when 'json' then Stubbing ::Protocols ::Json
311
285
when 'rest-json' then Stubbing ::Protocols ::RestJson
312
286
when 'rest-xml' then Stubbing ::Protocols ::RestXml
0 commit comments