ropensci / vcr

Record HTTP calls and replay them
https://docs.ropensci.org/vcr
Other
77 stars 12 forks source link

support looping through responses for the same request? #190

Closed maelle closed 3 years ago

maelle commented 3 years ago

Context: At the moment for faking some behavior of the server (e.g. an error, or simpler responses to decrease the fixture size) one needs to use webmockr. This is not ideal as it's quite different from vcr.

One alternative is

It's quite handy IMHO.

Now, one thing that this workflow can't handle is testing retries. So I wonder whether there'd be a way to add a field "count" to the fixture and e.g. have

http_interactions:
- request:
    count: 1
    method: get
    uri: https://kctbh9vrtdwd.statuspage.io/api/v2/components.json
    body:
      encoding: ''
      string: ''
    headers:
      Accept: application/json, text/xml, application/xml, */*
  response:
    - count: 1
      status_code: 502
    - count: 2
      status_code: 200

and vcr would loop through the responses?

sckott commented 3 years ago

vcr has something that may address this already, see e.g., in the Ruby vcr docs https://relishapp.com/vcr/vcr/v/5-1-0/docs/request-matching/playback-repeats

and we have that arg in this pkg https://docs.ropensci.org/vcr/reference/use_cassette.html - I've not used it yet, so can't be 100% certain it works as intended, so would be good to try

maelle commented 3 years ago

Oh actually the default behavior is the right one, awesome.

See the test of gh_organizations() in https://github.com/maelle/exemplighratia/pull/8, I think it fails once and then succeeds.

So I'm a bit undecided regarding what to encourage folks to use

Maybe the book should just mention both. webmockr allows more flexibility in how you define a sequence, but it's a new thing to learn after making the effort to understand vcr and cassettes. :thinking:

sckott commented 3 years ago

Oh actually the default behavior is the right one

of? do you mean the default value for allow_playback_repeats parameter?

maelle commented 3 years ago

Yes, I didn't have to change anything

maelle commented 3 years ago

I think this can be closed, it works and it's documented in https://docs.ropensci.org/vcr/articles/cassette-manual-editing.html#example-2-test-using-an-edited-cassette-with-a-503-then-a-200-1