def test_flagsChangedInsideFetchSpecificResponse(self):
        """
        Any unrequested flag information received along with other requested
        information in an untagged I{FETCH} received in response to a request
        issued with L{IMAP4Client.fetchSpecific} is passed to the
        C{flagsChanged} callback.
        """
        transport = StringTransport()
        c = StillSimplerClient()
        c.makeConnection(transport)
        c.lineReceived(b'* OK [IMAP4rev1]')


        def login():
            d = c.login(b'blah', b'blah')
            c.dataReceived(b'0001 OK LOGIN\r\n')
            return d


        def select():
            d = c.select('inbox')
            c.lineReceived(b'0002 OK SELECT')
            return d


        def fetch():
            d = c.fetchSpecific(b'1:*',
                headerType='HEADER.FIELDS',
                headerArgs=['SUBJECT'])
            # This response includes FLAGS after the requested data.
            c.dataReceived(b'* 1 FETCH (BODY[HEADER.FIELDS ("SUBJECT")] {22}\r\n')
            c.dataReceived(b'Subject: subject one\r\n')
            c.dataReceived(b' FLAGS (\\Recent))\r\n')
            # And this one includes it before!  Either is possible.
            c.dataReceived(b'* 2 FETCH (FLAGS (\\Seen) BODY[HEADER.FIELDS ("SUBJECT")] {22}\r\n')
            c.dataReceived(b'Subject: subject two\r\n')
            c.dataReceived(b')\r\n')
            c.dataReceived(b'0003 OK FETCH completed\r\n')
            return d


        def test(res):
            self.assertEqual(res, {
                1: [['BODY', ['HEADER.FIELDS', ['SUBJECT']],
                     'Subject: subject one\r\n']],
                2: [['BODY', ['HEADER.FIELDS', ['SUBJECT']],
                     'Subject: subject two\r\n']]
            })

            self.assertEqual(c.flags, {1: ['\\Recent'], 2: ['\\Seen']})

        return login(
            ).addCallback(strip(select)
            ).addCallback(strip(fetch)
            ).addCallback(test)

        Any unrequested flag information received along with other requested
        information in an untagged I{FETCH} received in response to a request
        issued with L{IMAP4Client.fetchSpecific} is passed to the
        C{flagsChanged} callback.
        
stackoverflow/venv/lib/python3.6/site-packages/twisted/mail/test/test_imap.py