diff --git a/boxsdk/pytest_plugin/betamax.py b/boxsdk/pytest_plugin/betamax.py index 973459674..60aab9cb4 100644 --- a/boxsdk/pytest_plugin/betamax.py +++ b/boxsdk/pytest_plugin/betamax.py @@ -5,6 +5,7 @@ import os from betamax import Betamax +from betamax_serializers import pretty_json import pytest import requests @@ -31,6 +32,7 @@ def betamax_cassette_library_dir(request): def configure_betamax(betamax_cassette_library_dir): if not os.path.exists(betamax_cassette_library_dir): os.makedirs(betamax_cassette_library_dir) + Betamax.register_serializer(pretty_json.PrettyJSONSerializer) with Betamax.configure() as config: config.cassette_library_dir = betamax_cassette_library_dir config.default_cassette_options['re_record_interval'] = 100 @@ -64,6 +66,7 @@ def betamax_recorder(configure_betamax, real_requests_session): # pylint:disab @pytest.fixture def betamax_cassette_recorder(betamax_recorder, betamax_cassette_name, betamax_use_cassette_kwargs): """Including this fixture causes the test to use a betamax cassette for network requests.""" + betamax_use_cassette_kwargs.setdefault('serialize_with', 'prettyjson') with betamax_recorder.use_cassette(betamax_cassette_name, **betamax_use_cassette_kwargs) as cassette_recorder: yield cassette_recorder @@ -87,7 +90,11 @@ def betamax_boxsdk_session(betamax_boxsdk_network, betamax_boxsdk_auth): Requires an implementation of the abstract `betamax_boxsdk_auth` fixture, of type `boxsdk.OAuth2`. """ - return BoxSession(oauth=betamax_boxsdk_auth, network_layer=betamax_boxsdk_network) + return BoxSession( + oauth=betamax_boxsdk_auth, + network_layer=betamax_boxsdk_network, + default_headers={'Accept-Encoding': None}, # Turn off gzip so that raw JSON responses are recorded. + ) @pytest.fixture diff --git a/setup.py b/setup.py index ab4671f60..9c6d63e9a 100644 --- a/setup.py +++ b/setup.py @@ -61,7 +61,7 @@ def main(): jwt_requires = ['pyjwt>=1.3.0', 'cryptography>=0.9.2'] extra_requires = defaultdict(list) extra_requires.update({'jwt': jwt_requires, 'redis': redis_requires, 'all': jwt_requires + redis_requires}) - extra_requires['pytest'] = ['betamax', 'pytest>=3.0.0'] + extra_requires['pytest'] = ['betamax', 'betamax-serializers', 'pytest>=3.0.0'] conditional_dependencies = { # Newer versions of pip and wheel, which support PEP 426, allow # environment markers for conditional dependencies to use operators @@ -97,7 +97,7 @@ def main(): packages=find_packages(exclude=['demo', 'docs', 'test', 'test*', '*test', '*test*']), install_requires=install_requires, extras_require=extra_requires, - tests_require=['betamax', 'pytest', 'pytest-xdist', 'mock', 'sqlalchemy', 'bottle', 'jsonpatch'], + tests_require=['betamax', 'betamax-serializers', 'pytest', 'pytest-xdist', 'mock', 'sqlalchemy', 'bottle', 'jsonpatch'], entry_points={ 'pytest11': [ # pytest fixtures that can help with testing boxsdk-powered applications. diff --git a/test/unit/network/cassettes/test_network::test_requests_session_network_works_with_betamax.json b/test/unit/network/cassettes/test_network::test_requests_session_network_works_with_betamax.json index 29af12442..a725d7d44 100644 --- a/test/unit/network/cassettes/test_network::test_requests_session_network_works_with_betamax.json +++ b/test/unit/network/cassettes/test_network::test_requests_session_network_works_with_betamax.json @@ -1 +1,64 @@ -{"http_interactions": [{"request": {"body": {"encoding": "utf-8", "string": ""}, "headers": {"User-Agent": ["python-requests/2.18.4"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "Connection": ["keep-alive"]}, "method": "GET", "uri": "https://api.box.com/2.0/users/me"}, "response": {"body": {"encoding": null, "string": ""}, "headers": {"Server": ["ATS"], "Date": ["Fri, 25 Aug 2017 02:49:37 GMT"], "Content-Length": ["0"], "Strict-Transport-Security": ["max-age=31536000; includeSubDomains"], "WWW-Authenticate": ["Bearer realm=\"Service\", error=\"invalid_request\", error_description=\"The access token was not found.\""], "Age": ["0"], "Connection": ["keep-alive"]}, "status": {"code": 401, "message": "Unauthorized"}, "url": "https://api.box.com/2.0/users/me"}, "recorded_at": "2017-08-25T02:49:37"}], "recorded_with": "betamax/0.8.0"} \ No newline at end of file +{ + "http_interactions": [ + { + "recorded_at": "2017-08-25T21:21:46", + "request": { + "body": { + "encoding": "utf-8", + "string": "" + }, + "headers": { + "Accept": [ + "*/*" + ], + "Accept-Encoding": [ + "gzip, deflate" + ], + "Connection": [ + "keep-alive" + ], + "User-Agent": [ + "python-requests/2.18.4" + ] + }, + "method": "GET", + "uri": "https://api.box.com/2.0/users/me" + }, + "response": { + "body": { + "encoding": null, + "string": "" + }, + "headers": { + "Age": [ + "0" + ], + "Connection": [ + "keep-alive" + ], + "Content-Length": [ + "0" + ], + "Date": [ + "Fri, 25 Aug 2017 21:21:45 GMT" + ], + "Server": [ + "ATS" + ], + "Strict-Transport-Security": [ + "max-age=31536000; includeSubDomains" + ], + "WWW-Authenticate": [ + "Bearer realm=\"Service\", error=\"invalid_request\", error_description=\"The access token was not found.\"" + ] + }, + "status": { + "code": 401, + "message": "Unauthorized" + }, + "url": "https://api.box.com/2.0/users/me" + } + } + ], + "recorded_with": "betamax/0.8.0" +} \ No newline at end of file