]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Correctly handle nested BODYSTRUCTURE responses
authorMenno Smits <menno@freshfoo.com>
Wed, 23 Apr 2014 12:50:17 +0000 (00:50 +1200)
committerMenno Smits <menno@freshfoo.com>
Wed, 23 Apr 2014 12:50:17 +0000 (00:50 +1200)
Nested BodyData elements are now generated for each subpart. This
means that when multipart sections exist in other multipart sections,
a list of subparts is now (correctly) generated just like what was
already done for the outmost parts.

Thanks to Brandon Rhodes for reporting this bug.

imapclient/response_parser.py
imapclient/test/test_response_parser.py

index a3294213da57d9976568221bcb209eedf63c669d..eb5288c9770899abdfc4b69dc1f8ef5e858576e3 100644 (file)
@@ -138,7 +138,7 @@ class BodyData(tuple):
             for i, part in enumerate(response):
                 if isinstance(part, six.string_types):
                     break
-            return cls((list(response[:i]),) + response[i:])
+            return cls(([cls.create(part) for part in response[:i]],) + response[i:])
         else:
             return cls(response)
             
index c8444929b83282e602d8ddcdaa3307c34e1774a5..1bfce08331804d7f365d714bd7d472b8cb6c8d1a 100644 (file)
@@ -258,10 +258,11 @@ class TestParseFetchResponse(unittest.TestCase):
     def test_BODY(self):
          self.check_BODYish_single_part('BODY')
          self.check_BODYish_multipart('BODY')
+         self.check_BODYish_nested_multipart('BODY')
 
     def test_BODYSTRUCTURE(self):
          self.check_BODYish_single_part('BODYSTRUCTURE')
-         self.check_BODYish_multipart('BODYSTRUCTURE')
+         self.check_BODYish_nested_multipart('BODYSTRUCTURE')
     
     def check_BODYish_single_part(self, respType):
         text =  '123 (UID 317 %s ("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "7BIT" 16 1))' % respType
@@ -283,6 +284,36 @@ class TestParseFetchResponse(unittest.TestCase):
                                         })
         self.assertTrue(parsed[269][respType].is_multipart)
 
+    def check_BODYish_nested_multipart(self, respType):
+        text = '1 (%s (' \
+               '(' \
+                 '("text" "html" ("charset" "utf-8") NIL NIL "7bit" 97 3 NIL NIL NIL NIL)' \
+                 '("text" "plain" ("charset" "utf-8") NIL NIL "7bit" 62 3 NIL NIL NIL NIL)' \
+                 '"alternative" ("boundary" "===============8211050864078048428==") NIL NIL NIL' \
+               ')' \
+               '("text" "plain" ("charset" "utf-8") NIL NIL "7bit" 16 1 NIL ("attachment" ("filename" "attachment.txt")) NIL NIL) ' \
+               '"mixed" ("boundary" "===============0373402508605428821==") NIL NIL NIL))' \
+               % respType
+
+        parsed = parse_fetch_response([text])
+        self.assertEqual(parsed, {1: {
+            respType: (
+                [
+                    (
+                        [
+                            ('text', 'html', ('charset', 'utf-8'), None, None, '7bit', 97, 3, None, None, None, None),
+                            ('text', 'plain', ('charset', 'utf-8'), None, None, '7bit', 62, 3, None, None, None, None)
+                        ], 'alternative', ('boundary', '===============8211050864078048428=='), None, None, None
+                    ),
+                    ('text', 'plain', ('charset', 'utf-8'), None, None, '7bit', 16, 1, None, ('attachment', ('filename', 'attachment.txt')), None, None)
+                ], 'mixed', ('boundary', '===============0373402508605428821=='), None, None, None,
+            ),
+            'SEQ': 1,
+        }})
+        self.assertTrue(parsed[1][respType].is_multipart)
+        self.assertTrue(parsed[1][respType][0][0].is_multipart)
+        self.assertFalse(parsed[1][respType][0][0][0][0].is_multipart)
+
     def test_partial_fetch(self):
         body = '01234567890123456789'
         self.assertEqual(parse_fetch_response(