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

[3.13] gh-71339: Use new assertion methods in the urllib tests (GH-129056) #132499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions 13 Lib/test/test_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from test.support import os_helper
from test.support import socket_helper
from test.support import warnings_helper
from test.support.testcase import ExtraAssertions
import os
try:
import ssl
Expand Down Expand Up @@ -139,7 +140,7 @@ def unfakeftp(self):
urllib.request.ftpwrapper = self._ftpwrapper_class


class urlopen_FileTests(unittest.TestCase):
class urlopen_FileTests(unittest.TestCase, ExtraAssertions):
"""Test urlopen() opening a temporary file.

Try to test as much functionality as possible so as to cut down on reliance
Expand Down Expand Up @@ -169,9 +170,7 @@ def test_interface(self):
# Make sure object returned by urlopen() has the specified methods
for attr in ("read", "readline", "readlines", "fileno",
"close", "info", "geturl", "getcode", "__iter__"):
self.assertTrue(hasattr(self.returned_obj, attr),
"object returned by urlopen() lacks %s attribute" %
attr)
self.assertHasAttr(self.returned_obj, attr)

def test_read(self):
self.assertEqual(self.text, self.returned_obj.read())
Expand Down Expand Up @@ -601,7 +600,7 @@ def test_URLopener_deprecation(self):
urllib.request.URLopener()


class urlopen_DataTests(unittest.TestCase):
class urlopen_DataTests(unittest.TestCase, ExtraAssertions):
"""Test urlopen() opening a data URL."""

def setUp(self):
Expand Down Expand Up @@ -640,9 +639,7 @@ def test_interface(self):
# Make sure object returned by urlopen() has the specified methods
for attr in ("read", "readline", "readlines",
"close", "info", "geturl", "getcode", "__iter__"):
self.assertTrue(hasattr(self.text_url_resp, attr),
"object returned by urlopen() lacks %s attribute" %
attr)
self.assertHasAttr(self.text_url_resp, attr)

def test_info(self):
self.assertIsInstance(self.text_url_resp.info(), email.message.Message)
Expand Down
17 changes: 9 additions & 8 deletions 17 Lib/test/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from test.support import os_helper
from test.support import requires_subprocess
from test.support import warnings_helper
from test.support.testcase import ExtraAssertions
from test import test_urllib
from unittest import mock

Expand Down Expand Up @@ -724,7 +725,7 @@ def sanepathname2url(path):
return urlpath


class HandlerTests(unittest.TestCase):
class HandlerTests(unittest.TestCase, ExtraAssertions):

def test_ftp(self):
class MockFTPWrapper:
Expand Down Expand Up @@ -1179,15 +1180,15 @@ def test_errors(self):
r = MockResponse(200, "OK", {}, "", url)
newr = h.http_response(req, r)
self.assertIs(r, newr)
self.assertFalse(hasattr(o, "proto")) # o.error not called
self.assertNotHasAttr(o, "proto") # o.error not called
r = MockResponse(202, "Accepted", {}, "", url)
newr = h.http_response(req, r)
self.assertIs(r, newr)
self.assertFalse(hasattr(o, "proto")) # o.error not called
self.assertNotHasAttr(o, "proto") # o.error not called
r = MockResponse(206, "Partial content", {}, "", url)
newr = h.http_response(req, r)
self.assertIs(r, newr)
self.assertFalse(hasattr(o, "proto")) # o.error not called
self.assertNotHasAttr(o, "proto") # o.error not called
# anything else calls o.error (and MockOpener returns None, here)
r = MockResponse(502, "Bad gateway", {}, "", url)
self.assertIsNone(h.http_response(req, r))
Expand Down Expand Up @@ -1402,7 +1403,7 @@ def http_open(self, req):
response = opener.open('http://example.com/')
expected = b'GET ' + result + b' '
request = handler.last_buf
self.assertTrue(request.startswith(expected), repr(request))
self.assertStartsWith(request, expected)

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


class MiscTests(unittest.TestCase):
class MiscTests(unittest.TestCase, ExtraAssertions):

def opener_has_handler(self, opener, handler_class):
self.assertTrue(any(h.__class__ == handler_class
Expand Down Expand Up @@ -1892,9 +1893,9 @@ def test_HTTPError_interface(self):
url = code = fp = None
hdrs = 'Content-Length: 42'
err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
self.assertTrue(hasattr(err, 'reason'))
self.assertHasAttr(err, 'reason')
self.assertEqual(err.reason, 'something bad happened')
self.assertTrue(hasattr(err, 'headers'))
self.assertHasAttr(err, 'headers')
self.assertEqual(err.headers, 'Content-Length: 42')
expected_errmsg = 'HTTP Error %s: %s' % (err.code, err.msg)
self.assertEqual(str(err), expected_errmsg)
Expand Down
6 changes: 3 additions & 3 deletions 6 Lib/test/test_urllib2_localnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from test import support
from test.support import hashlib_helper
from test.support import threading_helper
from test.support.testcase import ExtraAssertions

try:
import ssl
Expand Down Expand Up @@ -442,7 +443,7 @@ def log_message(self, *args):
return FakeHTTPRequestHandler


class TestUrlopen(unittest.TestCase):
class TestUrlopen(unittest.TestCase, ExtraAssertions):
"""Tests urllib.request.urlopen using the network.

