我正在用VCR记录我的测试套件的http请求。我必须忽略一个名为callback的参数,因为该参数是随机的,而且我不希望VCR在该参数更改时记录新的请求(因为只有参数更改,而不是请求本身)。
但是我的问题是,我需要根据我的客户端在运行时生成的callback的原始值来修改VCR为我的应用程序服务的响应体。
这是我的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?)回调中,callback的真正值在request.uri中。然后VCR忽略这个参数,并重放以前录制的盒式磁带。在c.before_playback回调中,我可以修改interaction.response.body,但是在录制磁带时,callback参数从磁带中得到了值。
如何在回调中获得callback的真实值?因为此回调是唯一允许修改响应体的回调。我在c.after_http_request(:stubbed?)中尝试了这个方法,在这里我得到了响应体和callback参数的实际值,但是这个钩子太晚了,因为我的应用程序已经收到了响应体。
任何猴子,肮脏的黑客或诡计将是伟大的!
发布于 2013-05-14 09:31:27
当有人把我带到正确的方向时,我找到了解决方案:https://github.com/nbibler/vcr-284
https://stackoverflow.com/questions/16077003
复制相似问题