Skip to content

Commit 7b23cbc

Browse files
committed
Add test for http.client audit events
1 parent 282a120 commit 7b23cbc

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Lib/test/audit-tests.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,24 @@ def hook(event, args):
341341
gc.get_referents(y)
342342

343343

344+
def test_http_client():
345+
import http.client
346+
347+
def hook(event, args):
348+
if event.startswith("http.client."):
349+
print(event, *args[1:])
350+
351+
sys.addaudithook(hook)
352+
353+
conn = http.client.HTTPConnection('www.python.org')
354+
try:
355+
conn.request('GET', '/')
356+
except OSError:
357+
print('http.client.send', '[cannot send]')
358+
finally:
359+
conn.close()
360+
361+
344362
if __name__ == "__main__":
345363
from test.support import suppress_msvcrt_asserts
346364

Lib/test/test_audit.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ def test_gc(self):
130130
["gc.get_objects", "gc.get_referrers", "gc.get_referents"]
131131
)
132132

133+
def test_http(self):
134+
support.import_module("http.client")
135+
returncode, events, stderr = self.run_python("test_http_client")
136+
if returncode:
137+
self.fail(stderr)
138+
139+
if support.verbose:
140+
print(*events, sep='\n')
141+
self.assertEqual(events[0][0], "http.client.connect")
142+
self.assertEqual(events[0][2], "www.python.org 80")
143+
self.assertEqual(events[1][0], "http.client.send")
144+
if events[1][2] != '[cannot send]':
145+
self.assertIn('HTTP', events[1][2])
146+
133147

134148
if __name__ == "__main__":
135149
unittest.main()

0 commit comments

Comments
 (0)