Skip to content

Commit

Permalink
You can now stub responses via Aws.config and client constructors.
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorrowe committed Jun 5, 2015
1 parent e345005 commit 2f72c00
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
47 changes: 39 additions & 8 deletions aws-sdk-core/lib/aws-sdk-core/client_stubs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,58 @@ 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
# 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.
# 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
# #=> #<struct Aws::S3::Types::ListBucketsOutput buckets=[], owner=nil>
#
# # 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
Expand Down Expand Up @@ -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 },
Expand Down
10 changes: 10 additions & 0 deletions aws-sdk-core/spec/aws/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2f72c00

Please sign in to comment.