Add test for `http.client` audit events

This commit is contained in:
gousaiyang 2020-07-08 23:32:30 -07:00
parent 6458e1602e
commit 03d5cfc77f
2 changed files with 33 additions and 0 deletions

View File

@ -323,6 +323,24 @@ def test_socket():
sock.close()
def test_http_client():
import http.client
def hook(event, args):
if event.startswith("http.client."):
print(event, *args[1:])
sys.addaudithook(hook)
conn = http.client.HTTPConnection('www.python.org')
try:
conn.request('GET', '/')
except OSError:
print('http.client.send', '[cannot send]')
finally:
conn.close()
if __name__ == "__main__":
from test.support import suppress_msvcrt_asserts

View File

@ -115,5 +115,20 @@ class AuditTest(unittest.TestCase):
self.assertEqual(events[2][0], "socket.bind")
self.assertTrue(events[2][2].endswith("('127.0.0.1', 8080)"))
def test_http(self):
support.import_module("http.client")
returncode, events, stderr = self.run_python("test_http_client")
if returncode:
self.fail(stderr)
if support.verbose:
print(*events, sep='\n')
self.assertEqual(events[0][0], "http.client.connect")
self.assertEqual(events[0][2], "www.python.org 80")
self.assertEqual(events[1][0], "http.client.send")
if events[1][2] != '[cannot send]':
self.assertIn('HTTP', events[1][2])
if __name__ == "__main__":
unittest.main()