Skip to content

Commit e4e2528

Browse files
committed
gh-145651: Add __repr__ to http.client.HTTPConnection and HTTPResponse
1 parent 5a15a52 commit e4e2528

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

Lib/http/client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ def __init__(self, sock, debuglevel=0, method=None, url=None):
294294
self.length = _UNKNOWN # number of bytes left in response
295295
self.will_close = _UNKNOWN # conn will close at end of response
296296

297+
def __repr__(self):
298+
if self.status is _UNKNOWN:
299+
return '<%s>' % (self.__class__.__name__,)
300+
return '<%s [%s %s]>' % (self.__class__.__name__, self.status, self.reason)
301+
297302
def _read_status(self):
298303
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
299304
if len(line) > _MAXLINE:
@@ -911,6 +916,9 @@ def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
911916
# tests to replace it with a suitable mockup
912917
self._create_connection = socket.create_connection
913918

919+
def __repr__(self):
920+
return '<%s %s:%s>' % (self.__class__.__name__, self.host, self.port if self.port is not None else self.default_port)
921+
914922
def set_tunnel(self, host, port=None, headers=None):
915923
"""Set up host and port for HTTP CONNECT tunnelling.
916924

Lib/test/test_httplib.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,6 +2363,48 @@ def test_getting_header_defaultint(self):
23632363
header = self.resp.getheader('No-Such-Header',default=42)
23642364
self.assertEqual(header, 42)
23652365

2366+
class ReprTest(TestCase):
2367+
2368+
def test_http_connection_repr_default_port(self):
2369+
conn = client.HTTPConnection('example.com')
2370+
self.assertEqual(repr(conn), '<HTTPConnection example.com:80>')
2371+
2372+
def test_http_connection_repr_explicit_port(self):
2373+
conn = client.HTTPConnection('example.com', 8080)
2374+
self.assertEqual(repr(conn), '<HTTPConnection example.com:8080>')
2375+
2376+
def test_https_connection_repr(self):
2377+
if not hasattr(client, 'HTTPSConnection'):
2378+
self.skipTest('ssl support required')
2379+
conn = client.HTTPSConnection('example.com')
2380+
self.assertEqual(repr(conn), '<HTTPSConnection example.com:443>')
2381+
2382+
def test_https_connection_repr_explicit_port(self):
2383+
if not hasattr(client, 'HTTPSConnection'):
2384+
self.skipTest('ssl support required')
2385+
conn = client.HTTPSConnection('example.com', 8443)
2386+
self.assertEqual(repr(conn), '<HTTPSConnection example.com:8443>')
2387+
2388+
def test_http_response_repr_before_read(self):
2389+
sock = FakeSocket(b'HTTP/1.1 200 OK\r\n\r\n')
2390+
resp = client.HTTPResponse(sock)
2391+
self.assertEqual(repr(resp), '<HTTPResponse>')
2392+
2393+
def test_http_response_repr_after_read(self):
2394+
body = b'HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n'
2395+
sock = FakeSocket(body)
2396+
resp = client.HTTPResponse(sock)
2397+
resp.begin()
2398+
self.assertEqual(repr(resp), '<HTTPResponse [200 OK]>')
2399+
2400+
def test_http_response_repr_not_found(self):
2401+
body = b'HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n'
2402+
sock = FakeSocket(body)
2403+
resp = client.HTTPResponse(sock)
2404+
resp.begin()
2405+
self.assertEqual(repr(resp), '<HTTPResponse [404 Not Found]>')
2406+
2407+
23662408
class TunnelTests(TestCase):
23672409
def setUp(self):
23682410
response_text = (

0 commit comments

Comments
 (0)