A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.aws.amazon.com/sdkforruby/api/Aws/ClientStubs.html below:

ClientStubs — AWS SDK for Ruby V2

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

You can provide stub data that will be returned by the client.

client = Aws::S3::Client.new(stub_responses: {
  list_buckets: { buckets: [{name: 'my-bucket' }] },
  get_object: { body: 'data' },
})

client.list_buckets.buckets.map(&:name) client.get_object(bucket:'name', key:'key').body.read 

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)

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)

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)

Aws::S3::Resource.new.buckets.map(&:name)
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.

client.stub_responses(:get_object, 'NotFound')
client.get_object(bucket:'aws-sdk', key:'foo')

client.stub_responses(:get_object, Timeout::Error)
client.get_object(bucket:'aws-sdk', key:'foo')

client.stub_responses(:get_object, RuntimeError.new('custom message'))
client.get_object(bucket:'aws-sdk', key:'foo')
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:

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')

resp = client.head_object(bucket:'aws-sdk', key:'foo')
resp.content_length 

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4