ktosiek / pytest-vcr

Py.test integration with VCR.py
MIT License
154 stars 25 forks source link

Doesn't work with httpx #46

Open rkhwaja opened 2 years ago

rkhwaja commented 2 years ago

I'm not sure whether this package is expected to work with httpx but I'm getting the following error when recording a new cassette with decode_compressed_response=True:

>           new_body = decompress_body(response["body"]["string"], encoding)
E           KeyError: 'body'
Sinkler commented 2 years ago

Monkey patch:

from vcr.stubs import httpx_stubs

def _transform_headers(httpx_response):
    out = {}
    for key, var in httpx_response.headers.raw:
        decoded_key = key.decode('utf-8')
        decoded_var = var.decode('utf-8')
        if decoded_key.lower() == 'content-encoding' and decoded_var in ('gzip', 'deflate'):
            continue
        out.setdefault(decoded_key, [])
        out[decoded_key].append(decoded_var)
    return out

httpx_stubs._transform_headers = _transform_headers

See: https://github.com/kevin1024/vcrpy/pull/591 and https://github.com/kevin1024/vcrpy/pull/649