Repository: mitmproxy/mitmproxy

Issue:
py3++: test_protocol_http2
Only 1 test passes for now!



Discussion:

Resolving patch:
diff --git a/mitmproxy/protocol/http2.py b/mitmproxy/protocol/http2.py
--- a/mitmproxy/protocol/http2.py
+++ b/mitmproxy/protocol/http2.py
@@ -197,7 +197,7 @@ def _handle_event(self, event, source_conn, other_conn, is_server):
                 self.client_conn.h2.push_stream(parent_eid, event.pushed_stream_id, event.headers)
                 self.client_conn.send(self.client_conn.h2.data_to_send())
 
-            headers = netlib.http.Headers([[str(k), str(v)] for k, v in event.headers])
+            headers = netlib.http.Headers([[k, v] for k, v in event.headers])
             self.streams[event.pushed_stream_id] = Http2SingleStreamLayer(self, event.pushed_stream_id, headers)
             self.streams[event.pushed_stream_id].timestamp_start = time.time()
             self.streams[event.pushed_stream_id].pushed = True
@@ -434,7 +434,7 @@ def send_request(self, message):
             self.server_conn.h2.safe_send_body(
                 self.is_zombie,
                 self.server_stream_id,
-                message.body
+                [message.body]
             )
 
         if self.zombie:  # pragma: no cover
@@ -453,7 +453,7 @@ def read_response_headers(self):
         return models.HTTPResponse(
             http_version=b"HTTP/2.0",
             status_code=status_code,
-            reason='',
+            reason=b'',
             headers=headers,
             content=None,
             timestamp_start=self.timestamp_start,


Test patch:
diff --git a/test/mitmproxy/test_protocol_http2.py b/test/mitmproxy/test_protocol_http2.py
--- a/test/mitmproxy/test_protocol_http2.py
+++ b/test/mitmproxy/test_protocol_http2.py
@@ -126,7 +126,7 @@ def _setup_connection(self):
         client.wfile.flush()
 
         # read CONNECT response
-        while client.rfile.readline() != "\r\n":
+        while client.rfile.readline() != b"\r\n":
             pass
 
         client.convert_to_ssl(alpn_protos=[b'h2'])
@@ -169,8 +169,8 @@ def handle_server_event(self, event, h2_conn, rfile, wfile):
         if isinstance(event, h2.events.ConnectionTerminated):
             return False
         elif isinstance(event, h2.events.RequestReceived):
-            assert ('client-foo', 'client-bar-1') in event.headers
-            assert ('client-foo', 'client-bar-2') in event.headers
+            assert (b'client-foo', b'client-bar-1') in event.headers
+            assert (b'client-foo', b'client-bar-2') in event.headers
 
             import warnings
             with warnings.catch_warnings():
@@ -203,7 +203,7 @@ def test_simple(self):
             (':path', '/'),
             ('ClIeNt-FoO', 'client-bar-1'),
             ('ClIeNt-FoO', 'client-bar-2'),
-        ], body='my request body echoed back to me')
+        ], body=b'my request body echoed back to me')
 
         done = False
         while not done:
@@ -275,7 +275,7 @@ def test_with_bodies(self):
                 (':scheme', 'https'),
                 (':path', '/'),
             ],
-            body='foobar with request body',
+            body=b'foobar with request body',
         )
 
         done = False
@@ -538,7 +538,7 @@ def handle_server_event(self, event, h2_conn, rfile, wfile):
                 (':status', '200'),
                 ('X-Stream-ID', str(event.stream_id)),
             ])
-            h2_conn.send_data(event.stream_id, b'Stream-ID {}'.format(event.stream_id))
+            h2_conn.send_data(event.stream_id, 'Stream-ID {}'.format(event.stream_id).encode())
             h2_conn.end_stream(event.stream_id)
             wfile.write(h2_conn.data_to_send())
             wfile.flush()
@@ -579,7 +579,7 @@ def test_max_concurrent_streams(self):
         assert len(self.master.state.flows) == len(new_streams)
         for flow in self.master.state.flows:
             assert flow.response.status_code == 200
-            assert "Stream-ID" in flow.response.body
+            assert b"Stream-ID " in flow.response.body
 
 
 @requires_alpn
@@ -598,7 +598,7 @@ def teardown_class(self):
     @classmethod
     def handle_server_event(self, event, h2_conn, rfile, wfile):
         if isinstance(event, h2.events.RequestReceived):
-            h2_conn.close_connection(error_code=5, last_stream_id=42, additional_data='foobar')
+            h2_conn.close_connection(error_code=5, last_stream_id=42, additional_data=b'foobar')
             wfile.write(h2_conn.data_to_send())
             wfile.flush()
         return True
@@ -630,4 +630,4 @@ def test_connection_terminated(self):
         assert connection_terminated_event is not None
         assert connection_terminated_event.error_code == 5
         assert connection_terminated_event.last_stream_id == 42
-        assert connection_terminated_event.additional_data == 'foobar'
+        assert connection_terminated_event.additional_data == b'foobar'