1 | n/a | import unittest |
---|
2 | n/a | from test.test_email import TestEmailBase, parameterize |
---|
3 | n/a | import textwrap |
---|
4 | n/a | from email import policy |
---|
5 | n/a | from email.message import EmailMessage |
---|
6 | n/a | from email.contentmanager import ContentManager, raw_data_manager |
---|
7 | n/a | |
---|
8 | n/a | |
---|
9 | n/a | @parameterize |
---|
10 | n/a | class TestContentManager(TestEmailBase): |
---|
11 | n/a | |
---|
12 | n/a | policy = policy.default |
---|
13 | n/a | message = EmailMessage |
---|
14 | n/a | |
---|
15 | n/a | get_key_params = { |
---|
16 | n/a | 'full_type': (1, 'text/plain',), |
---|
17 | n/a | 'maintype_only': (2, 'text',), |
---|
18 | n/a | 'null_key': (3, '',), |
---|
19 | n/a | } |
---|
20 | n/a | |
---|
21 | n/a | def get_key_as_get_content_key(self, order, key): |
---|
22 | n/a | def foo_getter(msg, foo=None): |
---|
23 | n/a | bar = msg['X-Bar-Header'] |
---|
24 | n/a | return foo, bar |
---|
25 | n/a | cm = ContentManager() |
---|
26 | n/a | cm.add_get_handler(key, foo_getter) |
---|
27 | n/a | m = self._make_message() |
---|
28 | n/a | m['Content-Type'] = 'text/plain' |
---|
29 | n/a | m['X-Bar-Header'] = 'foo' |
---|
30 | n/a | self.assertEqual(cm.get_content(m, foo='bar'), ('bar', 'foo')) |
---|
31 | n/a | |
---|
32 | n/a | def get_key_as_get_content_key_order(self, order, key): |
---|
33 | n/a | def bar_getter(msg): |
---|
34 | n/a | return msg['X-Bar-Header'] |
---|
35 | n/a | def foo_getter(msg): |
---|
36 | n/a | return msg['X-Foo-Header'] |
---|
37 | n/a | cm = ContentManager() |
---|
38 | n/a | cm.add_get_handler(key, foo_getter) |
---|
39 | n/a | for precedence, key in self.get_key_params.values(): |
---|
40 | n/a | if precedence > order: |
---|
41 | n/a | cm.add_get_handler(key, bar_getter) |
---|
42 | n/a | m = self._make_message() |
---|
43 | n/a | m['Content-Type'] = 'text/plain' |
---|
44 | n/a | m['X-Bar-Header'] = 'bar' |
---|
45 | n/a | m['X-Foo-Header'] = 'foo' |
---|
46 | n/a | self.assertEqual(cm.get_content(m), ('foo')) |
---|
47 | n/a | |
---|
48 | n/a | def test_get_content_raises_if_unknown_mimetype_and_no_default(self): |
---|
49 | n/a | cm = ContentManager() |
---|
50 | n/a | m = self._make_message() |
---|
51 | n/a | m['Content-Type'] = 'text/plain' |
---|
52 | n/a | with self.assertRaisesRegex(KeyError, 'text/plain'): |
---|
53 | n/a | cm.get_content(m) |
---|
54 | n/a | |
---|
55 | n/a | class BaseThing(str): |
---|
56 | n/a | pass |
---|
57 | n/a | baseobject_full_path = __name__ + '.' + 'TestContentManager.BaseThing' |
---|
58 | n/a | class Thing(BaseThing): |
---|
59 | n/a | pass |
---|
60 | n/a | testobject_full_path = __name__ + '.' + 'TestContentManager.Thing' |
---|
61 | n/a | |
---|
62 | n/a | set_key_params = { |
---|
63 | n/a | 'type': (0, Thing,), |
---|
64 | n/a | 'full_path': (1, testobject_full_path,), |
---|
65 | n/a | 'qualname': (2, 'TestContentManager.Thing',), |
---|
66 | n/a | 'name': (3, 'Thing',), |
---|
67 | n/a | 'base_type': (4, BaseThing,), |
---|
68 | n/a | 'base_full_path': (5, baseobject_full_path,), |
---|
69 | n/a | 'base_qualname': (6, 'TestContentManager.BaseThing',), |
---|
70 | n/a | 'base_name': (7, 'BaseThing',), |
---|
71 | n/a | 'str_type': (8, str,), |
---|
72 | n/a | 'str_full_path': (9, 'builtins.str',), |
---|
73 | n/a | 'str_name': (10, 'str',), # str name and qualname are the same |
---|
74 | n/a | 'null_key': (11, None,), |
---|
75 | n/a | } |
---|
76 | n/a | |
---|
77 | n/a | def set_key_as_set_content_key(self, order, key): |
---|
78 | n/a | def foo_setter(msg, obj, foo=None): |
---|
79 | n/a | msg['X-Foo-Header'] = foo |
---|
80 | n/a | msg.set_payload(obj) |
---|
81 | n/a | cm = ContentManager() |
---|
82 | n/a | cm.add_set_handler(key, foo_setter) |
---|
83 | n/a | m = self._make_message() |
---|
84 | n/a | msg_obj = self.Thing() |
---|
85 | n/a | cm.set_content(m, msg_obj, foo='bar') |
---|
86 | n/a | self.assertEqual(m['X-Foo-Header'], 'bar') |
---|
87 | n/a | self.assertEqual(m.get_payload(), msg_obj) |
---|
88 | n/a | |
---|
89 | n/a | def set_key_as_set_content_key_order(self, order, key): |
---|
90 | n/a | def foo_setter(msg, obj): |
---|
91 | n/a | msg['X-FooBar-Header'] = 'foo' |
---|
92 | n/a | msg.set_payload(obj) |
---|
93 | n/a | def bar_setter(msg, obj): |
---|
94 | n/a | msg['X-FooBar-Header'] = 'bar' |
---|
95 | n/a | cm = ContentManager() |
---|
96 | n/a | cm.add_set_handler(key, foo_setter) |
---|
97 | n/a | for precedence, key in self.get_key_params.values(): |
---|
98 | n/a | if precedence > order: |
---|
99 | n/a | cm.add_set_handler(key, bar_setter) |
---|
100 | n/a | m = self._make_message() |
---|
101 | n/a | msg_obj = self.Thing() |
---|
102 | n/a | cm.set_content(m, msg_obj) |
---|
103 | n/a | self.assertEqual(m['X-FooBar-Header'], 'foo') |
---|
104 | n/a | self.assertEqual(m.get_payload(), msg_obj) |
---|
105 | n/a | |
---|
106 | n/a | def test_set_content_raises_if_unknown_type_and_no_default(self): |
---|
107 | n/a | cm = ContentManager() |
---|
108 | n/a | m = self._make_message() |
---|
109 | n/a | msg_obj = self.Thing() |
---|
110 | n/a | with self.assertRaisesRegex(KeyError, self.testobject_full_path): |
---|
111 | n/a | cm.set_content(m, msg_obj) |
---|
112 | n/a | |
---|
113 | n/a | def test_set_content_raises_if_called_on_multipart(self): |
---|
114 | n/a | cm = ContentManager() |
---|
115 | n/a | m = self._make_message() |
---|
116 | n/a | m['Content-Type'] = 'multipart/foo' |
---|
117 | n/a | with self.assertRaises(TypeError): |
---|
118 | n/a | cm.set_content(m, 'test') |
---|
119 | n/a | |
---|
120 | n/a | def test_set_content_calls_clear_content(self): |
---|
121 | n/a | m = self._make_message() |
---|
122 | n/a | m['Content-Foo'] = 'bar' |
---|
123 | n/a | m['Content-Type'] = 'text/html' |
---|
124 | n/a | m['To'] = 'test' |
---|
125 | n/a | m.set_payload('abc') |
---|
126 | n/a | cm = ContentManager() |
---|
127 | n/a | cm.add_set_handler(str, lambda *args, **kw: None) |
---|
128 | n/a | m.set_content('xyz', content_manager=cm) |
---|
129 | n/a | self.assertIsNone(m['Content-Foo']) |
---|
130 | n/a | self.assertIsNone(m['Content-Type']) |
---|
131 | n/a | self.assertEqual(m['To'], 'test') |
---|
132 | n/a | self.assertIsNone(m.get_payload()) |
---|
133 | n/a | |
---|
134 | n/a | |
---|
135 | n/a | @parameterize |
---|
136 | n/a | class TestRawDataManager(TestEmailBase): |
---|
137 | n/a | # Note: these tests are dependent on the order in which headers are added |
---|
138 | n/a | # to the message objects by the code. There's no defined ordering in |
---|
139 | n/a | # RFC5322/MIME, so this makes the tests more fragile than the standards |
---|
140 | n/a | # require. However, if the header order changes it is best to understand |
---|
141 | n/a | # *why*, and make sure it isn't a subtle bug in whatever change was |
---|
142 | n/a | # applied. |
---|
143 | n/a | |
---|
144 | n/a | policy = policy.default.clone(max_line_length=60, |
---|
145 | n/a | content_manager=raw_data_manager) |
---|
146 | n/a | message = EmailMessage |
---|
147 | n/a | |
---|
148 | n/a | def test_get_text_plain(self): |
---|
149 | n/a | m = self._str_msg(textwrap.dedent("""\ |
---|
150 | n/a | Content-Type: text/plain |
---|
151 | n/a | |
---|
152 | n/a | Basic text. |
---|
153 | n/a | """)) |
---|
154 | n/a | self.assertEqual(raw_data_manager.get_content(m), "Basic text.\n") |
---|
155 | n/a | |
---|
156 | n/a | def test_get_text_html(self): |
---|
157 | n/a | m = self._str_msg(textwrap.dedent("""\ |
---|
158 | n/a | Content-Type: text/html |
---|
159 | n/a | |
---|
160 | n/a | <p>Basic text.</p> |
---|
161 | n/a | """)) |
---|
162 | n/a | self.assertEqual(raw_data_manager.get_content(m), |
---|
163 | n/a | "<p>Basic text.</p>\n") |
---|
164 | n/a | |
---|
165 | n/a | def test_get_text_plain_latin1(self): |
---|
166 | n/a | m = self._bytes_msg(textwrap.dedent("""\ |
---|
167 | n/a | Content-Type: text/plain; charset=latin1 |
---|
168 | n/a | |
---|
169 | n/a | Basìc tëxt. |
---|
170 | n/a | """).encode('latin1')) |
---|
171 | n/a | self.assertEqual(raw_data_manager.get_content(m), "Basìc tëxt.\n") |
---|
172 | n/a | |
---|
173 | n/a | def test_get_text_plain_latin1_quoted_printable(self): |
---|
174 | n/a | m = self._str_msg(textwrap.dedent("""\ |
---|
175 | n/a | Content-Type: text/plain; charset="latin-1" |
---|
176 | n/a | Content-Transfer-Encoding: quoted-printable |
---|
177 | n/a | |
---|
178 | n/a | Bas=ECc t=EBxt. |
---|
179 | n/a | """)) |
---|
180 | n/a | self.assertEqual(raw_data_manager.get_content(m), "Basìc tëxt.\n") |
---|
181 | n/a | |
---|
182 | n/a | def test_get_text_plain_utf8_base64(self): |
---|
183 | n/a | m = self._str_msg(textwrap.dedent("""\ |
---|
184 | n/a | Content-Type: text/plain; charset="utf8" |
---|
185 | n/a | Content-Transfer-Encoding: base64 |
---|
186 | n/a | |
---|
187 | n/a | QmFzw6xjIHTDq3h0Lgo= |
---|
188 | n/a | """)) |
---|
189 | n/a | self.assertEqual(raw_data_manager.get_content(m), "Basìc tëxt.\n") |
---|
190 | n/a | |
---|
191 | n/a | def test_get_text_plain_bad_utf8_quoted_printable(self): |
---|
192 | n/a | m = self._str_msg(textwrap.dedent("""\ |
---|
193 | n/a | Content-Type: text/plain; charset="utf8" |
---|
194 | n/a | Content-Transfer-Encoding: quoted-printable |
---|
195 | n/a | |
---|
196 | n/a | Bas=c3=acc t=c3=abxt=fd. |
---|
197 | n/a | """)) |
---|
198 | n/a | self.assertEqual(raw_data_manager.get_content(m), "Basìc tëxt�.\n") |
---|
199 | n/a | |
---|
200 | n/a | def test_get_text_plain_bad_utf8_quoted_printable_ignore_errors(self): |
---|
201 | n/a | m = self._str_msg(textwrap.dedent("""\ |
---|
202 | n/a | Content-Type: text/plain; charset="utf8" |
---|
203 | n/a | Content-Transfer-Encoding: quoted-printable |
---|
204 | n/a | |
---|
205 | n/a | Bas=c3=acc t=c3=abxt=fd. |
---|
206 | n/a | """)) |
---|
207 | n/a | self.assertEqual(raw_data_manager.get_content(m, errors='ignore'), |
---|
208 | n/a | "Basìc tëxt.\n") |
---|
209 | n/a | |
---|
210 | n/a | def test_get_text_plain_utf8_base64_recoverable_bad_CTE_data(self): |
---|
211 | n/a | m = self._str_msg(textwrap.dedent("""\ |
---|
212 | n/a | Content-Type: text/plain; charset="utf8" |
---|
213 | n/a | Content-Transfer-Encoding: base64 |
---|
214 | n/a | |
---|
215 | n/a | QmFzw6xjIHTDq3h0Lgo\xFF= |
---|
216 | n/a | """)) |
---|
217 | n/a | self.assertEqual(raw_data_manager.get_content(m, errors='ignore'), |
---|
218 | n/a | "Basìc tëxt.\n") |
---|
219 | n/a | |
---|
220 | n/a | def test_get_text_invalid_keyword(self): |
---|
221 | n/a | m = self._str_msg(textwrap.dedent("""\ |
---|
222 | n/a | Content-Type: text/plain |
---|
223 | n/a | |
---|
224 | n/a | Basic text. |
---|
225 | n/a | """)) |
---|
226 | n/a | with self.assertRaises(TypeError): |
---|
227 | n/a | raw_data_manager.get_content(m, foo='ignore') |
---|
228 | n/a | |
---|
229 | n/a | def test_get_non_text(self): |
---|
230 | n/a | template = textwrap.dedent("""\ |
---|
231 | n/a | Content-Type: {} |
---|
232 | n/a | Content-Transfer-Encoding: base64 |
---|
233 | n/a | |
---|
234 | n/a | Ym9ndXMgZGF0YQ== |
---|
235 | n/a | """) |
---|
236 | n/a | for maintype in 'audio image video application'.split(): |
---|
237 | n/a | with self.subTest(maintype=maintype): |
---|
238 | n/a | m = self._str_msg(template.format(maintype+'/foo')) |
---|
239 | n/a | self.assertEqual(raw_data_manager.get_content(m), b"bogus data") |
---|
240 | n/a | |
---|
241 | n/a | def test_get_non_text_invalid_keyword(self): |
---|
242 | n/a | m = self._str_msg(textwrap.dedent("""\ |
---|
243 | n/a | Content-Type: image/jpg |
---|
244 | n/a | Content-Transfer-Encoding: base64 |
---|
245 | n/a | |
---|
246 | n/a | Ym9ndXMgZGF0YQ== |
---|
247 | n/a | """)) |
---|
248 | n/a | with self.assertRaises(TypeError): |
---|
249 | n/a | raw_data_manager.get_content(m, errors='ignore') |
---|
250 | n/a | |
---|
251 | n/a | def test_get_raises_on_multipart(self): |
---|
252 | n/a | m = self._str_msg(textwrap.dedent("""\ |
---|
253 | n/a | Content-Type: multipart/mixed; boundary="===" |
---|
254 | n/a | |
---|
255 | n/a | --=== |
---|
256 | n/a | --===-- |
---|
257 | n/a | """)) |
---|
258 | n/a | with self.assertRaises(KeyError): |
---|
259 | n/a | raw_data_manager.get_content(m) |
---|
260 | n/a | |
---|
261 | n/a | def test_get_message_rfc822_and_external_body(self): |
---|
262 | n/a | template = textwrap.dedent("""\ |
---|
263 | n/a | Content-Type: message/{} |
---|
264 | n/a | |
---|
265 | n/a | To: foo@example.com |
---|
266 | n/a | From: bar@example.com |
---|
267 | n/a | Subject: example |
---|
268 | n/a | |
---|
269 | n/a | an example message |
---|
270 | n/a | """) |
---|
271 | n/a | for subtype in 'rfc822 external-body'.split(): |
---|
272 | n/a | with self.subTest(subtype=subtype): |
---|
273 | n/a | m = self._str_msg(template.format(subtype)) |
---|
274 | n/a | sub_msg = raw_data_manager.get_content(m) |
---|
275 | n/a | self.assertIsInstance(sub_msg, self.message) |
---|
276 | n/a | self.assertEqual(raw_data_manager.get_content(sub_msg), |
---|
277 | n/a | "an example message\n") |
---|
278 | n/a | self.assertEqual(sub_msg['to'], 'foo@example.com') |
---|
279 | n/a | self.assertEqual(sub_msg['from'].addresses[0].username, 'bar') |
---|
280 | n/a | |
---|
281 | n/a | def test_get_message_non_rfc822_or_external_body_yields_bytes(self): |
---|
282 | n/a | m = self._str_msg(textwrap.dedent("""\ |
---|
283 | n/a | Content-Type: message/partial |
---|
284 | n/a | |
---|
285 | n/a | To: foo@example.com |
---|
286 | n/a | From: bar@example.com |
---|
287 | n/a | Subject: example |
---|
288 | n/a | |
---|
289 | n/a | The real body is in another message. |
---|
290 | n/a | """)) |
---|
291 | n/a | self.assertEqual(raw_data_manager.get_content(m)[:10], b'To: foo@ex') |
---|
292 | n/a | |
---|
293 | n/a | def test_set_text_plain(self): |
---|
294 | n/a | m = self._make_message() |
---|
295 | n/a | content = "Simple message.\n" |
---|
296 | n/a | raw_data_manager.set_content(m, content) |
---|
297 | n/a | self.assertEqual(str(m), textwrap.dedent("""\ |
---|
298 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
299 | n/a | Content-Transfer-Encoding: 7bit |
---|
300 | n/a | |
---|
301 | n/a | Simple message. |
---|
302 | n/a | """)) |
---|
303 | n/a | self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content) |
---|
304 | n/a | self.assertEqual(m.get_content(), content) |
---|
305 | n/a | |
---|
306 | n/a | def test_set_text_html(self): |
---|
307 | n/a | m = self._make_message() |
---|
308 | n/a | content = "<p>Simple message.</p>\n" |
---|
309 | n/a | raw_data_manager.set_content(m, content, subtype='html') |
---|
310 | n/a | self.assertEqual(str(m), textwrap.dedent("""\ |
---|
311 | n/a | Content-Type: text/html; charset="utf-8" |
---|
312 | n/a | Content-Transfer-Encoding: 7bit |
---|
313 | n/a | |
---|
314 | n/a | <p>Simple message.</p> |
---|
315 | n/a | """)) |
---|
316 | n/a | self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content) |
---|
317 | n/a | self.assertEqual(m.get_content(), content) |
---|
318 | n/a | |
---|
319 | n/a | def test_set_text_charset_latin_1(self): |
---|
320 | n/a | m = self._make_message() |
---|
321 | n/a | content = "Simple message.\n" |
---|
322 | n/a | raw_data_manager.set_content(m, content, charset='latin-1') |
---|
323 | n/a | self.assertEqual(str(m), textwrap.dedent("""\ |
---|
324 | n/a | Content-Type: text/plain; charset="iso-8859-1" |
---|
325 | n/a | Content-Transfer-Encoding: 7bit |
---|
326 | n/a | |
---|
327 | n/a | Simple message. |
---|
328 | n/a | """)) |
---|
329 | n/a | self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content) |
---|
330 | n/a | self.assertEqual(m.get_content(), content) |
---|
331 | n/a | |
---|
332 | n/a | def test_set_text_short_line_minimal_non_ascii_heuristics(self): |
---|
333 | n/a | m = self._make_message() |
---|
334 | n/a | content = "et là il est monté sur moi et il commence à m'éto.\n" |
---|
335 | n/a | raw_data_manager.set_content(m, content) |
---|
336 | n/a | self.assertEqual(bytes(m), textwrap.dedent("""\ |
---|
337 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
338 | n/a | Content-Transfer-Encoding: 8bit |
---|
339 | n/a | |
---|
340 | n/a | et là il est monté sur moi et il commence à m'éto. |
---|
341 | n/a | """).encode('utf-8')) |
---|
342 | n/a | self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content) |
---|
343 | n/a | self.assertEqual(m.get_content(), content) |
---|
344 | n/a | |
---|
345 | n/a | def test_set_text_long_line_minimal_non_ascii_heuristics(self): |
---|
346 | n/a | m = self._make_message() |
---|
347 | n/a | content = ("j'ai un problème de python. il est sorti de son" |
---|
348 | n/a | " vivarium. et là il est monté sur moi et il commence" |
---|
349 | n/a | " à m'éto.\n") |
---|
350 | n/a | raw_data_manager.set_content(m, content) |
---|
351 | n/a | self.assertEqual(bytes(m), textwrap.dedent("""\ |
---|
352 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
353 | n/a | Content-Transfer-Encoding: quoted-printable |
---|
354 | n/a | |
---|
355 | n/a | j'ai un probl=C3=A8me de python. il est sorti de son vivari= |
---|
356 | n/a | um. et l=C3=A0 il est mont=C3=A9 sur moi et il commence = |
---|
357 | n/a | =C3=A0 m'=C3=A9to. |
---|
358 | n/a | """).encode('utf-8')) |
---|
359 | n/a | self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content) |
---|
360 | n/a | self.assertEqual(m.get_content(), content) |
---|
361 | n/a | |
---|
362 | n/a | def test_set_text_11_lines_long_line_minimal_non_ascii_heuristics(self): |
---|
363 | n/a | m = self._make_message() |
---|
364 | n/a | content = '\n'*10 + ( |
---|
365 | n/a | "j'ai un problème de python. il est sorti de son" |
---|
366 | n/a | " vivarium. et là il est monté sur moi et il commence" |
---|
367 | n/a | " à m'éto.\n") |
---|
368 | n/a | raw_data_manager.set_content(m, content) |
---|
369 | n/a | self.assertEqual(bytes(m), textwrap.dedent("""\ |
---|
370 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
371 | n/a | Content-Transfer-Encoding: quoted-printable |
---|
372 | n/a | """ + '\n'*10 + """ |
---|
373 | n/a | j'ai un probl=C3=A8me de python. il est sorti de son vivari= |
---|
374 | n/a | um. et l=C3=A0 il est mont=C3=A9 sur moi et il commence = |
---|
375 | n/a | =C3=A0 m'=C3=A9to. |
---|
376 | n/a | """).encode('utf-8')) |
---|
377 | n/a | self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content) |
---|
378 | n/a | self.assertEqual(m.get_content(), content) |
---|
379 | n/a | |
---|
380 | n/a | def test_set_text_maximal_non_ascii_heuristics(self): |
---|
381 | n/a | m = self._make_message() |
---|
382 | n/a | content = "áà äéèÄöÅ.\n" |
---|
383 | n/a | raw_data_manager.set_content(m, content) |
---|
384 | n/a | self.assertEqual(bytes(m), textwrap.dedent("""\ |
---|
385 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
386 | n/a | Content-Transfer-Encoding: 8bit |
---|
387 | n/a | |
---|
388 | n/a | áà äéèÄöÅ. |
---|
389 | n/a | """).encode('utf-8')) |
---|
390 | n/a | self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content) |
---|
391 | n/a | self.assertEqual(m.get_content(), content) |
---|
392 | n/a | |
---|
393 | n/a | def test_set_text_11_lines_maximal_non_ascii_heuristics(self): |
---|
394 | n/a | m = self._make_message() |
---|
395 | n/a | content = '\n'*10 + "áà äéèÄöÅ.\n" |
---|
396 | n/a | raw_data_manager.set_content(m, content) |
---|
397 | n/a | self.assertEqual(bytes(m), textwrap.dedent("""\ |
---|
398 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
399 | n/a | Content-Transfer-Encoding: 8bit |
---|
400 | n/a | """ + '\n'*10 + """ |
---|
401 | n/a | áà äéèÄöÅ. |
---|
402 | n/a | """).encode('utf-8')) |
---|
403 | n/a | self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content) |
---|
404 | n/a | self.assertEqual(m.get_content(), content) |
---|
405 | n/a | |
---|
406 | n/a | def test_set_text_long_line_maximal_non_ascii_heuristics(self): |
---|
407 | n/a | m = self._make_message() |
---|
408 | n/a | content = ("áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ" |
---|
409 | n/a | "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ" |
---|
410 | n/a | "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ.\n") |
---|
411 | n/a | raw_data_manager.set_content(m, content) |
---|
412 | n/a | self.assertEqual(bytes(m), textwrap.dedent("""\ |
---|
413 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
414 | n/a | Content-Transfer-Encoding: base64 |
---|
415 | n/a | |
---|
416 | n/a | w6HDoMOkw6nDqMSZw7bFkcOhw6DDpMOpw6jEmcO2xZHDocOgw6TDqcOoxJnD |
---|
417 | n/a | tsWRw6HDoMOkw6nDqMSZw7bFkcOhw6DDpMOpw6jEmcO2xZHDocOgw6TDqcOo |
---|
418 | n/a | xJnDtsWRw6HDoMOkw6nDqMSZw7bFkcOhw6DDpMOpw6jEmcO2xZHDocOgw6TD |
---|
419 | n/a | qcOoxJnDtsWRw6HDoMOkw6nDqMSZw7bFkcOhw6DDpMOpw6jEmcO2xZHDocOg |
---|
420 | n/a | w6TDqcOoxJnDtsWRLgo= |
---|
421 | n/a | """).encode('utf-8')) |
---|
422 | n/a | self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content) |
---|
423 | n/a | self.assertEqual(m.get_content(), content) |
---|
424 | n/a | |
---|
425 | n/a | def test_set_text_11_lines_long_line_maximal_non_ascii_heuristics(self): |
---|
426 | n/a | # Yes, it chooses "wrong" here. It's a heuristic. So this result |
---|
427 | n/a | # could change if we come up with a better heuristic. |
---|
428 | n/a | m = self._make_message() |
---|
429 | n/a | content = ('\n'*10 + |
---|
430 | n/a | "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ" |
---|
431 | n/a | "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ" |
---|
432 | n/a | "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ.\n") |
---|
433 | n/a | raw_data_manager.set_content(m, "\n"*10 + |
---|
434 | n/a | "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ" |
---|
435 | n/a | "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ" |
---|
436 | n/a | "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ.\n") |
---|
437 | n/a | self.assertEqual(bytes(m), textwrap.dedent("""\ |
---|
438 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
439 | n/a | Content-Transfer-Encoding: quoted-printable |
---|
440 | n/a | """ + '\n'*10 + """ |
---|
441 | n/a | =C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3= |
---|
442 | n/a | =A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4= |
---|
443 | n/a | =C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3= |
---|
444 | n/a | =A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99= |
---|
445 | n/a | =C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5= |
---|
446 | n/a | =91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1= |
---|
447 | n/a | =C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3= |
---|
448 | n/a | =A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9= |
---|
449 | n/a | =C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4= |
---|
450 | n/a | =99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6= |
---|
451 | n/a | =C5=91. |
---|
452 | n/a | """).encode('utf-8')) |
---|
453 | n/a | self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content) |
---|
454 | n/a | self.assertEqual(m.get_content(), content) |
---|
455 | n/a | |
---|
456 | n/a | def test_set_text_non_ascii_with_cte_7bit_raises(self): |
---|
457 | n/a | m = self._make_message() |
---|
458 | n/a | with self.assertRaises(UnicodeError): |
---|
459 | n/a | raw_data_manager.set_content(m,"áà äéèÄöÅ.\n", cte='7bit') |
---|
460 | n/a | |
---|
461 | n/a | def test_set_text_non_ascii_with_charset_ascii_raises(self): |
---|
462 | n/a | m = self._make_message() |
---|
463 | n/a | with self.assertRaises(UnicodeError): |
---|
464 | n/a | raw_data_manager.set_content(m,"áà äéèÄöÅ.\n", charset='ascii') |
---|
465 | n/a | |
---|
466 | n/a | def test_set_text_non_ascii_with_cte_7bit_and_charset_ascii_raises(self): |
---|
467 | n/a | m = self._make_message() |
---|
468 | n/a | with self.assertRaises(UnicodeError): |
---|
469 | n/a | raw_data_manager.set_content(m,"áà äéèÄöÅ.\n", cte='7bit', charset='ascii') |
---|
470 | n/a | |
---|
471 | n/a | def test_set_message(self): |
---|
472 | n/a | m = self._make_message() |
---|
473 | n/a | m['Subject'] = "Forwarded message" |
---|
474 | n/a | content = self._make_message() |
---|
475 | n/a | content['To'] = 'python@vivarium.org' |
---|
476 | n/a | content['From'] = 'police@monty.org' |
---|
477 | n/a | content['Subject'] = "get back in your box" |
---|
478 | n/a | content.set_content("Or face the comfy chair.") |
---|
479 | n/a | raw_data_manager.set_content(m, content) |
---|
480 | n/a | self.assertEqual(str(m), textwrap.dedent("""\ |
---|
481 | n/a | Subject: Forwarded message |
---|
482 | n/a | Content-Type: message/rfc822 |
---|
483 | n/a | Content-Transfer-Encoding: 8bit |
---|
484 | n/a | |
---|
485 | n/a | To: python@vivarium.org |
---|
486 | n/a | From: police@monty.org |
---|
487 | n/a | Subject: get back in your box |
---|
488 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
489 | n/a | Content-Transfer-Encoding: 7bit |
---|
490 | n/a | MIME-Version: 1.0 |
---|
491 | n/a | |
---|
492 | n/a | Or face the comfy chair. |
---|
493 | n/a | """)) |
---|
494 | n/a | payload = m.get_payload(0) |
---|
495 | n/a | self.assertIsInstance(payload, self.message) |
---|
496 | n/a | self.assertEqual(str(payload), str(content)) |
---|
497 | n/a | self.assertIsInstance(m.get_content(), self.message) |
---|
498 | n/a | self.assertEqual(str(m.get_content()), str(content)) |
---|
499 | n/a | |
---|
500 | n/a | def test_set_message_with_non_ascii_and_coercion_to_7bit(self): |
---|
501 | n/a | m = self._make_message() |
---|
502 | n/a | m['Subject'] = "Escape report" |
---|
503 | n/a | content = self._make_message() |
---|
504 | n/a | content['To'] = 'police@monty.org' |
---|
505 | n/a | content['From'] = 'victim@monty.org' |
---|
506 | n/a | content['Subject'] = "Help" |
---|
507 | n/a | content.set_content("j'ai un problème de python. il est sorti de son" |
---|
508 | n/a | " vivarium.") |
---|
509 | n/a | raw_data_manager.set_content(m, content) |
---|
510 | n/a | self.assertEqual(bytes(m), textwrap.dedent("""\ |
---|
511 | n/a | Subject: Escape report |
---|
512 | n/a | Content-Type: message/rfc822 |
---|
513 | n/a | Content-Transfer-Encoding: 8bit |
---|
514 | n/a | |
---|
515 | n/a | To: police@monty.org |
---|
516 | n/a | From: victim@monty.org |
---|
517 | n/a | Subject: Help |
---|
518 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
519 | n/a | Content-Transfer-Encoding: 8bit |
---|
520 | n/a | MIME-Version: 1.0 |
---|
521 | n/a | |
---|
522 | n/a | j'ai un problème de python. il est sorti de son vivarium. |
---|
523 | n/a | """).encode('utf-8')) |
---|
524 | n/a | # The choice of base64 for the body encoding is because generator |
---|
525 | n/a | # doesn't bother with heuristics and uses it unconditionally for utf-8 |
---|
526 | n/a | # text. |
---|
527 | n/a | # XXX: the first cte should be 7bit, too...that's a generator bug. |
---|
528 | n/a | # XXX: the line length in the body also looks like a generator bug. |
---|
529 | n/a | self.assertEqual(m.as_string(maxheaderlen=self.policy.max_line_length), |
---|
530 | n/a | textwrap.dedent("""\ |
---|
531 | n/a | Subject: Escape report |
---|
532 | n/a | Content-Type: message/rfc822 |
---|
533 | n/a | Content-Transfer-Encoding: 8bit |
---|
534 | n/a | |
---|
535 | n/a | To: police@monty.org |
---|
536 | n/a | From: victim@monty.org |
---|
537 | n/a | Subject: Help |
---|
538 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
539 | n/a | Content-Transfer-Encoding: base64 |
---|
540 | n/a | MIME-Version: 1.0 |
---|
541 | n/a | |
---|
542 | n/a | aidhaSB1biBwcm9ibMOobWUgZGUgcHl0aG9uLiBpbCBlc3Qgc29ydGkgZGUgc29uIHZpdmFyaXVt |
---|
543 | n/a | Lgo= |
---|
544 | n/a | """)) |
---|
545 | n/a | self.assertIsInstance(m.get_content(), self.message) |
---|
546 | n/a | self.assertEqual(str(m.get_content()), str(content)) |
---|
547 | n/a | |
---|
548 | n/a | def test_set_message_invalid_cte_raises(self): |
---|
549 | n/a | m = self._make_message() |
---|
550 | n/a | content = self._make_message() |
---|
551 | n/a | for cte in 'quoted-printable base64'.split(): |
---|
552 | n/a | for subtype in 'rfc822 external-body'.split(): |
---|
553 | n/a | with self.subTest(cte=cte, subtype=subtype): |
---|
554 | n/a | with self.assertRaises(ValueError) as ar: |
---|
555 | n/a | m.set_content(content, subtype, cte=cte) |
---|
556 | n/a | exc = str(ar.exception) |
---|
557 | n/a | self.assertIn(cte, exc) |
---|
558 | n/a | self.assertIn(subtype, exc) |
---|
559 | n/a | subtype = 'external-body' |
---|
560 | n/a | for cte in '8bit binary'.split(): |
---|
561 | n/a | with self.subTest(cte=cte, subtype=subtype): |
---|
562 | n/a | with self.assertRaises(ValueError) as ar: |
---|
563 | n/a | m.set_content(content, subtype, cte=cte) |
---|
564 | n/a | exc = str(ar.exception) |
---|
565 | n/a | self.assertIn(cte, exc) |
---|
566 | n/a | self.assertIn(subtype, exc) |
---|
567 | n/a | |
---|
568 | n/a | def test_set_image_jpg(self): |
---|
569 | n/a | for content in (b"bogus content", |
---|
570 | n/a | bytearray(b"bogus content"), |
---|
571 | n/a | memoryview(b"bogus content")): |
---|
572 | n/a | with self.subTest(content=content): |
---|
573 | n/a | m = self._make_message() |
---|
574 | n/a | raw_data_manager.set_content(m, content, 'image', 'jpeg') |
---|
575 | n/a | self.assertEqual(str(m), textwrap.dedent("""\ |
---|
576 | n/a | Content-Type: image/jpeg |
---|
577 | n/a | Content-Transfer-Encoding: base64 |
---|
578 | n/a | |
---|
579 | n/a | Ym9ndXMgY29udGVudA== |
---|
580 | n/a | """)) |
---|
581 | n/a | self.assertEqual(m.get_payload(decode=True), content) |
---|
582 | n/a | self.assertEqual(m.get_content(), content) |
---|
583 | n/a | |
---|
584 | n/a | def test_set_audio_aif_with_quoted_printable_cte(self): |
---|
585 | n/a | # Why you would use qp, I don't know, but it is technically supported. |
---|
586 | n/a | # XXX: the incorrect line length is because binascii.b2a_qp doesn't |
---|
587 | n/a | # support a line length parameter, but we must use it to get newline |
---|
588 | n/a | # encoding. |
---|
589 | n/a | # XXX: what about that lack of tailing newline? Do we actually handle |
---|
590 | n/a | # that correctly in all cases? That is, if the *source* has an |
---|
591 | n/a | # unencoded newline, do we add an extra newline to the returned payload |
---|
592 | n/a | # or not? And can that actually be disambiguated based on the RFC? |
---|
593 | n/a | m = self._make_message() |
---|
594 | n/a | content = b'b\xFFgus\tcon\nt\rent ' + b'z'*100 |
---|
595 | n/a | m.set_content(content, 'audio', 'aif', cte='quoted-printable') |
---|
596 | n/a | self.assertEqual(bytes(m), textwrap.dedent("""\ |
---|
597 | n/a | Content-Type: audio/aif |
---|
598 | n/a | Content-Transfer-Encoding: quoted-printable |
---|
599 | n/a | MIME-Version: 1.0 |
---|
600 | n/a | |
---|
601 | n/a | b=FFgus=09con=0At=0Dent=20zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz= |
---|
602 | n/a | zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz""").encode('latin-1')) |
---|
603 | n/a | self.assertEqual(m.get_payload(decode=True), content) |
---|
604 | n/a | self.assertEqual(m.get_content(), content) |
---|
605 | n/a | |
---|
606 | n/a | def test_set_video_mpeg_with_binary_cte(self): |
---|
607 | n/a | m = self._make_message() |
---|
608 | n/a | content = b'b\xFFgus\tcon\nt\rent ' + b'z'*100 |
---|
609 | n/a | m.set_content(content, 'video', 'mpeg', cte='binary') |
---|
610 | n/a | self.assertEqual(bytes(m), textwrap.dedent("""\ |
---|
611 | n/a | Content-Type: video/mpeg |
---|
612 | n/a | Content-Transfer-Encoding: binary |
---|
613 | n/a | MIME-Version: 1.0 |
---|
614 | n/a | |
---|
615 | n/a | """).encode('ascii') + |
---|
616 | n/a | # XXX: the second \n ought to be a \r, but generator gets it wrong. |
---|
617 | n/a | # THIS MEANS WE DON'T ACTUALLY SUPPORT THE 'binary' CTE. |
---|
618 | n/a | b'b\xFFgus\tcon\nt\nent zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz' + |
---|
619 | n/a | b'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz') |
---|
620 | n/a | self.assertEqual(m.get_payload(decode=True), content) |
---|
621 | n/a | self.assertEqual(m.get_content(), content) |
---|
622 | n/a | |
---|
623 | n/a | def test_set_application_octet_stream_with_8bit_cte(self): |
---|
624 | n/a | # In 8bit mode, universal line end logic applies. It is up to the |
---|
625 | n/a | # application to make sure the lines are short enough; we don't check. |
---|
626 | n/a | m = self._make_message() |
---|
627 | n/a | content = b'b\xFFgus\tcon\nt\rent\n' + b'z'*60 + b'\n' |
---|
628 | n/a | m.set_content(content, 'application', 'octet-stream', cte='8bit') |
---|
629 | n/a | self.assertEqual(bytes(m), textwrap.dedent("""\ |
---|
630 | n/a | Content-Type: application/octet-stream |
---|
631 | n/a | Content-Transfer-Encoding: 8bit |
---|
632 | n/a | MIME-Version: 1.0 |
---|
633 | n/a | |
---|
634 | n/a | """).encode('ascii') + |
---|
635 | n/a | b'b\xFFgus\tcon\nt\nent\n' + |
---|
636 | n/a | b'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz\n') |
---|
637 | n/a | self.assertEqual(m.get_payload(decode=True), content) |
---|
638 | n/a | self.assertEqual(m.get_content(), content) |
---|
639 | n/a | |
---|
640 | n/a | def test_set_headers_from_header_objects(self): |
---|
641 | n/a | m = self._make_message() |
---|
642 | n/a | content = "Simple message.\n" |
---|
643 | n/a | header_factory = self.policy.header_factory |
---|
644 | n/a | raw_data_manager.set_content(m, content, headers=( |
---|
645 | n/a | header_factory("To", "foo@example.com"), |
---|
646 | n/a | header_factory("From", "foo@example.com"), |
---|
647 | n/a | header_factory("Subject", "I'm talking to myself."))) |
---|
648 | n/a | self.assertEqual(str(m), textwrap.dedent("""\ |
---|
649 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
650 | n/a | To: foo@example.com |
---|
651 | n/a | From: foo@example.com |
---|
652 | n/a | Subject: I'm talking to myself. |
---|
653 | n/a | Content-Transfer-Encoding: 7bit |
---|
654 | n/a | |
---|
655 | n/a | Simple message. |
---|
656 | n/a | """)) |
---|
657 | n/a | |
---|
658 | n/a | def test_set_headers_from_strings(self): |
---|
659 | n/a | m = self._make_message() |
---|
660 | n/a | content = "Simple message.\n" |
---|
661 | n/a | raw_data_manager.set_content(m, content, headers=( |
---|
662 | n/a | "X-Foo-Header: foo", |
---|
663 | n/a | "X-Bar-Header: bar",)) |
---|
664 | n/a | self.assertEqual(str(m), textwrap.dedent("""\ |
---|
665 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
666 | n/a | X-Foo-Header: foo |
---|
667 | n/a | X-Bar-Header: bar |
---|
668 | n/a | Content-Transfer-Encoding: 7bit |
---|
669 | n/a | |
---|
670 | n/a | Simple message. |
---|
671 | n/a | """)) |
---|
672 | n/a | |
---|
673 | n/a | def test_set_headers_with_invalid_duplicate_string_header_raises(self): |
---|
674 | n/a | m = self._make_message() |
---|
675 | n/a | content = "Simple message.\n" |
---|
676 | n/a | with self.assertRaisesRegex(ValueError, 'Content-Type'): |
---|
677 | n/a | raw_data_manager.set_content(m, content, headers=( |
---|
678 | n/a | "Content-Type: foo/bar",) |
---|
679 | n/a | ) |
---|
680 | n/a | |
---|
681 | n/a | def test_set_headers_with_invalid_duplicate_header_header_raises(self): |
---|
682 | n/a | m = self._make_message() |
---|
683 | n/a | content = "Simple message.\n" |
---|
684 | n/a | header_factory = self.policy.header_factory |
---|
685 | n/a | with self.assertRaisesRegex(ValueError, 'Content-Type'): |
---|
686 | n/a | raw_data_manager.set_content(m, content, headers=( |
---|
687 | n/a | header_factory("Content-Type", " foo/bar"),) |
---|
688 | n/a | ) |
---|
689 | n/a | |
---|
690 | n/a | def test_set_headers_with_defective_string_header_raises(self): |
---|
691 | n/a | m = self._make_message() |
---|
692 | n/a | content = "Simple message.\n" |
---|
693 | n/a | with self.assertRaisesRegex(ValueError, 'a@fairly@@invalid@address'): |
---|
694 | n/a | raw_data_manager.set_content(m, content, headers=( |
---|
695 | n/a | 'To: a@fairly@@invalid@address',) |
---|
696 | n/a | ) |
---|
697 | n/a | print(m['To'].defects) |
---|
698 | n/a | |
---|
699 | n/a | def test_set_headers_with_defective_header_header_raises(self): |
---|
700 | n/a | m = self._make_message() |
---|
701 | n/a | content = "Simple message.\n" |
---|
702 | n/a | header_factory = self.policy.header_factory |
---|
703 | n/a | with self.assertRaisesRegex(ValueError, 'a@fairly@@invalid@address'): |
---|
704 | n/a | raw_data_manager.set_content(m, content, headers=( |
---|
705 | n/a | header_factory('To', 'a@fairly@@invalid@address'),) |
---|
706 | n/a | ) |
---|
707 | n/a | print(m['To'].defects) |
---|
708 | n/a | |
---|
709 | n/a | def test_set_disposition_inline(self): |
---|
710 | n/a | m = self._make_message() |
---|
711 | n/a | m.set_content('foo', disposition='inline') |
---|
712 | n/a | self.assertEqual(m['Content-Disposition'], 'inline') |
---|
713 | n/a | |
---|
714 | n/a | def test_set_disposition_attachment(self): |
---|
715 | n/a | m = self._make_message() |
---|
716 | n/a | m.set_content('foo', disposition='attachment') |
---|
717 | n/a | self.assertEqual(m['Content-Disposition'], 'attachment') |
---|
718 | n/a | |
---|
719 | n/a | def test_set_disposition_foo(self): |
---|
720 | n/a | m = self._make_message() |
---|
721 | n/a | m.set_content('foo', disposition='foo') |
---|
722 | n/a | self.assertEqual(m['Content-Disposition'], 'foo') |
---|
723 | n/a | |
---|
724 | n/a | # XXX: we should have a 'strict' policy mode (beyond raise_on_defect) that |
---|
725 | n/a | # would cause 'foo' above to raise. |
---|
726 | n/a | |
---|
727 | n/a | def test_set_filename(self): |
---|
728 | n/a | m = self._make_message() |
---|
729 | n/a | m.set_content('foo', filename='bar.txt') |
---|
730 | n/a | self.assertEqual(m['Content-Disposition'], |
---|
731 | n/a | 'attachment; filename="bar.txt"') |
---|
732 | n/a | |
---|
733 | n/a | def test_set_filename_and_disposition_inline(self): |
---|
734 | n/a | m = self._make_message() |
---|
735 | n/a | m.set_content('foo', disposition='inline', filename='bar.txt') |
---|
736 | n/a | self.assertEqual(m['Content-Disposition'], 'inline; filename="bar.txt"') |
---|
737 | n/a | |
---|
738 | n/a | def test_set_non_ascii_filename(self): |
---|
739 | n/a | m = self._make_message() |
---|
740 | n/a | m.set_content('foo', filename='ábárî.txt') |
---|
741 | n/a | self.assertEqual(bytes(m), textwrap.dedent("""\ |
---|
742 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
743 | n/a | Content-Transfer-Encoding: 7bit |
---|
744 | n/a | Content-Disposition: attachment; |
---|
745 | n/a | filename*=utf-8''%C3%A1b%C3%A1r%C3%AE.txt |
---|
746 | n/a | MIME-Version: 1.0 |
---|
747 | n/a | |
---|
748 | n/a | foo |
---|
749 | n/a | """).encode('ascii')) |
---|
750 | n/a | |
---|
751 | n/a | content_object_params = { |
---|
752 | n/a | 'text_plain': ('content', ()), |
---|
753 | n/a | 'text_html': ('content', ('html',)), |
---|
754 | n/a | 'application_octet_stream': (b'content', |
---|
755 | n/a | ('application', 'octet_stream')), |
---|
756 | n/a | 'image_jpeg': (b'content', ('image', 'jpeg')), |
---|
757 | n/a | 'message_rfc822': (message(), ()), |
---|
758 | n/a | 'message_external_body': (message(), ('external-body',)), |
---|
759 | n/a | } |
---|
760 | n/a | |
---|
761 | n/a | def content_object_as_header_receiver(self, obj, mimetype): |
---|
762 | n/a | m = self._make_message() |
---|
763 | n/a | m.set_content(obj, *mimetype, headers=( |
---|
764 | n/a | 'To: foo@example.com', |
---|
765 | n/a | 'From: bar@simple.net')) |
---|
766 | n/a | self.assertEqual(m['to'], 'foo@example.com') |
---|
767 | n/a | self.assertEqual(m['from'], 'bar@simple.net') |
---|
768 | n/a | |
---|
769 | n/a | def content_object_as_disposition_inline_receiver(self, obj, mimetype): |
---|
770 | n/a | m = self._make_message() |
---|
771 | n/a | m.set_content(obj, *mimetype, disposition='inline') |
---|
772 | n/a | self.assertEqual(m['Content-Disposition'], 'inline') |
---|
773 | n/a | |
---|
774 | n/a | def content_object_as_non_ascii_filename_receiver(self, obj, mimetype): |
---|
775 | n/a | m = self._make_message() |
---|
776 | n/a | m.set_content(obj, *mimetype, disposition='inline', filename='bár.txt') |
---|
777 | n/a | self.assertEqual(m['Content-Disposition'], 'inline; filename="bár.txt"') |
---|
778 | n/a | self.assertEqual(m.get_filename(), "bár.txt") |
---|
779 | n/a | self.assertEqual(m['Content-Disposition'].params['filename'], "bár.txt") |
---|
780 | n/a | |
---|
781 | n/a | def content_object_as_cid_receiver(self, obj, mimetype): |
---|
782 | n/a | m = self._make_message() |
---|
783 | n/a | m.set_content(obj, *mimetype, cid='some_random_stuff') |
---|
784 | n/a | self.assertEqual(m['Content-ID'], 'some_random_stuff') |
---|
785 | n/a | |
---|
786 | n/a | def content_object_as_params_receiver(self, obj, mimetype): |
---|
787 | n/a | m = self._make_message() |
---|
788 | n/a | params = {'foo': 'bár', 'abc': 'xyz'} |
---|
789 | n/a | m.set_content(obj, *mimetype, params=params) |
---|
790 | n/a | if isinstance(obj, str): |
---|
791 | n/a | params['charset'] = 'utf-8' |
---|
792 | n/a | self.assertEqual(m['Content-Type'].params, params) |
---|
793 | n/a | |
---|
794 | n/a | |
---|
795 | n/a | if __name__ == '__main__': |
---|
796 | n/a | unittest.main() |
---|