我正在使用Webmock测试ruby aws-sdk(在本例中为aws-sdk-batch)发出的http请求。
当请求失败时,Webmock会使用更方便的哈希值diff和partial matching处理json请求,但是只有当存根请求的Content-Type
为application/json
时,它才会这样做。 / p>
然而,aws-sdk-batch
gem创建一个带有空Content-type
(despite having documented the content type as application/json)的请求,并且似乎依赖于端点的默认行为将其解释为application/json
。
我想手动添加标头Content-type: application/json
,以便可以从Webmock对json请求正文的更好处理中受益。这可能吗?
未指定Content-Type:application/json
的示例Webmock响应:
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\"}"}})
如您所见,当这些测试针对字符串的测试失败时,您将面临一个痛苦的任务,一个接一个的字符地挑选主体,这是非常糟糕的开发人员体验。 Hashdiff的经验是FAR优越的。
答案 0 :(得分:0)
AWS开发工具包的基本http客户端称为Seahorse,它允许您添加用于更改请求链行为的处理程序。
可以添加标头seats = [
((50,250),(60,265)), #seat 1
((175,380),(185,395)), #seat 2
((300,450),(310,465)), #seat 3
((400,450),(410,465)), #seat 4
((600,465),(610,480)), #seat 5
((730,375),(740,390)) #seat 6
]
# ...
win.blit(pygame.transform.scale(player.hand[0].img,(card_size)),(seats[index][0]))
win.blit(pygame.transform.scale(player.hand[0].img,(card_size)),(seats[index][1]))
的处理程序如下所示:
Content-Type:application/json
然后可以将其添加到继承自Seahorse的AWS开发工具包服务中:
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
现在,Webmock将响应以下失败:
Aws::Batch::Client.add_plugin(ContentType)