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