kevin1024 / vcrpy

Automatically mock your HTTP interactions to simplify and speed up testing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AttributeError: 'VCRHTTPResponse' object has no attribute 'json'

gschnellOnera opened this issue · comments

I use pytest-recording, to mimic my http requests for testing. When switching to urllib3 to run the queries I encounter the following exception. AttributeError: 'VCRHTTPResponse' object has no attribute 'json'

from urllib3 import PoolManager, make_headers

HTTP_PoolManager = PoolManager()
HTTP_HEADER_AUTH = make_headers(basic_auth='user:password')
req = HTTP_PoolManager.request('GET', url, headers=HTTP_HEADER_AUTH)
if (req.status != 200):
    if req.headers['content-type'].split(';')[0] == "application/json":
        return req.json()
#error managment
retrun None

Versions:
python 3.8.10
pytest-recording: 0.13.0
vcrpy: 5.1.0
urllib3: 2.0.4

Workaround:

from urllib3 import PoolManager, make_headers
import json

HTTP_PoolManager = PoolManager()
HTTP_HEADER_AUTH = make_headers(basic_auth='user:password')
req = HTTP_PoolManager.request('GET', url, headers=HTTP_HEADER_AUTH)
if (req.status != 200):
    if req.headers['content-type'].split(';')[0] == "application/json":
        return json.loads(req.data)
#error managment
retrun None

I might be wrong, but AFAIK, your issue will be solved after vcrpy releases a new version that supports urllib3 v2 -> #707

@gschnellOnera the snippet you shared requires additional work to run in isolation. Could you turn it into a snippet reproducer that can be in isolation easily? Thanks in advance!