'If the C{flush} call in C{connectionLost} fails, the C{zlib.error}
exception is caught and turned into a L{ResponseFailed}.'

def test_flushError(self):

    class decompressobj(object, ):
        def __init__(self, wbits):
            pass
        def decompress(self, data):
            return 'x'
        def flush(self):
            raise zlib.error()
    oldDecompressObj = zlib.decompressobj
    zlib.decompressobj = decompressobj
    self.addCleanup(setattr, zlib, 'decompressobj', oldDecompressObj)
    deferred = self.agent.request('GET', 'http://example.com/foo')
    (req, res) = self.protocol.requests.pop()
    headers = http_headers.Headers({'content-encoding': ['gzip']})
    transport = StringTransport()
    response = Response(('HTTP', 1, 1), 200, 'OK', headers, transport)
    res.callback(response)
    def checkResponse(result):
        response._bodyDataReceived('data')
        response._bodyDataFinished()
        protocol = SimpleAgentProtocol()
        result.deliverBody(protocol)
        self.assertEqual(protocol.received, ['x', 'y'])
        return defer.gatherResults([protocol.made, protocol.finished])
    deferred.addCallback(checkResponse)
    self.assertFailure(deferred, client.ResponseFailed)
    def checkFailure(error):
        error.reasons[1].trap(zlib.error)
        self.assertIsInstance(error.response, Response)
    return deferred.addCallback(checkFailure)
