(self)
| 734 | class HandlerTests(unittest.TestCase): |
| 735 | |
| 736 | def test_ftp(self): |
| 737 | class MockFTPWrapper: |
| 738 | def __init__(self, data): |
| 739 | self.data = data |
| 740 | |
| 741 | def retrfile(self, filename, filetype): |
| 742 | self.filename, self.filetype = filename, filetype |
| 743 | return io.StringIO(self.data), len(self.data) |
| 744 | |
| 745 | def close(self): |
| 746 | pass |
| 747 | |
| 748 | class NullFTPHandler(urllib.request.FTPHandler): |
| 749 | def __init__(self, data): |
| 750 | self.data = data |
| 751 | |
| 752 | def connect_ftp(self, user, passwd, host, port, dirs, |
| 753 | timeout=socket._GLOBAL_DEFAULT_TIMEOUT): |
| 754 | self.user, self.passwd = user, passwd |
| 755 | self.host, self.port = host, port |
| 756 | self.dirs = dirs |
| 757 | self.ftpwrapper = MockFTPWrapper(self.data) |
| 758 | return self.ftpwrapper |
| 759 | |
| 760 | data = "rheum rhaponicum" |
| 761 | h = NullFTPHandler(data) |
| 762 | h.parent = MockOpener() |
| 763 | |
| 764 | for url, host, port, user, passwd, type_, dirs, filename, mimetype in [ |
| 765 | ("ftp://localhost/foo/bar/baz.html", |
| 766 | "localhost", ftplib.FTP_PORT, "", "", "I", |
| 767 | ["foo", "bar"], "baz.html", "text/html"), |
| 768 | ("ftp://parrot@localhost/foo/bar/baz.html", |
| 769 | "localhost", ftplib.FTP_PORT, "parrot", "", "I", |
| 770 | ["foo", "bar"], "baz.html", "text/html"), |
| 771 | ("ftp://%25parrot@localhost/foo/bar/baz.html", |
| 772 | "localhost", ftplib.FTP_PORT, "%parrot", "", "I", |
| 773 | ["foo", "bar"], "baz.html", "text/html"), |
| 774 | ("ftp://%2542parrot@localhost/foo/bar/baz.html", |
| 775 | "localhost", ftplib.FTP_PORT, "%42parrot", "", "I", |
| 776 | ["foo", "bar"], "baz.html", "text/html"), |
| 777 | ("ftp://localhost:80/foo/bar/", |
| 778 | "localhost", 80, "", "", "D", |
| 779 | ["foo", "bar"], "", None), |
| 780 | ("ftp://localhost/baz.gif;type=a", |
| 781 | "localhost", ftplib.FTP_PORT, "", "", "A", |
| 782 | [], "baz.gif", "image/gif"), |
| 783 | ]: |
| 784 | req = Request(url) |
| 785 | req.timeout = None |
| 786 | r = h.ftp_open(req) |
| 787 | # ftp authentication not yet implemented by FTPHandler |
| 788 | self.assertEqual(h.user, user) |
| 789 | self.assertEqual(h.passwd, passwd) |
| 790 | self.assertEqual(h.host, socket.gethostbyname(host)) |
| 791 | self.assertEqual(h.port, port) |
| 792 | self.assertEqual(h.dirs, dirs) |
| 793 | self.assertEqual(h.ftpwrapper.filename, filename) |
nothing calls this directly
no test coverage detected