Skip to content

Commit

Permalink
Fix decode header
Browse files Browse the repository at this point in the history
  • Loading branch information
ecarreras committed Jun 6, 2019
1 parent 3d31d41 commit 60e6d1b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
18 changes: 10 additions & 8 deletions qreu/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,13 @@ def header(self, header, default=None):
if header_value:
for part in decode_header(header_value):
if part[1]:
result.append(part[0].decode(part[1]))
encoded = part[0].decode(part[1])
elif isinstance(part[0], bytes):
result.append(part[0].decode('utf-8'))
encoded = part[0].decode('utf-8')
else:
result.append(part[0])
header_value = ''.join(result)
encoded = part[0]
result.append(encoded.strip())
header_value = ' '.join(result)

return header_value

Expand Down Expand Up @@ -301,12 +302,13 @@ def add_header(self, header, value):
result = []
for part in decode_header(header_value):
if part[1]:
result.append(part[0].decode(part[1]))
encoded = part[0].decode(part[1])
elif isinstance(part[0], bytes):
result.append(part[0].decode('utf-8'))
encoded = part[0].decode('utf-8')
else:
result.append(part[0])
header_value = ''.join(result)
encoded = part[0]
result.append(encoded.strip())
header_value = ' '.join(result)
self.bccs = header_value
else:
self.email[header] = header_value
Expand Down
2 changes: 1 addition & 1 deletion spec/address_spec.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding=utf-8
from qreu.address import parse, parse_list, AddressList, Address

from expects import *


Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
From: =?utf-8?Q?M=C3=B3nica=20de=20Test=20Example?= <monica@example.com>
8 changes: 6 additions & 2 deletions spec/qreu_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
with description('Parsing an Email'):
with before.all:
self.raw_messages = []
for fixture in range(0, 5):
for fixture in range(0, 6):
with open('spec/fixtures/{0}.txt'.format(fixture)) as f:
self.raw_messages.append(f.read())

Expand Down Expand Up @@ -109,6 +109,10 @@
expect(attch).to(contain_exactly('image.png'))
expect(c.body_parts['files']).to(contain_exactly('image.png'))

with it('should parse correctly address from weird header'):
c = Email.parse(self.raw_messages[5])
expect(c.from_).to(have_property('address', 'monica@example.com'))

with description("Creating an Email"):
with context("empty"):
with it("must have all attributes to None and work"):
Expand Down Expand Up @@ -390,7 +394,7 @@ def call_wrongly():

with it('must add addresses correctly as "name" <address>'):
address = 'spécial <special@example.com>'
parsed = 'spécial<special@example.com>'
parsed = 'spécial <special@example.com>'
e = Email(to=address)
expect(e.to).to(equal([parsed]))
e = Email(cc=address)
Expand Down
2 changes: 1 addition & 1 deletion spec/sendcontext_spec.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
# coding=utf-8
from __future__ import absolute_import, unicode_literals
from mamba import *
from expects import *
Expand Down

0 comments on commit 60e6d1b

Please sign in to comment.