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 c3ce756

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

File tree

Expand file treeCollapse file tree

4 files changed

+32
-31
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+32
-31
lines changed

‎Lib/test/test_http_cookiejar.py

Copy file name to clipboardExpand all lines: Lib/test/test_http_cookiejar.py
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import re
77
from test.support import os_helper
88
from test.support import warnings_helper
9+
from test.support.testcase import ExtraAssertions
910
import time
1011
import unittest
1112
import urllib.request
@@ -1436,7 +1437,7 @@ def cookiejar_from_cookie_headers(headers):
14361437
self.assertIsNone(cookie.expires)
14371438

14381439

1439-
class LWPCookieTests(unittest.TestCase):
1440+
class LWPCookieTests(unittest.TestCase, ExtraAssertions):
14401441
# Tests taken from libwww-perl, with a few modifications and additions.
14411442

14421443
def test_netscape_example_1(self):
@@ -1528,7 +1529,7 @@ def test_netscape_example_1(self):
15281529
h = req.get_header("Cookie")
15291530
self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
15301531
self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
1531-
self.assertTrue(h.startswith("SHIPPING=FEDEX;"))
1532+
self.assertStartsWith(h, "SHIPPING=FEDEX;")
15321533

15331534
def test_netscape_example_2(self):
15341535
# Second Example transaction sequence:

‎Lib/test/test_http_cookies.py

Copy file name to clipboardExpand all lines: Lib/test/test_http_cookies.py
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
from http import cookies
77
import pickle
88
from test import support
9+
from test.support.testcase import ExtraAssertions
910

1011

11-
class CookieTests(unittest.TestCase):
12+
class CookieTests(unittest.TestCase, ExtraAssertions):
1213

