Skip to content

Commit 34cd482

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 8c19a44 commit 34cd482

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
@@ -322,7 +322,7 @@ def begin(self):
322322

323323
if self.debuglevel > 0:
324324
for hdr in self.headers:
325-
print("header:", hdr, end=" ")
325+
print("header:", hdr + ":", self.headers.get(hdr))
326326

327327
# are we using the chunked-style of transfer encoding?
328328
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
@@ -343,6 +343,21 @@ def test_invalid_headers(self):
343343
with self.assertRaisesRegex(ValueError, 'Invalid header'):
344344
conn.putheader(name, value)
345345

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

347362
class TransferEncodingTest(TestCase):
348363
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)