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 7076d07

Browse filesBrowse files
gh-71339: Use new assertion methods in the http tests (GH-129058)
1 parent f98b9b4 commit 7076d07
Copy full SHA for 7076d07

File tree

Expand file treeCollapse file tree

4 files changed

+19
-22
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+19
-22
lines changed

‎Lib/test/test_http_cookiejar.py

Copy file name to clipboardExpand all lines: Lib/test/test_http_cookiejar.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,7 @@ def test_netscape_example_1(self):
15371537
h = req.get_header("Cookie")
15381538
self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
15391539
self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
1540-
self.assertTrue(h.startswith("SHIPPING=FEDEX;"))
1540+
self.assertStartsWith(h, "SHIPPING=FEDEX;")
15411541

15421542
def test_netscape_example_2(self):
15431543
# Second Example transaction sequence:

‎Lib/test/test_http_cookies.py

Copy file name to clipboardExpand all lines: Lib/test/test_http_cookies.py
+1-1Lines changed: 1 addition & 1 deletion
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

Copy file name to clipboardExpand all lines: Lib/test/test_httplib.py
+11-13Lines changed: 11 additions & 13 deletions
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')
@@ -1000,8 +1000,7 @@ def test_send_file(self):
10001000
sock = FakeSocket(body)
10011001
conn.sock = sock
10021002
conn.request('GET', '/foo', body)
1003-
self.assertTrue(sock.data.startswith(expected), '%r != %r' %
1004-
(sock.data[:len(expected)], expected))
1003+
self.assertStartsWith(sock.data, expected)
10051004

10061005
def test_send(self):
10071006
expected = b'this is a test this is only a test'
@@ -1564,7 +1563,7 @@ def mypeek(n=-1):
15641563
# then unbounded peek
15651564
p2 = resp.peek()
15661565
self.assertGreaterEqual(len(p2), len(p))
1567-
self.assertTrue(p2.startswith(p))
1566+
self.assertStartsWith(p2, p)
15681567
next = resp.read(len(p2))
15691568
self.assertEqual(next, p2)
15701569
else:
@@ -1589,7 +1588,7 @@ def _verify_readline(self, readline, expected, limit=5):
15891588
line = readline(limit)
15901589
if line and line != b"foo":
15911590
if len(line) < 5:
1592-
self.assertTrue(line.endswith(b"\n"))
1591+
self.assertEndsWith(line, b"\n")
15931592
all.append(line)
15941593
if not line:
15951594
break
@@ -1784,7 +1783,7 @@ def test_client_constants(self):
17841783
]
17851784
for const in expected:
17861785
with self.subTest(constant=const):
1787-
self.assertTrue(hasattr(client, const))
1786+
self.assertHasAttr(client, const)
17881787

17891788

17901789
class SourceAddressTest(TestCase):
@@ -2426,8 +2425,7 @@ def test_tunnel_connect_single_send_connection_setup(self):
24262425
msg=f'unexpected number of send calls: {mock_send.mock_calls}')
24272426
proxy_setup_data_sent = mock_send.mock_calls[0][1][0]
24282427
self.assertIn(b'CONNECT destination.com', proxy_setup_data_sent)
2429-
self.assertTrue(
2430-
proxy_setup_data_sent.endswith(b'\r\n\r\n'),
2428+
self.assertEndsWith(proxy_setup_data_sent, b'\r\n\r\n',
24312429
msg=f'unexpected proxy data sent {proxy_setup_data_sent!r}')
24322430

24332431
def test_connect_put_request(self):

‎Lib/test/test_httpservers.py

Copy file name to clipboardExpand all lines: Lib/test/test_httpservers.py
+6-7Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,7 @@ def test_get(self):
447447
self.con.request('GET', '/')
448448
self.con.getresponse()
449449

450-
self.assertTrue(
451-
err.getvalue().endswith('"GET / HTTP/1.1" 200 -\n'))
450+
self.assertEndsWith(err.getvalue(), '"GET / HTTP/1.1" 200 -\n')
452451

453452
def test_err(self):
454453
self.con = http.client.HTTPConnection(self.HOST, self.PORT)
@@ -459,8 +458,8 @@ def test_err(self):
459458
self.con.getresponse()
460459

461460
lines = err.getvalue().split('\n')
462-
self.assertTrue(lines[0].endswith('code 404, message File not found'))
463-
self.assertTrue(lines[1].endswith('"ERROR / HTTP/1.1" 404 -'))
461+
self.assertEndsWith(lines[0], 'code 404, message File not found')
462+
self.assertEndsWith(lines[1], '"ERROR / HTTP/1.1" 404 -')
464463

465464

466465
class SimpleHTTPServerTestCase(BaseTestCase):
@@ -584,7 +583,7 @@ def test_get_dir_redirect_location_domain_injection_bug(self):
584583
response = self.request(attack_url)
585584
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
586585
location = response.getheader('Location')
587-
self.assertFalse(location.startswith('//'), msg=location)
586+
self.assertNotStartsWith(location, '//')
588587
self.assertEqual(location, expected_location,
589588
msg='Expected Location header to start with a single / and '
590589
'end with a / as this is a directory redirect.')
@@ -607,7 +606,7 @@ def test_get_dir_redirect_location_domain_injection_bug(self):
607606
# We're just ensuring that the scheme and domain make it through, if
608607
# there are or aren't multiple slashes at the start of the path that
609608
# follows that isn't important in this Location: header.
610-
self.assertTrue(location.startswith('https://pypi.org/'), msg=location)
609+
self.assertStartsWith(location, 'https://pypi.org/')
611610

612611
def test_get(self):
613612
#constructs the path relative to the root directory of the HTTPServer
@@ -1206,7 +1205,7 @@ def test_extra_space(self):
12061205
b'Host: dummy\r\n'
12071206
b'\r\n'
12081207
)
1209-
self.assertTrue(result[0].startswith(b'HTTP/1.1 400 '))
1208+
self.assertStartsWith(result[0], b'HTTP/1.1 400 ')
12101209
self.verify_expected_headers(result[1:result.index(b'\r\n')])
12111210
self.assertFalse(self.handler.get_called)
12121211

0 commit comments

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