diff options
-rw-r--r-- | test/handler.py | 24 | ||||
-rw-r--r-- | test/serv.py | 6 | ||||
-rw-r--r-- | test/sslserv.py | 22 | ||||
-rw-r--r-- | test/test_server.py | 63 | ||||
-rwxr-xr-x | test/tserv | 30 | ||||
-rw-r--r-- | test/tutils.py | 68 |
6 files changed, 52 insertions, 161 deletions
diff --git a/test/handler.py b/test/handler.py deleted file mode 100644 index 8b717733..00000000 --- a/test/handler.py +++ /dev/null @@ -1,24 +0,0 @@ -import socket -from BaseHTTPServer import BaseHTTPRequestHandler - - -class TestRequestHandler(BaseHTTPRequestHandler): - default_request_version = "HTTP/1.1" - def setup(self): - self.connection = self.request - self.rfile = socket._fileobject(self.request, "rb", self.rbufsize) - self.wfile = socket._fileobject(self.request, "wb", self.wbufsize) - - def log_message(self, *args, **kwargs): - # Silence output - pass - - def do_GET(self): - data = "data: %s\npath: %s\n"%(self.headers, self.path) - self.send_response(200) - self.send_header("proxtest", "testing") - self.send_header("Content-type", "text-html") - self.send_header("Content-length", len(data)) - self.end_headers() - self.wfile.write(data) - diff --git a/test/serv.py b/test/serv.py deleted file mode 100644 index a89e60d6..00000000 --- a/test/serv.py +++ /dev/null @@ -1,6 +0,0 @@ -from BaseHTTPServer import HTTPServer -import handler - -def make(port): - server_address = ('127.0.0.1', port) - return HTTPServer(server_address, handler.TestRequestHandler) diff --git a/test/sslserv.py b/test/sslserv.py deleted file mode 100644 index 557ce869..00000000 --- a/test/sslserv.py +++ /dev/null @@ -1,22 +0,0 @@ -import socket -from SocketServer import BaseServer -from BaseHTTPServer import HTTPServer -import ssl -import handler - - -class SecureHTTPServer(HTTPServer): - def __init__(self, server_address, HandlerClass): - BaseServer.__init__(self, server_address, HandlerClass) - self.socket = ssl.wrap_socket( - socket.socket(self.address_family, self.socket_type), - keyfile = "data/serverkey.pem", - certfile = "data/serverkey.pem" - ) - self.server_bind() - self.server_activate() - - -def make(port): - server_address = ('127.0.0.1', port) - return SecureHTTPServer(server_address, handler.TestRequestHandler) diff --git a/test/test_server.py b/test/test_server.py index 50f1afe8..3bfbab5b 100644 --- a/test/test_server.py +++ b/test/test_server.py @@ -1,40 +1,15 @@ import urllib, urllib2 +import libpathod.test, requests +import libpry import tutils class uSanity(tutils.ProxTest): def test_http(self): - """ - Just check that the HTTP server is running. - """ - f = urllib.urlopen("http://127.0.0.1:%s"%tutils.HTTP_PORT, proxies={}) - assert f.read() - - def test_https(self): - """ - Just check that the HTTPS server is running. - """ - f = urllib.urlopen("https://127.0.0.1:%s"%tutils.HTTPS_PORT, proxies={}) - assert f.read() + assert self.pathod("205").status_code == 205 + assert self.log() class uProxy(tutils.ProxTest): - HOST = "127.0.0.1" - def _get(self, host=HOST): - proxy_support = urllib2.ProxyHandler( - {"http" : "http://127.0.0.1:%s"%tutils.PROXL_PORT} - ) - opener = urllib2.build_opener(proxy_support) - r = urllib2.Request("http://%s:%s"%(host, tutils.HTTP_PORT)) - return opener.open(r) - - def _sget(self, host=HOST): - proxy_support = urllib2.ProxyHandler( - {"https" : "https://127.0.0.1:%s"%tutils.PROXL_PORT} - ) - opener = urllib2.build_opener(proxy_support) - r = urllib2.Request("https://%s:%s"%(host, tutils.HTTPS_PORT)) - return opener.open(r) - def test_http(self): f = self._get() assert f.code == 200 @@ -46,32 +21,12 @@ class uProxy(tutils.ProxTest): assert "host" in l[1].headers assert l[2].code == 200 - def test_https(self): - f = self._sget() - assert f.code == 200 - assert f.read() - f.close() - - l = self.log() - assert l[0].address - assert "host" in l[1].headers - assert l[2].code == 200 - - # Disable these two for now: they take a long time. - def _test_http_nonexistent(self): - f = self._get("nonexistent") - assert f.code == 200 - assert "Error" in f.read() - - def _test_https_nonexistent(self): - f = self._sget("nonexistent") - assert f.code == 200 - assert "Error" in f.read() - tests = [ - tutils.TestServers(), [ + tutils.TestServer(), [ + uSanity(), + ], + tutils.TestServer(ssl=True), [ uSanity(), - uProxy(), - ] + ], ] diff --git a/test/tserv b/test/tserv deleted file mode 100755 index 5b35b72a..00000000 --- a/test/tserv +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python -""" - A simple program for testing the test HTTP/S servers. -""" -from optparse import OptionParser, OptionGroup -import sslserv, serv - -if __name__ == "__main__": - parser = OptionParser( - usage = "%prog [options] output", - version="%prog 0.1", - ) - parser.add_option( - "-s", "--ssl", action="store_true", - dest="ssl", default=False - ) - options, args = parser.parse_args() - - if options.ssl: - port = 8443 - print "Running on port %s"%port - s = sslserv.make(port) - else: - port = 8080 - print "Running on port %s"%port - s = serv.make(port) - try: - s.serve_forever() - except KeyboardInterrupt: - pass diff --git a/test/tutils.py b/test/tutils.py index 25a76c0e..8ebb1ecd 100644 --- a/test/tutils.py +++ b/test/tutils.py @@ -1,7 +1,8 @@ import threading, Queue import libpry from libmproxy import proxy, flow, controller -import serv, sslserv +import requests +import libpathod.test import random def treq(conn=None): @@ -39,15 +40,6 @@ def tflow_err(): return f -# Yes, the random ports are horrible. During development, sockets are often not -# properly closed during error conditions, which means you have to wait until -# you can re-bind to the same port. This is a pain in the ass, so we just pick -# a random port and keep moving. -PROXL_PORT = random.randint(10000, 20000) -HTTP_PORT = random.randint(20000, 30000) -HTTPS_PORT = random.randint(30000, 40000) - - class TestMaster(controller.Master): def __init__(self, port, testq): s = proxy.ProxyServer(proxy.ProxyConfig("data/testkey.pem"), port) @@ -64,8 +56,9 @@ class TestMaster(controller.Master): class ProxyThread(threading.Thread): - def __init__(self, port, testq): - self.tmaster = TestMaster(port, testq) + def __init__(self, testq): + self.port = random.randint(10000, 20000) + self.tmaster = TestMaster(self.port, testq) controller.should_exit = False threading.Thread.__init__(self) @@ -88,30 +81,55 @@ class ServerThread(threading.Thread): self.server.shutdown() -class TestServers(libpry.TestContainer): +class TestServer(libpry.TestContainer): + """ + Starts up a Pathod server and a mitmproxy instance. + """ + def __init__(self, ssl=None): + libpry.TestContainer.__init__(self) + self.ssl = ssl + def setUpAll(self): self.tqueue = Queue.Queue() # We don't make any concurrent requests, so we can access # the attributes on this object safely. - self.proxthread = ProxyThread(PROXL_PORT, self.tqueue) - self.threads = [ - ServerThread(serv.make(HTTP_PORT)), - ServerThread(sslserv.make(HTTPS_PORT)), - self.proxthread - ] - for i in self.threads: - i.start() + self.proxy = ProxyThread(self.tqueue) + self.server = libpathod.test.Daemon(ssl=self.ssl) + self.proxy.start() def setUp(self): - self.proxthread.tmaster.clear() + self.proxy.tmaster.clear() def tearDownAll(self): - for i in self.threads: - i.shutdown() + self.proxy.shutdown() + self.server.shutdown() class ProxTest(libpry.AutoTree): + def pathod(self, spec): + """ + Constructs a pathod request, with the appropriate base and proxy. + """ + return requests.get(self.urlbase + "/p/" + spec, proxies=self.proxies, verify=False) + + @property + def proxies(self): + """ + The URL base for the server instance. + """ + return { + "http" : "http://127.0.0.1:%s"%self.findAttr("proxy").port, + "https" : "http://127.0.0.1:%s"%self.findAttr("proxy").port + } + + @property + def urlbase(self): + """ + The URL base for the server instance. + """ + return self.findAttr("server").urlbase + def log(self): - pthread = self.findAttr("proxthread") + pthread = self.findAttr("proxy") return pthread.tmaster.log |