'If response bytes are delivered to L{HTTP11ClientProtocol} before the
L{Deferred} returned by L{Request.writeTo} fires, those response bytes
are parsed as part of the response.
The connection is also closed, because we\'re in a confusing state, and
therefore the C{quiescentCallback} isn\'t called.'

def test_receiveResponseBeforeRequestGenerationDone(self):

    quiescentResult = []
    transport = StringTransport()
    protocol = HTTP11ClientProtocol(quiescentResult.append)
    protocol.makeConnection(transport)
    request = SlowRequest()
    d = protocol.request(request)
    protocol.dataReceived('HTTP/1.1    200    OK\r\nX-Foo:    bar\r\nContent-Length:    6\r\n\r\nfoobar')
    def cbResponse(response):
        p = AccumulatingProtocol()
        whenFinished = p.closedDeferred = Deferred()
        response.deliverBody(p)
        self.assertEqual(protocol.state, u'TRANSMITTING_AFTER_RECEIVING_RESPONSE')
        self.assertTrue(transport.disconnecting)
        self.assertEqual(quiescentResult, [])
        return whenFinished.addCallback((lambda ign: (response, p.data)))
    d.addCallback(cbResponse)
    def cbAllResponse(result):
        (response, body) = result
        self.assertEqual(response.version, ('HTTP', 1, 1))
        self.assertEqual(response.code, 200)
        self.assertEqual(response.phrase, 'OK')
        self.assertEqual(response.headers, Headers({'x-foo': ['bar']}))
        self.assertEqual(body, 'foobar')
        request.finished.callback(None)
    d.addCallback(cbAllResponse)
    return d
