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 936f03e7fa)

Co-authored-by: Marco Strigl <mstrigl@suse.com>
This commit is contained in:
Miss Islington (bot) 2018-06-19 06:52:36 -07:00 committed by GitHub
parent 53d1e9fad3
commit 2edcf0a3db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 1 deletions

View File

@ -321,7 +321,7 @@ class HTTPResponse(io.BufferedIOBase):
if self.debuglevel > 0: if self.debuglevel > 0:
for hdr in self.headers: for hdr in self.headers:
print("header:", hdr, end=" ") print("header:", hdr + ":", self.headers.get(hdr))
# are we using the chunked-style of transfer encoding? # are we using the chunked-style of transfer encoding?
tr_enc = self.headers.get("transfer-encoding") tr_enc = self.headers.get("transfer-encoding")

View File

@ -344,6 +344,21 @@ class HeaderTests(TestCase):
with self.assertRaisesRegex(ValueError, 'Invalid header'): with self.assertRaisesRegex(ValueError, 'Invalid header'):
conn.putheader(name, value) conn.putheader(name, value)
def test_headers_debuglevel(self):
body = (
b'HTTP/1.1 200 OK\r\n'
b'First: val\r\n'
b'Second: val\r\n'
)
sock = FakeSocket(body)
resp = client.HTTPResponse(sock, debuglevel=1)
with support.captured_stdout() as output:
resp.begin()
lines = output.getvalue().splitlines()
self.assertEqual(lines[0], "reply: 'HTTP/1.1 200 OK\\r\\n'")
self.assertEqual(lines[1], "header: First: val")
self.assertEqual(lines[2], "header: Second: val")
class TransferEncodingTest(TestCase): class TransferEncodingTest(TestCase):
expected_body = b"It's just a flesh wound" expected_body = b"It's just a flesh wound"

View File

@ -0,0 +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.