Skip to content

gh-71339: Use new assertion methods in the http tests #129058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/test/test_http_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,7 @@ def test_netscape_example_1(self):
h = req.get_header("Cookie")
self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
self.assertTrue(h.startswith("SHIPPING=FEDEX;"))
self.assertStartsWith(h, "SHIPPING=FEDEX;")

def test_netscape_example_2(self):
# Second Example transaction sequence:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_http_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_special_attrs(self):
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
C['Customer']['expires'] = 0
# can't test exact output, it always depends on current date/time
self.assertTrue(C.output().endswith('GMT'))
self.assertEndsWith(C.output(), 'GMT')

# loading 'expires'
C = cookies.SimpleCookie()
Expand Down
24 changes: 11 additions & 13 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,31 +273,31 @@ def test_ipv6host_header(self):
sock = FakeSocket('')
conn.sock = sock
conn.request('GET', '/foo')
self.assertTrue(sock.data.startswith(expected))
self.assertStartsWith(sock.data, expected)

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

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

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

def test_malformed_headers_coped_with(self):
# Issue 19996
Expand Down Expand Up @@ -335,9 +335,9 @@ def test_parse_all_octets(self):
self.assertIsNotNone(resp.getheader('obs-text'))
self.assertIn('obs-text', resp.msg)
for folded in (resp.getheader('obs-fold'), resp.msg['obs-fold']):
self.assertTrue(folded.startswith('text'))
self.assertStartsWith(folded, 'text')
self.assertIn(' folded with space', folded)
self.assertTrue(folded.endswith('folded with tab'))
self.assertEndsWith(folded, 'folded with tab')

def test_invalid_headers(self):
conn = client.HTTPConnection('example.com')
Expand Down Expand Up @@ -1000,8 +1000,7 @@ def test_send_file(self):
sock = FakeSocket(body)
conn.sock = sock
conn.request('GET', '/foo', body)
self.assertTrue(sock.data.startswith(expected), '%r != %r' %
(sock.data[:len(expected)], expected))
self.assertStartsWith(sock.data, expected)

def test_send(self):
expected = b'this is a test this is only a test'
Expand Down Expand Up @@ -1545,7 +1544,7 @@ def mypeek(n=-1):
# then unbounded peek
p2 = resp.peek()
self.assertGreaterEqual(len(p2), len(p))
self.assertTrue(p2.startswith(p))
self.assertStartsWith(p2, p)
next = resp.read(len(p2))
self.assertEqual(next, p2)
else:
Expand All @@ -1570,7 +1569,7 @@ def _verify_readline(self, readline, expected, limit=5):
line = readline(limit)
if line and line != b"foo":
if len(line) < 5:
self.assertTrue(line.endswith(b"\n"))
self.assertEndsWith(line, b"\n")
all.append(line)
if not line:
break
Expand Down Expand Up @@ -1765,7 +1764,7 @@ def test_client_constants(self):
]
for const in expected:
with self.subTest(constant=const):
self.assertTrue(hasattr(client, const))
self.assertHasAttr(client, const)


class SourceAddressTest(TestCase):
Expand Down Expand Up @@ -2407,8 +2406,7 @@ def test_tunnel_connect_single_send_connection_setup(self):
msg=f'unexpected number of send calls: {mock_send.mock_calls}')
proxy_setup_data_sent = mock_send.mock_calls[0][1][0]
self.assertIn(b'CONNECT destination.com', proxy_setup_data_sent)
self.assertTrue(
proxy_setup_data_sent.endswith(b'\r\n\r\n'),
self.assertEndsWith(proxy_setup_data_sent, b'\r\n\r\n',
msg=f'unexpected proxy data sent {proxy_setup_data_sent!r}')

def test_connect_put_request(self):
Expand Down
13 changes: 6 additions & 7 deletions Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ def test_get(self):
self.con.request('GET', '/')
self.con.getresponse()

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

def test_err(self):
self.con = http.client.HTTPConnection(self.HOST, self.PORT)
Expand All @@ -347,8 +346,8 @@ def test_err(self):
self.con.getresponse()

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


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

def test_get(self):
#constructs the path relative to the root directory of the HTTPServer
Expand Down Expand Up @@ -1094,7 +1093,7 @@ def test_extra_space(self):
b'Host: dummy\r\n'
b'\r\n'
)
self.assertTrue(result[0].startswith(b'HTTP/1.1 400 '))
self.assertStartsWith(result[0], b'HTTP/1.1 400 ')
self.verify_expected_headers(result[1:result.index(b'\r\n')])
self.assertFalse(self.handler.get_called)

Expand Down
Loading