1

我正在用 VCR 为我的测试套件记录 http 请求。我必须忽略一个调用callback的参数,因为该参数是随机的,并且我不希望 VCR 在此参数更改时记录新的请求(因为只有参数更改,而不是请求本身)。

callback但我的问题是,我需要根据我的客户端在运行时生成的原始值来修改 VCR 为我的应用程序提供的响应正文。

这是我的录像机配置:

VCR.configure do |c|
  c.cassette_library_dir = 'spec/vcr_cassettes'
  c.hook_into :webmock
  c.allow_http_connections_when_no_cassette = false
  c.ignore_localhost = true

  c.before_http_request(:stubbed?) do |request|
    # request.uri is the ORIGINAL request made by the client
    puts request.uri
  end

  c.before_playback do |interaction, cassette|
    # request uri got the value from the cassette
    interaction.request.uri
    # and here I want to modify the interaction.response.body
    # based on the callback parameter from the original request from the client
    interaction.response.body = "...."
  end

  # add the callback and no caching _ parameter to the ignored parameters
  c.default_cassette_options = {
    record: :once,
    match_requests_on: [:method, VCR.request_matchers.uri_without_params("callback", "_")]
  }
end

c.before_http_request(:stubbed?)回调中,真正的值callbackrequest.uri. 然后 VCR 忽略此参数,并重放之前录制的磁带。在c.before_playback回调中,我可以修改interaction.response.body,但callback参数在录制时从盒式磁带中获取值。

如何获得回调内部的真正价值?因为此回调是唯一允许修改响应正文的回调。我试过了callbackc.before_playbackc.after_http_request(:stubbed?)获取响应正文和参数的实际值的位置callback,但是这个钩子为时已晚,因为我的应用程序已经收到了响应正文。

任何猴子补丁,肮脏的黑客或技巧都会很棒!

4

1 回答 1

2

当有人把我引向正确的方向时,我在 Github 上找到了解决方案:https ://github.com/nbibler/vcr-284

于 2013-05-14T09:31:27.820 回答