2012-06-29 22:51:13 +00:00
|
|
|
import json, cStringIO
|
2012-06-26 05:28:07 +00:00
|
|
|
from libpathod import pathoc, test, version
|
|
|
|
import tutils
|
|
|
|
|
|
|
|
|
2013-01-03 21:37:26 +00:00
|
|
|
class _TestDaemon:
|
2012-06-26 05:28:07 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpAll(self):
|
|
|
|
self.d = test.Daemon(
|
2013-01-03 21:37:26 +00:00
|
|
|
ssl=self.ssl,
|
2012-06-26 05:28:07 +00:00
|
|
|
staticdir=tutils.test_data.path("data"),
|
|
|
|
anchors=[("/anchor/.*", "202")]
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownAll(self):
|
|
|
|
self.d.shutdown()
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.d.clear_log()
|
|
|
|
|
|
|
|
def test_info(self):
|
2013-01-03 21:37:26 +00:00
|
|
|
c = pathoc.Pathoc(
|
|
|
|
"127.0.0.1",
|
|
|
|
self.d.port,
|
|
|
|
ssl = self.ssl
|
|
|
|
)
|
2012-06-26 05:28:07 +00:00
|
|
|
c.connect()
|
|
|
|
_, _, _, _, content = c.request("get:/api/info")
|
|
|
|
assert tuple(json.loads(content)["version"]) == version.IVERSION
|
|
|
|
|
2013-01-03 21:37:26 +00:00
|
|
|
|
|
|
|
class TestDaemonSSL(_TestDaemon):
|
|
|
|
ssl = True
|
|
|
|
def test_sni(self):
|
|
|
|
c = pathoc.Pathoc(
|
|
|
|
"127.0.0.1",
|
|
|
|
self.d.port,
|
|
|
|
ssl = True,
|
|
|
|
sni = "foobar.com"
|
|
|
|
)
|
|
|
|
c.connect()
|
|
|
|
c.request("get:/p/200")
|
|
|
|
_, _, _, _, content = c.request("get:/api/log")
|
|
|
|
d = json.loads(content)
|
|
|
|
assert d["log"][0]["request"]["sni"] == "foobar.com"
|
|
|
|
|
|
|
|
|
|
|
|
class TestDaemon(_TestDaemon):
|
|
|
|
ssl = False
|
2012-09-27 23:38:49 +00:00
|
|
|
def tval(self, requests, showreq=False, showresp=False, explain=False, hexdump=False, timeout=None, ignorecodes=None, ignoretimeout=None):
|
2012-06-29 22:51:13 +00:00
|
|
|
c = pathoc.Pathoc("127.0.0.1", self.d.port)
|
|
|
|
c.connect()
|
2012-09-25 23:07:22 +00:00
|
|
|
if timeout:
|
|
|
|
c.settimeout(timeout)
|
2012-06-29 22:51:13 +00:00
|
|
|
s = cStringIO.StringIO()
|
2012-09-26 02:25:39 +00:00
|
|
|
for i in requests:
|
|
|
|
c.print_request(
|
|
|
|
i,
|
|
|
|
showreq = showreq,
|
|
|
|
showresp = showresp,
|
|
|
|
explain = explain,
|
|
|
|
hexdump = hexdump,
|
2012-09-26 21:44:25 +00:00
|
|
|
ignorecodes = ignorecodes,
|
2012-09-27 23:38:49 +00:00
|
|
|
ignoretimeout = ignoretimeout,
|
2012-09-26 02:25:39 +00:00
|
|
|
fp = s
|
|
|
|
)
|
2012-06-29 22:51:13 +00:00
|
|
|
return s.getvalue()
|
|
|
|
|
2013-01-03 21:37:26 +00:00
|
|
|
def test_ssl_error(self):
|
|
|
|
c = pathoc.Pathoc("127.0.0.1", self.d.port, ssl = True)
|
|
|
|
tutils.raises("ssl handshake", c.connect)
|
|
|
|
|
2012-09-26 21:44:25 +00:00
|
|
|
def test_ignorecodes(self):
|
|
|
|
assert "200" in self.tval(["get:'/p/200:b@1'"])
|
|
|
|
assert "200" not in self.tval(["get:'/p/200:b@1'"], ignorecodes=[200])
|
|
|
|
assert "200" not in self.tval(["get:'/p/200:b@1'"], ignorecodes=[200, 201])
|
|
|
|
assert "202" in self.tval(["get:'/p/202:b@1'"], ignorecodes=[200, 201])
|
|
|
|
|
2012-09-25 23:07:22 +00:00
|
|
|
def test_timeout(self):
|
|
|
|
assert "Timeout" in self.tval(["get:'/p/200:p0,10'"], timeout=0.01)
|
|
|
|
assert "HTTP" in self.tval(["get:'/p/200:p5,10'"], showresp=True, timeout=0.01)
|
2012-10-24 21:59:18 +00:00
|
|
|
assert not "HTTP" in self.tval(["get:'/p/200:p5,10'"], showresp=True, timeout=0.01, ignoretimeout=True)
|
2012-09-25 23:07:22 +00:00
|
|
|
|
2012-09-25 22:38:47 +00:00
|
|
|
def test_showresp(self):
|
2012-07-21 08:20:37 +00:00
|
|
|
reqs = [ "get:/api/info:p0,0", "get:/api/info:p0,0" ]
|
2012-09-25 22:12:30 +00:00
|
|
|
assert self.tval(reqs).count("200") == 2
|
2012-09-25 23:07:22 +00:00
|
|
|
assert self.tval(reqs, showresp=True).count("unprintables escaped") == 2
|
|
|
|
assert self.tval(reqs, showresp=True, hexdump=True).count("hex dump") == 2
|
|
|
|
|
|
|
|
def test_showresp_httperr(self):
|
|
|
|
v = self.tval(["get:'/p/200:d20'"], showresp=True)
|
|
|
|
assert "Invalid headers" in v
|
|
|
|
assert "HTTP/" in v
|
2012-06-29 22:51:13 +00:00
|
|
|
|
2012-10-30 22:23:53 +00:00
|
|
|
def test_explain(self):
|
|
|
|
reqs = [ "get:/p/200:b@100" ]
|
|
|
|
assert not "b@100" in self.tval(reqs, explain=True)
|
|
|
|
|
2012-09-25 22:38:47 +00:00
|
|
|
def test_showreq(self):
|
|
|
|
reqs = [ "get:/api/info:p0,0", "get:/api/info:p0,0" ]
|
|
|
|
assert self.tval(reqs, showreq=True).count("unprintables escaped") == 2
|
|
|
|
assert self.tval(reqs, showreq=True, hexdump=True).count("hex dump") == 2
|
|
|
|
|
2012-06-29 22:51:13 +00:00
|
|
|
def test_parse_err(self):
|
|
|
|
assert "Error parsing" in self.tval(["foo"])
|
|
|
|
|
|
|
|
def test_conn_err(self):
|
|
|
|
assert "Invalid server response" in self.tval(["get:'/p/200:d2'"])
|
2012-07-22 11:37:46 +00:00
|
|
|
|
|
|
|
def test_fileread(self):
|
|
|
|
d = tutils.test_data.path("data/request")
|
2012-10-04 22:14:17 +00:00
|
|
|
assert "foo" in self.tval(["+%s"%d], showreq=True)
|
2012-07-22 11:37:46 +00:00
|
|
|
assert "File" in self.tval(["+/nonexistent"])
|
2012-07-30 01:52:40 +00:00
|
|
|
|