Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve documentation on placeholders for uri substitution #196

Open
kratsg opened this issue Mar 20, 2022 · 3 comments
Open

improve documentation on placeholders for uri substitution #196

kratsg opened this issue Mar 20, 2022 · 3 comments

Comments

@kratsg
Copy link

kratsg commented Mar 20, 2022

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?

@sigmavirus24
Copy link
Collaborator

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

@kratsg
Copy link
Author

kratsg commented Mar 20, 2022

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
Copy link
Author

kratsg commented Mar 20, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants