I'm recording http requests with VCR for my test suite. I have to ignore a parameter called callback
because the parameter is random, and I don't want VCR to record a new request when this parameter changes (because only the parameter changes, not the request itself).
But my problem is, that I need to modify the response body VCR is serving to my app, based on the original value of callback
that my client generated at runtime.
Here is my VCR configuration:
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
In the c.before_http_request(:stubbed?)
callback, the real value of callback
is in the request.uri
. Then VCR ignores this parameter, and replays the former recorded cassette. In the c.before_playback
callback, I can modify the interaction.response.body
, but the callback
parameter got the value from the cassette, when it was recorded.
How is it possible to get the real value of callback
inside the c.before_playback
callback? Because this callback is the only one that allows to modify the response body. I tried it out with c.after_http_request(:stubbed?)
where I get the response body AND the real value of the callback
parameter, but this hook is too late, because my app already received the response body.
Any monkeypatch, dirty hack or trick would be great!
I found the solution on Github when someone put me into the right direction: https://github.com/nbibler/vcr-284