Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a5811f6

Browse filesBrowse files
gh-71339: Use new assertion methods in the urllib tests (GH-129056)
(cherry picked from commit f98b9b4) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent f206b98 commit a5811f6
Copy full SHA for a5811f6

File tree

Expand file treeCollapse file tree

5 files changed

+25
-27
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+25
-27
lines changed

‎Lib/test/test_urllib.py

Copy file name to clipboardExpand all lines: Lib/test/test_urllib.py
+5-8Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from test.support import os_helper
1313
from test.support import socket_helper
1414
from test.support import warnings_helper
15+
from test.support.testcase import ExtraAssertions
1516
import os
1617
try:
1718
import ssl
@@ -139,7 +140,7 @@ def unfakeftp(self):
139140
urllib.request.ftpwrapper = self._ftpwrapper_class
140141

141142

142-
class urlopen_FileTests(unittest.TestCase):
143+
class urlopen_FileTests(unittest.TestCase, ExtraAssertions):
143144
"""Test urlopen() opening a temporary file.
144145
145146
Try to test as much functionality as possible so as to cut down on reliance
@@ -169,9 +170,7 @@ def test_interface(self):
169170
# Make sure object returned by urlopen() has the specified methods
170171
for attr in ("read", "readline", "readlines", "fileno",
171172
"close", "info", "geturl", "getcode", "__iter__"):
172-
self.assertTrue(hasattr(self.returned_obj, attr),
173-
"object returned by urlopen() lacks %s attribute" %
174-
attr)
173+
self.assertHasAttr(self.returned_obj, attr)
175174

176175
def test_read(self):
177176
self.assertEqual(self.text, self.returned_obj.read())
@@ -601,7 +600,7 @@ def test_URLopener_deprecation(self):
601600
urllib.request.URLopener()
602601

603602

604-
class urlopen_DataTests(unittest.TestCase):
603+
class urlopen_DataTests(unittest.TestCase, ExtraAssertions):
605604
"""Test urlopen() opening a data URL."""
606605

607606
def setUp(self):
@@ -640,9 +639,7 @@ def test_interface(self):
640639
# Make sure object returned by urlopen() has the specified methods
641640
for attr in ("read", "readline", "readlines",
642641
"close", "info", "geturl", "getcode", "__iter__"):
643-
self.assertTrue(hasattr(self.text_url_resp, attr),
644-
"object returned by urlopen() lacks %s attribute" %
645-
attr)
642+
self.assertHasAttr(self.text_url_resp, attr)
646643

647644
def test_info(self):
648645
self.assertIsInstance(self.text_url_resp.info(), email.message.Message)

‎Lib/test/test_urllib2.py

Copy file name to clipboardExpand all lines: Lib/test/test_urllib2.py
+9-8Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from test.support import os_helper
44
from test.support import requires_subprocess
55
from test.support import warnings_helper
6+
from test.support.testcase import ExtraAssertions
67
from test import test_urllib
78
from unittest import mock
89

@@ -724,7 +725,7 @@ def sanepathname2url(path):
724725
return urlpath
725726

726727

727-
class HandlerTests(unittest.TestCase):
728+
class HandlerTests(unittest.TestCase, ExtraAssertions):
728729

729730
def test_ftp(self):
730731
class MockFTPWrapper:
@@ -1179,15 +1180,15 @@ def test_errors(self):
11791180
r = MockResponse(200, "OK", {}, "", url)
11801181
newr = h.http_response(req, r)
11811182
self.assertIs(r, newr)
1182-
self.assertFalse(hasattr(o, "proto")) # o.error not called
1183+
self.assertNotHasAttr(o, "proto") # o.error not called
11831184
r = MockResponse(202, "Accepted", {}, "", url)
11841185
newr = h.http_response(req, r)
11851186
self.assertIs(r, newr)
1186-
self.assertFalse(hasattr(o, "proto")) # o.error not called
1187+
self.assertNotHasAttr(o, "proto") # o.error not called
11871188
r = MockResponse(206, "Partial content", {}, "", url)
11881189
newr = h.http_response(req, r)
11891190
self.assertIs(r, newr)
1190-
self.assertFalse(hasattr(o, "proto")) # o.error not called
1191+
self.assertNotHasAttr(o, "proto") # o.error not called
11911192
# anything else calls o.error (and MockOpener returns None, here)
11921193
r = MockResponse(502, "Bad gateway", {}, "", url)
11931194
self.assertIsNone(h.http_response(req, r))
@@ -1402,7 +1403,7 @@ def http_open(self, req):
14021403
response = opener.open('http://example.com/')
14031404
expected = b'GET ' + result + b' '
14041405
request = handler.last_buf
1405-
self.assertTrue(request.startswith(expected), repr(request))
1406+
self.assertStartsWith(request, expected)
14061407

14071408
def test_redirect_head_request(self):
14081409
from_url = "http://example.com/a.html"
@@ -1833,7 +1834,7 @@ def test_invalid_closed(self):
18331834
self.assertTrue(conn.fakesock.closed, "Connection not closed")
18341835

18351836

1836-
class MiscTests(unittest.TestCase):
1837+
class MiscTests(unittest.TestCase, ExtraAssertions):
18371838

18381839
def opener_has_handler(self, opener, handler_class):
18391840
self.assertTrue(any(h.__class__ == handler_class
@@ -1892,9 +1893,9 @@ def test_HTTPError_interface(self):
18921893
url = code = fp = None
18931894
hdrs = 'Content-Length: 42'
18941895
err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
1895-
self.assertTrue(hasattr(err, 'reason'))
1896+
self.assertHasAttr(err, 'reason')
18961897
self.assertEqual(err.reason, 'something bad happened')
1897-
self.assertTrue(hasattr(err, 'headers'))
1898+
self.assertHasAttr(err, 'headers')
18981899
self.assertEqual(err.headers, 'Content-Length: 42')
18991900
expected_errmsg = 'HTTP Error %s: %s' % (err.code, err.msg)
19001901
self.assertEqual(str(err), expected_errmsg)

‎Lib/test/test_urllib2_localnet.py

Copy file name to clipboardExpand all lines: Lib/test/test_urllib2_localnet.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from test import support
1212
from test.support import hashlib_helper
1313
from test.support import threading_helper
14+
from test.support.testcase import ExtraAssertions
1415

1516
try:
1617
import ssl
@@ -442,7 +443,7 @@ def log_message(self, *args):
442443
return FakeHTTPRequestHandler
443444

444445

445-
class TestUrlopen(unittest.TestCase):
446+
class TestUrlopen(unittest.TestCase, ExtraAssertions):
446447
"""Tests urllib.request.urlopen using the network.
447448
448449
These tests are not exhaustive. Assuming that testing using files does a
@@ -606,8 +607,7 @@ def test_basic(self):
606607
handler = self.start_server()
607608
with urllib.request.urlopen("http://localhost:%s" % handler.port) as open_url:
608609
for attr in ("read", "close", "info", "geturl"):
609-
self.assertTrue(hasattr(open_url, attr), "object returned from "
610-
"urlopen lacks the %s attribute" % attr)
610+
self.assertHasAttr(open_url, attr)
611611
self.assertTrue(open_url.read(), "calling 'read' failed")
612612

