gh-71339: Use new assertion methods in the http tests (GH-129058)
This commit is contained in:
parent
f98b9b4cbb
commit
7076d076c2
@ -1537,7 +1537,7 @@ class LWPCookieTests(unittest.TestCase):
|
|||||||
h = req.get_header("Cookie")
|
h = req.get_header("Cookie")
|
||||||
self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
|
self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
|
||||||
self.assertIn("CUSTOMER=WILE_E_COYOTE", 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):
|
def test_netscape_example_2(self):
|
||||||
# Second Example transaction sequence:
|
# Second Example transaction sequence:
|
||||||
|
@ -180,7 +180,7 @@ class CookieTests(unittest.TestCase):
|
|||||||
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
|
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
|
||||||
C['Customer']['expires'] = 0
|
C['Customer']['expires'] = 0
|
||||||
# can't test exact output, it always depends on current date/time
|
# 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'
|
# loading 'expires'
|
||||||
C = cookies.SimpleCookie()
|
C = cookies.SimpleCookie()
|
||||||
|
@ -273,7 +273,7 @@ class HeaderTests(TestCase):
|
|||||||
sock = FakeSocket('')
|
sock = FakeSocket('')
|
||||||
conn.sock = sock
|
conn.sock = sock
|
||||||
conn.request('GET', '/foo')
|
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' \
|
expected = b'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \
|
||||||
b'Accept-Encoding: identity\r\n\r\n'
|
b'Accept-Encoding: identity\r\n\r\n'
|
||||||
@ -281,7 +281,7 @@ class HeaderTests(TestCase):
|
|||||||
sock = FakeSocket('')
|
sock = FakeSocket('')
|
||||||
conn.sock = sock
|
conn.sock = sock
|
||||||
conn.request('GET', '/foo')
|
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' \
|
expected = b'GET /foo HTTP/1.1\r\nHost: [fe80::]\r\n' \
|
||||||
b'Accept-Encoding: identity\r\n\r\n'
|
b'Accept-Encoding: identity\r\n\r\n'
|
||||||
@ -289,7 +289,7 @@ class HeaderTests(TestCase):
|
|||||||
sock = FakeSocket('')
|
sock = FakeSocket('')
|
||||||
conn.sock = sock
|
conn.sock = sock
|
||||||
conn.request('GET', '/foo')
|
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' \
|
expected = b'GET /foo HTTP/1.1\r\nHost: [fe80::]:81\r\n' \
|
||||||
b'Accept-Encoding: identity\r\n\r\n'
|
b'Accept-Encoding: identity\r\n\r\n'
|
||||||
@ -297,7 +297,7 @@ class HeaderTests(TestCase):
|
|||||||
sock = FakeSocket('')
|
sock = FakeSocket('')
|
||||||
conn.sock = sock
|
conn.sock = sock
|
||||||
conn.request('GET', '/foo')
|
conn.request('GET', '/foo')
|
||||||
self.assertTrue(sock.data.startswith(expected))
|
self.assertStartsWith(sock.data, expected)
|
||||||
|
|
||||||
def test_malformed_headers_coped_with(self):
|
def test_malformed_headers_coped_with(self):
|
||||||
# Issue 19996
|
# Issue 19996
|
||||||
@ -335,9 +335,9 @@ class HeaderTests(TestCase):
|
|||||||
self.assertIsNotNone(resp.getheader('obs-text'))
|
self.assertIsNotNone(resp.getheader('obs-text'))
|
||||||
self.assertIn('obs-text', resp.msg)
|
self.assertIn('obs-text', resp.msg)
|
||||||
for folded in (resp.getheader('obs-fold'), resp.msg['obs-fold']):
|
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.assertIn(' folded with space', folded)
|
||||||
self.assertTrue(folded.endswith('folded with tab'))
|
self.assertEndsWith(folded, 'folded with tab')
|
||||||
|
|
||||||
def test_invalid_headers(self):
|
def test_invalid_headers(self):
|
||||||
conn = client.HTTPConnection('example.com')
|
conn = client.HTTPConnection('example.com')
|
||||||
@ -1000,8 +1000,7 @@ class BasicTest(TestCase):
|
|||||||
sock = FakeSocket(body)
|
sock = FakeSocket(body)
|
||||||
conn.sock = sock
|
conn.sock = sock
|
||||||
conn.request('GET', '/foo', body)
|
conn.request('GET', '/foo', body)
|
||||||
self.assertTrue(sock.data.startswith(expected), '%r != %r' %
|
self.assertStartsWith(sock.data, expected)
|
||||||
(sock.data[:len(expected)], expected))
|
|
||||||
|
|
||||||
def test_send(self):
|
def test_send(self):
|
||||||
expected = b'this is a test this is only a test'
|
expected = b'this is a test this is only a test'
|
||||||
@ -1564,7 +1563,7 @@ class ExtendedReadTest(TestCase):
|
|||||||
# then unbounded peek
|
# then unbounded peek
|
||||||
p2 = resp.peek()
|
p2 = resp.peek()
|
||||||
self.assertGreaterEqual(len(p2), len(p))
|
self.assertGreaterEqual(len(p2), len(p))
|
||||||
self.assertTrue(p2.startswith(p))
|
self.assertStartsWith(p2, p)
|
||||||
next = resp.read(len(p2))
|
next = resp.read(len(p2))
|
||||||
self.assertEqual(next, p2)
|
self.assertEqual(next, p2)
|
||||||
else:
|
else:
|
||||||
@ -1589,7 +1588,7 @@ class ExtendedReadTest(TestCase):
|
|||||||
line = readline(limit)
|
line = readline(limit)
|
||||||
if line and line != b"foo":
|
if line and line != b"foo":
|
||||||
if len(line) < 5:
|
if len(line) < 5:
|
||||||
self.assertTrue(line.endswith(b"\n"))
|
self.assertEndsWith(line, b"\n")
|
||||||
all.append(line)
|
all.append(line)
|
||||||
if not line:
|
if not line:
|
||||||
break
|
break
|
||||||
@ -1784,7 +1783,7 @@ class OfflineTest(TestCase):
|
|||||||
]
|
]
|
||||||
for const in expected:
|
for const in expected:
|
||||||
with self.subTest(constant=const):
|
with self.subTest(constant=const):
|
||||||
self.assertTrue(hasattr(client, const))
|
self.assertHasAttr(client, const)
|
||||||
|
|
||||||
|
|
||||||
class SourceAddressTest(TestCase):
|
class SourceAddressTest(TestCase):
|
||||||
@ -2426,8 +2425,7 @@ class TunnelTests(TestCase):
|
|||||||
msg=f'unexpected number of send calls: {mock_send.mock_calls}')
|
msg=f'unexpected number of send calls: {mock_send.mock_calls}')
|
||||||
proxy_setup_data_sent = mock_send.mock_calls[0][1][0]
|
proxy_setup_data_sent = mock_send.mock_calls[0][1][0]
|
||||||
self.assertIn(b'CONNECT destination.com', proxy_setup_data_sent)
|
self.assertIn(b'CONNECT destination.com', proxy_setup_data_sent)
|
||||||
self.assertTrue(
|
self.assertEndsWith(proxy_setup_data_sent, b'\r\n\r\n',
|
||||||
proxy_setup_data_sent.endswith(b'\r\n\r\n'),
|
|
||||||
msg=f'unexpected proxy data sent {proxy_setup_data_sent!r}')
|
msg=f'unexpected proxy data sent {proxy_setup_data_sent!r}')
|
||||||
|
|
||||||
def test_connect_put_request(self):
|
def test_connect_put_request(self):
|
||||||
|
@ -447,8 +447,7 @@ class RequestHandlerLoggingTestCase(BaseTestCase):
|
|||||||
self.con.request('GET', '/')
|
self.con.request('GET', '/')
|
||||||
self.con.getresponse()
|
self.con.getresponse()
|
||||||
|
|
||||||
self.assertTrue(
|
self.assertEndsWith(err.getvalue(), '"GET / HTTP/1.1" 200 -\n')
|
||||||
err.getvalue().endswith('"GET / HTTP/1.1" 200 -\n'))
|
|
||||||
|
|
||||||
def test_err(self):
|
def test_err(self):
|
||||||
self.con = http.client.HTTPConnection(self.HOST, self.PORT)
|
self.con = http.client.HTTPConnection(self.HOST, self.PORT)
|
||||||
@ -459,8 +458,8 @@ class RequestHandlerLoggingTestCase(BaseTestCase):
|
|||||||
self.con.getresponse()
|
self.con.getresponse()
|
||||||
|
|
||||||
lines = err.getvalue().split('\n')
|
lines = err.getvalue().split('\n')
|
||||||
self.assertTrue(lines[0].endswith('code 404, message File not found'))
|
self.assertEndsWith(lines[0], 'code 404, message File not found')
|
||||||
self.assertTrue(lines[1].endswith('"ERROR / HTTP/1.1" 404 -'))
|
self.assertEndsWith(lines[1], '"ERROR / HTTP/1.1" 404 -')
|
||||||
|
|
||||||
|
|
||||||
class SimpleHTTPServerTestCase(BaseTestCase):
|
class SimpleHTTPServerTestCase(BaseTestCase):
|
||||||
@ -584,7 +583,7 @@ class SimpleHTTPServerTestCase(BaseTestCase):
|
|||||||
response = self.request(attack_url)
|
response = self.request(attack_url)
|
||||||
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
|
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
|
||||||
location = response.getheader('Location')
|
location = response.getheader('Location')
|
||||||
self.assertFalse(location.startswith('//'), msg=location)
|
self.assertNotStartsWith(location, '//')
|
||||||
self.assertEqual(location, expected_location,
|
self.assertEqual(location, expected_location,
|
||||||
msg='Expected Location header to start with a single / and '
|
msg='Expected Location header to start with a single / and '
|
||||||
'end with a / as this is a directory redirect.')
|
'end with a / as this is a directory redirect.')
|
||||||
@ -607,7 +606,7 @@ class SimpleHTTPServerTestCase(BaseTestCase):
|
|||||||
# We're just ensuring that the scheme and domain make it through, if
|
# 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
|
# there are or aren't multiple slashes at the start of the path that
|
||||||
# follows that isn't important in this Location: header.
|
# 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):
|
def test_get(self):
|
||||||
#constructs the path relative to the root directory of the HTTPServer
|
#constructs the path relative to the root directory of the HTTPServer
|
||||||
@ -1206,7 +1205,7 @@ class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
|
|||||||
b'Host: dummy\r\n'
|
b'Host: dummy\r\n'
|
||||||
b'\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.verify_expected_headers(result[1:result.index(b'\r\n')])
|
||||||
self.assertFalse(self.handler.get_called)
|
self.assertFalse(self.handler.get_called)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user