ClientStubs — AWS SDK for Ruby V3 (original) (raw)
This method returns an undefined value.
Configures what data / errors should be returned from the named operation when response stubbing is enabled.
Basic usage
When you enable response stubbing, the client will generate fake responses and will not make any HTTP requests.
client = Aws::S3::Client.new(stub_responses: true)
client.list_buckets
#=> #<struct Aws::S3::Types::ListBucketsOutput buckets=[], owner=nil>
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: { buckets: [{name: 'my-bucket' }] },
get_object: { body: 'data' },
})
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_responses(:list_buckets, {
buckets: [{ name: 'my-bucket' }]
})
client.list_buckets.buckets.map(&:name)
#=> ['my-bucket']
With a Resource class #stub_responses on the corresponding client:
s3 = Aws::S3::Resource.new(stub_responses: true)
s3.client.stub_responses(:list_buckets, {
buckets: [{ name: 'my-bucket' }]
})
s3.buckets.map(&:name)
#=> ['my-bucket']
Lastly, default stubs can be configured via Aws.config
:
Aws.config[:s3] = {
stub_responses: {
list_buckets: { buckets: [{name: 'my-bucket' }] }
}
}
Aws::S3::Client.new.list_buckets.buckets.map(&:name)
#=> ['my-bucket']
Aws::S3::Resource.new.buckets.map(&:name)
#=> ['my-bucket']
Dynamic Stubbing
In addition to creating static stubs, it's also possible to generate stubs dynamically based on the parameters with which operations were called, by passing a Proc
object:
s3 = Aws::S3::Resource.new(stub_responses: true)
s3.client.stub_responses(:put_object, -> (context) {
s3.client.stub_responses(:get_object, content_type: context.params[:content_type])
})
The yielded object is an instance of Seahorse::Client::RequestContext.
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.
# simulate service errors, give the error code
client.stub_responses(:get_object, 'NotFound')
client.get_object(bucket:'aws-sdk', key:'foo')
#=> raises Aws::S3::Errors::NotFound
# to simulate other errors, give the error class, you must
# be able to construct an instance with `.new`
client.stub_responses(:get_object, Timeout::Error)
client.get_object(bucket:'aws-sdk', key:'foo')
#=> raises new Timeout::Error
# or you can give an instance of an error class
client.stub_responses(:get_object, RuntimeError.new('custom message'))
client.get_object(bucket:'aws-sdk', key:'foo')
#=> raises the given runtime error object
Stubbing HTTP Responses
As an alternative to providing the response data, you can provide an HTTP response.
client.stub_responses(:get_object, {
status_code: 200,
headers: { 'header-name' => 'header-value' },
body: "...",
})
To stub a HTTP response, pass a Hash with all three of the following keys set:
:status_code
- - The HTTP status code:headers
- Hash<string,string> - A hash of HTTP header keys and values</string,string>:body
- <string,io> - The HTTP response body.</string,io>
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