pytest-vcr icon indicating copy to clipboard operation
pytest-vcr copied to clipboard

Doesn't work with httpx

Open rkhwaja opened this issue 2 years ago • 1 comments

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'

rkhwaja avatar Oct 05 '21 10:10 rkhwaja

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

Sinkler avatar Aug 31 '22 20:08 Sinkler