16
16
from test import support
17
17
from test.support import os_helper
18
18
from test.support import socket_helper
19
+ from test.support.testcase import ExtraAssertions
19
20
20
21
support.requires_working_socket(module=True)
21
22
@@ -134,7 +135,7 @@ def connect(self):
134
135
def create_connection(self, *pos, **kw):
135
136
return FakeSocket(*self.fake_socket_args)
136
137
137
- class HeaderTests(TestCase):
138
+ class HeaderTests(TestCase, ExtraAssertions ):
138
139
def test_auto_headers(self):
139
140
# Some headers are added automatically, but should not be added by
140
141
# .request() if they are explicitly set.
@@ -273,31 +274,31 @@ def test_ipv6host_header(self):
273
274
sock = FakeSocket('')
274
275
conn.sock = sock
275
276
conn.request('GET', '/foo')
276
- self.assertTrue (sock.data.startswith( expected) )
277
+ self.assertStartsWith (sock.data, expected)
277
278
278
279
expected = b'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \
279
280
b'Accept-Encoding: identity\r\n\r\n'
280
281
conn = client.HTTPConnection('[2001:102A::]')
281
282
sock = FakeSocket('')
282
283
conn.sock = sock
283
284
conn.request('GET', '/foo')
284
- self.assertTrue (sock.data.startswith( expected) )
285
+ self.assertStartsWith (sock.data, expected)
285
286
286
287
expected = b'GET /foo HTTP/1.1\r\nHost: [fe80::]\r\n' \
287
288
b'Accept-Encoding: identity\r\n\r\n'
288
289
conn = client.HTTPConnection('[fe80::%2]')
289
290
sock = FakeSocket('')
290
291
conn.sock = sock
291
292
conn.request('GET', '/foo')
292
- self.assertTrue (sock.data.startswith( expected) )
293
+ self.assertStartsWith (sock.data, expected)
293
294
294
295
expected = b'GET /foo HTTP/1.1\r\nHost: [fe80::]:81\r\n' \
295
296
b'Accept-Encoding: identity\r\n\r\n'
296
297
conn = client.HTTPConnection('[fe80::%2]:81')
297
298
sock = FakeSocket('')
298
299
conn.sock = sock
299
300
conn.request('GET', '/foo')
300
- self.assertTrue (sock.data.startswith( expected) )
301
+ self.assertStartsWith (sock.data, expected)
301
302
302
303
def test_malformed_headers_coped_with(self):
303
304
# Issue 19996
@@ -335,9 +336,9 @@ def test_parse_all_octets(self):
335
336
self.assertIsNotNone(resp.getheader('obs-text'))
336
337
self.assertIn('obs-text', resp.msg)
337
338
for folded in (resp.getheader('obs-fold'), resp.msg['obs-fold']):
338
- self.assertTrue (folded.startswith( 'text') )
339
+ self.assertStartsWith (folded, 'text')
339
340
self.assertIn(' folded with space', folded)
340
- self.assertTrue (folded.endswith( 'folded with tab') )
341
+ self.assertEndsWith (folded, 'folded with tab')
341
342
342
343
def test_invalid_headers(self):
343
344
conn = client.HTTPConnection('example.com')
@@ -537,7 +538,7 @@ def _parse_chunked(self, data):
537
538
return b''.join(body)
538
539
539
540
540
- class BasicTest(TestCase):
541
+ class BasicTest(TestCase, ExtraAssertions ):
541
542
def test_dir_with_added_behavior_on_status(self):
542
543
# see issue40084
543
544
self.assertTrue({'description', 'name', 'phrase', 'value'} <= set(dir(HTTPStatus(404))))
@@ -989,8 +990,7 @@ def test_send_file(self):
989
990
sock = FakeSocket(body)
990
991
conn.sock = sock
991
992
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)
994
994
995
995
def test_send(self):
996
996
expected = b'this is a test this is only a test'
@@ -1494,7 +1494,7 @@ def _encode_request(self, str_url):
1494
1494
conn.putrequest('GET', '/☃')
1495
1495
1496
1496
1497
- class ExtendedReadTest(TestCase):
1497
+ class ExtendedReadTest(TestCase, ExtraAssertions ):
1498
1498
"""
1499
1499
Test peek(), read1(), readline()
1500
1500
"""
@@ -1553,7 +1553,7 @@ def mypeek(n=-1):
1553
1553
# then unbounded peek
1554
1554
p2 = resp.peek()
1555
1555
self.assertGreaterEqual(len(p2), len(p))
1556
- self.assertTrue (p2.startswith(p) )
1556
+ self.assertStartsWith (p2, p )
1557
1557
next = resp.read(len(p2))
1558
1558
self.assertEqual(next, p2)
1559
1559
else:
@@ -1578,7 +1578,7 @@ def _verify_readline(self, readline, expected, limit=5):
1578
1578
line = readline(limit)
1579
1579
if line and line != b"foo":
1580
1580
if len(line) < 5:
1581
- self.assertTrue (line.endswith( b"\n") )
1581
+ self.assertEndsWith (line, b"\n")
1582
1582
all.append(line)
1583
1583
if not line:
1584
1584
break
@@ -1687,7 +1687,7 @@ def readline(self, limit):
1687
1687
raise
1688
1688
1689
1689
1690
- class OfflineTest(TestCase):
1690
+ class OfflineTest(TestCase, ExtraAssertions ):
1691
1691
def test_all(self):
1692
1692
# Documented objects defined in the module should be in __all__
1693
1693
expected = {"responses"} # Allowlist documented dict() object
@@ -1773,7 +1773,7 @@ def test_client_constants(self):
1773
1773
]
1774
1774
for const in expected:
1775
1775
with self.subTest(constant=const):
1776
- self.assertTrue(hasattr( client, const) )
1776
+ self.assertHasAttr( client, const)
1777
1777
1778
1778
1779
1779
class SourceAddressTest(TestCase):
@@ -2241,7 +2241,7 @@ def test_getting_header_defaultint(self):
2241
2241
header = self.resp.getheader('No-Such-Header',default=42)
2242
2242
self.assertEqual(header, 42)
2243
2243
2244
- class TunnelTests(TestCase):
2244
+ class TunnelTests(TestCase, ExtraAssertions ):
2245
2245
def setUp(self):
2246
2246
response_text = (
2247
2247
'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):
2415
2415
msg=f'unexpected number of send calls: {mock_send.mock_calls}')
2416
2416
proxy_setup_data_sent = mock_send.mock_calls[0][1][0]
2417
2417
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',
2420
2419
msg=f'unexpected proxy data sent {proxy_setup_data_sent!r}')
2421
2420
2422
2421
def test_connect_put_request(self):