2014-10-24 01:01:34 +00:00
|
|
|
import tempfile
|
2014-10-25 02:30:54 +00:00
|
|
|
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
|
2015-09-21 21:03:45 +00:00
|
|
|
|
|
|
|
import netlib
|
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
|
|
|
from netlib import tcp
|
2012-07-23 04:18:47 +00:00
|
|
|
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)
|
|
|
|
|
2014-10-25 02:30:54 +00:00
|
|
|
|
2015-04-19 20:56:47 +00:00
|
|
|
class DaemonTests(object):
|
2012-07-23 07:55:33 +00:00
|
|
|
noweb = False
|
2012-07-24 22:34:57 +00:00
|
|
|
noapi = False
|
2012-07-26 08:01:51 +00:00
|
|
|
nohang = False
|
2012-07-23 07:55:33 +00:00
|
|
|
ssl = False
|
2012-09-30 23:01:02 +00:00
|
|
|
timeout = None
|
2012-10-24 21:59:18 +00:00
|
|
|
hexdump = False
|
2014-03-02 00:45:35 +00:00
|
|
|
ssloptions = None
|
2015-06-04 08:36:50 +00:00
|
|
|
nocraft = False
|
2014-10-24 01:01:34 +00:00
|
|
|
|
2012-07-23 04:18:47 +00:00
|
|
|
@classmethod
|
2015-09-21 21:03:45 +00:00
|
|
|
def setup_class(cls):
|
|
|
|
opts = cls.ssloptions or {}
|
|
|
|
cls.confdir = tempfile.mkdtemp()
|
|
|
|
opts["confdir"] = cls.confdir
|
2014-03-02 00:45:35 +00:00
|
|
|
so = pathod.SSLOptions(**opts)
|
2015-09-21 21:03:45 +00:00
|
|
|
cls.d = test.Daemon(
|
2012-07-23 04:18:47 +00:00
|
|
|
staticdir=test_data.path("data"),
|
2014-10-25 02:30:54 +00:00
|
|
|
anchors=[
|
2015-06-12 11:41:04 +00:00
|
|
|
(re.compile("/anchor/.*"), "202:da")
|
2014-10-25 02:30:54 +00:00
|
|
|
],
|
2015-09-21 21:03:45 +00:00
|
|
|
ssl=cls.ssl,
|
2015-06-18 16:12:11 +00:00
|
|
|
ssloptions=so,
|
|
|
|
sizelimit=1 * 1024 * 1024,
|
2015-09-21 21:03:45 +00:00
|
|
|
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
|
2012-07-23 04:18:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
2015-09-21 21:03:45 +00:00
|
|
|
def teardown_class(cls):
|
|
|
|
cls.d.shutdown()
|
|
|
|
shutil.rmtree(cls.confdir)
|
2012-07-23 04:18:47 +00:00
|
|
|
|
2015-09-21 21:03:45 +00:00
|
|
|
def teardown(self):
|
2012-07-24 22:34:57 +00:00
|
|
|
if not (self.noweb or self.noapi):
|
|
|
|
self.d.clear_log()
|
2012-07-23 04:18:47 +00:00
|
|
|
|
2012-07-23 04:28:34 +00:00
|
|
|
def getpath(self, path, params=None):
|
2012-07-23 07:55:33 +00:00
|
|
|
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
|
2012-07-23 04:18:47 +00:00
|
|
|
|
|
|
|
def get(self, spec):
|
2015-07-19 16:17:09 +00:00
|
|
|
resp = requests.get(self.d.p(spec), verify=False)
|
|
|
|
return resp
|
2012-07-23 04:18:47 +00:00
|
|
|
|
2015-04-29 20:03:26 +00:00
|
|
|
def pathoc(
|
|
|
|
self,
|
2015-05-31 06:38:11 +00:00
|
|
|
specs,
|
2015-04-29 20:03:26 +00:00
|
|
|
timeout=None,
|
|
|
|
connect_to=None,
|
|
|
|
ssl=None,
|
2015-06-16 11:52:41 +00:00
|
|
|
ws_read_limit=None,
|
|
|
|
use_http2=False,
|
2015-04-29 20:03:26 +00:00
|
|
|
):
|
2015-06-08 03:23:56 +00:00
|
|
|
"""
|
|
|
|
Returns a (messages, text log) tuple.
|
|
|
|
"""
|
2013-01-05 07:29:46 +00:00
|
|
|
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,
|
2015-04-30 01:59:10 +00:00
|
|
|
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
|
|
|
)
|
2013-01-05 07:29:46 +00:00
|
|
|
c.connect(connect_to)
|
2015-05-31 06:38:11 +00:00
|
|
|
ret = []
|
|
|
|
for i in specs:
|
2015-06-05 04:03:17 +00:00
|
|
|
resp = c.request(i)
|
|
|
|
if resp:
|
|
|
|
ret.append(resp)
|
|
|
|
for frm in c.wait():
|
|
|
|
ret.append(frm)
|
2015-06-28 00:16:02 +00:00
|
|
|
c.stop()
|
2015-06-08 03:23:56 +00:00
|
|
|
return ret, logfp.getvalue()
|
2012-06-09 03:08:51 +00:00
|
|
|
|
2015-06-18 16:12:11 +00:00
|
|
|
|
2015-09-21 21:03:45 +00:00
|
|
|
tmpdir = netlib.tutils.tmpdir
|
2012-06-09 03:08:51 +00:00
|
|
|
|
2015-09-21 21:03:45 +00:00
|
|
|
raises = netlib.tutils.raises
|
2012-06-09 03:08:51 +00:00
|
|
|
|
2016-05-20 23:53:14 +00:00
|
|
|
test_data = netlib.utils.Data(__name__)
|
2015-05-03 01:54:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def render(r, settings=language.Settings()):
|
2015-05-15 23:31:53 +00:00
|
|
|
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()
|