'Verify that when too many bytes have been written by a body producer
and then the body producer\'s C{startProducing} L{Deferred} fires that
the producer is unregistered from the transport and that the
L{Deferred} returned from L{Request.writeTo} is fired with a L{Failure}
wrapping a L{WrongBodyLength}.
@param finisher: A callable which will be invoked with the body
producer after too many bytes have been written to the transport.
It should fire the startProducing Deferred somehow.'

def _sendRequestBodyWithTooManyBytesTest(self, finisher):

    producer = StringProducer(3)
    request = Request('POST', '/bar', _boringHeaders, producer)
    writeDeferred = request.writeTo(self.transport)
    producer.consumer.write('ab')
    self.assertFalse(producer.stopped)
    producer.consumer.write('cd')
    self.assertTrue(producer.stopped)
    self.assertIdentical(self.transport.producer, None)
    def cbFailed(exc):
        self.assertEqual(self.transport.value(), 'POST    /bar    HTTP/1.1\r\nConnection:    close\r\nContent-Length:    3\r\nHost:    example.com\r\n\r\nab')
        self.transport.clear()
        self.assertRaises(ExcessWrite, producer.consumer.write, 'ef')
        finisher(producer)
        self.assertEqual(self.transport.value(), '')
    d = self.assertFailure(writeDeferred, WrongBodyLength)
    d.addCallback(cbFailed)
    return d
