bpo-37363: Add audit events to the `http.client` module (GH-21321)
Add audit events to the `http.client` module
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py
index 8e66594..2addf97 100644
--- a/Lib/test/audit-tests.py
+++ b/Lib/test/audit-tests.py
@@ -341,6 +341,24 @@ def hook(event, args):
gc.get_referents(y)
+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
diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py
index 58180e1..456a5da 100644
--- a/Lib/test/test_audit.py
+++ b/Lib/test/test_audit.py
@@ -130,6 +130,20 @@ def test_gc(self):
["gc.get_objects", "gc.get_referrers", "gc.get_referents"]
)
+ def test_http(self):
+ import_helper.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()