Skip to content

improve documentation on placeholders for uri substitution #196

@kratsg

Description

@kratsg

Betamax supports uri substitution (

def replace_in_uri(self, text_to_replace, placeholder):
if text_to_replace == '':
return
for (obj, key) in (('request', 'uri'), ('response', 'url')):
uri = self.data[obj][key]
if text_to_replace in uri:
self.data[obj][key] = uri.replace(
text_to_replace, placeholder
)
) 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions