Skip to content

Commit 2edcf0a

Browse files
bpo-33365: print the header values beside the keys (GH-6611)
with debuglevel=1 only the header keys got printed. With this change the header values get printed as well and the single header entries get '\n' as a separator. (cherry picked from commit 936f03e) Co-authored-by: Marco Strigl <[email protected]>
1 parent 53d1e9f commit 2edcf0a

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

Lib/http/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def begin(self):
321321

322322
if self.debuglevel > 0:
323323
for hdr in self.headers:
324-
print("header:", hdr, end=" ")
324+
print("header:", hdr + ":", self.headers.get(hdr))
325325

326326
# are we using the chunked-style of transfer encoding?
327327
tr_enc = self.headers.get("transfer-encoding")

Lib/test/test_httplib.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,21 @@ def test_invalid_headers(self):
344344
with self.assertRaisesRegex(ValueError, 'Invalid header'):
345345
conn.putheader(name, value)
346346

347+
def test_headers_debuglevel(self):
348+
body = (
349+
b'HTTP/1.1 200 OK\r\n'
350+
b'First: val\r\n'
351+
b'Second: val\r\n'
352+
)
353+
sock = FakeSocket(body)
354+
resp = client.HTTPResponse(sock, debuglevel=1)
355+
with support.captured_stdout() as output:
356+
resp.begin()
357+
lines = output.getvalue().splitlines()
358+
self.assertEqual(lines[0], "reply: 'HTTP/1.1 200 OK\\r\\n'")
359+
self.assertEqual(lines[1], "header: First: val")
360+
self.assertEqual(lines[2], "header: Second: val")
361+
347362

348363
class TransferEncodingTest(TestCase):
349364
expected_body = b"It's just a flesh wound"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Print the header values besides the header keys instead just the header keys if *debuglevel* is set to >0 in :mod:`http.client`. Patch by Marco Strigl.

0 commit comments

Comments
 (0)