|
7 | 7 | import email.message |
8 | 8 | import io |
9 | 9 | import unittest |
| 10 | +from unittest.mock import patch |
10 | 11 | from test import support |
11 | 12 | import os |
12 | 13 | import sys |
@@ -89,6 +90,26 @@ def unfakehttp(self): |
89 | 90 | http.client.HTTPConnection = self._connection_class |
90 | 91 |
|
91 | 92 |
|
| 93 | +class FakeFTPMixin(object): |
| 94 | + def fakeftp(self): |
| 95 | + class FakeFtpWrapper(object): |
| 96 | + def __init__(self, user, passwd, host, port, dirs, timeout=None, |
| 97 | + persistent=True): |
| 98 | + pass |
| 99 | + |
| 100 | + def retrfile(self, file, type): |
| 101 | + return io.BytesIO(), 0 |
| 102 | + |
| 103 | + def close(self): |
| 104 | + pass |
| 105 | + |
| 106 | + self._ftpwrapper_class = urllib.request.ftpwrapper |
| 107 | + urllib.request.ftpwrapper = FakeFtpWrapper |
| 108 | + |
| 109 | + def unfakeftp(self): |
| 110 | + urllib.request.ftpwrapper = self._ftpwrapper_class |
| 111 | + |
| 112 | + |
92 | 113 | class urlopen_FileTests(unittest.TestCase): |
93 | 114 | """Test urlopen() opening a temporary file. |
94 | 115 |
|
@@ -195,7 +216,7 @@ def test_getproxies_environment_keep_no_proxies(self): |
195 | 216 | self.env.set('NO_PROXY', 'localhost, anotherdomain.com, newdomain.com') |
196 | 217 | self.assertTrue(urllib.request.proxy_bypass_environment('anotherdomain.com')) |
197 | 218 |
|
198 | | -class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin): |
| 219 | +class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin, FakeFTPMixin): |
199 | 220 | """Test urlopen() opening a fake http connection.""" |
200 | 221 |
|
201 | 222 | def check_read(self, ver): |
@@ -309,6 +330,15 @@ def test_ftp_nonexisting(self): |
309 | 330 | self.assertFalse(e.exception.filename) |
310 | 331 | self.assertTrue(e.exception.reason) |
311 | 332 |
|
| 333 | + @patch.object(urllib.request, 'MAXFTPCACHE', 0) |
| 334 | + def test_ftp_cache_pruning(self): |
| 335 | + self.fakeftp() |
| 336 | + try: |
| 337 | + urllib.request.ftpcache['test'] = urllib.request.ftpwrapper('user', 'pass', 'localhost', 21, []) |
| 338 | + urlopen('ftp://localhost') |
| 339 | + finally: |
| 340 | + self.unfakeftp() |
| 341 | + |
312 | 342 |
|
313 | 343 | def test_userpass_inurl(self): |
314 | 344 | self.fakehttp(b"HTTP/1.0 200 OK\r\n\r\nHello!") |
|
0 commit comments