Files
videoreg.ru/scripts/rebuild-v2/check-fetch-rate-limit.py
T
2026-10-26 19:08:48 +03:00

22 lines
1.2 KiB
Python

"""A simulated429 must persist Retry-After and prevent subsequent network calls."""
import tempfile,unittest
from pathlib import Path
from unittest.mock import patch
from types import SimpleNamespace
import fetch
class RateLimit(unittest.TestCase):
def test_stop_all_following_wayback_fetches(self):
with tempfile.TemporaryDirectory() as directory:
root=Path(directory);raw=root/'raw';raw.mkdir()
def respond(cmd,**kwargs):
Path(cmd[cmd.index('-D')+1]).write_text('HTTP/2 429\nRetry-After: 120\n')
Path(cmd[cmd.index('-o')+1]).write_text('Too Many Requests')
return SimpleNamespace(stdout='429\nhttps://web.archive.org/web/test',stderr='',returncode=0)
with patch.object(fetch,'ROOT',root),patch.object(fetch,'RAW',raw),patch.object(fetch.subprocess,'run',side_effect=respond) as network:
result=fetch.fetch('https://web.archive.org/web/test','first.html')
self.assertEqual(result['httpStatus'],'429')
self.assertIn('120',(raw/'wayback-rate-limit.json').read_text())
with self.assertRaisesRegex(RuntimeError,'stop marker'):fetch.fetch('https://web.archive.org/cdx/search/cdx','second.json')
self.assertEqual(network.call_count,1)
if __name__=='__main__':unittest.main()