Skip to content

Commit 7807722

Browse files
serhiy-storchakamiss-islington
authored andcommitted
gh-71339: Use new assertion methods in the urllib tests (GH-129056)
(cherry picked from commit f98b9b4) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent f206b98 commit 7807722

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
@@ -169,9 +169,7 @@ def test_interface(self):
169169
# Make sure object returned by urlopen() has the specified methods
170170
for attr in ("read", "readline", "readlines", "fileno",
171171
"close", "info", "geturl", "getcode", "__iter__"):
172-
self.assertTrue(hasattr(self.returned_obj, attr),
173-
"object returned by urlopen() lacks %s attribute" %
174-
attr)
172+
self.assertHasAttr(self.returned_obj, attr)
175173

176174
def test_read(self):
177175
self.assertEqual(self.text, self.returned_obj.read())
@@ -640,9 +638,7 @@ def test_interface(self):
640638
# Make sure object returned by urlopen() has the specified methods
641639
for attr in ("read", "readline", "readlines",
642640
"close", "info", "geturl", "getcode", "__iter__"):
643-
self.assertTrue(hasattr(self.text_url_resp, attr),
644-
"object returned by urlopen() lacks %s attribute" %
645-
attr)
641+
self.assertHasAttr(self.text_url_resp, attr)
646642

647643
def test_info(self):
648644
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
@@ -1179,15 +1179,15 @@ def test_errors(self):
11791179
r = MockResponse(200, "OK", {}, "", url)
11801180
newr = h.http_response(req, r)
11811181
self.assertIs(r, newr)
1182-
self.assertFalse(hasattr(o, "proto")) # o.error not called
1182+
self.assertNotHasAttr(o, "proto") # o.error not called
11831183
r = MockResponse(202, "Accepted", {}, "", url)
11841184
newr = h.http_response(req, r)
11851185
self.assertIs(r, newr)
1186-
self.assertFalse(hasattr(o, "proto")) # o.error not called
1186+
self.assertNotHasAttr(o, "proto") # o.error not called
11871187
r = MockResponse(206, "Partial content", {}, "", url)
11881188
newr = h.http_response(req, r)
11891189
self.assertIs(r, newr)
1190-
self.assertFalse(hasattr(o, "proto")) # o.error not called
1190+
self.assertNotHasAttr(o, "proto") # o.error not called
11911191
# anything else calls o.error (and MockOpener returns None, here)
11921192
r = MockResponse(502, "Bad gateway", {}, "", url)
11931193
self.assertIsNone(h.http_response(req, r))
@@ -1402,7 +1402,7 @@ def http_open(self, req):
14021402
response = opener.open('http://example.com/')
14031403
expected = b'GET ' + result + b' '
14041404
request = handler.last_buf
1405-
self.assertTrue(request.startswith(expected), repr(request))
1405+
self.assertStartsWith(request, expected)
14061406

14071407
def test_redirect_head_request(self):
14081408
from_url = "http://example.com/a.html"
@@ -1892,9 +1892,9 @@ def test_HTTPError_interface(self):
18921892
url = code = fp = None
18931893
hdrs = 'Content-Length: 42'
18941894
err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
1895-
self.assertTrue(hasattr(err, 'reason'))
1895+
self.assertHasAttr(err, 'reason')
18961896
self.assertEqual(err.reason, 'something bad happened')
1897-
self.assertTrue(hasattr(err, 'headers'))
1897+
self.assertHasAttr(err, 'headers')
18981898
self.assertEqual(err.headers, 'Content-Length: 42')
18991899
expected_errmsg = 'HTTP Error %s: %s' % (err.code, err.msg)
19001900
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
@@ -70,8 +70,7 @@ def test_basic(self):
7070
with self.urlopen(self.url) as open_url:
7171
for attr in ("read", "readline", "readlines", "fileno", "close",
7272
"info", "geturl"):
73-
self.assertTrue(hasattr(open_url, attr), "object returned from "
74-
"urlopen lacks the %s attribute" % attr)
73+
self.assertHasAttr(open_url, attr)
7574
self.assertTrue(open_url.read(), "calling 'read' failed")
7675

7776
def test_readlines(self):

Lib/test/test_urlparse.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,14 +1033,13 @@ def test_parse_fragments(self):
10331033
with self.subTest(url=url, function=func):
10341034
result = func(url, allow_fragments=False)
10351035
self.assertEqual(result.fragment, "")
1036-
self.assertTrue(
1037-
getattr(result, attr).endswith("#" + expected_frag))
1036+
self.assertEndsWith(getattr(result, attr),
1037+
"#" + expected_frag)
10381038
self.assertEqual(func(url, "", False).fragment, "")
10391039

10401040
result = func(url, allow_fragments=True)
10411041
self.assertEqual(result.fragment, expected_frag)
1042-
self.assertFalse(
1043-
getattr(result, attr).endswith(expected_frag))
1042+
self.assertNotEndsWith(getattr(result, attr), expected_frag)
10441043
self.assertEqual(func(url, "", True).fragment,
10451044
expected_frag)
10461045
self.assertEqual(func(url).fragment, expected_frag)

0 commit comments

Comments
 (0)