2014-09-06 23:38:44 +00:00
|
|
|
import json
|
|
|
|
import cStringIO
|
2014-10-25 02:30:54 +00:00
|
|
|
import re
|
|
|
|
|
2015-04-19 06:04:27 +00:00
|
|
|
from netlib import tcp, http
|
2014-10-25 02:30:54 +00:00
|
|
|
from libpathod import pathoc, test, version, pathod, language
|
2012-06-26 05:28:07 +00:00
|
|
|
import tutils
|
|
|
|
|
2014-09-06 23:38:44 +00:00
|
|
|
|
2013-03-02 03:57:00 +00:00
|
|
|
def test_response():
|
2014-03-02 00:45:35 +00:00
|
|
|
r = pathoc.Response("1.1", 200, "Message", {}, None, None)
|
2013-03-02 03:57:00 +00:00
|
|
|
assert repr(r)
|
|
|
|
|
2012-06-26 05:28:07 +00:00
|
|
|
|
2013-01-03 21:37:26 +00:00
|
|
|
class _TestDaemon:
|
2013-05-12 21:03:48 +00:00
|
|
|
ssloptions = pathod.SSLOptions()
|
2014-09-06 23:38:44 +00:00
|
|
|
|
2012-06-26 05:28:07 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpAll(self):
|
|
|
|
self.d = test.Daemon(
|
2015-04-19 20:56:47 +00:00
|
|
|
ssl = self.ssl,
|
|
|
|
ssloptions = self.ssloptions,
|
|
|
|
staticdir = tutils.test_data.path("data"),
|
|
|
|
anchors = [
|
2014-10-25 02:30:54 +00:00
|
|
|
(re.compile("/anchor/.*"), language.parse_response("202"))
|
|
|
|
]
|
2012-06-26 05:28:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@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(
|
2014-01-28 18:28:20 +00:00
|
|
|
("127.0.0.1", self.d.port),
|
2013-01-03 21:37:26 +00:00
|
|
|
ssl = self.ssl
|
|
|
|
)
|
2012-06-26 05:28:07 +00:00
|
|
|
c.connect()
|
2015-04-19 06:04:27 +00:00
|
|
|
resp = c.request("get:/api/info")
|
|
|
|
assert tuple(json.loads(resp.content)["version"]) == version.IVERSION
|
2012-06-26 05:28:07 +00:00
|
|
|
|
2014-10-25 02:30:54 +00:00
|
|
|
def tval(
|
|
|
|
self,
|
|
|
|
requests,
|
|
|
|
showreq=False,
|
|
|
|
showresp=False,
|
|
|
|
explain=False,
|
|
|
|
showssl=False,
|
|
|
|
hexdump=False,
|
|
|
|
timeout=None,
|
2015-04-20 03:42:33 +00:00
|
|
|
ignorecodes=(),
|
2015-04-19 06:04:27 +00:00
|
|
|
ignoretimeout=None,
|
|
|
|
showsummary=True
|
2014-10-25 02:30:54 +00:00
|
|
|
):
|
2015-04-19 03:55:22 +00:00
|
|
|
s = cStringIO.StringIO()
|
2015-04-19 06:04:27 +00:00
|
|
|
c = pathoc.Pathoc(
|
|
|
|
("127.0.0.1", self.d.port),
|
|
|
|
ssl = self.ssl,
|
|
|
|
showreq = showreq,
|
|
|
|
showresp = showresp,
|
|
|
|
explain = explain,
|
|
|
|
hexdump = hexdump,
|
|
|
|
ignorecodes = ignorecodes,
|
|
|
|
ignoretimeout = ignoretimeout,
|
|
|
|
showsummary = showsummary,
|
|
|
|
fp = s
|
|
|
|
)
|
2015-04-19 03:55:22 +00:00
|
|
|
c.connect(showssl=showssl, fp=s)
|
2014-03-02 06:04:56 +00:00
|
|
|
if timeout:
|
|
|
|
c.settimeout(timeout)
|
|
|
|
for i in requests:
|
2014-10-25 21:50:32 +00:00
|
|
|
r = language.parse_requests(i)[0]
|
|
|
|
if explain:
|
2015-04-22 03:49:17 +00:00
|
|
|
r = r.freeze(language.Settings())
|
2015-04-19 06:04:27 +00:00
|
|
|
try:
|
|
|
|
c.request(r)
|
|
|
|
except (http.HttpError, tcp.NetLibError), v:
|
|
|
|
pass
|
2014-03-02 06:04:56 +00:00
|
|
|
return s.getvalue()
|
|
|
|
|
2013-01-03 21:37:26 +00:00
|
|
|
|
|
|
|
class TestDaemonSSL(_TestDaemon):
|
|
|
|
ssl = True
|
2015-04-18 22:43:16 +00:00
|
|
|
ssloptions = pathod.SSLOptions(
|
|
|
|
request_client_cert=True,
|
|
|
|
sans = ["test1.com", "test2.com"]
|
|
|
|
)
|
2014-09-06 23:38:44 +00:00
|
|
|
|
2013-01-03 21:37:26 +00:00
|
|
|
def test_sni(self):
|
|
|
|
c = pathoc.Pathoc(
|
2014-01-28 18:28:20 +00:00
|
|
|
("127.0.0.1", self.d.port),
|
2013-01-03 21:37:26 +00:00
|
|
|
ssl = True,
|
|
|
|
sni = "foobar.com"
|
|
|
|
)
|
|
|
|
c.connect()
|
|
|
|
c.request("get:/p/200")
|
2013-02-26 20:07:16 +00:00
|
|
|
r = c.request("get:/api/log")
|
|
|
|
d = json.loads(r.content)
|
2013-01-03 21:37:26 +00:00
|
|
|
assert d["log"][0]["request"]["sni"] == "foobar.com"
|
|
|
|
|
2014-03-02 06:04:56 +00:00
|
|
|
def test_showssl(self):
|
2014-09-06 23:38:44 +00:00
|
|
|
assert "certificate chain" in self.tval(["get:/p/200"], showssl=True)
|
2014-03-02 06:04:56 +00:00
|
|
|
|
2013-01-20 09:37:43 +00:00
|
|
|
def test_clientcert(self):
|
|
|
|
c = pathoc.Pathoc(
|
2014-01-28 18:28:20 +00:00
|
|
|
("127.0.0.1", self.d.port),
|
2013-01-20 09:37:43 +00:00
|
|
|
ssl = True,
|
|
|
|
clientcert = tutils.test_data.path("data/clientcert/client.pem")
|
|
|
|
)
|
|
|
|
c.connect()
|
|
|
|
c.request("get:/p/200")
|
2013-02-26 20:07:16 +00:00
|
|
|
r = c.request("get:/api/log")
|
|
|
|
d = json.loads(r.content)
|
2013-01-20 09:37:43 +00:00
|
|
|
assert d["log"][0]["request"]["clientcert"]["keyinfo"]
|
|
|
|
|
2013-01-03 21:37:26 +00:00
|
|
|
|
|
|
|
class TestDaemon(_TestDaemon):
|
|
|
|
ssl = False
|
2014-09-06 23:38:44 +00:00
|
|
|
|
2013-01-03 21:37:26 +00:00
|
|
|
def test_ssl_error(self):
|
2014-01-28 18:28:20 +00:00
|
|
|
c = pathoc.Pathoc(("127.0.0.1", self.d.port), ssl = True)
|
2013-01-03 21:37:26 +00:00
|
|
|
tutils.raises("ssl handshake", c.connect)
|
|
|
|
|
2014-03-02 06:04:56 +00:00
|
|
|
def test_showssl(self):
|
2014-09-06 23:38:44 +00:00
|
|
|
assert not "certificate chain" in self.tval(["get:/p/200"], showssl=True)
|
2014-03-02 06:04:56 +00:00
|
|
|
|
2012-09-26 21:44:25 +00:00
|
|
|
def test_ignorecodes(self):
|
2015-04-19 06:04:27 +00:00
|
|
|
assert "200" in self.tval(["get:'/p/200:b@1'"])
|
|
|
|
assert "200" in self.tval(["get:'/p/200:b@1'"])
|
2012-09-26 21:44:25 +00:00
|
|
|
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)
|
2015-04-19 06:04:27 +00:00
|
|
|
assert not "HTTP" in self.tval(["get:'/p/200:p3,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):
|
2014-09-06 23:38:44 +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):
|
2015-04-19 06:04:27 +00:00
|
|
|
v = self.tval(["get:'/p/200:d20'"], showresp=True, showsummary=True)
|
2012-09-25 23:07:22 +00:00
|
|
|
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):
|
2014-10-25 02:30:54 +00:00
|
|
|
reqs = ["get:/p/200:b@100"]
|
|
|
|
assert "b@100" not in self.tval(reqs, explain=True)
|
2012-10-30 22:23:53 +00:00
|
|
|
|
2012-09-25 22:38:47 +00:00
|
|
|
def test_showreq(self):
|
2014-10-25 02:30:54 +00:00
|
|
|
reqs = ["get:/api/info:p0,0", "get:/api/info:p0,0"]
|
2012-09-25 22:38:47 +00:00
|
|
|
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_conn_err(self):
|
|
|
|
assert "Invalid server response" in self.tval(["get:'/p/200:d2'"])
|
2012-07-22 11:37:46 +00:00
|
|
|
|
2013-03-02 03:57:00 +00:00
|
|
|
def test_connect_fail(self):
|
|
|
|
to = ("foobar", 80)
|
2014-01-28 18:28:20 +00:00
|
|
|
c = pathoc.Pathoc(("127.0.0.1", self.d.port))
|
2013-12-15 05:42:58 +00:00
|
|
|
c.rfile, c.wfile = cStringIO.StringIO(), cStringIO.StringIO()
|
|
|
|
tutils.raises("connect failed", c.http_connect, to)
|
|
|
|
c.rfile = cStringIO.StringIO(
|
2013-03-02 03:57:00 +00:00
|
|
|
"HTTP/1.1 500 OK\r\n"
|
|
|
|
)
|
2013-12-15 05:42:58 +00:00
|
|
|
tutils.raises("connect failed", c.http_connect, to)
|
|
|
|
c.rfile = cStringIO.StringIO(
|
2013-03-02 03:57:00 +00:00
|
|
|
"HTTP/1.1 200 OK\r\n"
|
|
|
|
)
|
2013-12-15 05:42:58 +00:00
|
|
|
c.http_connect(to)
|