MCPcopy Index your code
hub / github.com/python/cpython / test_file

Method test_file

Lib/test/test_urllib2.py:822–915  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

820 self.fail("Did not raise ftplib exception")
821
822 def test_file(self):
823 import email.utils
824 h = urllib.request.FileHandler()
825 o = h.parent = MockOpener()
826
827 TESTFN = os_helper.TESTFN
828 towrite = b"hello, world\n"
829 canonurl = urllib.request.pathname2url(os.path.abspath(TESTFN), add_scheme=True)
830 parsed = urlsplit(canonurl)
831 if parsed.netloc:
832 raise unittest.SkipTest("non-local working directory")
833 urls = [
834 canonurl,
835 parsed._replace(netloc='localhost').geturl(),
836 parsed._replace(netloc=socket.gethostbyname('localhost')).geturl(),
837 ]
838 try:
839 localaddr = socket.gethostbyname(socket.gethostname())
840 except socket.gaierror:
841 localaddr = ''
842 if localaddr:
843 urls.append(parsed._replace(netloc=localaddr).geturl())
844
845 for url in urls:
846 f = open(TESTFN, "wb")
847 try:
848 try:
849 f.write(towrite)
850 finally:
851 f.close()
852
853 r = h.file_open(Request(url))
854 try:
855 data = r.read()
856 headers = r.info()
857 respurl = r.geturl()
858 finally:
859 r.close()
860 stats = os.stat(TESTFN)
861 modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
862 finally:
863 os.remove(TESTFN)
864 self.assertEqual(data, towrite)
865 self.assertEqual(headers["Content-type"], "text/plain")
866 self.assertEqual(headers["Content-length"], "13")
867 self.assertEqual(headers["Last-modified"], modified)
868 self.assertEqual(respurl, canonurl)
869
870 for url in [
871 parsed._replace(netloc='localhost:80').geturl(),
872 "file:///file_does_not_exist.txt",
873 "file://not-a-local-host.com//dir/file.txt",
874 "file://%s:80%s/%s" % (socket.gethostbyname('localhost'),
875 os.getcwd(), TESTFN),
876 "file://somerandomhost.ontheinternet.com%s/%s" %
877 (os.getcwd(), TESTFN),
878 ]:
879 try:

Callers

nothing calls this directly

Calls 15

urlsplitFunction · 0.90
RequestClass · 0.90
MockOpenerClass · 0.85
assertFalseMethod · 0.80
openFunction · 0.50
abspathMethod · 0.45
geturlMethod · 0.45
_replaceMethod · 0.45
appendMethod · 0.45
writeMethod · 0.45
closeMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected