Skip to content

Commit

Permalink
Upgrade cassettes when using Python 3.10+
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio committed Dec 19, 2023
1 parent 46419cf commit 2fab84c
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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."""

Expand All @@ -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):
Expand Down

0 comments on commit 2fab84c

Please sign in to comment.