Skip to content

Commit 037cd62

Browse files
gh-71339: Use new assertion methods in the urllib tests
1 parent c463270 commit 037cd62

File tree

5 files changed

+13
-20
lines changed

5 files changed

+13
-20
lines changed

Lib/test/test_urllib.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ def test_interface(self):
120120
# Make sure object returned by urlopen() has the specified methods
121121
for attr in ("read", "readline", "readlines", "fileno",
122122
"close", "info", "geturl", "getcode", "__iter__"):
123-
self.assertTrue(hasattr(self.returned_obj, attr),
124-
"object returned by urlopen() lacks %s attribute" %
125-
attr)
123+
self.assertHasAttr(self.returned_obj, attr)
126124

127125
def test_read(self):
128126
self.assertEqual(self.text, self.returned_obj.read())
@@ -543,9 +541,7 @@ def test_interface(self):
543541
# Make sure object returned by urlopen() has the specified methods
544542
for attr in ("read", "readline", "readlines",
545543
"close", "info", "geturl", "getcode", "__iter__"):
546-
self.assertTrue(hasattr(self.text_url_resp, attr),
547-
"object returned by urlopen() lacks %s attribute" %
548-
attr)
544+
self.assertHasAttr(self.text_url_resp, attr)
549545

550546
def test_info(self):
551547
self.assertIsInstance(self.text_url_resp.info(), email.message.Message)

Lib/test/test_urllib2.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,15 +1197,15 @@ def test_errors(self):
11971197
r = MockResponse(200, "OK", {}, "", url)
11981198
newr = h.http_response(req, r)
11991199
self.assertIs(r, newr)
1200-
self.assertFalse(hasattr(o, "proto")) # o.error not called
1200+
self.assertNotHasAttr(o, "proto") # o.error not called
12011201
r = MockResponse(202, "Accepted", {}, "", url)
12021202
newr = h.http_response(req, r)
12031203
self.assertIs(r, newr)
1204-
self.assertFalse(hasattr(o, "proto")) # o.error not called
1204+
self.assertNotHasAttr(o, "proto") # o.error not called
12051205
r = MockResponse(206, "Partial content", {}, "", url)
12061206
newr = h.http_response(req, r)
12071207
self.assertIs(r, newr)
1208-
self.assertFalse(hasattr(o, "proto")) # o.error not called
1208+
self.assertNotHasAttr(o, "proto") # o.error not called
12091209
# anything else calls o.error (and MockOpener returns None, here)
12101210
r = MockResponse(502, "Bad gateway", {}, "", url)
12111211
self.assertIsNone(h.http_response(req, r))
@@ -1420,7 +1420,7 @@ def http_open(self, req):
14201420
response = opener.open('http://example.com/')
14211421
expected = b'GET ' + result + b' '
14221422
request = handler.last_buf
1423-
self.assertTrue(request.startswith(expected), repr(request))
1423+
self.assertStartsWith(request, expected)
14241424

14251425
def test_redirect_head_request(self):
14261426
from_url = "http://example.com/a.html"
@@ -1910,9 +1910,9 @@ def test_HTTPError_interface(self):
19101910
url = code = fp = None
19111911
hdrs = 'Content-Length: 42'
19121912
err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
1913-
self.assertTrue(hasattr(err, 'reason'))
1913+
self.assertHasAttr(err, 'reason')
19141914
self.assertEqual(err.reason, 'something bad happened')
1915-
self.assertTrue(hasattr(err, 'headers'))
1915+
self.assertHasAttr(err, 'headers')
19161916
self.assertEqual(err.headers, 'Content-Length: 42')
19171917
expected_errmsg = 'HTTP Error %s: %s' % (err.code, err.msg)
19181918
self.assertEqual(str(err), expected_errmsg)

Lib/test/test_urllib2_localnet.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,7 @@ def test_basic(self):
606606
handler = self.start_server()
607607
with urllib.request.urlopen("http://localhost:%s" % handler.port) as open_url:
608608
for attr in ("read", "close", "info", "geturl"):
609-
self.assertTrue(hasattr(open_url, attr), "object returned from "
610-
"urlopen lacks the %s attribute" % attr)
609+
self.assertHasAttr(open_url, attr)
611610
self.assertTrue(open_url.read(), "calling 'read' failed")
612611

613612
def test_info(self):

Lib/test/test_urllibnet.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def test_basic(self):
7171
with self.urlopen(self.url) as open_url:
7272
for attr in ("read", "readline", "readlines", "fileno", "close",
7373
"info", "geturl"):
74-
self.assertTrue(hasattr(open_url, attr), "object returned from "
75-
"urlopen lacks the %s attribute" % attr)
74+
self.assertHasAttr(open_url, attr)
7675
self.assertTrue(open_url.read(), "calling 'read' failed")
7776

7877
def test_readlines(self):

Lib/test/test_urlparse.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,14 +1164,13 @@ def test_parse_fragments(self):
11641164
with self.subTest(url=url, function=func):
11651165
result = func(url, allow_fragments=False)
11661166
self.assertEqual(result.fragment, "")
1167-
self.assertTrue(
1168-
getattr(result, attr).endswith("#" + expected_frag))
1167+
self.assertEndsWith(getattr(result, attr),
1168+
"#" + expected_frag)
11691169
self.assertEqual(func(url, "", False).fragment, "")
11701170

11711171
result = func(url, allow_fragments=True)
11721172
self.assertEqual(result.fragment, expected_frag)
1173-
self.assertFalse(
1174-
getattr(result, attr).endswith(expected_frag))
1173+
self.assertNotEndsWith(getattr(result, attr), expected_frag)
11751174
self.assertEqual(func(url, "", True).fragment,
11761175
expected_frag)
11771176
self.assertEqual(func(url).fragment, expected_frag)

0 commit comments

Comments
 (0)