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

AttributeError: 'NoneType' object has no attribute 'decode' when 'body' in match_on, fixed with 'raw_body' #255

Closed
jhh3000 opened this issue May 16, 2016 · 2 comments · Fixed by #278
Assignees

Comments

@jhh3000
Copy link

jhh3000 commented May 16, 2016

When using vcr with the following configuration with 'body', there are errors with loading the JSON response -- it could be due to a blank response or blank payload. When I use 'raw_body' it works perfectly. Here's the stack trace (relevant sections):

  File "/home/vagrant/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 473, in get
    return self.request('GET', url, **kwargs)
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 461, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 370, in send
    timeout=timeout
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 518, in urlopen
    body=body, headers=headers)
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 353, in _make_request
    httplib_response = conn.getresponse(buffering=True)
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/vcr/stubs/__init__.py", line 219, in getresponse
    if self.cassette.can_play_response_for(self._vcr_request):
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/vcr/cassette.py", line 232, in can_play_response_for
    return request and request in self and \
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/vcr/cassette.py", line 303, in __contains__
    for index, response in self._responses(request):
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/vcr/cassette.py", line 227, in _responses
    if requests_match(request, stored_request, self._match_on):
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/vcr/matchers.py", line 97, in requests_match
    matches = [(m(r1, r2), m) for m in matchers]
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/vcr/matchers.py", line 80, in body
    return transformer(read_body(r1)) == transformer(read_body(r2))
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/vcr/matchers.py", line 52, in _transform_json
    return json.loads(body.decode('utf-8'))
AttributeError: 'NoneType' object has no attribute 'decode'
@maroux
Copy link

maroux commented Jul 16, 2016

@jhh3000 I ran into the same problem - it looks like your cassette may be problematic. For me, I had a request like this:

- request:
    body: null
    headers:
      Content-Length: ['41']
      Content-Type: [application/json]
    method: GET

The Content-Type being json is the problem here. Removing that header seems to fix the cassette. This was caused by me editing the cassette by hand. Hope this helps.

@simon-weber
Copy link
Contributor

simon-weber commented Sep 30, 2016

This is a bug, right? Even though it may be atypical, null is valid json. Either the cassette should be persisting 'null' instead of null, or matchers._transform_json should be able to handle None.

Probably we want to do both? That way, cassettes will be backwards compatible.

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

Successfully merging a pull request may close this issue.

4 participants