mirror of
https://github.com/python/cpython.git
synced 2024-12-03 23:06:43 +08:00
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)
|
||||
|
||||
def test_getcode(self):
|
||||
self.assertEqual(self.returned_obj.getcode(), None)
|
||||
self.assertIsNone(self.returned_obj.getcode())
|
||||
|
||||
def test_iter(self):
|
||||
# Test iterator
|
||||
@ -131,7 +131,7 @@ class ProxyTests(unittest.TestCase):
|
||||
self.env.set('NO_PROXY', 'localhost')
|
||||
proxies = urllib.request.getproxies_environment()
|
||||
# getproxies_environment use lowered case truncated (no '_proxy') keys
|
||||
self.assertEquals('localhost', proxies['no'])
|
||||
self.assertEqual('localhost', proxies['no'])
|
||||
|
||||
|
||||
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"'])]
|
||||
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():
|
||||
@ -743,9 +743,9 @@ class HandlerTests(unittest.TestCase):
|
||||
h.file_open(req)
|
||||
# XXXX remove OSError when bug fixed
|
||||
except (urllib.error.URLError, OSError):
|
||||
self.assertTrue(not ftp)
|
||||
self.assertFalse(ftp)
|
||||
else:
|
||||
self.assertTrue(o.req is req)
|
||||
self.assertIs(o.req, req)
|
||||
self.assertEqual(req.type, "ftp")
|
||||
self.assertEqual(req.type is "ftp", ftp)
|
||||
|
||||
@ -848,19 +848,19 @@ class HandlerTests(unittest.TestCase):
|
||||
# all 2xx are passed through
|
||||
r = MockResponse(200, "OK", {}, "", url)
|
||||
newr = h.http_response(req, r)
|
||||
self.assertTrue(r is newr)
|
||||
self.assertTrue(not hasattr(o, "proto")) # o.error not called
|
||||
self.assertIs(r, newr)
|
||||
self.assertFalse(hasattr(o, "proto")) # o.error not called
|
||||
r = MockResponse(202, "Accepted", {}, "", url)
|
||||
newr = h.http_response(req, r)
|
||||
self.assertTrue(r is newr)
|
||||
self.assertTrue(not hasattr(o, "proto")) # o.error not called
|
||||
self.assertIs(r, newr)
|
||||
self.assertFalse(hasattr(o, "proto")) # o.error not called
|
||||
r = MockResponse(206, "Partial content", {}, "", url)
|
||||
newr = h.http_response(req, r)
|
||||
self.assertTrue(r is newr)
|
||||
self.assertTrue(not hasattr(o, "proto")) # o.error not called
|
||||
self.assertIs(r, newr)
|
||||
self.assertFalse(hasattr(o, "proto")) # o.error not called
|
||||
# anything else calls o.error (and MockOpener returns None, here)
|
||||
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.args, (req, r, 502, "Bad gateway", {}))
|
||||
|
||||
@ -872,12 +872,14 @@ class HandlerTests(unittest.TestCase):
|
||||
req = Request("http://example.com/")
|
||||
r = MockResponse(200, "OK", {}, "")
|
||||
newreq = h.http_request(req)
|
||||
self.assertTrue(cj.ach_req is req is newreq)
|
||||
self.assertEquals(req.get_origin_req_host(), "example.com")
|
||||
self.assertTrue(not req.is_unverifiable())
|
||||
self.assertIs(cj.ach_req, req)
|
||||
self.assertIs(cj.ach_req, newreq)
|
||||
self.assertEqual(req.get_origin_req_host(), "example.com")
|
||||
self.assertFalse(req.is_unverifiable())
|
||||
newr = h.http_response(req, r)
|
||||
self.assertTrue(cj.ec_req is req)
|
||||
self.assertTrue(cj.ec_r is r is newr)
|
||||
self.assertIs(cj.ec_req, req)
|
||||
self.assertIs(cj.ec_r, r)
|
||||
self.assertIs(r, newr)
|
||||
|
||||
def test_redirect(self):
|
||||
from_url = "http://example.com/a.html"
|
||||
@ -905,7 +907,7 @@ class HandlerTests(unittest.TestCase):
|
||||
try:
|
||||
self.assertEqual(o.req.get_method(), "GET")
|
||||
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
|
||||
# (possibly dragged from before being a POST)
|
||||
@ -964,7 +966,7 @@ class HandlerTests(unittest.TestCase):
|
||||
cp = urllib.request.HTTPCookieProcessor(cj)
|
||||
o = build_test_opener(hh, hdeh, hrh, cp)
|
||||
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):
|
||||
o = OpenerDirector()
|
||||
@ -1198,11 +1200,8 @@ class MiscTests(unittest.TestCase):
|
||||
self.opener_has_handler(o, MyOtherHTTPHandler)
|
||||
|
||||
def opener_has_handler(self, opener, handler_class):
|
||||
for h in opener.handlers:
|
||||
if h.__class__ == handler_class:
|
||||
break
|
||||
else:
|
||||
self.assertTrue(False)
|
||||
self.assertTrue(any(h.__class__ == handler_class
|
||||
for h in opener.handlers))
|
||||
|
||||
class RequestTests(unittest.TestCase):
|
||||
|
||||
@ -1217,7 +1216,7 @@ class RequestTests(unittest.TestCase):
|
||||
self.assertEqual("GET", self.get.get_method())
|
||||
|
||||
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.get.add_data("spam")
|
||||
self.assertTrue(self.get.has_data())
|
||||
@ -1243,7 +1242,7 @@ class RequestTests(unittest.TestCase):
|
||||
self.assertEqual("www.python.org", req.get_host())
|
||||
|
||||
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.assertTrue(self.get.has_proxy())
|
||||
self.assertEqual("www.python.org", self.get.get_origin_req_host())
|
||||
|
@ -172,7 +172,7 @@ class DigestAuthHandler:
|
||||
auth_validated = False
|
||||
|
||||
# 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.
|
||||
|
||||
for path in [request_handler.path, request_handler.short_path]:
|
||||
@ -340,7 +340,7 @@ def GetRequestHandler(responses):
|
||||
|
||||
|
||||
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
|
||||
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)
|
||||
data = self.urlopen("http://localhost:%s/" % handler.port)
|
||||
self.assertEquals(data, expected_response)
|
||||
self.assertEquals(handler.requests, ["/", "/somewhere_else"])
|
||||
self.assertEqual(data, expected_response)
|
||||
self.assertEqual(handler.requests, ["/", "/somewhere_else"])
|
||||
|
||||
def test_chunked(self):
|
||||
expected_response = b"hello world"
|
||||
@ -407,7 +407,7 @@ class TestUrlopen(BaseTestCase):
|
||||
response = [(200, [("Transfer-Encoding", "chunked")], chunked_start)]
|
||||
handler = self.start_server(response)
|
||||
data = self.urlopen("http://localhost:%s/" % handler.port)
|
||||
self.assertEquals(data, expected_response)
|
||||
self.assertEqual(data, expected_response)
|
||||
|
||||
def test_404(self):
|
||||
expected_response = b"Bad bad bad..."
|
||||
@ -421,23 +421,23 @@ class TestUrlopen(BaseTestCase):
|
||||
else:
|
||||
self.fail("404 should raise URLError")
|
||||
|
||||
self.assertEquals(data, expected_response)
|
||||
self.assertEquals(handler.requests, ["/weeble"])
|
||||
self.assertEqual(data, expected_response)
|
||||
self.assertEqual(handler.requests, ["/weeble"])
|
||||
|
||||
def test_200(self):
|
||||
expected_response = b"pycon 2008..."
|
||||
handler = self.start_server([(200, [], expected_response)])
|
||||
data = self.urlopen("http://localhost:%s/bizarre" % handler.port)
|
||||
self.assertEquals(data, expected_response)
|
||||
self.assertEquals(handler.requests, ["/bizarre"])
|
||||
self.assertEqual(data, expected_response)
|
||||
self.assertEqual(handler.requests, ["/bizarre"])
|
||||
|
||||
def test_200_with_parameters(self):
|
||||
expected_response = b"pycon 2008..."
|
||||
handler = self.start_server([(200, [], expected_response)])
|
||||
data = self.urlopen("http://localhost:%s/bizarre" % handler.port,
|
||||
b"get=with_feeling")
|
||||
self.assertEquals(data, expected_response)
|
||||
self.assertEquals(handler.requests, ["/bizarre", b"get=with_feeling"])
|
||||
self.assertEqual(data, expected_response)
|
||||
self.assertEqual(handler.requests, ["/bizarre", b"get=with_feeling"])
|
||||
|
||||
def test_sending_headers(self):
|
||||
handler = self.start_server()
|
||||
|
Loading…
Reference in New Issue
Block a user