def test_withPotentialDataLoss(self):
"""
If the full body of the L{IResponse} passed to L{client.readBody} is
not definitely received, the L{Deferred} returned by L{client.readBody}
fires with a L{Failure} wrapping L{client.PartialDownloadError} with
the content that was received.
"""
response = DummyResponse()
d = client.readBody(response)
response.protocol.dataReceived(b"first")
response.protocol.dataReceived(b"second")
response.protocol.connectionLost(Failure(PotentialDataLoss()))
failure = self.failureResultOf(d)
failure.trap(client.PartialDownloadError)
self.assertEqual({
"status": failure.value.status,
"message": failure.value.message,
"body": failure.value.response,
}, {
"status": b"200",
"message": b"OK",
"body": b"firstsecond",
})
python类PartialDownloadError()的实例源码
def test_downloadPageBrokenDownload(self):
"""
If the connection is closed before the number of bytes indicated by
I{Content-Length} have been received, the L{Deferred} returned by
L{downloadPage} fails with L{PartialDownloadError}.
"""
# test what happens when download gets disconnected in the middle
path = FilePath(self.mktemp())
d = client.downloadPage(self.getURL("broken"), path.path)
d = self.assertFailure(d, client.PartialDownloadError)
def checkResponse(response):
"""
The HTTP status code from the server is propagated through the
C{PartialDownloadError}.
"""
self.assertEqual(response.status, b"200")
self.assertEqual(response.message, b"OK")
return response
d.addCallback(checkResponse)
def cbFailed(ignored):
self.assertEqual(path.getContent(), b"abc")
d.addCallback(cbFailed)
return d
def test_downloadPageLogsFileCloseError(self):
"""
If there is an exception closing the file being written to after the
connection is prematurely closed, that exception is logged.
"""
class BrokenFile:
def write(self, bytes):
pass
def close(self):
raise IOError(ENOSPC, "No file left on device")
d = client.downloadPage(self.getURL("broken"), BrokenFile())
d = self.assertFailure(d, client.PartialDownloadError)
def cbFailed(ignored):
self.assertEqual(len(self.flushLoggedErrors(IOError)), 1)
d.addCallback(cbFailed)
return d
def testBrokenDownload(self):
# test what happens when download gets disconnected in the middle
d = client.getPage(self.getURL("broken"))
d = self.assertFailure(d, client.PartialDownloadError)
d.addCallback(lambda exc: self.assertEquals(exc.response, "abc"))
return d
def testBrokenDownload(self):
# test what happens when download gets disconnected in the middle
d = client.getPage(self.getURL("broken"))
d = self.assertFailure(d, client.PartialDownloadError)
d.addCallback(lambda exc: self.assertEquals(exc.response, "abc"))
return d
def test_getPageBrokenDownload(self):
"""
If the connection is closed before the number of bytes indicated by
I{Content-Length} have been received, the L{Deferred} returned by
L{getPage} fails with L{PartialDownloadError}.
"""
d = client.getPage(self.getURL("broken"))
d = self.assertFailure(d, client.PartialDownloadError)
d.addCallback(lambda exc: self.assertEqual(exc.response, b"abc"))
return d
def _processResponseBodyFail(self, failure, request, response):
if failure.check(PartialDownloadError):
return failure.value.response
failure_string = handleAllFailures(failure)
HTTPTest.addToReport(self, request, response,
failure_string=failure_string)
return response
def _onRequestBodyError(self, failure):
if self._canceled:
Log.d("Auth Request canceled")
return
if isinstance(failure.value, PartialDownloadError):
self._onRequestBodyReady(failure.value.response)
else:
self._onError(self.ERROR_AUTH_REQUEST, str(failure))
def _onCredentialsPollBodyError(self, failure):
if self._canceled:
Log.d("Auth Request canceled")
return
if isinstance(failure.value, PartialDownloadError):
self._onCredentialsPollBodyReady(failure.value.response)
else:
self._onError(self.ERROR_CREDENTIALS_REQUEST_PARSE, str(failure))