613613
def test_info(self):

‎Lib/test/test_urllibnet.py

Copy file name to clipboardExpand all lines: Lib/test/test_urllibnet.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from test import support
33
from test.support import os_helper
44
from test.support import socket_helper
5+
from test.support.testcase import ExtraAssertions
56

67
import contextlib
78
import socket
@@ -34,7 +35,7 @@ def testURLread(self):
3435
f.read()
3536

3637

37-
class urlopenNetworkTests(unittest.TestCase):
38+
class urlopenNetworkTests(unittest.TestCase, ExtraAssertions):
3839
"""Tests urllib.request.urlopen using the network.
3940
4041
These tests are not exhaustive. Assuming that testing using files does a
@@ -70,8 +71,7 @@ def test_basic(self):
7071
with self.urlopen(self.url) as open_url:
7172
for attr in ("read", "readline", "readlines", "fileno", "close",
7273
"info", "geturl"):
73-
self.assertTrue(hasattr(open_url, attr), "object returned from "
74-
"urlopen lacks the %s attribute" % attr)
74+
self.assertHasAttr(open_url, attr)
7575
self.assertTrue(open_url.read(), "calling 'read' failed")
7676

7777
def test_readlines(self):

‎Lib/test/test_urlparse.py

Copy file name to clipboardExpand all lines: Lib/test/test_urlparse.py
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unicodedata
33
import unittest
44
import urllib.parse
5+
from test.support.testcase import ExtraAssertions
56

67
RFC1808_BASE = "http://a/b/c/d;p?q#f"
78
RFC2396_BASE = "http://a/b/c/d;p?q"
@@ -101,7 +102,7 @@
101102
(b"%81=%A9", {b'\x81': [b'\xa9']}),
102103
]
103104

104-
class UrlParseTestCase(unittest.TestCase):
105+
class UrlParseTestCase(unittest.TestCase, ExtraAssertions):
105106

106107
def checkRoundtrips(self, url, parsed, split, url2=None):
107108
if url2 is None:
@@ -1033,14 +1034,13 @@ def test_parse_fragments(self):
10331034
with self.subTest(url=url, function=func):
10341035
result = func(url, allow_fragments=False)
10351036
self.assertEqual(result.fragment, "")
1036-
self.assertTrue(
1037-
getattr(result, attr).endswith("#" + expected_frag))
1037+
self.assertEndsWith(getattr(result, attr),
1038+
"#" + expected_frag)
10381039
self.assertEqual(func(url, "", False).fragment, "")
10391040

10401041
result = func(url, allow_fragments=True)
10411042
self.assertEqual(result.fragment, expected_frag)
1042-
self.assertFalse(
1043-
getattr(result, attr).endswith(expected_frag))
1043+
self.assertNotEndsWith(getattr(result, attr), expected_frag)
10441044
self.assertEqual(func(url, "", True).fragment,
10451045
expected_frag)
10461046
self.assertEqual(func(url).fragment, expected_frag)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.