From 2fab84c12fc525bc184ae111cdb4b49847f3a260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Chaves?= Date: Tue, 19 Dec 2023 14:32:11 +0100 Subject: [PATCH] Upgrade cassettes when using Python 3.10+ --- tests/conftest.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 9eb79d83..0eb41aff 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,9 +1,9 @@ -# -*- coding: utf-8 -*- import base64 import os import pickle import pytest import re +import sys import zlib from scrapinghub.hubstorage.serialization import MSGPACK_AVAILABLE @@ -31,6 +31,17 @@ TEST_DASH_ENDPOINT = os.getenv('DASH_ENDPOINT', DEFAULT_DASH_ENDPOINT) +# https://github.com/kevin1024/vcrpy/issues/719#issuecomment-1811544263 +def upgrade_cassette(cassette): + for interaction in cassette['interactions']: + response = interaction.get('response', {}) + headers = response.get('headers', {}) + contentType = headers.get('content-encoding') or headers.get('Content-Encoding') + compressed_string = response['body']['string'] + if contentType and contentType[0] == 'gzip': + response['body']['string'] = zlib.decompress(compressed_string, zlib.MAX_WBITS | 16).decode('utf-8') + + class VCRGzipSerializer(object): """Custom ZIP serializer for VCR.py.""" @@ -45,7 +56,10 @@ def serialize(self, cassette_dict): def deserialize(self, cassette_string): # receives a string, must return a dict decoded = base64.b64decode(cassette_string.encode('utf8')) - return pickle.loads(zlib.decompress(decoded)) + cassette = pickle.loads(zlib.decompress(decoded)) + if sys.version_info >= (3, 10): + upgrade_cassette(cassette) + return cassette def normalize_endpoint(uri, endpoint, default_endpoint):