diff --git a/aws-sdk-core/lib/aws-sdk-core/client_stubs.rb b/aws-sdk-core/lib/aws-sdk-core/client_stubs.rb index a453574265b..f80ef49e290 100644 --- a/aws-sdk-core/lib/aws-sdk-core/client_stubs.rb +++ b/aws-sdk-core/lib/aws-sdk-core/client_stubs.rb @@ -12,6 +12,12 @@ def initialize(*args) @stubs = {} @stub_mutex = Mutex.new super + if Hash === @config.stub_responses + @config.stub_responses.each do |operation_name, stubs| + apply_stubs(operation_name, Array === stubs ? stubs : [stubs]) + end + @config.stub_responses = true + end end # Configures what data / errors should be returned from the named operation @@ -19,19 +25,45 @@ def initialize(*args) # # ## Basic usage # - # By default, fake responses are generated. You can override the default - # fake data with specific response data by passing a hash. + # When you enable response stubbing, the client will generate fake + # responses and will not make any HTTP requests. # - # # enable response stubbing in the client constructor # client = Aws::S3::Client.new(stub_responses: true) + # client.list_buckets + # #=> # # - # # specify the response data for #list_buckets - # client.stub_responses(:list_buckets, buckets:[{name:'aws-sdk'}]) + # You can provide stub data that will be returned by the client. + # + # # stub data in the constructor + # client = Aws::S3::Client.new(stub_responses: { + # list_buckets: { bukets: [{name: 'my-bucket' }] }, + # get_object: { body: 'data' }, + # }) # - # # no api calls made, stub returned - # client.list_buckets.map(&:name) + # client.list_buckets.buckets.map(&:name) #=> ['my-bucket'] + # client.get_object(bucket:'name', key:'key').body.read #=> 'data' + # + # You can also specify the stub data using {#stub_responses} + # + # client = Aws::S3::Client.new(stub_responses: true) + # client.stub_resposnes(:list_buckets, { + # buckets: [{ name: 'my-bucket' }] + # }) + # + # client.list_buckets.buckets.map(&:name) #=> ['my-bucket'] # #=> ['aws-sdk'] # + # Lastly, default stubs can be configured via `Aws.config`: + # + # Aws.config[:s3] = { + # stub_responses: { + # list_buckets: { bukets: [{name: 'my-bucket' }] } + # } + # } + # + # Aws::S3::Client.new.list_buckets.buckets.map(&:name) + # #=> ['my-bucket'] + # # ## Stubbing Errors # # When stubbing is enabled, the SDK will default to generate @@ -73,7 +105,6 @@ def initialize(*args) # 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 }, diff --git a/aws-sdk-core/spec/aws/client_spec.rb b/aws-sdk-core/spec/aws/client_spec.rb index 686ac26d385..7fb7d3ddf45 100644 --- a/aws-sdk-core/spec/aws/client_spec.rb +++ b/aws-sdk-core/spec/aws/client_spec.rb @@ -80,6 +80,16 @@ module Aws expect(client.head_bucket(bucket:'aws-sdk').data.to_h).to eq({}) end + it 'accepts stubs given to the constructor' do + client = S3::Client.new(stub_responses: { + list_buckets: { buckets: [{ name: 'b1' }, { name:'b2' }] }, + get_object: [{ body: 'a' }, { body: 'b' }], + }) + expect(client.list_buckets.buckets.map(&:name)).to eq(%w(b1 b2)) + expect(client.get_object(bucket:'name', key:'key').body.read).to eq('a') + expect(client.get_object(bucket:'name', key:'key').body.read).to eq('b') + end + end end end