Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix binary bodies #79

Merged
merged 3 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
language: python
matrix:
include:
- python: 2.6
dist: trusty
sudo: false
- python: 2.7
dist: trusty
sudo: false
- python: 3.3
dist: trusty
sudo: false
- python: 3.4
dist: trusty
sudo: false
- python: 3.5
dist: trusty
sudo: false
- python: 3.6
dist: trusty
sudo: false
- python: 3.7
dist: xenial
sudo: true
- python: 3.8
dist: xenial
sudo: true
- python: 3.9
dist: xenial
sudo: true

cache: pip
before_install:
Expand Down
13 changes: 10 additions & 3 deletions placebo/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import json
import pickle
import datetime
import base64
from io import BytesIO

from botocore.response import StreamingBody
from six import StringIO


class UTC(datetime.tzinfo):
Expand Down Expand Up @@ -78,7 +80,9 @@ def deserialize(obj):
if class_name == 'datetime':
return datetime.datetime(tzinfo=utc, **target)
if class_name == 'StreamingBody':
return StringIO(target['body'])
b64_body = obj['body']
decoded_body = base64.b64decode(b64_body)
return BytesIO(decoded_body)
# Return unrecognized structures as-is
return obj

Expand All @@ -103,9 +107,12 @@ def serialize(obj):
return result
if isinstance(obj, StreamingBody):
result['body'] = obj.read()
obj._raw_stream = StringIO(result['body'])
obj._raw_stream = BytesIO(result['body'])
obj._amount_read = 0
return result
if isinstance(obj, bytes):
encoded = base64.b64encode(obj)
return encoded.decode('utf-8')
# Raise a TypeError if the object isn't recognized
raise TypeError("Type not serializable")

Expand Down
10 changes: 3 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@
'Intended Audience :: System Administrators',
'Natural Language :: English',
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7'
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9'
],
)
22 changes: 21 additions & 1 deletion tests/unit/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import unittest
import json

from six import StringIO, BytesIO
from io import StringIO, BytesIO

from botocore.response import StreamingBody

from placebo.serializer import serialize, deserialize, utc, Format
from placebo.serializer import get_serializer, get_deserializer
Expand All @@ -33,6 +35,15 @@

date_json = """{"LoginProfile": {"CreateDate": {"__class__": "datetime", "day": 4, "hour": 9, "microsecond": 0, "minute": 1, "month": 1, "second": 2, "year": 2015}, "UserName": "baz"}}"""

content = b'this is a test'
stream = BytesIO(content)

streaming_body_sample = {
'Body': StreamingBody(stream, len(content))
}

streaming_body_json = """{"Body": {"__class__": "StreamingBody", "__module__": "botocore.response", "body": "dGhpcyBpcyBhIHRlc3Q="}}"""


class TestSerializers(unittest.TestCase):

Expand All @@ -44,6 +55,15 @@ def test_datetime_from_json(self):
response = json.loads(date_json, object_hook=deserialize)
self.assertEqual(response, date_sample)

def test_streaming_body_to_json(self):
result = json.dumps(
streaming_body_sample, default=serialize, sort_keys=True)
self.assertEqual(result, streaming_body_json)

def test_streaming_body_from_json(self):
response = json.loads(streaming_body_json, object_hook=deserialize)
self.assertEqual(response['Body'].read(), content)

def test_roundtrip_json(self):
ser = get_serializer(Format.JSON)
deser = get_deserializer(Format.JSON)
Expand Down