1314
def test_basic(self):
1415
cases = [
@@ -180,7 +181,7 @@ def test_special_attrs(self):
180181
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
181182
C['Customer']['expires'] = 0
182183
# can't test exact output, it always depends on current date/time
183-
self.assertTrue(C.output().endswith('GMT'))
184+
self.assertEndsWith(C.output(), 'GMT')
184185

185186
# loading 'expires'
186187
C = cookies.SimpleCookie()

‎Lib/test/test_httplib.py

Copy file name to clipboardExpand all lines: Lib/test/test_httplib.py
+17-18Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from test import support
1717
from test.support import os_helper
1818
from test.support import socket_helper
19+
from test.support.testcase import ExtraAssertions
1920

2021
support.requires_working_socket(module=True)
2122

@@ -134,7 +135,7 @@ def connect(self):
134135
def create_connection(self, *pos, **kw):
135136
return FakeSocket(*self.fake_socket_args)
136137

137-
class HeaderTests(TestCase):
138+
class HeaderTests(TestCase, ExtraAssertions):
138139
def test_auto_headers(self):
139140
# Some headers are added automatically, but should not be added by
140141
# .request() if they are explicitly set.
@@ -273,31 +274,31 @@ def test_ipv6host_header(self):
273274
sock = FakeSocket('')
274275
conn.sock = sock
275276
conn.request('GET', '/foo')
276-
self.assertTrue(sock.data.startswith(expected))
277+
self.assertStartsWith(sock.data, expected)
277278

278279
expected = b'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \
279280
b'Accept-Encoding: identity\r\n\r\n'
280281
conn = client.HTTPConnection('[2001:102A::]')
281282
sock = FakeSocket('')
282283
conn.sock = sock
283284
conn.request('GET', '/foo')
284-
self.assertTrue(sock.data.startswith(expected))
285+
self.assertStartsWith(sock.data, expected)
285286

286287
expected = b'GET /foo HTTP/1.1\r\nHost: [fe80::]\r\n' \
287288
b'Accept-Encoding: identity\r\n\r\n'
288289
conn = client.HTTPConnection('[fe80::%2]')
289290
sock = FakeSocket('')
290291
conn.sock = sock
291292
conn.request('GET', '/foo')
292-
self.assertTrue(sock.data.startswith(expected))
293+
self.assertStartsWith(sock.data, expected)
293294

294295
expected = b'GET /foo HTTP/1.1\r\nHost: [fe80::]:81\r\n' \
295296
b'Accept-Encoding: identity\r\n\r\n'
296297
conn = client.HTTPConnection('[fe80::%2]:81')
297298
sock = FakeSocket('')
298299
conn.sock = sock
299300
conn.request('GET', '/foo')
300-
self.assertTrue(sock.data.startswith(expected))
301+
self.assertStartsWith(sock.data, expected)
301302

302303
def test_malformed_headers_coped_with(self):
303304
# Issue 19996
@@ -335,9 +336,9 @@ def test_parse_all_octets(self):
335336
self.assertIsNotNone(resp.getheader('obs-text'))
336337
self.assertIn('obs-text', resp.msg)
337338
for folded in (resp.getheader('obs-fold'), resp.msg['obs-fold']):
338-
self.assertTrue(folded.startswith('text'))
339+
self.assertStartsWith(folded, 'text')
339340
self.assertIn(' folded with space', folded)
340-
self.assertTrue(folded.endswith('folded with tab'))
341+
self.assertEndsWith(folded, 'folded with tab')
341342

342343
def test_invalid_headers(self):
343344
conn = client.HTTPConnection('example.com')
@@ -537,7 +538,7 @@ def _parse_chunked(self, data):
537538
return b''.join(body)
538539

539540

540-
class BasicTest(TestCase):
541+
class BasicTest(TestCase, ExtraAssertions):
541542
def test_dir_with_added_behavior_on_status(self):
542543
# see issue40084
543544
self.assertTrue({'description', 'name', 'phrase', 'value'} <= set(dir(HTTPStatus(404))))
@@ -989,8 +990,7 @@ def test_send_file(self):
989990
sock = FakeSocket(body)
990991
conn.sock = sock
991992
conn.request('GET', '/foo', body)
992-
self.assertTrue(sock.data.startswith(expected), '%r != %r' %
993-
(sock.data[:len(expected)], expected))
993+
self.assertStartsWith(sock.data, expected)
994994

995995
def test_send(self):
996996
expected = b'this is a test this is only a test'
@@ -1494,7 +1494,7 @@ def _encode_request(self, str_url):
14941494
conn.putrequest('GET', '/☃')
14951495

14961496

1497-
class ExtendedReadTest(TestCase):
1497+
class ExtendedReadTest(TestCase, ExtraAssertions):
14981498
"""
14991499
Test peek(), read1(), readline()
15001500
"""
@@ -1553,7 +1553,7 @@ def mypeek(n=-1):
15531553
# then unbounded peek
15541554
p2 = resp.peek()
15551555
self.assertGreaterEqual(len(p2), len(p))
1556-
self.assertTrue(p2.startswith(p))
1556+
self.assertStartsWith(p2, p)
15571557
next = resp.read(len(p2))
15581558
self.assertEqual(next, p2)
15591559
else:
@@ -1578,7 +1578,7 @@ def _verify_readline(self, readline, expected, limit=5):
15781578
line = readline(limit)
15791579
if line and line != b"foo":
15801580
if len(line) < 5:
1581-
self.assertTrue(line.endswith(b"\n"))
1581+
self.assertEndsWith(line, b"\n")
15821582
all.append(line)
15831583
if not line:
15841584
break
@@ -1687,7 +1687,7 @@ def readline(self, limit):
16871687
raise
16881688

16891689

1690-
class OfflineTest(TestCase):
1690+
class OfflineTest(TestCase, ExtraAssertions):
16911691
def test_all(self):
16921692
# Documented objects defined in the module should be in __all__
16931693
expected = {"responses"} # Allowlist documented dict() object
@@ -1773,7 +1773,7 @@ def test_client_constants(self):
17731773
]
17741774
for const in expected:
17751775
with self.subTest(constant=const):
1776-
self.assertTrue(hasattr(client, const))
1776+
self.assertHasAttr(client, const)
17771777

17781778

17791779
class SourceAddressTest(TestCase):
@@ -2241,7 +2241,7 @@ def test_getting_header_defaultint(self):
22412241
header = self.resp.getheader('No-Such-Header',default=42)
22422242
self.assertEqual(header, 42)
22432243

