http2: improve error handling in tests

This commit is contained in:
Thomas Kriechbaumer 2016-06-22 22:09:57 +02:00
parent aaf0bba496
commit 5d0de16f57
2 changed files with 31 additions and 13 deletions

View File

@ -1,6 +1,7 @@
import codecs import codecs
import hyperframe import hyperframe
from ...exceptions import HttpException
def http2_read_raw_frame(rfile): def http2_read_raw_frame(rfile):
@ -8,7 +9,7 @@ def http2_read_raw_frame(rfile):
length = int(codecs.encode(header[:3], 'hex_codec'), 16) length = int(codecs.encode(header[:3], 'hex_codec'), 16)
if length == 4740180: if length == 4740180:
raise ValueError("Length field looks more like HTTP/1.1: %s" % rfile.peek(20)) raise HttpException("Length field looks more like HTTP/1.1:\n{}".format(rfile.read(-1)))
body = rfile.safe_read(length) body = rfile.safe_read(length)
return [header, body] return [header, body]

View File

@ -13,6 +13,7 @@ from mitmproxy.cmdline import APP_HOST, APP_PORT
import netlib import netlib
from ..netlib import tservers as netlib_tservers from ..netlib import tservers as netlib_tservers
from netlib.exceptions import HttpException
from netlib.http.http2 import framereader from netlib.http.http2 import framereader
from . import tservers from . import tservers
@ -50,6 +51,9 @@ class _Http2ServerBase(netlib_tservers.ServerTestBase):
try: try:
raw = b''.join(framereader.http2_read_raw_frame(self.rfile)) raw = b''.join(framereader.http2_read_raw_frame(self.rfile))
events = h2_conn.receive_data(raw) events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
assert False
except: except:
break break
self.wfile.write(h2_conn.data_to_send()) self.wfile.write(h2_conn.data_to_send())
@ -60,9 +64,7 @@ class _Http2ServerBase(netlib_tservers.ServerTestBase):
if not self.server.handle_server_event(event, h2_conn, self.rfile, self.wfile): if not self.server.handle_server_event(event, h2_conn, self.rfile, self.wfile):
done = True done = True
break break
except Exception as e: except:
print(repr(e))
print(traceback.format_exc())
done = True done = True
break break
@ -200,9 +202,12 @@ class TestSimple(_Http2TestBase, _Http2ServerBase):
done = False done = False
while not done: while not done:
try: try:
events = h2_conn.receive_data(b''.join(framereader.http2_read_raw_frame(client.rfile))) raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
except: events = h2_conn.receive_data(raw)
break except HttpException:
print(traceback.format_exc())
assert False
client.wfile.write(h2_conn.data_to_send()) client.wfile.write(h2_conn.data_to_send())
client.wfile.flush() client.wfile.flush()
@ -270,9 +275,12 @@ class TestWithBodies(_Http2TestBase, _Http2ServerBase):
done = False done = False
while not done: while not done:
try: try:
events = h2_conn.receive_data(b''.join(framereader.http2_read_raw_frame(client.rfile))) raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
except: events = h2_conn.receive_data(raw)
break except HttpException:
print(traceback.format_exc())
assert False
client.wfile.write(h2_conn.data_to_send()) client.wfile.write(h2_conn.data_to_send())
client.wfile.flush() client.wfile.flush()
@ -364,6 +372,9 @@ class TestPushPromise(_Http2TestBase, _Http2ServerBase):
try: try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile)) raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
events = h2_conn.receive_data(raw) events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
assert False
except: except:
break break
client.wfile.write(h2_conn.data_to_send()) client.wfile.write(h2_conn.data_to_send())
@ -412,9 +423,12 @@ class TestPushPromise(_Http2TestBase, _Http2ServerBase):
responses = 0 responses = 0
while not done: while not done:
try: try:
events = h2_conn.receive_data(b''.join(framereader.http2_read_raw_frame(client.rfile))) raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
except: events = h2_conn.receive_data(raw)
break except HttpException:
print(traceback.format_exc())
assert False
client.wfile.write(h2_conn.data_to_send()) client.wfile.write(h2_conn.data_to_send())
client.wfile.flush() client.wfile.flush()
@ -481,6 +495,9 @@ class TestConnectionLost(_Http2TestBase, _Http2ServerBase):
try: try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile)) raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
h2_conn.receive_data(raw) h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
assert False
except: except:
break break
try: try: