betamaxpy / betamax

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

improve documentation on placeholders for uri substitution #196

Open kratsg opened 2 years ago

kratsg commented 2 years ago

Betamax supports uri substitution (https://github.com/betamaxpy/betamax/blob/2c12cee59ac365f39497a3718eed04ab9c6ce988/src/betamax/cassette/interaction.py#L99-L107) however you might typically need to quote the string you're looking for first. E.G. something like this


from urllib.parse import urlparse, parse_qs, quote

def filter_requests(interaction, current_cassette):
    token = parse_qs(urlparse(interaction.data['request']['uri']).query).get('authz')

    if token is not None:
        current_cassette.placeholders.append(
            betamax.cassette.cassette.Placeholder(
                placeholder='EOS_TOKEN', replace=quote(token[0])
            )   
        )   

betamax.Betamax.register_serializer(pretty_json.PrettyJSONSerializer)
with betamax.Betamax.configure() as config:
    config.default_cassette_options['serialize_with'] = 'prettyjson'
    config.before_record(callback=filter_requests)

It would be nice if betamax checked for the quoted value as well, because you don't get it at the time of filtering the requests through the interaction?

sigmavirus24 commented 2 years ago

Is this a docs issue or a feature request? It's hard to tell.

kratsg commented 2 years ago

Is this a docs issue or a feature request? It's hard to tell.

I'm showing code example of how to get around this for now, but it's not clear if this should be a feature request or not. I've waffled because I think betamax is doing the right thing in taking the string provided by the user and looking for it. I would be a little worried if it started replacing strings I didn't specify exactly.

kratsg commented 2 years ago

Also somewhat related -- the placeholder functionality will not replace anything in response bodies that were base64-encoded (which is another gotcha).