您如何为Ruby AWS SDK设置内容类型的标题



我正在使用WebMock测试Ruby AWS-SDK提出的HTTP请求(在这种情况下为AWS-SDK批次(。

WebMock在请求失败时处理JSON请求,并具有更方便的哈希差异和部分匹配,但是只有在固执请求的Content-Typeapplication/json

但是,aws-sdk-batch GEM使用空的Content-type创建了一个请求(尽管已记录了内容类型为应用程序/JSON(,并且似乎依赖端点的默认行为将其解释为application/json

我想手动添加标题Content-type: application/json,以便我可以从Webmock更好地处理JSON请求主体中受益。这可能吗?


示例网络货物响应未指定Content-Type:application/json

     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: POST https://batch.us-east-1.amazonaws.com/v1/submitjob with body '{"jobName":"Fakie","jobQueue":"queue","jobDefinition":"def","parameters":{"task":"{"fake":"town"}"}}' with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'', 'Authorization'=>'AWS4-HMAC-SHA256 Credential=ACCESS_KEY_ID/20190701/us-east-1/batch/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=5f4bf85ba48333e6cda6ff613b4ea2faacd0417b4136621c58b87a488c3019ee', 'Content-Length'=>'106', 'Content-Type'=>'', 'Host'=>'batch.us-east-1.amazonaws.com', 'User-Agent'=>'aws-sdk-ruby3/3.54.2 ruby/2.5.5 x86_64-darwin18 aws-sdk-batch/1.20.0', 'X-Amz-Content-Sha256'=>'cf52595364d1a588b4ca4fdeaddb8170e4ad944fa28ac6df647484bb596de9c4', 'X-Amz-Date'=>'20190701T215756Z'}
       You can stub this request with the following snippet:
       stub_request(:post, "https://batch.us-east-1.amazonaws.com/v1/submitjob").
         with(
           body: "{"jobName":"Fakie","jobQueue":"quue","jobDefinition":"def","parameters":{"task":"{\"fake\":\"town\"}"}}",
           headers: {
          'Accept'=>'*/*',
          'Accept-Encoding'=>'',
          'Authorization'=>'AWS4-HMAC-SHA256 Credential=ACCESS_KEY_ID/20190701/us-east-1/batch/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=5f4bf85ba48333e6cda6ff613b4ea2faacd0417b4136621c58b87a488c3019ee',
          'Content-Length'=>'106',
          'Content-Type'=>'',
          'Host'=>'batch.us-east-1.amazonaws.com',
          'User-Agent'=>'aws-sdk-ruby3/3.54.2 ruby/2.5.5 x86_64-darwin18 aws-sdk-batch/1.20.0',
          'X-Amz-Content-Sha256'=>'cf52595364d1a588b4ca4fdeaddb8170e4ad944fa28ac6df647484bb596de9c4',
          'X-Amz-Date'=>'20190701T215756Z'
           }).
         to_return(status: 200, body: "", headers: {})
       registered request stubs:
       stub_request(:post, "https://batch.us-east-1.amazonaws.com/v1/submitjob").
         with(
           body: {"jobDefinition"=>"def", "jobName"=>"Wrong", "jobQueue"=>"queue", "parameters"=>{"task"=>"{"fake":"town"}"}})

您可以看到,当这些测试因字符串而失败时,您却遇到了通过角色挑选身体角色的痛苦任务,这是一种非常可怕的开发人员体验。哈迪夫体验要优越。

AWS SDK的基本HTTP客户端称为Seahorse,允许您添加更改请求链行为的处理程序。

可以添加标头Content-Type:application/json的处理程序看起来如下:

  class ContentType < Seahorse::Client::Plugin
    class Handler < Seahorse::Client::Handler
      def call(context)
        context.http_request.headers['Content-Type'] = 'application/json'
        @handler.call(context)
      end
    end
    handler(Handler, step: :sign, priority: 0)
  end

然后可以将其添加到Seahorse继承的AWS SDK服务中:

Aws::Batch::Client.add_plugin(ContentType)

现在WebMock将响应这样的故障:

...
       registered request stubs:
       stub_request(:post, "https://batch.us-east-1.amazonaws.com/v1/submitjob").
         with(
           body: {"jobDefinition"=>"def", "jobName"=>"Wrong", "jobQueue"=>"queueName", "parameters"=>{"task"=>"{"fake":"town"}"}})
       Body diff:
        [["~", "jobName", "Fakie", "Wrong"]]

相关内容

  • 没有找到相关文章

最新更新