]> granicus.if.org Git - python/commitdiff
Issue #13128: Print response headers for CONNECT requests when debuglevel > 0.
authorBerker Peksag <berker.peksag@gmail.com>
Tue, 3 Feb 2015 10:22:11 +0000 (12:22 +0200)
committerBerker Peksag <berker.peksag@gmail.com>
Tue, 3 Feb 2015 10:22:11 +0000 (12:22 +0200)
Patch by Demian Brecht.

Lib/http/client.py
Lib/test/test_httplib.py
Misc/NEWS

index 392715b24dc4f0a0e8674f9c2e727920d98995e3..a77e501296f5a147e426446bcf5f61a7f6ac125b 100644 (file)
@@ -776,6 +776,9 @@ class HTTPConnection:
             if line in (b'\r\n', b'\n', b''):
                 break
 
+            if self.debuglevel > 0:
+                print('header:', line.decode())
+
     def connect(self):
         """Connect to the host and port specified in __init__."""
         self.sock = self._create_connection(
index 31d3184b2a705402ca99a76f1c089ab24034853e..40ef250e9fb5a274daf938dfd24c44db48b31f9d 100644 (file)
@@ -1269,17 +1269,18 @@ class TunnelTests(TestCase):
             'HTTP/1.1 200 OK\r\n' # Reply to HEAD
             'Content-Length: 42\r\n\r\n'
         )
-
-        def create_connection(address, timeout=None, source_address=None):
-            return FakeSocket(response_text, host=address[0], port=address[1])
-
         self.host = 'proxy.com'
         self.conn = client.HTTPConnection(self.host)
-        self.conn._create_connection = create_connection
+        self.conn._create_connection = self._create_connection(response_text)
 
     def tearDown(self):
         self.conn.close()
 
+    def _create_connection(self, response_text):
+        def create_connection(address, timeout=None, source_address=None):
+            return FakeSocket(response_text, host=address[0], port=address[1])
+        return create_connection
+
     def test_set_tunnel_host_port_headers(self):
         tunnel_host = 'destination.com'
         tunnel_port = 8888
@@ -1320,6 +1321,18 @@ class TunnelTests(TestCase):
         self.assertIn(b'CONNECT destination.com', self.conn.sock.data)
         self.assertIn(b'Host: destination.com', self.conn.sock.data)
 
+    def test_tunnel_debuglog(self):
+        expected_header = 'X-Dummy: 1'
+        response_text = 'HTTP/1.0 200 OK\r\n{}\r\n\r\n'.format(expected_header)
+
+        self.conn.set_debuglevel(1)
+        self.conn._create_connection = self._create_connection(response_text)
+        self.conn.set_tunnel('destination.com')
+
+        with support.captured_stdout() as output:
+            self.conn.request('PUT', '/', '')
+        lines = output.getvalue().splitlines()
+        self.assertIn('header: {}'.format(expected_header), lines)
 
 
 @support.reap_threads
index 4952455bd224d9921cbc3cd43f2d1c7df149dcb7..bbe7573e1b23d9cd2ba7e433b495f6e94f4e7c78 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -232,6 +232,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #13128: Print response headers for CONNECT requests when debuglevel
+  > 0. Patch by Demian Brecht.
+
 - Issue #15381: Optimized io.BytesIO to make less allocations and copyings.
 
 - Issue #22818: Splitting on a pattern that could match an empty string now