gh-146292: Add colour to http.server logs (GH-146293)

Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
This commit is contained in:
Hugo van Kemenade 2026-04-08 15:10:05 +03:00 committed by GitHub
parent 756358524e
commit 2c8f26cf5c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 123 additions and 11 deletions

View file

@ -28,10 +28,12 @@
import threading
from unittest import mock
from io import BytesIO, StringIO
from _colorize import get_theme
import unittest
from test import support
from test.support import (
force_not_colorized,
is_apple, import_helper, os_helper, threading_helper
)
from test.support.script_helper import kill_python, spawn_python
@ -480,6 +482,7 @@ def do_GET(self):
def do_ERROR(self):
self.send_error(HTTPStatus.NOT_FOUND, 'File not found')
@force_not_colorized
def test_get(self):
self.con = http.client.HTTPConnection(self.HOST, self.PORT)
self.con.connect()
@ -490,6 +493,7 @@ def test_get(self):
self.assertEndsWith(err.getvalue(), '"GET / HTTP/1.1" 200 -\n')
@force_not_colorized
def test_err(self):
self.con = http.client.HTTPConnection(self.HOST, self.PORT)
self.con.connect()
@ -503,6 +507,39 @@ def test_err(self):
self.assertEndsWith(lines[1], '"ERROR / HTTP/1.1" 404 -')
@support.force_colorized_test_class
class RequestHandlerColorizedLoggingTestCase(RequestHandlerLoggingTestCase):
def test_get(self):
t = get_theme(force_color=True).http_server
self.con = http.client.HTTPConnection(self.HOST, self.PORT)
self.con.connect()
with support.captured_stderr() as err:
self.con.request("GET", "/")
self.con.getresponse()
output = err.getvalue()
self.assertIn(f"{t.path}/{t.reset}", output)
self.assertIn(f"{t.status_ok}200", output)
self.assertIn(t.reset, output)
def test_err(self):
t = get_theme(force_color=True).http_server
self.con = http.client.HTTPConnection(self.HOST, self.PORT)
self.con.connect()
with support.captured_stderr() as err:
self.con.request("ERROR", "/")
self.con.getresponse()
lines = err.getvalue().split("\n")
self.assertIn(
f"{t.error}code 404, message File not found{t.reset}", lines[0]
)
self.assertIn(f"{t.status_client_error}404", lines[1])
class SimpleHTTPServerTestCase(BaseTestCase):
class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler):
pass
@ -935,6 +972,7 @@ def verify_http_server_response(self, response):
match = self.HTTPResponseMatch.search(response)
self.assertIsNotNone(match)
@force_not_colorized
def test_unprintable_not_logged(self):
# We call the method from the class directly as our Socketless
# Handler subclass overrode it... nice for everything BUT this test.