1616from test import support
1717from test .support import os_helper
1818from test .support import socket_helper
19+ from test .support .testcase import ExtraAssertions
1920
2021support .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 \n Host: [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 \n Host: [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 \n Host: [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
17791779class 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 ):
0 commit comments