From a29ebe31dc86462398ed8d5910cd700fb49571d3 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Thu, 7 Jun 2012 17:02:17 +1200 Subject: [PATCH] SSL support for test struss. Also, rewrite requests usage for latest version. --- libpathod/test.py | 29 +++++++++++++++++------------ test/test_test.py | 28 ++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/libpathod/test.py b/libpathod/test.py index 53cbd8d5d..508605bf9 100644 --- a/libpathod/test.py +++ b/libpathod/test.py @@ -1,36 +1,41 @@ import json, threading, Queue import requests -import pathod +import pathod, utils IFACE = "127.0.0.1" class Daemon: - def __init__(self, staticdir=None, anchors=()): + def __init__(self, staticdir=None, anchors=(), ssl=None): self.app = pathod.make_app(staticdir=staticdir, anchors=anchors) self.q = Queue.Queue() - self.thread = PaThread(self.q, self.app) + self.thread = PaThread(self.q, self.app, ssl) self.thread.start() self.port = self.q.get(True, 5) - self.urlbase = "http://%s:%s"%(IFACE, self.port) + self.urlbase = "%s://%s:%s"%("https" if ssl else "http", IFACE, self.port) def info(self): - resp = requests.get("%s/api/info"%self.urlbase) - if resp.ok: - return json.loads(resp.read()) + resp = requests.get("%s/api/info"%self.urlbase, verify=False) + return resp.json def shutdown(self): - requests.post("%s/api/shutdown"%self.urlbase) + requests.post("%s/api/shutdown"%self.urlbase, verify=False) class PaThread(threading.Thread): - def __init__(self, q, app): + def __init__(self, q, app, ssl): threading.Thread.__init__(self) - self.q = q - self.app = app + self.q, self.app, self.ssl = q, app, ssl self.port = None # begin nocover def run(self): - self.server, self.port = pathod.make_server(self.app, 0, IFACE, None) + if self.ssl is True: + ssloptions = dict( + keyfile = utils.data.path("resources/server.key"), + certfile = utils.data.path("resources/server.crt"), + ) + else: + ssloptions = self.ssl + self.server, self.port = pathod.make_server(self.app, 0, IFACE, ssloptions) self.q.put(self.port) pathod.run(self.server) diff --git a/test/test_test.py b/test/test_test.py index c10cee4cd..086f3dfcc 100644 --- a/test/test_test.py +++ b/test/test_test.py @@ -1,8 +1,9 @@ -import time +import time, logging import libpry import requests -from libpathod import test, version +from libpathod import test, version, utils +logging.disable(logging.CRITICAL) class uDaemonManual(libpry.AutoTree): def test_startstop(self): @@ -11,8 +12,27 @@ class uDaemonManual(libpry.AutoTree): assert rsp.ok assert rsp.status_code == 202 d.shutdown() - rsp = requests.get("http://localhost:%s/p/202"%d.port) - assert not rsp.ok + libpry.raises(requests.ConnectionError, requests.get, "http://localhost:%s/p/202"%d.port) + + def test_startstop_ssl(self): + d = test.Daemon(ssl=True) + rsp = requests.get("https://localhost:%s/p/202"%d.port, verify=False) + assert rsp.ok + assert rsp.status_code == 202 + d.shutdown() + libpry.raises(requests.ConnectionError, requests.get, "http://localhost:%s/p/202"%d.port) + + def test_startstop_ssl_explicit(self): + ssloptions = dict( + keyfile = utils.data.path("resources/server.key"), + certfile = utils.data.path("resources/server.crt"), + ) + d = test.Daemon(ssl=ssloptions) + rsp = requests.get("https://localhost:%s/p/202"%d.port, verify=False) + assert rsp.ok + assert rsp.status_code == 202 + d.shutdown() + libpry.raises(requests.ConnectionError, requests.get, "http://localhost:%s/p/202"%d.port) class uDaemon(libpry.AutoTree):