Skip to content

Commit 2c8ed6b

Browse files
committed
Use prettyjson and disable gzip with betamax
1 parent 9deb303 commit 2c8ed6b

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

boxsdk/pytest_plugin/betamax.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66

77
from betamax import Betamax
8+
from betamax_serializers import pretty_json
89
import pytest
910
import requests
1011

@@ -31,6 +32,7 @@ def betamax_cassette_library_dir(request):
3132
def configure_betamax(betamax_cassette_library_dir):
3233
if not os.path.exists(betamax_cassette_library_dir):
3334
os.makedirs(betamax_cassette_library_dir)
35+
Betamax.register_serializer(pretty_json.PrettyJSONSerializer)
3436
with Betamax.configure() as config:
3537
config.cassette_library_dir = betamax_cassette_library_dir
3638
config.default_cassette_options['re_record_interval'] = 100
@@ -64,6 +66,7 @@ def betamax_recorder(configure_betamax, real_requests_session): # pylint:disab
6466
@pytest.fixture
6567
def betamax_cassette_recorder(betamax_recorder, betamax_cassette_name, betamax_use_cassette_kwargs):
6668
"""Including this fixture causes the test to use a betamax cassette for network requests."""
69+
betamax_use_cassette_kwargs.setdefault('serialize_with', 'prettyjson')
6770
with betamax_recorder.use_cassette(betamax_cassette_name, **betamax_use_cassette_kwargs) as cassette_recorder:
6871
yield cassette_recorder
6972

@@ -87,7 +90,11 @@ def betamax_boxsdk_session(betamax_boxsdk_network, betamax_boxsdk_auth):
8790
Requires an implementation of the abstract `betamax_boxsdk_auth` fixture,
8891
of type `boxsdk.OAuth2`.
8992
"""
90-
return BoxSession(oauth=betamax_boxsdk_auth, network_layer=betamax_boxsdk_network)
93+
return BoxSession(
94+
oauth=betamax_boxsdk_auth,
95+
network_layer=betamax_boxsdk_network,
96+
default_headers={'Accept-Encoding': None}, # Turn off gzip so that raw JSON responses are recorded.
97+
)
9198

9299

93100
@pytest.fixture

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def main():
6161
jwt_requires = ['pyjwt>=1.3.0', 'cryptography>=0.9.2']
6262
extra_requires = defaultdict(list)
6363
extra_requires.update({'jwt': jwt_requires, 'redis': redis_requires, 'all': jwt_requires + redis_requires})
64-
extra_requires['pytest'] = ['betamax', 'pytest>=3.0.0']
64+
extra_requires['pytest'] = ['betamax', 'betamax-serializers', 'pytest>=3.0.0']
6565
conditional_dependencies = {
6666
# Newer versions of pip and wheel, which support PEP 426, allow
6767
# environment markers for conditional dependencies to use operators
@@ -97,7 +97,7 @@ def main():
9797
packages=find_packages(exclude=['demo', 'docs', 'test', 'test*', '*test', '*test*']),
9898
install_requires=install_requires,
9999
extras_require=extra_requires,
100-
tests_require=['betamax', 'pytest', 'pytest-xdist', 'mock', 'sqlalchemy', 'bottle', 'jsonpatch'],
100+
tests_require=['betamax', 'betamax-serializers', 'pytest', 'pytest-xdist', 'mock', 'sqlalchemy', 'bottle', 'jsonpatch'],
101101
entry_points={
102102
'pytest11': [
103103
# pytest fixtures that can help with testing boxsdk-powered applications.
Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,64 @@
1-
{"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"}
1+
{
2+
"http_interactions": [
3+
{
4+
"recorded_at": "2017-08-25T21:21:46",
5+
"request": {
6+
"body": {
7+
"encoding": "utf-8",
8+
"string": ""
9+
},
10+
"headers": {
11+
"Accept": [
12+
"*/*"
13+
],
14+
"Accept-Encoding": [
15+
"gzip, deflate"
16+
],
17+
"Connection": [
18+
"keep-alive"
19+
],
20+
"User-Agent": [
21+
"python-requests/2.18.4"
22+
]
23+
},
24+
"method": "GET",
25+
"uri": "https://api.box.com/2.0/users/me"
26+
},
27+
"response": {
28+
"body": {
29+
"encoding": null,
30+
"string": ""
31+
},
32+
"headers": {
33+
"Age": [
34+
"0"
35+
],
36+
"Connection": [
37+
"keep-alive"
38+
],
39+
"Content-Length": [
40+
"0"
41+
],
42+
"Date": [
43+
"Fri, 25 Aug 2017 21:21:45 GMT"
44+
],
45+
"Server": [
46+
"ATS"
47+
],
48+
"Strict-Transport-Security": [
49+
"max-age=31536000; includeSubDomains"
50+
],
51+
"WWW-Authenticate": [
52+
"Bearer realm=\"Service\", error=\"invalid_request\", error_description=\"The access token was not found.\""
53+
]
54+
},
55+
"status": {
56+
"code": 401,
57+
"message": "Unauthorized"
58+
},
59+
"url": "https://api.box.com/2.0/users/me"
60+
}
61+
}
62+
],
63+
"recorded_with": "betamax/0.8.0"
64+
}

0 commit comments

Comments
 (0)