| 
									
										
										
										
											2012-05-25 18:42:14 -04:00
										 |  |  | import unittest | 
					
						
							|  |  |  | import textwrap | 
					
						
							|  |  |  | import copy | 
					
						
							|  |  |  | import pickle | 
					
						
							| 
									
										
										
										
											2012-05-28 21:09:04 -04:00
										 |  |  | import email | 
					
						
							|  |  |  | import email.message | 
					
						
							| 
									
										
										
										
											2012-05-25 18:42:14 -04:00
										 |  |  | from email import policy | 
					
						
							| 
									
										
										
										
											2012-05-27 15:03:38 -04:00
										 |  |  | from email.headerregistry import HeaderRegistry | 
					
						
							| 
									
										
										
										
											2012-05-31 18:00:45 -04:00
										 |  |  | from test.test_email import TestEmailBase, parameterize | 
					
						
							| 
									
										
										
										
											2012-05-25 18:42:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-31 18:00:45 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | @parameterize | 
					
						
							|  |  |  | class TestPickleCopyHeader(TestEmailBase): | 
					
						
							| 
									
										
										
										
											2012-05-25 18:42:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-28 21:09:04 -04:00
										 |  |  |     header_factory = HeaderRegistry() | 
					
						
							| 
									
										
										
										
											2012-05-25 18:42:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-28 21:09:04 -04:00
										 |  |  |     unstructured = header_factory('subject', 'this is a test') | 
					
						
							| 
									
										
										
										
											2012-05-25 18:42:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-30 21:53:40 -04:00
										 |  |  |     header_params = { | 
					
						
							|  |  |  |         'subject': ('subject', 'this is a test'), | 
					
						
							|  |  |  |         'from':    ('from',    'frodo@mordor.net'), | 
					
						
							|  |  |  |         'to':      ('to',      'a: k@b.com, y@z.com;, j@f.com'), | 
					
						
							|  |  |  |         'date':    ('date',    'Tue, 29 May 2012 09:24:26 +1000'), | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def header_as_deepcopy(self, name, value): | 
					
						
							| 
									
										
										
										
											2012-05-28 21:09:04 -04:00
										 |  |  |         header = self.header_factory(name, value) | 
					
						
							|  |  |  |         h = copy.deepcopy(header) | 
					
						
							|  |  |  |         self.assertEqual(str(h), str(header)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-30 21:53:40 -04:00
										 |  |  |     def header_as_pickle(self, name, value): | 
					
						
							| 
									
										
										
										
											2012-05-28 21:09:04 -04:00
										 |  |  |         header = self.header_factory(name, value) | 
					
						
							| 
									
										
										
										
											2014-12-15 14:03:42 +02:00
										 |  |  |         for proto in range(pickle.HIGHEST_PROTOCOL + 1): | 
					
						
							|  |  |  |             p = pickle.dumps(header, proto) | 
					
						
							|  |  |  |             h = pickle.loads(p) | 
					
						
							|  |  |  |             self.assertEqual(str(h), str(header)) | 
					
						
							| 
									
										
										
										
											2012-05-25 18:42:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-31 18:00:45 -04:00
										 |  |  | @parameterize | 
					
						
							|  |  |  | class TestPickleCopyMessage(TestEmailBase): | 
					
						
							| 
									
										
										
										
											2012-05-25 18:42:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-30 21:53:40 -04:00
										 |  |  |     # Message objects are a sequence, so we have to make them a one-tuple in | 
					
						
							|  |  |  |     # msg_params so they get passed to the parameterized test method as a | 
					
						
							|  |  |  |     # single argument instead of as a list of headers. | 
					
						
							|  |  |  |     msg_params = {} | 
					
						
							| 
									
										
										
										
											2012-05-28 21:09:04 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Note: there will be no custom header objects in the parsed message. | 
					
						
							| 
									
										
										
										
											2012-05-30 21:53:40 -04:00
										 |  |  |     msg_params['parsed'] = (email.message_from_string(textwrap.dedent("""\
 | 
					
						
							| 
									
										
										
										
											2012-05-28 21:09:04 -04:00
										 |  |  |         Date: Tue, 29 May 2012 09:24:26 +1000 | 
					
						
							|  |  |  |         From: frodo@mordor.net | 
					
						
							|  |  |  |         To: bilbo@underhill.org | 
					
						
							|  |  |  |         Subject: help | 
					
						
							| 
									
										
										
										
											2012-05-25 18:42:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-28 21:09:04 -04:00
										 |  |  |         I think I forgot the ring. | 
					
						
							| 
									
										
										
										
											2012-05-30 21:53:40 -04:00
										 |  |  |         """), policy=policy.default),)
 | 
					
						
							| 
									
										
										
										
											2012-05-25 18:42:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-30 21:53:40 -04:00
										 |  |  |     msg_params['created'] = (email.message.Message(policy=policy.default),) | 
					
						
							|  |  |  |     msg_params['created'][0]['Date'] = 'Tue, 29 May 2012 09:24:26 +1000' | 
					
						
							|  |  |  |     msg_params['created'][0]['From'] = 'frodo@mordor.net' | 
					
						
							|  |  |  |     msg_params['created'][0]['To'] = 'bilbo@underhill.org' | 
					
						
							|  |  |  |     msg_params['created'][0]['Subject'] = 'help' | 
					
						
							|  |  |  |     msg_params['created'][0].set_payload('I think I forgot the ring.') | 
					
						
							| 
									
										
										
										
											2012-05-25 18:42:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-30 21:53:40 -04:00
										 |  |  |     def msg_as_deepcopy(self, msg): | 
					
						
							| 
									
										
										
										
											2012-05-28 21:09:04 -04:00
										 |  |  |         msg2 = copy.deepcopy(msg) | 
					
						
							|  |  |  |         self.assertEqual(msg2.as_string(), msg.as_string()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-30 21:53:40 -04:00
										 |  |  |     def msg_as_pickle(self, msg): | 
					
						
							| 
									
										
										
										
											2014-12-15 14:03:42 +02:00
										 |  |  |         for proto in range(pickle.HIGHEST_PROTOCOL + 1): | 
					
						
							|  |  |  |             p = pickle.dumps(msg, proto) | 
					
						
							|  |  |  |             msg2 = pickle.loads(p) | 
					
						
							|  |  |  |             self.assertEqual(msg2.as_string(), msg.as_string()) | 
					
						
							| 
									
										
										
										
											2012-05-28 21:09:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-25 18:42:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     unittest.main() |