summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorFolker Bernitt <fbernitt@thoughtworks.com>2015-09-09 18:15:39 +0200
committerFolker Bernitt <fbernitt@thoughtworks.com>2015-09-09 18:15:39 +0200
commit83ca259ee6e14281dd7087a2187d7a592f6b9e77 (patch)
tree45a41a231b485787216242f17ecbb1b5d1a2c31f /service
parent7f5c0ec1424ac629c4ab60d72ff9b1a3717fb4c8 (diff)
Fix mixed encodings in mail subjects
- Issue #450
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/adapter/mailstore/leap_mailstore.py10
-rw-r--r--service/test/unit/adapter/mailstore/test_leap_mail.py8
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)