betamaxpy / betamax

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

Body matcher issue with requests 2.11.0. #114

Closed bboe closed 8 years ago

bboe commented 8 years ago

Session showing upgrade to requests 2.11.0 results in failure:

(praw) bboe@spock:betamax_issue$ ./betamax_issue.py 
 Betamax:  0.7.2
Requests: 2.10.0
{'args': {},
 'data': '{"a": 2}',
 'files': {},
 'form': {},
 'headers': {'Accept': '*/*',
             'Accept-Encoding': 'gzip, deflate',
             'Content-Length': '8',
             'Content-Type': 'application/json',
             'Host': 'httpbin.org',
             'User-Agent': 'python-requests/2.10.0'},
 'json': {'a': 2},
 'origin': '98.182.19.155',
 'url': 'https://httpbin.org/post'}
(praw) bboe@spock:betamax_issue$ pip install -U requests
Collecting requests
  Using cached requests-2.11.0-py2.py3-none-any.whl
Installing collected packages: requests
  Found existing installation: requests 2.10.0
    Uninstalling requests-2.10.0:
      Successfully uninstalled requests-2.10.0
Successfully installed requests-2.11.0
(praw) bboe@spock:betamax_issue$ ./betamax_issue.py 
 Betamax:  0.7.2
Requests: 2.11.0
Traceback (most recent call last):
  File "./betamax_issue.py", line 25, in <module>
    sys.exit(main())
  File "./betamax_issue.py", line 18, in main
    response = session.post('https://httpbin.org/post', json={'a': 2})
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/requests/sessions.py", line 514, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/requests/sessions.py", line 471, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/requests/sessions.py", line 581, in send
    r = adapter.send(request, **kwargs)
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/betamax/adapter.py", line 123, in send
    interaction = current_cassette.find_match(request)
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/betamax/cassette/cassette.py", line 133, in find_match
    if not interaction.match(curried_matchers):
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/betamax/cassette/interaction.py", line 58, in match
    return all(m(request) for m in matchers)
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/betamax/cassette/interaction.py", line 58, in <genexpr>
    return all(m(request) for m in matchers)
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/betamax/matchers/body.py", line 17, in match
    request_body = request.body.encode('utf-8')
AttributeError: 'bytes' object has no attribute 'encode'

Test code source:

#!/usr/bin/env python
from __future__ import print_function
import json
import sys

import betamax
import requests

def main():
    print(' Betamax: {:>6}'.format(betamax.__version__))
    print('Requests: {:>6}'.format(requests.__version__))

    with requests.Session() as session:
        with betamax.Betamax(session, cassette_library_dir='.') as recorder:
            recorder.use_cassette(
                'test', match_requests_on=['body'])
            response = session.post('https://httpbin.org/post', json={'a': 2})

    import pprint
    pprint.pprint(response.json())

if __name__ == '__main__':
    sys.exit(main())

The fix appears to be coercion of the request.body now that it is of type bytes. I'll make a quick PR for it.

sigmavirus24 commented 8 years ago

Thanks for the report @bboe!