您如何使用AWS ruby SDK模拟lambda响应?
提供的文档仅提供了S3的基本用法示例,这些示例与lambda请求无关。
https://docs.aws.amazon.com/sdkforruby/api/Aws/ClientStubs.html
遵循的代码stub_responses
,它将带您进入convert_stub
。似乎有三个可行的方法可以模拟以下响应:Proc
,Hash
和else
语句。
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
在下面的模拟示例中,我具有这样的AWS客户端设置。
aws_credentials = {
region: 'us-east-1',
access_key_id: Rails.application.secrets.aws_key,
secret_access_key: Rails.application.secrets.aws_secret,
stub_responses: Rails.env.test?
}
LAMBDA_CLIENT = Aws::Lambda::Client.new(aws_credentials)
模拟Aws :: Lambda :: Types :: InvocationResponse
该else
语句允许您从本质上返回相同的对象。因此,模拟响应的最佳方法是利用在测试环境之外使用的同一类。Aws::Lambda::Types::InvocationResponse
context do
before do
LAMBDA_CLIENT.stub_responses(
:invoke,
Aws::Lambda::Types::InvocationResponse.new(
executed_version: "$LATEST",
function_error: nil,
log_result: nil,
payload: StringIO.new("hello".to_json),
status_code: 200
)
)
end
it { ... }
end
模拟HTTP响应
遵循使用a的逻辑Hash
,我们需要深入了解其http_response_stub
作用。
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
显然,他们正在寻找Hash
具有以下键的[:body, :headers, :status_code]
context do
before do
LAMBDA_CLIENT.stub_responses(
:invoke,
{
body: {testing: true}.to_json,
headers: {},
status_code: 200
}
)
end
it { ... }
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句