Skip to content

Commit

Permalink
Use prettyjson and disable gzip with betamax
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoldow committed Aug 25, 2017
1 parent 9deb303 commit 2c8ed6b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
9 changes: 8 additions & 1 deletion boxsdk/pytest_plugin/betamax.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os

from betamax import Betamax
from betamax_serializers import pretty_json
import pytest
import requests

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

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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"}
{
"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"
}

0 comments on commit 2c8ed6b

Please sign in to comment.