mitmproxy/test/pathod/tutils.py

153 lines
3.5 KiB
Python
Raw Normal View History

2014-10-24 01:01:34 +00:00
import tempfile
import re
2014-10-24 01:01:34 +00:00
import shutil
import requests
2016-10-17 03:38:31 +00:00
import io
import urllib
from netlib import tcp
from netlib import utils
from netlib import tutils
2016-05-28 12:36:43 +00:00
from pathod import language
from pathod import pathoc
from pathod import pathod
from pathod import test
2015-07-03 00:48:35 +00:00
def treader(bytes):
"""
Construct a tcp.Read object from bytes.
"""
2016-10-17 03:38:31 +00:00
fp = io.BytesIO(bytes)
2015-07-03 00:48:35 +00:00
return tcp.Reader(fp)
2016-10-17 04:29:45 +00:00
class DaemonTests:
nohang = False
ssl = False
timeout = None
hexdump = False
ssloptions = None
2015-06-04 08:36:50 +00:00
nocraft = False
explain = True
2014-10-24 01:01:34 +00:00
@classmethod
def setup_class(cls):
opts = cls.ssloptions or {}
cls.confdir = tempfile.mkdtemp()
opts["confdir"] = cls.confdir
so = pathod.SSLOptions(**opts)
cls.d = test.Daemon(
staticdir=test_data.path("data"),
anchors=[
(re.compile("/anchor/.*"), "202:da")
],
ssl=cls.ssl,
2015-06-18 16:12:11 +00:00
ssloptions=so,
sizelimit=1 * 1024 * 1024,
nohang=cls.nohang,
timeout=cls.timeout,
hexdump=cls.hexdump,
nocraft=cls.nocraft,
2015-06-18 16:12:11 +00:00
logreq=True,
logresp=True,
explain=cls.explain
)
@classmethod
def teardown_class(cls):
cls.d.shutdown()
shutil.rmtree(cls.confdir)
def teardown(self):
self.d.wait_for_silence()
self.d.clear_log()
def _getpath(self, path, params=None):
scheme = "https" if self.ssl else "http"
2015-07-19 16:17:09 +00:00
resp = requests.get(
2015-05-30 00:03:13 +00:00
"%s://localhost:%s/%s" % (
2014-10-24 01:01:34 +00:00
scheme,
self.d.port,
path
),
verify=False,
params=params
2012-07-23 04:28:34 +00:00
)
2015-07-19 16:17:09 +00:00
return resp
def getpath(self, path, params=None):
2016-10-17 03:38:31 +00:00
logfp = io.StringIO()
c = pathoc.Pathoc(
("localhost", self.d.port),
ssl=self.ssl,
fp=logfp,
)
with c.connect():
if params:
2016-06-08 16:04:56 +00:00
path = path + "?" + urllib.parse.urlencode(params)
resp = c.request("get:%s" % path)
return resp
def get(self, spec):
2016-10-17 03:38:31 +00:00
logfp = io.StringIO()
c = pathoc.Pathoc(
("localhost", self.d.port),
ssl=self.ssl,
fp=logfp,
)
with c.connect():
resp = c.request(
"get:/p/%s" % urllib.parse.quote(spec)
)
return resp
2015-04-29 20:03:26 +00:00
def pathoc(
self,
specs,
2015-04-29 20:03:26 +00:00
timeout=None,
connect_to=None,
ssl=None,
ws_read_limit=None,
use_http2=False,
2015-04-29 20:03:26 +00:00
):
"""
Returns a (messages, text log) tuple.
"""
if ssl is None:
ssl = self.ssl
2016-10-17 03:38:31 +00:00
logfp = io.StringIO()
2015-04-29 20:03:26 +00:00
c = pathoc.Pathoc(
("localhost", self.d.port),
ssl=ssl,
ws_read_limit=ws_read_limit,
2015-06-18 16:12:11 +00:00
timeout=timeout,
fp=logfp,
use_http2=use_http2,
2015-04-29 20:03:26 +00:00
)
with c.connect(connect_to):
ret = []
for i in specs:
resp = c.request(i)
if resp:
ret.append(resp)
for frm in c.wait():
ret.append(frm)
c.stop()
return ret, logfp.getvalue()
2012-06-09 03:08:51 +00:00
2015-06-18 16:12:11 +00:00
tmpdir = tutils.tmpdir
2012-06-09 03:08:51 +00:00
raises = tutils.raises
2012-06-09 03:08:51 +00:00
test_data = utils.Data(__name__)
2015-05-03 01:54:52 +00:00
def render(r, settings=language.Settings()):
r = r.resolve(settings)
2016-10-17 03:38:31 +00:00
s = io.BytesIO()
2015-05-03 01:54:52 +00:00
assert language.serve(r, s, settings)
return s.getvalue()