-
Notifications
You must be signed in to change notification settings - Fork 62
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
tests fails on follow up runs #211
Comments
def test_submission(self, bot, recorder, request):
submission = bot.reddit.submission(id='w03cku')
with recorder.use_cassette(request.node.name):
assert submission.author Seems to me like it doesn't perform a request while betamax is recording but I don't know your bot code |
The issue is with the 'Content-Length' header mismatch, as the placeholder is shorter in length than the original value. I can patch the content-length manually before playback... but I wonder if there's a better way. |
You likely need the preserve exact body bytes option which I don't see you setting here explicitly and which is documented in cases like this |
Well, I need to strip out an access token from the body... my understanding (based on the docs) is the body would remain unchanged with that option, thus exposing my access token. |
This ended up working as a before playback callback... ideally I would like to do this before record, but I couldn't get it to work. def patch_content_length(
interaction: cassette.cassette.Interaction,
current_cassette: cassette.cassette.Cassette,
):
"""Fix the Content-Length header in the response after sanitizing tokens."""
request_uri = interaction.data['request']['uri']
response = interaction.data['response']
# # We only care about requests that generate an access token.
if ('api/v1/access_token' not in request_uri or
response['status']['code'] != 200):
return
body = response['body']['string']
content_length = len(body.encode('utf-8'))
response['headers']['Content-Length'] = [str(content_length)] |
You're other option is that since you're using identity for accept encoding you ensure that whatever you use as a placeholder is the right length so whatever you replace it with is the same length otherwise you'll encounter this for everything. |
https://betamax.readthedocs.io/en/latest/configuring.html#filtering-sensitive-data describes before record hook which happens after sanitization |
Right... the problem being that when the length of the placeholder is different than the original length for what you want to replace... the "Content-Length" header no longer matches... and follow up tests will fail due to the IncompleteRead error, since it's expecting more bytes than it actually receives. |
Right, there might be a way to disable that in urllib3/http.client but I barely have the time right now to answer this issue. I could review a PR to do that but not sure when I'd have a chance to kick off a release |
I have the below code to test a praw reddit bot. The first test always succeeds, but follow up tests fail to find the required calls in the cassette. I discovered that it is related to the
filter_access_token
callback, and if I disable that, then follow up tests will succeed. Am I doing something wrong in the callback function?I get the following error.
I've been trying to follow this (somewhat outdated) blog/tutorial (https://leviroth.github.io/2017-05-16-testing-reddit-bots-with-betamax/) on using betamax and I believe this is the last issue I have to solve.
Edit:
I did find that I can use
instead of
and it will work... but I'm not sure that's the best solution.
The text was updated successfully, but these errors were encountered: