diff options
-rw-r--r-- | service/pixelated/adapter/mailstore/leap_mailstore.py | 10 | ||||
-rw-r--r-- | service/test/unit/adapter/mailstore/test_leap_mail.py | 8 |
2 files changed, 13 insertions, 5 deletions
diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py index 6dad12b0..eb12bd1d 100644 --- a/service/pixelated/adapter/mailstore/leap_mailstore.py +++ b/service/pixelated/adapter/mailstore/leap_mailstore.py @@ -114,11 +114,11 @@ class LeapMail(Mail): if isinstance(header_value, list): return self.remove_duplicates([self._decoded_header_utf_8(v) for v in header_value]) elif header_value is not None: - content, encoding = decode_header(header_value)[0] - if encoding: - return unicode(content, encoding=encoding) - else: - return unicode(content, encoding='ascii') + def encode_chunk(content, encoding): + return unicode(content, encoding=encoding or 'ascii') + + encoded_chunks = [encode_chunk(content, encoding) for content, encoding in decode_header(header_value)] + return ' '.join(encoded_chunks) # decode_header strips whitespaces on all chunks, joining over ' ' is only a workaround, not a proper fix def as_dict(self): return { diff --git a/service/test/unit/adapter/mailstore/test_leap_mail.py b/service/test/unit/adapter/mailstore/test_leap_mail.py index 571e2f60..925bfdce 100644 --- a/service/test/unit/adapter/mailstore/test_leap_mail.py +++ b/service/test/unit/adapter/mailstore/test_leap_mail.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # # Copyright (c) 2015 ThoughtWorks, Inc. # @@ -114,6 +115,13 @@ class TestLeapMail(TestCase): self.assertEqual([expected_address], mail.as_dict()['replying']['all']['cc-field']) self.assertEqual(expected_address, mail.as_dict()['replying']['single']) + def test_as_dict_with_mixed_encodings(self): + subject = 'Another test with =?iso-8859-1?B?3G1s5Px0?= =?iso-8859-1?Q?s?=' + mail = LeapMail('', 'INBOX', + {'Subject': subject}) + + self.assertEqual(u'Another test with Ümläüts', mail.as_dict()['header']['subject']) + def test_raw_constructed_by_headers_and_body(self): body = 'some body content' mail = LeapMail('doc id', 'INBOX', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}, ('foo', 'bar'), body=body) |