如何使用 ruby Aws::Lambda::Client SDK 存根响应
How to Stub Responses with ruby Aws::Lambda::Client SDK
如何使用 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
语句允许您从本质上 return 返回相同的对象。因此,模拟响应的最佳方法是利用在测试环境之外使用的相同 class。 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 响应
按照使用 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
显然他们正在寻找具有以下键 [:body, :headers, :status_code]
的 Hash
context do
before do
LAMBDA_CLIENT.stub_responses(
:invoke,
{
body: {testing: true}.to_json,
headers: {},
status_code: 200
}
)
end
it { ... }
end
如何使用 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
语句允许您从本质上 return 返回相同的对象。因此,模拟响应的最佳方法是利用在测试环境之外使用的相同 class。 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 响应
按照使用 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
显然他们正在寻找具有以下键 [:body, :headers, :status_code]
Hash
context do
before do
LAMBDA_CLIENT.stub_responses(
:invoke,
{
body: {testing: true}.to_json,
headers: {},
status_code: 200
}
)
end
it { ... }
end