These tests are not exhaustive. Assuming that testing using files does a
Expand Down Expand Up @@ -606,8 +607,7 @@ def test_basic(self):
handler = self.start_server()
with urllib.request.urlopen("http://localhost:%s" % handler.port) as open_url:
for attr in ("read", "close", "info", "geturl"):
self.assertTrue(hasattr(open_url, attr), "object returned from "
"urlopen lacks the %s attribute" % attr)
self.assertHasAttr(open_url, attr)
self.assertTrue(open_url.read(), "calling 'read' failed")

def test_info(self):
Expand Down
6 changes: 3 additions & 3 deletions 6 Lib/test/test_urllibnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from test import support
from test.support import os_helper
from test.support import socket_helper
from test.support.testcase import ExtraAssertions

import contextlib
import socket
Expand Down Expand Up @@ -34,7 +35,7 @@ def testURLread(self):
f.read()


class urlopenNetworkTests(unittest.TestCase):
class urlopenNetworkTests(unittest.TestCase, ExtraAssertions):
"""Tests urllib.request.urlopen using the network.

These tests are not exhaustive. Assuming that testing using files does a
Expand Down Expand Up @@ -70,8 +71,7 @@ def test_basic(self):
with self.urlopen(self.url) as open_url:
for attr in ("read", "readline", "readlines", "fileno", "close",
"info", "geturl"):
self.assertTrue(hasattr(open_url, attr), "object returned from "
"urlopen lacks the %s attribute" % attr)
self.assertHasAttr(open_url, attr)
self.assertTrue(open_url.read(), "calling 'read' failed")

def test_readlines(self):
Expand Down
10 changes: 5 additions & 5 deletions 10 Lib/test/test_urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unicodedata
import unittest
import urllib.parse
from test.support.testcase import ExtraAssertions

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

class UrlParseTestCase(unittest.TestCase):
class UrlParseTestCase(unittest.TestCase, ExtraAssertions):

def checkRoundtrips(self, url, parsed, split, url2=None):
if url2 is None:
Expand Down Expand Up @@ -1033,14 +1034,13 @@ def test_parse_fragments(self):
with self.subTest(url=url, function=func):
result = func(url, allow_fragments=False)
self.assertEqual(result.fragment, "")
self.assertTrue(
getattr(result, attr).endswith("#" + expected_frag))
self.assertEndsWith(getattr(result, attr),
"#" + expected_frag)
self.assertEqual(func(url, "", False).fragment, "")

result = func(url, allow_fragments=True)
self.assertEqual(result.fragment, expected_frag)
self.assertFalse(
getattr(result, attr).endswith(expected_frag))
self.assertNotEndsWith(getattr(result, attr), expected_frag)
self.assertEqual(func(url, "", True).fragment,
expected_frag)
self.assertEqual(func(url).fragment, expected_frag)
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.