Martin v. Löwis | a6026c6 | 2002-09-22 09:01:08 +0000 | [diff] [blame] | 1 | import mimetools |
| 2 | import multifile |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 3 | import io |
Martin v. Löwis | a6026c6 | 2002-09-22 09:01:08 +0000 | [diff] [blame] | 4 | |
| 5 | msg = """Mime-Version: 1.0 |
| 6 | Content-Type: multipart/mixed; |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 7 | boundary="=====================_590453667==_" |
Martin v. Löwis | a6026c6 | 2002-09-22 09:01:08 +0000 | [diff] [blame] | 8 | X-OriginalArrivalTime: 05 Feb 2002 03:43:23.0310 (UTC) FILETIME=[42D88CE0:01C1ADF7] |
| 9 | |
| 10 | --=====================_590453667==_ |
| 11 | Content-Type: multipart/alternative; |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 12 | boundary="=====================_590453677==_.ALT" |
Martin v. Löwis | a6026c6 | 2002-09-22 09:01:08 +0000 | [diff] [blame] | 13 | |
| 14 | --=====================_590453677==_.ALT |
| 15 | Content-Type: text/plain; charset="us-ascii"; format=flowed |
| 16 | |
| 17 | test A |
| 18 | --=====================_590453677==_.ALT |
| 19 | Content-Type: text/html; charset="us-ascii" |
| 20 | |
| 21 | <html> |
| 22 | <b>test B</font></b></html> |
| 23 | |
| 24 | --=====================_590453677==_.ALT-- |
| 25 | |
| 26 | --=====================_590453667==_ |
| 27 | Content-Type: text/plain; charset="us-ascii" |
| 28 | Content-Disposition: attachment; filename="att.txt" |
| 29 | |
| 30 | Attached Content. |
| 31 | Attached Content. |
| 32 | Attached Content. |
| 33 | Attached Content. |
| 34 | |
| 35 | --=====================_590453667==_-- |
| 36 | |
| 37 | """ |
| 38 | |
Martin v. Löwis | a6026c6 | 2002-09-22 09:01:08 +0000 | [diff] [blame] | 39 | def getMIMEMsg(mf): |
| 40 | global boundaries, linecount |
| 41 | msg = mimetools.Message(mf) |
| 42 | |
| 43 | #print "TYPE: %s" % msg.gettype() |
| 44 | if msg.getmaintype() == 'multipart': |
| 45 | boundary = msg.getparam("boundary") |
| 46 | boundaries += 1 |
| 47 | |
| 48 | mf.push(boundary) |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 49 | while mf.next(): |
| 50 | getMIMEMsg(mf) |
Martin v. Löwis | a6026c6 | 2002-09-22 09:01:08 +0000 | [diff] [blame] | 51 | mf.pop() |
| 52 | else: |
| 53 | lines = mf.readlines() |
| 54 | linecount += len(lines) |
| 55 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 56 | def test_main(): |
Michael W. Hudson | 5bf2516 | 2004-08-03 11:14:09 +0000 | [diff] [blame] | 57 | global boundaries, linecount |
| 58 | boundaries = 0 |
| 59 | linecount = 0 |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 60 | f = io.StringIO(msg) |
Martin v. Löwis | a6026c6 | 2002-09-22 09:01:08 +0000 | [diff] [blame] | 61 | getMIMEMsg(multifile.MultiFile(f)) |
| 62 | assert boundaries == 2 |
| 63 | assert linecount == 9 |
| 64 | |
| 65 | if __name__ == '__main__': |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 66 | test_main() |