Use unittest specific methods for some urllib test cases. And replace urllib2 with urllib.request in comments.
This commit is contained in:
parent
1f594ad424
commit
419e384601
|
@ -102,7 +102,7 @@ class urlopen_FileTests(unittest.TestCase):
|
||||||
self.assertEqual(self.returned_obj.geturl(), self.pathname)
|
self.assertEqual(self.returned_obj.geturl(), self.pathname)
|
||||||
|
|
||||||
def test_getcode(self):
|
def test_getcode(self):
|
||||||
self.assertEqual(self.returned_obj.getcode(), None)
|
self.assertIsNone(self.returned_obj.getcode())
|
||||||
|
|
||||||
def test_iter(self):
|
def test_iter(self):
|
||||||
# Test iterator
|
# Test iterator
|
||||||
|
@ -131,7 +131,7 @@ class ProxyTests(unittest.TestCase):
|
||||||
self.env.set('NO_PROXY', 'localhost')
|
self.env.set('NO_PROXY', 'localhost')
|
||||||
proxies = urllib.request.getproxies_environment()
|
proxies = urllib.request.getproxies_environment()
|
||||||
# getproxies_environment use lowered case truncated (no '_proxy') keys
|
# getproxies_environment use lowered case truncated (no '_proxy') keys
|
||||||
self.assertEquals('localhost', proxies['no'])
|
self.assertEqual('localhost', proxies['no'])
|
||||||
|
|
||||||
|
|
||||||
class urlopen_HttpTests(unittest.TestCase):
|
class urlopen_HttpTests(unittest.TestCase):
|
||||||
|
|
|
@ -41,7 +41,7 @@ class TrivialTests(unittest.TestCase):
|
||||||
('a="b\\"c", d="e\\,f", g="h\\\\i"',
|
('a="b\\"c", d="e\\,f", g="h\\\\i"',
|
||||||
['a="b"c"', 'd="e,f"', 'g="h\\i"'])]
|
['a="b"c"', 'd="e,f"', 'g="h\\i"'])]
|
||||||
for string, list in tests:
|
for string, list in tests:
|
||||||
self.assertEquals(urllib.request.parse_http_list(string), list)
|
self.assertEqual(urllib.request.parse_http_list(string), list)
|
||||||
|
|
||||||
|
|
||||||
def test_request_headers_dict():
|
def test_request_headers_dict():
|
||||||
|
@ -743,9 +743,9 @@ class HandlerTests(unittest.TestCase):
|
||||||
h.file_open(req)
|
h.file_open(req)
|
||||||
# XXXX remove OSError when bug fixed
|
# XXXX remove OSError when bug fixed
|
||||||
except (urllib.error.URLError, OSError):
|
except (urllib.error.URLError, OSError):
|
||||||
self.assertTrue(not ftp)
|
self.assertFalse(ftp)
|
||||||
else:
|
else:
|
||||||
self.assertTrue(o.req is req)
|
self.assertIs(o.req, req)
|
||||||
self.assertEqual(req.type, "ftp")
|
self.assertEqual(req.type, "ftp")
|
||||||
self.assertEqual(req.type is "ftp", ftp)
|
self.assertEqual(req.type is "ftp", ftp)
|
||||||
|
|
||||||
|
@ -848,19 +848,19 @@ class HandlerTests(unittest.TestCase):
|
||||||
# all 2xx are passed through
|
# all 2xx are passed through
|
||||||
r = MockResponse(200, "OK", {}, "", url)
|
r = MockResponse(200, "OK", {}, "", url)
|
||||||
newr = h.http_response(req, r)
|
newr = h.http_response(req, r)
|
||||||
self.assertTrue(r is newr)
|
self.assertIs(r, newr)
|
||||||
self.assertTrue(not hasattr(o, "proto")) # o.error not called
|
self.assertFalse(hasattr(o, "proto")) # o.error not called
|
||||||
r = MockResponse(202, "Accepted", {}, "", url)
|
r = MockResponse(202, "Accepted", {}, "", url)
|
||||||
newr = h.http_response(req, r)
|
newr = h.http_response(req, r)
|
||||||
self.assertTrue(r is newr)
|
self.assertIs(r, newr)
|
||||||
self.assertTrue(not hasattr(o, "proto")) # o.error not called
|
self.assertFalse(hasattr(o, "proto")) # o.error not called
|
||||||
r = MockResponse(206, "Partial content", {}, "", url)
|
r = MockResponse(206, "Partial content", {}, "", url)
|
||||||
newr = h.http_response(req, r)
|
newr = h.http_response(req, r)
|
||||||
self.assertTrue(r is newr)
|
self.assertIs(r, newr)
|
||||||
self.assertTrue(not hasattr(o, "proto")) # o.error not called
|
self.assertFalse(hasattr(o, "proto")) # o.error not called
|
||||||
# anything else calls o.error (and MockOpener returns None, here)
|
# anything else calls o.error (and MockOpener returns None, here)
|
||||||
r = MockResponse(502, "Bad gateway", {}, "", url)
|
r = MockResponse(502, "Bad gateway", {}, "", url)
|
||||||
self.assertTrue(h.http_response(req, r) is None)
|
self.assertIsNone(h.http_response(req, r))
|
||||||
self.assertEqual(o.proto, "http") # o.error called
|
self.assertEqual(o.proto, "http") # o.error called
|
||||||
self.assertEqual(o.args, (req, r, 502, "Bad gateway", {}))
|
self.assertEqual(o.args, (req, r, 502, "Bad gateway", {}))
|
||||||
|
|
||||||
|
@ -872,12 +872,14 @@ class HandlerTests(unittest.TestCase):
|
||||||
req = Request("http://example.com/")
|
req = Request("http://example.com/")
|
||||||
r = MockResponse(200, "OK", {}, "")
|
r = MockResponse(200, "OK", {}, "")
|
||||||
newreq = h.http_request(req)
|
newreq = h.http_request(req)
|
||||||
self.assertTrue(cj.ach_req is req is newreq)
|
self.assertIs(cj.ach_req, req)
|
||||||
self.assertEquals(req.get_origin_req_host(), "example.com")
|
self.assertIs(cj.ach_req, newreq)
|
||||||
self.assertTrue(not req.is_unverifiable())
|
self.assertEqual(req.get_origin_req_host(), "example.com")
|
||||||
|
self.assertFalse(req.is_unverifiable())
|
||||||
newr = h.http_response(req, r)
|
newr = h.http_response(req, r)
|
||||||
self.assertTrue(cj.ec_req is req)
|
self.assertIs(cj.ec_req, req)
|
||||||
self.assertTrue(cj.ec_r is r is newr)
|
self.assertIs(cj.ec_r, r)
|
||||||
|
self.assertIs(r, newr)
|
||||||
|
|
||||||
def test_redirect(self):
|
def test_redirect(self):
|
||||||
from_url = "http://example.com/a.html"
|
from_url = "http://example.com/a.html"
|
||||||
|
@ -905,7 +907,7 @@ class HandlerTests(unittest.TestCase):
|
||||||
try:
|
try:
|
||||||
self.assertEqual(o.req.get_method(), "GET")
|
self.assertEqual(o.req.get_method(), "GET")
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
self.assertTrue(not o.req.has_data())
|
self.assertFalse(o.req.has_data())
|
||||||
|
|
||||||
# now it's a GET, there should not be headers regarding content
|
# now it's a GET, there should not be headers regarding content
|
||||||
# (possibly dragged from before being a POST)
|
# (possibly dragged from before being a POST)
|
||||||
|
@ -964,7 +966,7 @@ class HandlerTests(unittest.TestCase):
|
||||||
cp = urllib.request.HTTPCookieProcessor(cj)
|
cp = urllib.request.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.assertTrue(not hh.req.has_header("Cookie"))
|
self.assertFalse(hh.req.has_header("Cookie"))
|
||||||
|
|
||||||
def test_proxy(self):
|
def test_proxy(self):
|
||||||
o = OpenerDirector()
|
o = OpenerDirector()
|
||||||
|
@ -1198,11 +1200,8 @@ class MiscTests(unittest.TestCase):
|
||||||
self.opener_has_handler(o, MyOtherHTTPHandler)
|
self.opener_has_handler(o, MyOtherHTTPHandler)
|
||||||
|
|
||||||
def opener_has_handler(self, opener, handler_class):
|
def opener_has_handler(self, opener, handler_class):
|
||||||
for h in opener.handlers:
|
self.assertTrue(any(h.__class__ == handler_class
|
||||||
if h.__class__ == handler_class:
|
for h in opener.handlers))
|
||||||
break
|
|
||||||
else:
|
|
||||||
self.assertTrue(False)
|
|
||||||
|
|
||||||
class RequestTests(unittest.TestCase):
|
class RequestTests(unittest.TestCase):
|
||||||
|
|
||||||
|
@ -1217,7 +1216,7 @@ class RequestTests(unittest.TestCase):
|
||||||
self.assertEqual("GET", self.get.get_method())
|
self.assertEqual("GET", self.get.get_method())
|
||||||
|
|
||||||
def test_add_data(self):
|
def test_add_data(self):
|
||||||
self.assertTrue(not self.get.has_data())
|
self.assertFalse(self.get.has_data())
|
||||||
self.assertEqual("GET", self.get.get_method())
|
self.assertEqual("GET", self.get.get_method())
|
||||||
self.get.add_data("spam")
|
self.get.add_data("spam")
|
||||||
self.assertTrue(self.get.has_data())
|
self.assertTrue(self.get.has_data())
|
||||||
|
@ -1243,7 +1242,7 @@ class RequestTests(unittest.TestCase):
|
||||||
self.assertEqual("www.python.org", req.get_host())
|
self.assertEqual("www.python.org", req.get_host())
|
||||||
|
|
||||||
def test_proxy(self):
|
def test_proxy(self):
|
||||||
self.assertTrue(not self.get.has_proxy())
|
self.assertFalse(self.get.has_proxy())
|
||||||
self.get.set_proxy("www.perl.org", "http")
|
self.get.set_proxy("www.perl.org", "http")
|
||||||
self.assertTrue(self.get.has_proxy())
|
self.assertTrue(self.get.has_proxy())
|
||||||
self.assertEqual("www.python.org", self.get.get_origin_req_host())
|
self.assertEqual("www.python.org", self.get.get_origin_req_host())
|
||||||
|
|
|
@ -172,7 +172,7 @@ class DigestAuthHandler:
|
||||||
auth_validated = False
|
auth_validated = False
|
||||||
|
|
||||||
# MSIE uses short_path in its validation, but Python's
|
# MSIE uses short_path in its validation, but Python's
|
||||||
# urllib2 uses the full path, so we're going to see if
|
# urllib.request uses the full path, so we're going to see if
|
||||||
# either of them works here.
|
# either of them works here.
|
||||||
|
|
||||||
for path in [request_handler.path, request_handler.short_path]:
|
for path in [request_handler.path, request_handler.short_path]:
|
||||||
|
@ -340,7 +340,7 @@ def GetRequestHandler(responses):
|
||||||
|
|
||||||
|
|
||||||
class TestUrlopen(BaseTestCase):
|
class TestUrlopen(BaseTestCase):
|
||||||
"""Tests urllib2.urlopen using the network.
|
"""Tests urllib.request.urlopen using the network.
|
||||||
|
|
||||||
These tests are not exhaustive. Assuming that testing using files does a
|
These tests are not exhaustive. Assuming that testing using files does a
|
||||||
good job overall of some of the basic interface features. There are no
|
good job overall of some of the basic interface features. There are no
|
||||||
|
@ -392,8 +392,8 @@ class TestUrlopen(BaseTestCase):
|
||||||
|
|
||||||
handler = self.start_server(responses)
|
handler = self.start_server(responses)
|
||||||
data = self.urlopen("http://localhost:%s/" % handler.port)
|
data = self.urlopen("http://localhost:%s/" % handler.port)
|
||||||
self.assertEquals(data, expected_response)
|
self.assertEqual(data, expected_response)
|
||||||
self.assertEquals(handler.requests, ["/", "/somewhere_else"])
|
self.assertEqual(handler.requests, ["/", "/somewhere_else"])
|
||||||
|
|
||||||
def test_chunked(self):
|
def test_chunked(self):
|
||||||
expected_response = b"hello world"
|
expected_response = b"hello world"
|
||||||
|
@ -407,7 +407,7 @@ class TestUrlopen(BaseTestCase):
|
||||||
response = [(200, [("Transfer-Encoding", "chunked")], chunked_start)]
|
response = [(200, [("Transfer-Encoding", "chunked")], chunked_start)]
|
||||||
handler = self.start_server(response)
|
handler = self.start_server(response)
|
||||||
data = self.urlopen("http://localhost:%s/" % handler.port)
|
data = self.urlopen("http://localhost:%s/" % handler.port)
|
||||||
self.assertEquals(data, expected_response)
|
self.assertEqual(data, expected_response)
|
||||||
|
|
||||||
def test_404(self):
|
def test_404(self):
|
||||||
expected_response = b"Bad bad bad..."
|
expected_response = b"Bad bad bad..."
|
||||||
|
@ -421,23 +421,23 @@ class TestUrlopen(BaseTestCase):
|
||||||
else:
|
else:
|
||||||
self.fail("404 should raise URLError")
|
self.fail("404 should raise URLError")
|
||||||
|
|
||||||
self.assertEquals(data, expected_response)
|
self.assertEqual(data, expected_response)
|
||||||
self.assertEquals(handler.requests, ["/weeble"])
|
self.assertEqual(handler.requests, ["/weeble"])
|
||||||
|
|
||||||
def test_200(self):
|
def test_200(self):
|
||||||
expected_response = b"pycon 2008..."
|
expected_response = b"pycon 2008..."
|
||||||
handler = self.start_server([(200, [], expected_response)])
|
handler = self.start_server([(200, [], expected_response)])
|
||||||
data = self.urlopen("http://localhost:%s/bizarre" % handler.port)
|
data = self.urlopen("http://localhost:%s/bizarre" % handler.port)
|
||||||
self.assertEquals(data, expected_response)
|
self.assertEqual(data, expected_response)
|
||||||
self.assertEquals(handler.requests, ["/bizarre"])
|
self.assertEqual(handler.requests, ["/bizarre"])
|
||||||
|
|
||||||
def test_200_with_parameters(self):
|
def test_200_with_parameters(self):
|
||||||
expected_response = b"pycon 2008..."
|
expected_response = b"pycon 2008..."
|
||||||
handler = self.start_server([(200, [], expected_response)])
|
handler = self.start_server([(200, [], expected_response)])
|
||||||
data = self.urlopen("http://localhost:%s/bizarre" % handler.port,
|
data = self.urlopen("http://localhost:%s/bizarre" % handler.port,
|
||||||
b"get=with_feeling")
|
b"get=with_feeling")
|
||||||
self.assertEquals(data, expected_response)
|
self.assertEqual(data, expected_response)
|
||||||
self.assertEquals(handler.requests, ["/bizarre", b"get=with_feeling"])
|
self.assertEqual(handler.requests, ["/bizarre", b"get=with_feeling"])
|
||||||
|
|
||||||
def test_sending_headers(self):
|
def test_sending_headers(self):
|
||||||
handler = self.start_server()
|
handler = self.start_server()
|
||||||
|
|
Loading…
Reference in New Issue