forked from repoze/repoze.sendmail
/
test_encoding.py
235 lines (188 loc) · 8.7 KB
/
test_encoding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import unittest
class Test_best_charset(unittest.TestCase):
def _callFUT(self, *args, **kw):
from repoze.sendmail.encoding import best_charset
return best_charset(*args, **kw)
def test_w_ascii(self):
from repoze.sendmail._compat import b
value = 'foo'
best, encoded = self._callFUT(value)
self.assertEqual(encoded, b('foo'))
self.assertEqual(best, 'ascii')
def test_w_latin_1(self):
from repoze.sendmail._compat import b
latin_1_encoded = b('LaPe\xf1a')
best, encoded = self._callFUT(latin_1_encoded.decode('latin_1'))
self.assertEqual(best, 'latin_1')
self.assertEqual(encoded, latin_1_encoded)
def test_w_utf_8(self):
from repoze.sendmail._compat import b
utf_8_encoded = b('mo \xe2\x82\xac')
best, encoded = self._callFUT(utf_8_encoded.decode('utf_8'))
self.assertEqual(best, 'utf_8')
self.assertEqual(encoded, utf_8_encoded)
class TestEncoding(unittest.TestCase):
def _callFUT(self, message):
from repoze.sendmail.encoding import encode_message
return encode_message(message)
def _makeMessage(self):
from email.message import Message
return Message()
def test_encoding_ascii_headers(self):
from repoze.sendmail._compat import b
to = ', '.join(['Chris McDonough <chrism@example.com>',
'"Chris Rossi, M.D." <chrisr@example.com>'])
message = self._makeMessage()
message['To'] = to
from_ = 'Ross Patterson <rpatterson@example.com>'
message['From'] = from_
subject = 'I know what you did last PyCon'
message['Subject'] = subject
encoded = self._callFUT(message)
self.assertTrue(
b('To: Chris McDonough <chrism@example.com>, "Chris Rossi,')
in encoded)
self.assertTrue(b('From: ')+from_.encode('ascii') in encoded)
self.assertTrue(b('Subject: ')+subject.encode('ascii') in encoded)
def test_encoding_latin_1_headers(self):
from repoze.sendmail._compat import b
latin_1_encoded = b('LaPe\xf1a')
latin_1 = latin_1_encoded.decode('latin_1')
to = ', '.join([
'"' + latin_1 + ' McDonough, M.D." <chrism@example.com>',
'Chris Rossi <chrisr@example.com>'])
message = self._makeMessage()
message['To'] = to
from_ = latin_1 + ' Patterson <rpatterson@example.com>'
message['From'] = from_
subject = 'I know what you did last ' + latin_1
message['Subject'] = subject
encoded = self._callFUT(message)
self.assertTrue(b('To: =?iso-8859-1?') in encoded)
self.assertTrue(b('From: =?iso-8859-1?') in encoded)
self.assertTrue(b('Subject: =?iso-8859-1?') in encoded)
self.assertTrue(b('<chrism@example.com>') in encoded)
self.assertTrue(b('<chrisr@example.com>') in encoded)
self.assertTrue(b('<rpatterson@example.com>') in encoded)
def test_encoding_utf_8_headers(self):
from repoze.sendmail._compat import b
utf_8_encoded = b('mo \xe2\x82\xac')
utf_8 = utf_8_encoded.decode('utf_8')
to = ', '.join([
'"' + utf_8 + ' McDonough, M.D." <chrism@example.com>',
'Chris Rossi <chrisr@example.com>'])
message = self._makeMessage()
message['To'] = to
from_ = utf_8 + ' Patterson <rpatterson@example.com>'
message['From'] = from_
subject = 'I know what you did last ' + utf_8
message['Subject'] = subject
encoded = self._callFUT(message)
self.assertTrue(b('To: =?utf') in encoded)
self.assertTrue(b('From: =?utf') in encoded)
self.assertTrue(b('Subject: =?utf') in encoded)
self.assertTrue(b('<chrism@example.com>') in encoded)
self.assertTrue(b('<chrisr@example.com>') in encoded)
self.assertTrue(b('<rpatterson@example.com>') in encoded)
def test_encoding_ascii_header_parameters(self):
from repoze.sendmail._compat import b
message = self._makeMessage()
message['Content-Disposition'] = 'attachment; filename=foo.ppt'
encoded = self._callFUT(message)
self.assertTrue(
b('Content-Disposition: attachment; filename="foo.ppt"')
in encoded)
def test_encoding_latin_1_header_parameters(self):
from repoze.sendmail._compat import b
from repoze.sendmail._compat import quote
latin_1_encoded = b('LaPe\xf1a')
latin_1 = latin_1_encoded.decode('latin_1')
message = self._makeMessage()
message['Content-Disposition'] = (
'attachment; filename=' + latin_1 + '.ppt')
encoded = self._callFUT(message)
self.assertTrue(
b("Content-Disposition: attachment; filename*=") in encoded)
self.assertTrue(b("latin_1''") + quote(latin_1_encoded).encode('ascii')
in encoded)
def test_encoding_utf_8_header_parameters(self):
from repoze.sendmail._compat import b
from repoze.sendmail._compat import quote
utf_8_encoded = b('mo \xe2\x82\xac')
utf_8 = utf_8_encoded.decode('utf_8')
message = self._makeMessage()
message['Content-Disposition'] = (
'attachment; filename=' + utf_8 +'.ppt')
encoded = self._callFUT(message)
self.assertTrue(
b("Content-Disposition: attachment; filename*=") in encoded)
self.assertTrue(b("utf_8''") + quote(utf_8_encoded).encode('ascii')
in encoded)
def test_encoding_ascii_body(self):
body = 'I know what you did last PyCon'
message = self._makeMessage()
message.set_payload(body)
encoded = self._callFUT(message)
self.assertTrue(body.encode('ascii') in encoded)
def test_encoding_latin_1_body(self):
import quopri
from repoze.sendmail._compat import b
latin_1_encoded = b('LaPe\xf1a')
latin_1 = latin_1_encoded.decode('latin_1')
body = 'I know what you did last ' + latin_1
message = self._makeMessage()
message.set_payload(body)
encoded = self._callFUT(message)
self.assertTrue(quopri.encodestring(body.encode('latin_1')) in encoded)
def test_encoding_utf_8_body(self):
from repoze.sendmail._compat import b
from repoze.sendmail._compat import encodestring
utf_8_encoded = b('mo \xe2\x82\xac')
utf_8 = utf_8_encoded.decode('utf_8')
body = 'I know what you did last '+ utf_8
message = self._makeMessage()
message.set_payload(body)
encoded = self._callFUT(message)
self.assertTrue(encodestring(body.encode('utf_8')) in encoded)
def test_binary_body(self):
from email.mime import application
from email.mime import multipart
from repoze.sendmail._compat import encodestring
from repoze.sendmail._compat import b
body = b('I know what you did last PyCon')
message = multipart.MIMEMultipart()
message.attach(application.MIMEApplication(body))
encoded = self._callFUT(message)
self.assertTrue(encodestring(body) in encoded)
def test_encoding_multipart(self):
from email.mime import multipart
from email.mime import nonmultipart
from repoze.sendmail._compat import encodestring
from repoze.sendmail._compat import b
message = multipart.MIMEMultipart('alternative')
utf_8_encoded = b('mo \xe2\x82\xac')
utf_8 = utf_8_encoded.decode('utf_8')
plain_string = utf_8
plain_part = nonmultipart.MIMENonMultipart('plain', 'plain')
plain_part.set_payload(plain_string)
message.attach(plain_part)
html_string = '<p>'+utf_8+'</p>'
html_part = nonmultipart.MIMENonMultipart('text', 'html')
html_part.set_payload(html_string)
message.attach(html_part)
encoded = self._callFUT(message)
self.assertTrue(encodestring(plain_string.encode('utf_8')) in encoded)
self.assertTrue(encodestring(html_string.encode('utf_8')) in encoded)