test_urllib2: reenable the rest of the tests
This commit is contained in:
@@ -586,104 +586,94 @@ class HandlerTests(unittest.TestCase):
|
|||||||
self.assertEqual(headers.get("Content-type"), mimetype)
|
self.assertEqual(headers.get("Content-type"), mimetype)
|
||||||
self.assertEqual(int(headers["Content-length"]), len(data))
|
self.assertEqual(int(headers["Content-length"]), len(data))
|
||||||
|
|
||||||
# def test_file(self):
|
def test_file(self):
|
||||||
# import time, rfc822, socket
|
import rfc822
|
||||||
# import sys
|
h = urllib2.FileHandler()
|
||||||
# h = urllib2.FileHandler()
|
o = h.parent = MockOpener()
|
||||||
# o = h.parent = MockOpener()
|
|
||||||
|
|
||||||
# sys.stderr.write('--------1-----------\n')
|
TESTFN = test_support.TESTFN
|
||||||
|
urlpath = sanepathname2url(os.path.abspath(TESTFN))
|
||||||
|
towrite = "hello, world\n"
|
||||||
|
urls = [
|
||||||
|
"file://localhost%s" % urlpath,
|
||||||
|
"file://%s" % urlpath,
|
||||||
|
"file://%s%s" % (socket.gethostbyname('localhost'), urlpath),
|
||||||
|
]
|
||||||
|
try:
|
||||||
|
localaddr = socket.gethostbyname(socket.gethostname())
|
||||||
|
except socket.gaierror:
|
||||||
|
localaddr = ''
|
||||||
|
if localaddr:
|
||||||
|
urls.append("file://%s%s" % (localaddr, urlpath))
|
||||||
|
|
||||||
# TESTFN = test_support.TESTFN
|
for url in urls:
|
||||||
# urlpath = sanepathname2url(os.path.abspath(TESTFN))
|
f = open(TESTFN, "wb")
|
||||||
# towrite = "hello, world\n"
|
try:
|
||||||
# urls = [
|
try:
|
||||||
# "file://localhost%s" % urlpath,
|
f.write(towrite)
|
||||||
# "file://%s" % urlpath,
|
finally:
|
||||||
# "file://%s%s" % (socket.gethostbyname('localhost'), urlpath),
|
f.close()
|
||||||
# ]
|
|
||||||
# sys.stderr.write('--------1-----------\n')
|
|
||||||
# try:
|
|
||||||
# localaddr = socket.gethostbyname(socket.gethostname())
|
|
||||||
# except socket.gaierror:
|
|
||||||
# localaddr = ''
|
|
||||||
# if localaddr:
|
|
||||||
# urls.append("file://%s%s" % (localaddr, urlpath))
|
|
||||||
|
|
||||||
# sys.stderr.write('--------1-----------\n')
|
r = h.file_open(Request(url))
|
||||||
# for url in urls:
|
try:
|
||||||
# f = open(TESTFN, "wb")
|
data = r.read()
|
||||||
# try:
|
headers = r.info()
|
||||||
# try:
|
newurl = r.geturl()
|
||||||
# f.write(towrite)
|
finally:
|
||||||
# finally:
|
r.close()
|
||||||
# f.close()
|
stats = os.stat(TESTFN)
|
||||||
|
modified = rfc822.formatdate(stats.st_mtime)
|
||||||
|
finally:
|
||||||
|
os.remove(TESTFN)
|
||||||
|
self.assertEqual(data, towrite)
|
||||||
|
self.assertEqual(headers["Content-type"], "text/plain")
|
||||||
|
self.assertEqual(headers["Content-length"], "13")
|
||||||
|
self.assertEqual(headers["Last-modified"], modified)
|
||||||
|
|
||||||
# sys.stderr.write(`url`)
|
for url in [
|
||||||
# sys.stderr.write('\n')
|
"file://localhost:80%s" % urlpath,
|
||||||
|
# XXXX bug: these fail with socket.gaierror, should be URLError
|
||||||
# r = h.file_open(Request(url))
|
## "file://%s:80%s/%s" % (socket.gethostbyname('localhost'),
|
||||||
# try:
|
## os.getcwd(), TESTFN),
|
||||||
# data = r.read()
|
## "file://somerandomhost.ontheinternet.com%s/%s" %
|
||||||
# headers = r.info()
|
## (os.getcwd(), TESTFN),
|
||||||
# newurl = r.geturl()
|
]:
|
||||||
# finally:
|
try:
|
||||||
# r.close()
|
f = open(TESTFN, "wb")
|
||||||
# stats = os.stat(TESTFN)
|
try:
|
||||||
# modified = rfc822.formatdate(stats.st_mtime)
|
f.write(towrite)
|
||||||
# finally:
|
finally:
|
||||||
# os.remove(TESTFN)
|
f.close()
|
||||||
# self.assertEqual(data, towrite)
|
|
||||||
# self.assertEqual(headers["Content-type"], "text/plain")
|
|
||||||
# self.assertEqual(headers["Content-length"], "13")
|
|
||||||
# self.assertEqual(headers["Last-modified"], modified)
|
|
||||||
|
|
||||||
# sys.stderr.write('--------1-----------\n')
|
self.assertRaises(urllib2.URLError,
|
||||||
# for url in [
|
h.file_open, Request(url))
|
||||||
# "file://localhost:80%s" % urlpath,
|
finally:
|
||||||
# # XXXX bug: these fail with socket.gaierror, should be URLError
|
os.remove(TESTFN)
|
||||||
# ## "file://%s:80%s/%s" % (socket.gethostbyname('localhost'),
|
|
||||||
# ## os.getcwd(), TESTFN),
|
|
||||||
# ## "file://somerandomhost.ontheinternet.com%s/%s" %
|
|
||||||
# ## (os.getcwd(), TESTFN),
|
|
||||||
# ]:
|
|
||||||
# try:
|
|
||||||
# f = open(TESTFN, "wb")
|
|
||||||
# try:
|
|
||||||
# f.write(towrite)
|
|
||||||
# finally:
|
|
||||||
# f.close()
|
|
||||||
|
|
||||||
# self.assertRaises(urllib2.URLError,
|
h = urllib2.FileHandler()
|
||||||
# h.file_open, Request(url))
|
o = h.parent = MockOpener()
|
||||||
# finally:
|
# XXXX why does // mean ftp (and /// mean not ftp!), and where
|
||||||
# os.remove(TESTFN)
|
# is file: scheme specified? I think this is really a bug, and
|
||||||
|
# what was intended was to distinguish between URLs like:
|
||||||
# sys.stderr.write('--------1-----------\n')
|
# file:/blah.txt (a file)
|
||||||
# h = urllib2.FileHandler()
|
# file://localhost/blah.txt (a file)
|
||||||
# o = h.parent = MockOpener()
|
# file:///blah.txt (a file)
|
||||||
# # XXXX why does // mean ftp (and /// mean not ftp!), and where
|
# file://ftp.example.com/blah.txt (an ftp URL)
|
||||||
# # is file: scheme specified? I think this is really a bug, and
|
for url, ftp in [
|
||||||
# # what was intended was to distinguish between URLs like:
|
("file://ftp.example.com//foo.txt", True),
|
||||||
# # file:/blah.txt (a file)
|
("file://ftp.example.com///foo.txt", False),
|
||||||
# # file://localhost/blah.txt (a file)
|
# XXXX bug: fails with OSError, should be URLError
|
||||||
# # file:///blah.txt (a file)
|
("file://ftp.example.com/foo.txt", False),
|
||||||
# # file://ftp.example.com/blah.txt (an ftp URL)
|
]:
|
||||||
# for url, ftp in [
|
req = Request(url)
|
||||||
# ("file://ftp.example.com//foo.txt", True),
|
try:
|
||||||
# ("file://ftp.example.com///foo.txt", False),
|
h.file_open(req)
|
||||||
# # XXXX bug: fails with OSError, should be URLError
|
# XXXX remove OSError when bug fixed
|
||||||
# ("file://ftp.example.com/foo.txt", False),
|
except (urllib2.URLError, OSError):
|
||||||
# ]:
|
self.assert_(not ftp)
|
||||||
# req = Request(url)
|
else:
|
||||||
# try:
|
self.assert_(o.req is req)
|
||||||
# h.file_open(req)
|
self.assertEqual(req.type, "ftp")
|
||||||
# # XXXX remove OSError when bug fixed
|
|
||||||
# except (urllib2.URLError, OSError):
|
|
||||||
# self.assert_(not ftp)
|
|
||||||
# else:
|
|
||||||
# self.assert_(o.req is req)
|
|
||||||
# self.assertEqual(req.type, "ftp")
|
|
||||||
|
|
||||||
def test_http(self):
|
def test_http(self):
|
||||||
class MockHTTPResponse:
|
class MockHTTPResponse:
|
||||||
@@ -867,22 +857,22 @@ class HandlerTests(unittest.TestCase):
|
|||||||
self.assertEqual(count,
|
self.assertEqual(count,
|
||||||
urllib2.HTTPRedirectHandler.max_redirections)
|
urllib2.HTTPRedirectHandler.max_redirections)
|
||||||
|
|
||||||
# don't want to add test_cookielib here
|
# don't want to add test_cookielib here
|
||||||
# def test_cookie_redirect(self):
|
def dont_test_cookie_redirect(self):
|
||||||
# # cookies shouldn't leak into redirected requests
|
# cookies shouldn't leak into redirected requests
|
||||||
# from cookielib import CookieJar
|
from cookielib import CookieJar
|
||||||
|
|
||||||
# from test.test_cookielib import interact_netscape
|
from test.test_cookielib import interact_netscape
|
||||||
|
|
||||||
# cj = CookieJar()
|
cj = CookieJar()
|
||||||
# interact_netscape(cj, "http://www.example.com/", "spam=eggs")
|
interact_netscape(cj, "http://www.example.com/", "spam=eggs")
|
||||||
# hh = MockHTTPHandler(302, "Location: http://www.cracker.com/\r\n\r\n")
|
hh = MockHTTPHandler(302, "Location: http://www.cracker.com/\r\n\r\n")
|
||||||
# hdeh = urllib2.HTTPDefaultErrorHandler()
|
hdeh = urllib2.HTTPDefaultErrorHandler()
|
||||||
# hrh = urllib2.HTTPRedirectHandler()
|
hrh = urllib2.HTTPRedirectHandler()
|
||||||
# cp = urllib2.HTTPCookieProcessor(cj)
|
cp = urllib2.HTTPCookieProcessor(cj)
|
||||||
# o = build_test_opener(hh, hdeh, hrh, cp)
|
o = build_test_opener(hh, hdeh, hrh, cp)
|
||||||
# o.open("http://www.example.com/")
|
o.open("http://www.example.com/")
|
||||||
# self.assert_(not hh.req.has_header("Cookie"))
|
self.assert_(not hh.req.has_header("Cookie"))
|
||||||
|
|
||||||
def test_proxy(self):
|
def test_proxy(self):
|
||||||
o = OpenerDirector()
|
o = OpenerDirector()
|
||||||
@@ -1064,14 +1054,10 @@ class MiscTests(unittest.TestCase):
|
|||||||
|
|
||||||
|
|
||||||
def test_main(verbose=None):
|
def test_main(verbose=None):
|
||||||
#from test import test_urllib2
|
|
||||||
#test_support.run_doctest(test_urllib2, verbose)
|
|
||||||
#test_support.run_doctest(urllib2, verbose)
|
|
||||||
tests = (TrivialTests,
|
tests = (TrivialTests,
|
||||||
OpenerDirectorTests,
|
OpenerDirectorTests,
|
||||||
HandlerTests,
|
HandlerTests,
|
||||||
MiscTests)
|
MiscTests)
|
||||||
tests = [HandlerTests]
|
|
||||||
test_support.run_unittest(*tests)
|
test_support.run_unittest(*tests)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Reference in New Issue
Block a user