module Aws::ClientStubs
def api_requests(options = {})
-
(NotImplementedError)
- Raises `NotImplementedError` when the client is not stubbed
Returns:
-
(Array)
- Returns an array of the api requests made, each request object contains the
def api_requests(options = {}) if config.stub_responses if options[:exclude_presign] @api_requests.reject {|req| req[:context][:presigned_url] } else @api_requests end else msg = 'This method is only implemented for stubbed clients, and is ' msg << 'available when you enable stubbing in the constructor with `stub_responses: true`' raise NotImplementedError.new(msg) end end
def apply_stubs(operation_name, stubs)
plugin to provide a HTTP response that triggers all normal events
HTTP response (when possible). This enables the response stubbing
This method converts the given stub data and converts it to a
def apply_stubs(operation_name, stubs) @stub_mutex.synchronize do @stubs[operation_name.to_sym] = stubs.map do |stub| convert_stub(operation_name, stub) end end end
def convert_stub(operation_name, stub)
def convert_stub(operation_name, stub) case stub when Proc then stub when Exception, Class then { error: stub } when String then service_error_stub(stub) when Hash then http_response_stub(operation_name, stub) else { data: stub } end end
def data_to_http_resp(operation_name, data)
def data_to_http_resp(operation_name, data) api = config.api operation = api.operation(operation_name) ParamValidator.validate!(operation.output, data) protocol_helper.stub_data(api, operation, data) end
def default_stub(operation_name)
def default_stub(operation_name) stub = stub_data(operation_name) http_response_stub(operation_name, stub) end
def hash_to_http_resp(data)
def hash_to_http_resp(data) http_resp = Seahorse::Client::Http::Response.new http_resp.status_code = data[:status_code] http_resp.headers.update(data[:headers]) http_resp.body = data[:body] http_resp end
def http_response_stub(operation_name, data)
def http_response_stub(operation_name, data) if Hash === data && data.keys.sort == [:body, :headers, :status_code] { http: hash_to_http_resp(data) } else { http: data_to_http_resp(operation_name, data) } end end
def next_stub(context)
- Api: - private
def next_stub(context) operation_name = context.operation_name.to_sym stub = @stub_mutex.synchronize do stubs = @stubs[operation_name] || [] case stubs.length when 0 then default_stub(operation_name) when 1 then stubs.first else stubs.shift end end Proc === stub ? convert_stub(operation_name, stub.call(context)) : stub end
def protocol_helper
def protocol_helper case config.api.metadata['protocol'] when 'json' then Stubbing::Protocols::Json when 'query' then Stubbing::Protocols::Query when 'ec2' then Stubbing::Protocols::EC2 when 'rest-json' then Stubbing::Protocols::RestJson when 'rest-xml' then Stubbing::Protocols::RestXml else raise "unsupported protocol" end.new end
def service_error_stub(error_code)
def service_error_stub(error_code) { http: protocol_helper.stub_error(error_code) } end
def setup_stubbing
- Api: - private
def setup_stubbing @stubs = {} @stub_mutex = Mutex.new 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 # When a client is stubbed allow the user to access the requests made @api_requests = [] requests = @api_requests self.handle do |context| requests << { operation_name: context.operation_name, params: context.params, context: context } @handler.call(context) end end
def stub_data(operation_name, data = {})
-
(Structure)
- Returns a stubbed response data structure. The
Parameters:
-
data
(Hash
) -- -
operation_name
(Symbol
) --
def stub_data(operation_name, data = {}) Stubbing::StubData.new(operation(operation_name)).stub(data) end
def stub_responses(operation_name, *stubs)
-
(RuntimeError)
- Raises a runtime error when called
Returns:
-
(void)
-
Parameters:
-
stubs
(Mixed
) -- One or more responses to return from the named -
operation_name
(Symbol
) --
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