betamaxpy / betamax

A VCR imitation designed only for python-requests.
https://betamax.readthedocs.io/en/latest/
Other
565 stars 62 forks source link

Recording a cassette when url changes #140

Closed jonathan-s closed 7 years ago

jonathan-s commented 7 years ago

I have a situation which is a bit of an edge case. The url I was hitting was always throwing a ConnectionError because the service redirected to another url and that url happened to be a non-valid url. So I'm more or less doing the following

s = requests.Session()
with Betamax(s).use_cassette('example') as vcr:
    try:
        r = s.get('https://example.com')
    except ConnectionError:
        r = s.get('https://example.com/modify-url-to-make-it-work')

Recording the cassette is fine, but when you try to replay it it will complain that https://example.com is not in the cassette. Do you have any suggestion for this?

sigmavirus24 commented 7 years ago

So the answer here is unfortunately not.

Capturing and replaying these exceptions was never quite in scope for Betamax. It'd be rather difficult for us to make it work correctly every-time especially when Betamax is more concerned with the validity of the data, headers, etc. in the Response than anything else.

jonathan-s commented 7 years ago

Thanks for the reply :). I almost guessed as much.

sigmavirus24 commented 7 years ago

For this kind of testing I tend to suggest something like Mock, but I understand you're not trying to assert the handling of the ConnectionError necessarily you're just trying to test something with this in it and Betamax is hindering that. I'm sorry for that. :(

jonathan-s commented 7 years ago

Yeah pretty much. One testcase less, it's not the end of the world. Thanks a lot for making betamax though, really helpful!

sigmavirus24 commented 7 years ago

Thanks a lot for making betamax though, really helpful!

I'm glad to hear that! I'm going to close this for now. Let me know if we need to reopen it.