module Aws::ClientStubs

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.

Public Class Methods

included(subclass) click to toggle source

@api private

# File lib/aws-sdk-core/client_stubs.rb, line 12
def self.included(subclass)
  subclass.add_plugin('Aws::Plugins::StubResponses')
end
new(*args) click to toggle source
Calls superclass method
# File lib/aws-sdk-core/client_stubs.rb, line 16
def initialize(*args)
  @stubs = {}
  @stub_mutex = Mutex.new
  super
end

Public Instance Methods

next_stub(operation_name) click to toggle source

@api private

# File lib/aws-sdk-core/client_stubs.rb, line 93
def next_stub(operation_name)
  @stub_mutex.synchronize do
    stubs = @stubs[operation_name.to_sym] || []
    case stubs.length
    when 0 then new_stub(operation_name)
    when 1 then stubs.first
    else stubs.shift
    end
  end
end
stub_responses(operation_name, *stubs) click to toggle source

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
%x:stub_responses => true`.
# File lib/aws-sdk-core/client_stubs.rb, line 82
def stub_responses(operation_name, *stubs)
  if config.stub_responses
    apply_stubs(operation_name, stubs.flatten)
  else
    msg = 'stubbing is not enabled; enable stubbing in the constructor '
    msg << 'with `:stub_responses => true`'
    raise msg
  end
end

Private Instance Methods

apply_stubs(operation_name, stubs) click to toggle source
# File lib/aws-sdk-core/client_stubs.rb, line 113
def apply_stubs(operation_name, stubs)
  @stub_mutex.synchronize do
    @stubs[operation_name.to_sym] = stubs.map do |stub|
      case stub
      when Exception then stub
      when String then service_error_class(stub)
      when Hash then new_stub(operation_name, stub)
      else stub
      end
    end
  end
end
new_stub(operation_name, data = nil) click to toggle source

@param [Symbol] operation_name @param [Hash, nil] data @return [Structure]

# File lib/aws-sdk-core/client_stubs.rb, line 109
def new_stub(operation_name, data = nil)
  Stub.new(operation(operation_name).output).format(data || {})
end
service_error_class(name) click to toggle source
# File lib/aws-sdk-core/client_stubs.rb, line 126
def service_error_class(name)
  svc_module = Aws.const_get(self.class.name.split('::')[1])
  svc_module.const_get(:Errors).const_get(name)
end