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-05-29 09:43:29 +00:00
|
|
|
import requests
|
2016-03-20 18:40:03 +00:00
|
|
|
from six.moves import cStringIO as StringIO
|
2016-06-05 17:28:11 +00:00
|
|
|
from six import BytesIO
|
2016-06-02 07:32:50 +00:00
|
|
|
import urllib
|
2015-09-21 21:03:45 +00:00
|
|
|
|
2016-05-29 09:43:29 +00:00
|
|
|
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
|
2016-05-29 09:43:29 +00:00
|
|
|
|
2012-07-23 04:18:47 +00:00
|
|
|
|
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-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
|
2016-06-05 01:24:46 +00:00
|
|
|
explain = True
|
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
|
|
|
nohang=cls.nohang,
|
|
|
|
timeout=cls.timeout,
|
|
|
|
hexdump=cls.hexdump,
|
|
|
|
nocraft=cls.nocraft,
|
2015-06-18 16:12:11 +00:00
|
|
|
logreq=True,
|
|
|
|
logresp=True,
|
2016-06-05 01:24:46 +00:00
|
|
|
explain=cls.explain
|
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):
|
2016-06-02 23:47:07 +00:00
|
|
|
self.d.wait_for_silence()
|
2016-06-05 01:24:46 +00:00
|
|
|
self.d.clear_log()
|
2012-07-23 04:18:47 +00:00
|
|
|
|
2016-06-02 07:32:50 +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
|
|
|
|
2016-06-02 07:32:50 +00:00
|
|
|
def getpath(self, path, params=None):
|
|
|
|
logfp = StringIO()
|
|
|
|
c = pathoc.Pathoc(
|
|
|
|
("localhost", self.d.port),
|
|
|
|
ssl=self.ssl,
|
|
|
|
fp=logfp,
|
|
|
|
)
|
2016-06-03 00:06:36 +00:00
|
|
|
with c.connect():
|
|
|
|
if params:
|
|
|
|
path = path + "?" + urllib.urlencode(params)
|
|
|
|
resp = c.request("get:%s" % path)
|
|
|
|
return resp
|
2016-06-02 07:32:50 +00:00
|
|
|
|
2012-07-23 04:18:47 +00:00
|
|
|
def get(self, spec):
|
2016-06-02 07:32:50 +00:00
|
|
|
logfp = StringIO()
|
|
|
|
c = pathoc.Pathoc(
|
|
|
|
("localhost", self.d.port),
|
|
|
|
ssl=self.ssl,
|
|
|
|
fp=logfp,
|
|
|
|
)
|
2016-06-03 00:06:36 +00:00
|
|
|
with c.connect():
|
2016-06-04 23:47:52 +00:00
|
|
|
resp = c.request(
|
|
|
|
"get:/p/%s" % urllib.quote(spec).encode("string_escape")
|
|
|
|
)
|
2016-06-03 00:06:36 +00:00
|
|
|
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
|
|
|
)
|
2016-06-03 00:06:36 +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
|
|
|
|
2016-05-29 09:43:29 +00:00
|
|
|
tmpdir = tutils.tmpdir
|
2012-06-09 03:08:51 +00:00
|
|
|
|
2016-05-29 09:43:29 +00:00
|
|
|
raises = tutils.raises
|
2012-06-09 03:08:51 +00:00
|
|
|
|
2016-05-29 09:43:29 +00:00
|
|
|
test_data = 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-06-05 17:28:11 +00:00
|
|
|
s = BytesIO()
|
2015-05-03 01:54:52 +00:00
|
|
|
assert language.serve(r, s, settings)
|
|
|
|
return s.getvalue()
|