require'thread'moduleAws# This module provides the ability to specify the data and/or errors to# return when a client is using stubbed responses. Pass# `:stub_responses => true` to a client constructor to enable this# behavior.moduleClientStubs# @api privatedefself.included(subclass)subclass.add_plugin('Aws::Plugins::StubResponses')enddefinitialize(*args)@stubs={}@stub_mutex=Mutex.newsuperend# Configures what data / errors should be returned from the named operation# when response stubbing is enabled.## ## Basic usage## By default, fake responses are generated. You can override the default# fake data with specific response data by passing a hash.## # enable response stubbing in the client constructor# client = Aws::S3::Client.new(stub_responses: true)## # specify the response data for #list_buckets# client.stub_responses(:list_buckets, buckets:[{name:'aws-sdk'}])## # no api calls made, stub returned# client.list_buckets.map(&:name)# #=> ['aws-sdk']## ## Stubbing Errors## When stubbing is enabled, the SDK will default to generate# fake responses with placeholder values. You can override the data# returned. You can also specify errors it should raise.## client.stub_responses(:get_object, 'NotFound')# client.get_object(bucket:'aws-sdk', key:'foo')# #=> raises Aws::S3::Errors::NotFound## client.stub_responses(:get_object, Timeout::Error)# client.get_object(bucket:'aws-sdk', key:'foo')# #=> raises new Timeout::Error## client.stub_responses(:get_object, RuntimeError.new('custom message'))# client.get_object(bucket:'aws-sdk', key:'foo')# #=> raises the given runtime error object## ## Stubbing Multiple Responses## Calling an operation multiple times will return similar responses.# You can configure multiple stubs and they will be returned in sequence.### client.stub_responses(:head_object, [# 'NotFound',# { content_length: 150 },# ])## client.head_object(bucket:'aws-sdk', key:'foo')# #=> raises Aws::S3::Errors::NotFound## resp = client.head_object(bucket:'aws-sdk', key:'foo')# resp.content_length #=> 150## @param [Symbol] operation_name# @param [Mixed] stubs One or more responses to return from the named# operation.# @return [void]# @raise [RuntimeError] Raises a runtime error when called# on a client that has not enabled response stubbing via# `:stub_responses => true`.defstub_responses(operation_name,*stubs)ifconfig.stub_responsesapply_stubs(operation_name,stubs.flatten)elsemsg='stubbing is not enabled; enable stubbing in the constructor 'msg<<'with `:stub_responses => true`'raisemsgendend# @api privatedefnext_stub(operation_name)@stub_mutex.synchronizedostubs=@stubs[operation_name.to_sym]||[]casestubs.lengthwhen0thennew_stub(operation_name)when1thenstubs.firstelsestubs.shiftendendendprivate# @param [Symbol] operation_name# @param [Hash, nil] data# @return [Structure]defnew_stub(operation_name,data=nil)Stub.new(operation(operation_name).output).format(data||{})enddefapply_stubs(operation_name,stubs)@stub_mutex.synchronizedo@stubs[operation_name.to_sym]=stubs.mapdo|stub|casestubwhenExceptionthenstubwhenStringthenservice_error_class(stub)whenHashthennew_stub(operation_name,stub)elsestubendendendenddefservice_error_class(name)svc_module=Aws.const_get(self.class.name.split('::')[1])svc_module.const_get(:Errors).const_get(name)endclassStub# @param [Seahorse::Models::Shapes::Structure] output_shape This should# be the output shape for an operation.definitialize(output_shape)@shape=output_shapeend# @param [Hash] data An optional hash of data to format into the stubbed# object.defformat(data={})if@shape.nil?empty_stub(data)elsevalidate_data(data)stub(@shape,data)endendprivatedefstub(shape,value)caseshapewhenSeahorse::Model::Shapes::Structurethenstub_structure(shape,value)whenSeahorse::Model::Shapes::Listthenstub_list(shape,value||[])whenSeahorse::Model::Shapes::Mapthenstub_map(shape,value||{})elsestub_scalar(shape,value)endenddefstub_structure(shape,hash)ifhashstructure_obj(shape,hash)elsenilendenddefstructure_obj(shape,hash)stubs=Structure.new(shape.member_names)shape.members.eachdo|member_name,member_shape|ifhash.key?(member_name)&&hash[member_name].nil?stubs[member_name]=nilelsevalue=structure_value(shape,member_name,member_shape,hash)stubs[member_name]=stub(member_shape,value)endendstubsenddefstructure_value(shape,member_name,member_shape,hash)ifhash.key?(member_name)hash[member_name]elsifSeahorse::Model::Shapes::Structure===member_shape&&shape.required.include?(member_name)then{}elsenilendenddefstub_list(shape,array)stubs=[]array.eachdo|value|stubs<<stub(shape.member,value)endstubsenddefstub_map(shape,value)stubs={}value.eachdo|key,value|stubs[key]=stub(shape.value,value)endstubsenddefstub_scalar(shape,value)ifvalue.nil?caseshapewhenSeahorse::Model::Shapes::Stringthenshape.namewhenSeahorse::Model::Shapes::Integerthen0whenSeahorse::Model::Shapes::Floatthen0.0whenSeahorse::Model::Shapes::BooleanthenfalsewhenSeahorse::Model::Shapes::TimestampthenTime.nowelsenilendelsevalueendenddefempty_stub(data)ifdata.empty?Structure.new(data)elsemsg='unable to generate a stubbed response from the given data; 'msg<<'this operation does not return data'raiseArgumentError,msgendenddefvalidate_data(data)args=[@shape,{validate_required:false}]Seahorse::Client::ParamValidator.new(*args).validate!(data)endendendend