(self)
| 803 | self.fail("Did not raise ftplib exception") |
| 804 | |
| 805 | def test_file(self): |
| 806 | import email.utils |
| 807 | h = urllib.request.FileHandler() |
| 808 | o = h.parent = MockOpener() |
| 809 | |
| 810 | TESTFN = os_helper.TESTFN |
| 811 | towrite = b"hello, world\n" |
| 812 | canonurl = urllib.request.pathname2url(os.path.abspath(TESTFN), add_scheme=True) |
| 813 | parsed = urlsplit(canonurl) |
| 814 | if parsed.netloc: |
| 815 | raise unittest.SkipTest("non-local working directory") |
| 816 | urls = [ |
| 817 | canonurl, |
| 818 | parsed._replace(netloc='localhost').geturl(), |
| 819 | parsed._replace(netloc=socket.gethostbyname('localhost')).geturl(), |
| 820 | ] |
| 821 | try: |
| 822 | localaddr = socket.gethostbyname(socket.gethostname()) |
| 823 | except socket.gaierror: |
| 824 | localaddr = '' |
| 825 | if localaddr: |
| 826 | urls.append(parsed._replace(netloc=localaddr).geturl()) |
| 827 | |
| 828 | for url in urls: |
| 829 | f = open(TESTFN, "wb") |
| 830 | try: |
| 831 | try: |
| 832 | f.write(towrite) |
| 833 | finally: |
| 834 | f.close() |
| 835 | |
| 836 | r = h.file_open(Request(url)) |
| 837 | try: |
| 838 | data = r.read() |
| 839 | headers = r.info() |
| 840 | respurl = r.geturl() |
| 841 | finally: |
| 842 | r.close() |
| 843 | stats = os.stat(TESTFN) |
| 844 | modified = email.utils.formatdate(stats.st_mtime, usegmt=True) |
| 845 | finally: |
| 846 | os.remove(TESTFN) |
| 847 | self.assertEqual(data, towrite) |
| 848 | self.assertEqual(headers["Content-type"], "text/plain") |
| 849 | self.assertEqual(headers["Content-length"], "13") |
| 850 | self.assertEqual(headers["Last-modified"], modified) |
| 851 | self.assertEqual(respurl, canonurl) |
| 852 | |
| 853 | for url in [ |
| 854 | parsed._replace(netloc='localhost:80').geturl(), |
| 855 | "file:///file_does_not_exist.txt", |
| 856 | "file://not-a-local-host.com//dir/file.txt", |
| 857 | "file://%s:80%s/%s" % (socket.gethostbyname('localhost'), |
| 858 | os.getcwd(), TESTFN), |
| 859 | "file://somerandomhost.ontheinternet.com%s/%s" % |
| 860 | (os.getcwd(), TESTFN), |
| 861 | ]: |
| 862 | try: |
nothing calls this directly
no test coverage detected