aws-sdk中使用webmock的正则表达式无效

时间:2015-07-26 15:08:24

标签: ruby regex rspec aws-sdk webmock

我正在尝试使用带有rspec的webmock将请求存根到Aws,但我似乎无法让正则表达式为SQS轮询工作。如果我运行rspec,webmock会生成一个'正确'存根,供我在before(:each)块中使用,在我的spec_helper.rb中如下:

    You can stub this request with the following snippet:

stub_request(:post, "https://sqs.us-west-2.amazonaws.com/123456789012/backlog").
  with(:body => "Action=ReceiveMessage&AttributeName.1=All&MaxNumberOfMessages=1&MessageAttributeName.1=All&QueueUrl=https%3A%2F%2Fsqs.us-west-2.amazonaws.com%2F123456789012%2Fbacklog&Version=2012-11-05&VisibilityTimeout=0&WaitTimeSeconds=20",
       :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'', 'Authorization'=>'AWS4-HMAC-SHA256 Credential=MY_ACCESS_KEY/20150726/us-west-2/sqs/aws4_request, SignedHeaders=content-type;host;user-agent;x-amz-content-sha256;x-amz-date, Signature=large_alpha-numeric-signature', 'Content-Length'=>'224', 'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8', 'Host'=>'sqs.us-west-2.amazonaws.com', 'User-Agent'=>'aws-sdk-ruby2/2.1.7 ruby/2.2.2 x86_64-darwin14', 'X-Amz-Content-Sha256'=>'69336339ae76cf370477d4dsaf667as0b5dd8d25762c7c78sad8a', 'X-Amz-Date'=>'20150726T143009Z'}).
  to_return(:status => 200, :body => "", :headers => {})

所以在spec_helper.rb我有

RSpec.configure do |config|
  config.before(:each) do
    stub_request(:post, "https://sqs.us-west-2.amazonaws.com/123456789012/backlog").
      with(:body => "Action=ReceiveMessage&AttributeName.1=All&MaxNumberOfMessages=1&MessageAttributeName.1=All&QueueUrl=https%3A%2F%2Fsqs.us-west-2.amazonaws.com%2F123456789012%2Fbacklog&Version=2012-11-05&VisibilityTimeout=0&WaitTimeSeconds=20",
        :headers => {'Accept'=>'*/*',
          'Accept-Encoding'=>'',
          'Authorization'=>"AWS4-HMAC-SHA256 Credential=MY_ACCESS_KEY/20150726/us-west-2/sqs/aws4_request,  SignedHeaders=content-type;host;user-agent;x-amz-content-sha256;x-amz-date, Signature=" + /"^[a-zA-Z0-9]*$"/,
          'Content-Length'=>'224', 
          'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8',
          'Host'=>'sqs.us-west-2.amazonaws.com',
          'User-Agent'=>'aws-sdk-ruby2/2.1.7 ruby/2.2.2 x86_64-darwin14',
          'X-Amz-Content-Sha256'=>'694236339ae76cf370477d4dsaf667as0b5dd8d25762c7c78sad8a',
          'X-Amz-Date'=>""+ /"^[a-zA-Z0-9]*$"/}).
              to_return(:status => 200, :body => "", :headers => {})
end

我正在尝试使用正则表达式的区域是SignatureX-Amz-Date,因为它们是在运行rspec的不同尝试之间似乎发生变化的唯一两个区域。 问题是正则表达式似乎不起作用,因为即使我已经将它添加到spec_helper.rb中,每次运行套件时,我都会从webmock返回推荐的存根,而不是传递或失败的测试。它应该在这一点上传递,从我从webmock文档和几个教程中理解。 我应该如何更改这个以使webmock能够针对Aws SQS轮询使用我的测试套件? 我已经在我的桌子上猛击了几天,所以任何帮助都非常感激。

2 个答案:

答案 0 :(得分:0)

签名可能是使用Time.now的副产品生成的,我猜你实际上并不想测试它。而只是做:

stub_request(:post, "https://sqs.us-west-2.amazonaws.com/123456789012/backlog").and_return(:status => 200, :body => "", :headers => {})

如果您希望在URL上更不具体(比如省略该ID),您甚至可以使用正则表达式匹配:

stub_request(:post, /amazonaws.com/).and_return(:status => 200, :body => "", :headers => {})

答案 1 :(得分:0)

我有同样的问题,仅针对'X-Amz-Date'有解决方案。

由于日期采用特殊格式,因此请在模拟和方法调用周围使用Timecop.freeze块。

Timecop.freeze do
  stub_request(:get, "https://s3.eu-central-1.amazonaws.com/test_bucket/test").
      with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'', 'Authorization'=>'AWS4-HMAC-SHA256 Credential=access_key/20190814/eu-central-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=9e6a6a209a0cb05346a058c12ef706ebff185ae3b72f3e542e1becbc97e8ea7a', 'Content-Length'=>'0', 'Content-Type'=>'', 'Host'=>'s3.eu-central-1.amazonaws.com', 'User-Agent'=>'aws-sdk-ruby2/2.9.44 ruby/2.6.3 x86_64-darwin18 resources', 'X-Amz-Content-Sha256'=>'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'X-Amz-Date'=>"#{time.utc.iso8601(0).gsub(/[^\p{Alnum}]/, '')}"}).
      to_return(status: 200, body: "", headers: {})
  AwsService.bucket.object("test").get
end

正则表达式仅保留字母数字字符,而Timecop会花费时间。