net: download_using_mirrors() to download like fdroidclient does

This commit is contained in:
Hans-Christoph Steiner 2024-02-26 18:13:53 +01:00
parent 49dcc53076
commit 2e3f6d273a
2 changed files with 106 additions and 3 deletions

View file

@ -121,6 +121,28 @@ class NetTest(unittest.TestCase):
net.download_file('http://localhost:%d/f.txt' % server.port)
server.stop()
def test_download_using_mirrors_retries(self):
server = RetryServer()
f = net.download_using_mirrors(
[
'https://fake.com/f.txt', # 404 or 301 Redirect
'https://httpbin.org/status/403',
'https://httpbin.org/status/500',
'http://localhost:1/f.txt', # ConnectionError
'http://localhost:%d/' % server.port,
],
)
# strip the HTTP headers and compare the reply
self.assertEqual(server.reply.split(b'\n\n')[1], Path(f).read_bytes())
server.stop()
def test_download_using_mirrors_retries_not_forever(self):
"""The retry logic should eventually exit with an error."""
server = RetryServer(failures=5)
with self.assertRaises(requests.exceptions.ConnectionError):
net.download_using_mirrors(['http://localhost:%d/' % server.port])
server.stop()
if __name__ == "__main__":
os.chdir(os.path.dirname(__file__))