Skip to content

Commit

Permalink
Fix exception raised by sending binary content (fixes #1).
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-osman committed Jun 14, 2016
1 parent 756719e commit 7e05922
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
15 changes: 8 additions & 7 deletions pyhectane/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from os.path import basename

from requests import Session
from six import string_types
from six import binary_type, string_types


class Connection:
Expand Down Expand Up @@ -35,18 +35,19 @@ def _process_attachments(self, attachments):
if the filename cannot be determined, it will be set to "untitled".
"""
for a in attachments:
if isinstance(a, dict):
yield a
else:
if not isinstance(a, dict):
if isinstance(a, string_types):
a = open(a, 'rb')
filename = basename(getattr(a, 'name', 'untitled'))
yield {
a = {
"filename": filename,
"content_type": guess_type(filename)[0] or 'application/octet-stream',
"content": b64encode(a.read()).decode(),
"encoded": True,
"content": a.read(),
}
if isinstance(a['content'], binary_type):
a['content'] = b64encode(a['content']).decode()
a['encoded'] = True
yield a

def raw(self, from_, to, body):
"""
Expand Down
7 changes: 4 additions & 3 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,13 @@ def test_send_empty_content(self):
with self._r:
self._c.send(self._FROM, [self._TO], self._SUBJECT)

def test_send_attachment_dict(self):
def test_send_attachment_binary(self):
with self._r:
self._c.send(self._FROM, [self._TO], self._SUBJECT, self._SDATA,
attachments=[{}])
attachments=[{'content': self._BDATA}])
data = loads(self._r.data.decode())
eq_(len(data['attachments']), 1)
eq_(data['attachments'][0]['encoded'], True)
eq_(b64decode(data['attachments'][0]['content']), self._BDATA)

def test_send_attachment_file(self):
with NamedTemporaryFile() as f:
Expand Down

0 comments on commit 7e05922

Please sign in to comment.