fix(test): avoid impact of system proxies on urllib test suite by mocking relevant functions

Signed-off-by: Frost Ming <me@frostming.com>
This commit is contained in:
Frost Ming 2025-10-15 10:56:27 +08:00
parent 9a87ce8b57
commit 4669c445cb
No known key found for this signature in database
GPG key ID: 5BFA9CB4DDA943BF
3 changed files with 18 additions and 5 deletions

View file

@ -7,6 +7,7 @@
import email.message
import io
import unittest
import unittest.mock
from test import support
from test.support import os_helper
from test.support import socket_helper
@ -86,9 +87,15 @@ def fakehttp(self, fakedata, mock_close=False):
fake_http_class = fakehttp(fakedata, mock_close=mock_close)
self._connection_class = http.client.HTTPConnection
http.client.HTTPConnection = fake_http_class
# Disable proxies during the test
self.getproxies = unittest.mock.patch.object(urllib.request, 'getproxies', return_value={})
self.getproxies.start()
# Clear cached opener
urllib.request.install_opener(None)
def unfakehttp(self):
http.client.HTTPConnection = self._connection_class
self.getproxies.stop()
class urlopen_FileTests(unittest.TestCase):