mitmproxy/test/pathod/tutils.py

127 lines
2.9 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
2016-03-20 18:40:03 +00:00
from six.moves import cStringIO as StringIO
import netlib
2016-02-16 19:59:33 +00:00
from pathod import utils, test, pathoc, pathod, language
2015-07-03 00:48:35 +00:00
from netlib import tcp
import requests
2015-07-03 00:48:35 +00:00
def treader(bytes):
"""
Construct a tcp.Read object from bytes.
"""
2016-03-20 18:40:03 +00:00
fp = StringIO(bytes)
2015-07-03 00:48:35 +00:00
return tcp.Reader(fp)
2015-04-19 20:56:47 +00:00
class DaemonTests(object):
noweb = False
noapi = False
nohang = False
ssl = False
timeout = None
hexdump = False
ssloptions = None
2015-06-04 08:36:50 +00:00
nocraft = False
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,
noweb=cls.noweb,
noapi=cls.noapi,
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=True
)
@classmethod
def teardown_class(cls):
cls.d.shutdown()
shutil.rmtree(cls.confdir)
def teardown(self):
if not (self.noweb or self.noapi):
self.d.clear_log()
2012-07-23 04:28:34 +00:00
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 get(self, spec):
2015-07-19 16:17:09 +00:00
resp = requests.get(self.d.p(spec), verify=False)
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-03-20 18:40:03 +00:00
logfp = 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
)
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 = netlib.tutils.tmpdir
2012-06-09 03:08:51 +00:00
raises = netlib.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-03-20 18:40:03 +00:00
s = StringIO()
2015-05-03 01:54:52 +00:00
assert language.serve(r, s, settings)
return s.getvalue()