mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-27 02:24:18 +00:00
Change test API to allow multiple pathoc requests
Add simple unit test for websocket server
This commit is contained in:
parent
2ebe994375
commit
6122f2da44
@ -103,8 +103,15 @@ class PathodHandler(tcp.BaseHandler):
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
frm = websockets.Frame.from_file(self.rfile)
|
frm = websockets.Frame.from_file(self.rfile)
|
||||||
|
retlog = dict(
|
||||||
|
type="wsframe",
|
||||||
|
frame=dict(
|
||||||
|
),
|
||||||
|
cipher=None,
|
||||||
|
)
|
||||||
|
self.addlog(retlog)
|
||||||
break
|
break
|
||||||
except tcp.NetLibTimeout:
|
except tcp.NetLibTimeout: # pragma: no cover
|
||||||
pass
|
pass
|
||||||
lg(frm.human_readable())
|
lg(frm.human_readable())
|
||||||
return self.handle_websocket, None
|
return self.handle_websocket, None
|
||||||
|
@ -48,7 +48,7 @@ class Daemon:
|
|||||||
l = self.log()
|
l = self.log()
|
||||||
if not l:
|
if not l:
|
||||||
return None
|
return None
|
||||||
return l[-1]
|
return l[0]
|
||||||
|
|
||||||
def log(self):
|
def log(self):
|
||||||
"""
|
"""
|
||||||
|
@ -36,7 +36,7 @@ class TestTimeout(tutils.DaemonTests):
|
|||||||
# increase test performance
|
# increase test performance
|
||||||
# This is a bodge - we have some platform difference that causes
|
# This is a bodge - we have some platform difference that causes
|
||||||
# different exceptions to be raised here.
|
# different exceptions to be raised here.
|
||||||
tutils.raises(Exception, self.pathoc, "get:/:p1,1")
|
tutils.raises(Exception, self.pathoc, ["get:/:p1,1"])
|
||||||
assert self.d.last_log()["type"] == "timeout"
|
assert self.d.last_log()["type"] == "timeout"
|
||||||
|
|
||||||
|
|
||||||
@ -58,10 +58,10 @@ class TestNotAfterConnect(tutils.DaemonTests):
|
|||||||
|
|
||||||
def test_connect(self):
|
def test_connect(self):
|
||||||
r = self.pathoc(
|
r = self.pathoc(
|
||||||
r"get:'http://foo.com/p/202':da",
|
[r"get:'http://foo.com/p/202':da"],
|
||||||
connect_to=("localhost", self.d.port)
|
connect_to=("localhost", self.d.port)
|
||||||
)
|
)
|
||||||
assert r.status_code == 202
|
assert r[0].status_code == 202
|
||||||
|
|
||||||
|
|
||||||
class TestCustomCert(tutils.DaemonTests):
|
class TestCustomCert(tutils.DaemonTests):
|
||||||
@ -71,7 +71,7 @@ class TestCustomCert(tutils.DaemonTests):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def test_connect(self):
|
def test_connect(self):
|
||||||
r = self.pathoc(r"get:/p/202")
|
r = self.pathoc([r"get:/p/202"])[0]
|
||||||
assert r.status_code == 202
|
assert r.status_code == 202
|
||||||
assert r.sslinfo
|
assert r.sslinfo
|
||||||
assert "test.com" in str(r.sslinfo.certchain[0].get_subject())
|
assert "test.com" in str(r.sslinfo.certchain[0].get_subject())
|
||||||
@ -84,7 +84,7 @@ class TestSSLCN(tutils.DaemonTests):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def test_connect(self):
|
def test_connect(self):
|
||||||
r = self.pathoc(r"get:/p/202")
|
r = self.pathoc([r"get:/p/202"])[0]
|
||||||
assert r.status_code == 202
|
assert r.status_code == 202
|
||||||
assert r.sslinfo
|
assert r.sslinfo
|
||||||
assert r.sslinfo.certchain[0].get_subject().CN == "foo.com"
|
assert r.sslinfo.certchain[0].get_subject().CN == "foo.com"
|
||||||
@ -120,7 +120,7 @@ class CommonTests(tutils.DaemonTests):
|
|||||||
assert "too large" in l["response"]["msg"]
|
assert "too large" in l["response"]["msg"]
|
||||||
|
|
||||||
def test_preline(self):
|
def test_preline(self):
|
||||||
r = self.pathoc(r"get:'/p/200':i0,'\r\n'")
|
r = self.pathoc([r"get:'/p/200':i0,'\r\n'"])[0]
|
||||||
assert r.status_code == 200
|
assert r.status_code == 200
|
||||||
|
|
||||||
def test_info(self):
|
def test_info(self):
|
||||||
@ -166,14 +166,14 @@ class CommonTests(tutils.DaemonTests):
|
|||||||
tutils.raises(
|
tutils.raises(
|
||||||
http.HttpError,
|
http.HttpError,
|
||||||
self.pathoc,
|
self.pathoc,
|
||||||
"get:/:h'content-length'='foo'"
|
["get:/:h'content-length'='foo'"]
|
||||||
)
|
)
|
||||||
l = self.d.last_log()
|
l = self.d.last_log()
|
||||||
assert l["type"] == "error"
|
assert l["type"] == "error"
|
||||||
assert "Content-Length unknown" in l["msg"]
|
assert "Content-Length unknown" in l["msg"]
|
||||||
|
|
||||||
def test_invalid_headers(self):
|
def test_invalid_headers(self):
|
||||||
tutils.raises(http.HttpError, self.pathoc, "get:/:h'\t'='foo'")
|
tutils.raises(http.HttpError, self.pathoc, ["get:/:h'\t'='foo'"])
|
||||||
l = self.d.last_log()
|
l = self.d.last_log()
|
||||||
assert l["type"] == "error"
|
assert l["type"] == "error"
|
||||||
assert "Invalid headers" in l["msg"]
|
assert "Invalid headers" in l["msg"]
|
||||||
@ -188,33 +188,37 @@ class CommonTests(tutils.DaemonTests):
|
|||||||
assert "File access denied" in rsp.content
|
assert "File access denied" in rsp.content
|
||||||
|
|
||||||
def test_proxy(self):
|
def test_proxy(self):
|
||||||
r = self.pathoc(r"get:'http://foo.com/p/202':da")
|
r = self.pathoc([r"get:'http://foo.com/p/202':da"])[0]
|
||||||
assert r.status_code == 202
|
assert r.status_code == 202
|
||||||
|
|
||||||
def test_websocket(self):
|
def test_websocket(self):
|
||||||
r = self.pathoc("ws:/p/", ws_read_limit=0)
|
r = self.pathoc(["ws:/p/"], ws_read_limit=0)[0]
|
||||||
assert r.status_code == 101
|
assert r.status_code == 101
|
||||||
|
|
||||||
r = self.pathoc("ws:/p/ws", ws_read_limit=0)
|
r = self.pathoc(["ws:/p/ws"], ws_read_limit=0)[0]
|
||||||
assert r.status_code == 101
|
assert r.status_code == 101
|
||||||
|
|
||||||
|
def test_websocket_frame(self):
|
||||||
|
r = self.pathoc(["ws:/p/", "wf:b@10"], ws_read_limit=0)
|
||||||
|
assert self.d.last_log()["type"] == "wsframe"
|
||||||
|
|
||||||
|
|
||||||
class TestDaemon(CommonTests):
|
class TestDaemon(CommonTests):
|
||||||
ssl = False
|
ssl = False
|
||||||
|
|
||||||
def test_connect(self):
|
def test_connect(self):
|
||||||
r = self.pathoc(
|
r = self.pathoc(
|
||||||
r"get:'http://foo.com/p/202':da",
|
[r"get:'http://foo.com/p/202':da"],
|
||||||
connect_to=("localhost", self.d.port),
|
connect_to=("localhost", self.d.port),
|
||||||
ssl=True
|
ssl=True
|
||||||
)
|
)[0]
|
||||||
assert r.status_code == 202
|
assert r.status_code == 202
|
||||||
|
|
||||||
def test_connect_err(self):
|
def test_connect_err(self):
|
||||||
tutils.raises(
|
tutils.raises(
|
||||||
http.HttpError,
|
http.HttpError,
|
||||||
self.pathoc,
|
self.pathoc,
|
||||||
r"get:'http://foo.com/p/202':da",
|
[r"get:'http://foo.com/p/202':da"],
|
||||||
connect_to=("localhost", self.d.port)
|
connect_to=("localhost", self.d.port)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -234,6 +238,6 @@ class TestDaemonSSL(CommonTests):
|
|||||||
assert "SSL" in l["msg"]
|
assert "SSL" in l["msg"]
|
||||||
|
|
||||||
def test_ssl_cipher(self):
|
def test_ssl_cipher(self):
|
||||||
r = self.pathoc(r"get:/p/202")
|
r = self.pathoc([r"get:/p/202"])[0]
|
||||||
assert r.status_code == 202
|
assert r.status_code == 202
|
||||||
assert self.d.last_log()["cipher"][1] > 0
|
assert self.d.last_log()["cipher"][1] > 0
|
||||||
|
@ -67,7 +67,7 @@ class DaemonTests(object):
|
|||||||
|
|
||||||
def pathoc(
|
def pathoc(
|
||||||
self,
|
self,
|
||||||
spec,
|
specs,
|
||||||
timeout=None,
|
timeout=None,
|
||||||
connect_to=None,
|
connect_to=None,
|
||||||
ssl=None,
|
ssl=None,
|
||||||
@ -84,7 +84,10 @@ class DaemonTests(object):
|
|||||||
c.connect(connect_to)
|
c.connect(connect_to)
|
||||||
if timeout:
|
if timeout:
|
||||||
c.settimeout(timeout)
|
c.settimeout(timeout)
|
||||||
return c.request(spec)
|
ret = []
|
||||||
|
for i in specs:
|
||||||
|
ret.append(c.request(i))
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
|
Loading…
Reference in New Issue
Block a user