Skip to content

Commit 1b17735

Browse files
serhiy-storchakamiss-islington
authored andcommitted
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 1b17735

File tree

4 files changed

+19
-22
lines changed

4 files changed

+19
-22
lines changed

Lib/test/test_http_cookiejar.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ def test_netscape_example_1(self):
15281528
h = req.get_header("Cookie")
15291529
self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
15301530
self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
1531-
self.assertTrue(h.startswith("SHIPPING=FEDEX;"))
1531+
self.assertStartsWith(h, "SHIPPING=FEDEX;")
15321532

15331533
def test_netscape_example_2(self):
15341534
# Second Example transaction sequence:

Lib/test/test_http_cookies.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def test_special_attrs(self):
180180
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
181181
C['Customer']['expires'] = 0
182182
# can't test exact output, it always depends on current date/time
183-
self.assertTrue(C.output().endswith('GMT'))
183+
self.assertEndsWith(C.output(), 'GMT')
184184

185185
# loading 'expires'
186186
C = cookies.SimpleCookie()

Lib/test/test_httplib.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -273,31 +273,31 @@ def test_ipv6host_header(self):
273273
sock = FakeSocket('')
274274
conn.sock = sock
275275
conn.request('GET', '/foo')
276-
self.assertTrue(sock.data.startswith(expected))
276+
self.assertStartsWith(sock.data, expected)
277277

278278
expected = b'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \
279279
b'Accept-Encoding: identity\r\n\r\n'
280280
conn = client.HTTPConnection('[2001:102A::]')
281281
sock = FakeSocket('')
282282
conn.sock = sock
283283
conn.request('GET', '/foo')
284-
self.assertTrue(sock.data.startswith(expected))
284+
self.assertStartsWith(sock.data, expected)
285285

286286
expected = b'GET /foo HTTP/1.1\r\nHost: [fe80::]\r\n' \
287287
b'Accept-Encoding: identity\r\n\r\n'
288288
conn = client.HTTPConnection('[fe80::%2]')
289289
sock = FakeSocket('')
290290
conn.sock = sock
291291
conn.request('GET', '/foo')
292-
self.assertTrue(sock.data.startswith(expected))
292+
self.assertStartsWith(sock.data, expected)
293293

294294
expected = b'GET /foo HTTP/1.1\r\nHost: [fe80::]:81\r\n' \
295295
b'Accept-Encoding: identity\r\n\r\n'
296296
conn = client.HTTPConnection('[fe80::%2]:81')
297297
sock = FakeSocket('')
298298
conn.sock = sock
299299
conn.request('GET', '/foo')
300-
self.assertTrue(sock.data.startswith(expected))
300+
self.assertStartsWith(sock.data, expected)
301301

302302
def test_malformed_headers_coped_with(self):
303303
# Issue 19996
@@ -335,9 +335,9 @@ def test_parse_all_octets(self):
335335
self.assertIsNotNone(resp.getheader('obs-text'))
336336
self.assertIn('obs-text', resp.msg)
337337
for folded in (resp.getheader('obs-fold'), resp.msg['obs-fold']):
338-
self.assertTrue(folded.startswith('text'))
338+
self.assertStartsWith(folded, 'text')
339339
self.assertIn(' folded with space', folded)
340-
self.assertTrue(folded.endswith('folded with tab'))
340+
self.assertEndsWith(folded, 'folded with tab')
341341

342342
def test_invalid_headers(self):
343343
conn = client.HTTPConnection('example.com')
@@ -989,8 +989,7 @@ def test_send_file(self):
989989
sock = FakeSocket(body)
990990
conn.sock = sock
991991
conn.request('GET', '/foo', body)
992-
self.assertTrue(sock.data.startswith(expected), '%r != %r' %
993-
(sock.data[:len(expected)], expected))
992+
self.assertStartsWith(sock.data, expected)
994993

995994
def test_send(self):
996995
expected = b'this is a test this is only a test'
@@ -1553,7 +1552,7 @@ def mypeek(n=-1):
15531552
# then unbounded peek
15541553
p2 = resp.peek()
15551554
self.assertGreaterEqual(len(p2), len(p))
1556-
self.assertTrue(p2.startswith(p))
1555+
self.assertStartsWith(p2, p)
15571556
next = resp.read(len(p2))
15581557
self.assertEqual(next, p2)
15591558
else:
@@ -1578,7 +1577,7 @@ def _verify_readline(self, readline, expected, limit=5):
15781577
line = readline(limit)
15791578
if line and line != b"foo":
15801579
if len(line) < 5:
1581-
self.assertTrue(line.endswith(b"\n"))
1580+
self.assertEndsWith(line, b"\n")
15821581
all.append(line)
15831582
if not line:
15841583
break
@@ -1773,7 +1772,7 @@ def test_client_constants(self):
17731772
]
17741773
for const in expected:
17751774
with self.subTest(constant=const):
1776-
self.assertTrue(hasattr(client, const))
1775+
self.assertHasAttr(client, const)
17771776

17781777

17791778
class SourceAddressTest(TestCase):
@@ -2415,8 +2414,7 @@ def test_tunnel_connect_single_send_connection_setup(self):
24152414
msg=f'unexpected number of send calls: {mock_send.mock_calls}')
24162415
proxy_setup_data_sent = mock_send.mock_calls[0][1][0]
24172416
self.assertIn(b'CONNECT destination.com', proxy_setup_data_sent)
2418-
self.assertTrue(
2419-
proxy_setup_data_sent.endswith(b'\r\n\r\n'),
2417+
self.assertEndsWith(proxy_setup_data_sent, b'\r\n\r\n',
24202418
msg=f'unexpected proxy data sent {proxy_setup_data_sent!r}')
24212419

24222420
def test_connect_put_request(self):

Lib/test/test_httpservers.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,7 @@ def test_get(self):
335335
self.con.request('GET', '/')
336336
self.con.getresponse()
337337

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

341340
def test_err(self):
342341
self.con = http.client.HTTPConnection(self.HOST, self.PORT)
@@ -347,8 +346,8 @@ def test_err(self):
347346
self.con.getresponse()
348347

349348
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 -'))
349+
self.assertEndsWith(lines[0], 'code 404, message File not found')
350+
self.assertEndsWith(lines[1], '"ERROR / HTTP/1.1" 404 -')
352351

353352

354353
class SimpleHTTPServerTestCase(BaseTestCase):
@@ -472,7 +471,7 @@ def test_get_dir_redirect_location_domain_injection_bug(self):
472471
response = self.request(attack_url)
473472
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
474473
location = response.getheader('Location')
475-
self.assertFalse(location.startswith('//'), msg=location)
474+
self.assertNotStartsWith(location, '//')
476475
self.assertEqual(location, expected_location,
477476
msg='Expected Location header to start with a single / and '
478477
'end with a / as this is a directory redirect.')
@@ -495,7 +494,7 @@ def test_get_dir_redirect_location_domain_injection_bug(self):
495494
# We're just ensuring that the scheme and domain make it through, if
496495
# there are or aren't multiple slashes at the start of the path that
497496
# follows that isn't important in this Location: header.
498-
self.assertTrue(location.startswith('https://pypi.org/'), msg=location)
497+
self.assertStartsWith(location, 'https://pypi.org/')
499498

500499
def test_get(self):
501500
#constructs the path relative to the root directory of the HTTPServer
@@ -1094,7 +1093,7 @@ def test_extra_space(self):
10941093
b'Host: dummy\r\n'
10951094
b'\r\n'
10961095
)
1097-
self.assertTrue(result[0].startswith(b'HTTP/1.1 400 '))
1096+
self.assertStartsWith(result[0], b'HTTP/1.1 400 ')
10981097
self.verify_expected_headers(result[1:result.index(b'\r\n')])
10991098
self.assertFalse(self.handler.get_called)
11001099

0 commit comments

Comments
 (0)