首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Rspec中存在且不存在url参数的stub_request正则表达式

在Rspec中存在且不存在url参数的stub_request正则表达式
EN

Stack Overflow用户
提问于 2016-12-31 04:30:28
回答 1查看 1.8K关注 0票数 1

我需要用两个变体对请求进行存根,以下是来自Amazon的示例URL:

代码语言:javascript
复制
https://mws.amazonservices.jp/Orders/2013-09-01
  ?AWSAccessKeyId=0PB842EXAMPLE7N4ZTR2
  &Action=ListOrders
  &MWSAuthToken=amzn.mws.4ea38b7b-f563-7709-4bae-87aeaEXAMPLE
  &MarketplaceId.Id.1=A1VC38T7YXB528
  &FulfillmentChannel.Channel.1=MFN
  &PaymentMethod.Method.1=COD
  &PaymentMethod.Method.2=Other
  &OrderStatus.Status.1=Unshipped
  &OrderStatus.Status.2=PendingAvailability
  &SellerId=A2NEXAMPLETF53
  &Signature=ZQLpf8vEXAMPLE0iC265pf18n0%3D
  &SignatureVersion=2
  &SignatureMethod=HmacSHA256
  &LastUpdatedAfter=2013-08-01T18%3A12%3A21
  &Timestamp=2013-09-05T18%3A12%3A21.687Z
  &Version=2013-09-01

https://mws.amazonservices.jp/Orders/2013-09-01
  ?AWSAccessKeyId=0PB842EXAMPLE7N4ZTR2
  &Action=ListOrdersByNextToken
  &MWSAuthToken=amzn.mws.4ea38b7b-f563-7709-4bae-87aeaEXAMPLE
  &SellerId=A2986ZQ066CH2F
  &Signature=ZQLpf8vEXAMPLE0iC265pf18n0%3D
  &SignatureVersion=2
  &SignatureMethod=HmacSHA256
  &NextToken=2YgYW55IGNhcm5hbCBwbGVhc3VyZS4%3D
  &Timestamp=2013-09-05T18%3A12%3A21.687Z
  &Version=2013-09-01

现在,我已经通过克拉伊洛的帮助验证了regex,但是stub_request似乎不接受regex?该正则表达式已通过球状验证。

代码语言:javascript
复制
stub_request(:any, %r{/^.*amazonservices.com.*(&Action=ListOrders&).*$/}).to_return(body: order_fixture_with_pages, status: 200, headers: { 'Content-Type': 'text/xml' })
stub_request(:any, %r{/^.*amazonservices.com.*(&Action=ListOrdersByNextToken&).*$/}).to_return(body: order_fixture_no_pages, status: 200, headers: { 'Content-Type': 'text/xml' })

更新

在修复regex之后,我们现在尝试通过存根请求使用查询字符串变量,但它不起作用。有人在想吗?

代码语言:javascript
复制
stub_request(:any, /.*amazonservices.com.*/).
with(:query => hash_including({"Action" => 'ListOrders'})).
to_return(body: order_fixture_with_pages, status: 200, headers: { 'Content-Type': 'text/xml' })

我正在使用Rails 5和Rspec 3.5。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-31 22:42:13

经过讨论,我们需要搜索这个请求的主体,而不是查询参数。

代码语言:javascript
复制
before do
  stub_request(:any, /.*amazonservices.com.*/).
  with(body: /^.*(&Action=ListOrders&).*$/).
  to_return(body: order_fixture_with_pages, status: 200, headers: { 'Content-Type': 'text/xml' })

  stub_request(:any, /.*amazonservices.com.*/).
  with(body: /^.*(&Action=ListOrdersByNextToken&).*$/).
  to_return(body: order_fixture_no_pages, status: 200, headers: { 'Content-Type': 'text/xml' })
end

还有,负判读很难。取而代之的是积极的判断。:)

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41405477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档