Shift SSL parameters into Pathoc class

This commit is contained in:
Aldo Cortesi 2013-01-04 10:37:26 +13:00
parent b07ab253b7
commit d7f641c6ee
3 changed files with 41 additions and 10 deletions

View File

@ -7,12 +7,21 @@ class PathocError(Exception): pass
class Pathoc(tcp.TCPClient):
def __init__(self, host, port):
def __init__(self, host, port, ssl=None, sni=None):
tcp.TCPClient.__init__(self, host, port)
self.settings = dict(
staticdir = os.getcwd(),
unconstrained_file_access = True,
)
self.ssl, self.sni = ssl, sni
def connect(self):
tcp.TCPClient.connect(self)
if self.ssl:
try:
self.convert_to_ssl(sni=self.sni)
except tcp.NetLibError, v:
raise PathocError(str(v))
def request(self, spec):
"""

8
pathoc
View File

@ -96,18 +96,12 @@ if __name__ == "__main__":
try:
for i in range(args.repeat):
p = pathoc.Pathoc(args.host, port)
p = pathoc.Pathoc(args.host, port, args.ssl, args.sni)
try:
p.connect()
except tcp.NetLibError, v:
print >> sys.stderr, str(v)
sys.exit(1)
if args.ssl:
try:
p.convert_to_ssl(sni=args.sni)
except tcp.NetLibError, v:
print "\n>> %s"%v
continue
if args.timeout:
p.settimeout(args.timeout)
for spec in args.request:

View File

@ -3,10 +3,11 @@ from libpathod import pathoc, test, version
import tutils
class TestDaemon:
class _TestDaemon:
@classmethod
def setUpAll(self):
self.d = test.Daemon(
ssl=self.ssl,
staticdir=tutils.test_data.path("data"),
anchors=[("/anchor/.*", "202")]
)
@ -19,11 +20,34 @@ class TestDaemon:
self.d.clear_log()
def test_info(self):
c = pathoc.Pathoc("127.0.0.1", self.d.port)
c = pathoc.Pathoc(
"127.0.0.1",
self.d.port,
ssl = self.ssl
)
c.connect()
_, _, _, _, content = c.request("get:/api/info")
assert tuple(json.loads(content)["version"]) == version.IVERSION
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
def tval(self, requests, showreq=False, showresp=False, explain=False, hexdump=False, timeout=None, ignorecodes=None, ignoretimeout=None):
c = pathoc.Pathoc("127.0.0.1", self.d.port)
c.connect()
@ -43,6 +67,10 @@ class TestDaemon:
)
return s.getvalue()
def test_ssl_error(self):
c = pathoc.Pathoc("127.0.0.1", self.d.port, ssl = True)
tutils.raises("ssl handshake", c.connect)
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])