2244-
class TunnelTests(TestCase):
2244+
class TunnelTests(TestCase, ExtraAssertions):
22452245
def setUp(self):
22462246
response_text = (
22472247
'HTTP/1.1 200 OK\r\n\r\n' # Reply to CONNECT
@@ -2415,8 +2415,7 @@ def test_tunnel_connect_single_send_connection_setup(self):
24152415
msg=f'unexpected number of send calls: {mock_send.mock_calls}')
24162416
proxy_setup_data_sent = mock_send.mock_calls[0][1][0]
24172417
self.assertIn(b'CONNECT destination.com', proxy_setup_data_sent)
2418-
self.assertTrue(
2419-
proxy_setup_data_sent.endswith(b'\r\n\r\n'),
2418+
self.assertEndsWith(proxy_setup_data_sent, b'\r\n\r\n',
24202419
msg=f'unexpected proxy data sent {proxy_setup_data_sent!r}')
24212420

24222421
def test_connect_put_request(self):

‎Lib/test/test_httpservers.py

Copy file name to clipboardExpand all lines: Lib/test/test_httpservers.py
+9-9Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from test.support import (
3434
is_apple, os_helper, requires_subprocess, threading_helper
3535
)
36+
from test.support.testcase import ExtraAssertions
3637

3738
support.requires_working_socket(module=True)
3839

@@ -66,7 +67,7 @@ def stop(self):
6667
self.join()
6768

6869

69-
class BaseTestCase(unittest.TestCase):
70+
class BaseTestCase(unittest.TestCase, ExtraAssertions):
7071
def setUp(self):
7172
self._threads = threading_helper.threading_setup()
7273
os.environ = os_helper.EnvironmentVarGuard()
@@ -335,8 +336,7 @@ def test_get(self):
335336
self.con.request('GET', '/')
336337
self.con.getresponse()
337338

338-
self.assertTrue(
339-
err.getvalue().endswith('"GET / HTTP/1.1" 200 -\n'))
339+
self.assertEndsWith(err.getvalue(), '"GET / HTTP/1.1" 200 -\n')
340340

341341
def test_err(self):
342342
self.con = http.client.HTTPConnection(self.HOST, self.PORT)
@@ -347,8 +347,8 @@ def test_err(self):
347347
self.con.getresponse()
348348

349349
lines = err.getvalue().split('\n')
350-
self.assertTrue(lines[0].endswith('code 404, message File not found'))
351-
self.assertTrue(lines[1].endswith('"ERROR / HTTP/1.1" 404 -'))
350+
self.assertEndsWith(lines[0], 'code 404, message File not found')
351+
self.assertEndsWith(lines[1], '"ERROR / HTTP/1.1" 404 -')
352352

353353

354354
class SimpleHTTPServerTestCase(BaseTestCase):
@@ -472,7 +472,7 @@ def test_get_dir_redirect_location_domain_injection_bug(self):
472472
response = self.request(attack_url)
473473
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
474474
location = response.getheader('Location')
475-
self.assertFalse(location.startswith('//'), msg=location)
475+
self.assertNotStartsWith(location, '//')
476476
self.assertEqual(location, expected_location,
477477
msg='Expected Location header to start with a single / and '
478478
'end with a / as this is a directory redirect.')
@@ -495,7 +495,7 @@ def test_get_dir_redirect_location_domain_injection_bug(self):
495495
# We're just ensuring that the scheme and domain make it through, if
496496
# there are or aren't multiple slashes at the start of the path that
497497
# follows that isn't important in this Location: header.
498-
self.assertTrue(location.startswith('https://pypi.org/'), msg=location)
498+
self.assertStartsWith(location, 'https://pypi.org/')
499499

500500
def test_get(self):
501501
#constructs the path relative to the root directory of the HTTPServer
@@ -1006,7 +1006,7 @@ def numWrites(self):
10061006
return len(self.datas)
10071007

10081008

1009-
class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
1009+
class BaseHTTPRequestHandlerTestCase(unittest.TestCase, ExtraAssertions):
10101010
"""Test the functionality of the BaseHTTPServer.
10111011
10121012
Test the support for the Expect 100-continue header.
@@ -1094,7 +1094,7 @@ def test_extra_space(self):
10941094
b'Host: dummy\r\n'
10951095
b'\r\n'
10961096
)
1097-
self.assertTrue(result[0].startswith(b'HTTP/1.1 400 '))
1097+
self.assertStartsWith(result[0], b'HTTP/1.1 400 ')
10981098
self.verify_expected_headers(result[1:result.index(b'\r\n')])
10991099
self.assertFalse(self.handler.get_called)
11001100

0 commit comments

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