Mercurial > hg > htest
changeset 0:26f27678ae32
initial commit
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 09 Sep 2016 16:15:58 -0700 |
parents | |
children | f3e18bcfb90b |
files | README.txt htest/__init__.py htest/httptest.py htest/session.py htest/web.py setup.py tests/test_htest.py |
diffstat | 7 files changed, 228 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.txt Fri Sep 09 16:15:58 2016 -0700 @@ -0,0 +1,11 @@ +htest +=========== + + + +---- + +Jeff Hammel + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/htest/__init__.py Fri Sep 09 16:15:58 2016 -0700 @@ -0,0 +1,2 @@ +# +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/htest/httptest.py Fri Sep 09 16:15:58 2016 -0700 @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" + +""" + +# imports +import argparse +import os +import subprocess +import sys +import time + +# python requirements +# (key, value) = (module, PyPI name) +requirements = () +for module, package in requirements: + try: + globals()[module] = __import__(module) + except ImportError: + # install requirement and try again + subprocess.check_call(['pip', 'install', package]) + args = [sys.executable] + sys.argv + os.execl(sys.executable, *args) + +# module globals +__all__ = ['main', 'Parser'] +here = os.path.dirname(os.path.realpath(__file__)) +string = (str, unicode) + +def ensure_dir(directory): + """ensure a directory exists""" + if os.path.exists(directory): + if not os.path.isdir(directory): + raise OSError("Not a directory: '{}'".format(directory)) + return directory + os.makedirs(directory) + return directory + + +class Parser(argparse.ArgumentParser): + """CLI option parser""" + def __init__(self, **kwargs): + kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter) + kwargs.setdefault('description', __doc__) + argparse.ArgumentParser.__init__(self, **kwargs) + self.add_argument('--monitor', dest='monitor', + type=float, metavar='SLEEP', + help="run in monitor mode") + self.options = None + + def parse_args(self, *args, **kw): + options = argparse.ArgumentParser.parse_args(self, *args, **kw) + self.validate(options) + self.options = options + return options + + def validate(self, options): + """validate options""" + +def main(args=sys.argv[1:]): + """CLI""" + + # parse command line options + parser = Parser() + options = parser.parse_args(args) + + try: + while True: + if options.monitor: + time.sleep(options.monitor) + else: + break + except KeyboardInterrupt: + pass + +if __name__ == '__main__': + main() + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/htest/session.py Fri Sep 09 16:15:58 2016 -0700 @@ -0,0 +1,13 @@ +""" +HTTP session abstraction +""" + +import requests + +class SessionProfiling(object): + + def __init__(self, session=None): + self.session = requests.session() if session is None else session + + def __call__(self, method, url, **kwargs): + raise NotImplementedError('TODO')
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/htest/web.py Fri Sep 09 16:15:58 2016 -0700 @@ -0,0 +1,49 @@ +#!/usr/bin/env python + +""" +web handler for htest +""" + +# imports +import argparse +import sys +from webob import Request, Response, exc +from wsgiref import simple_server + +class Handler(object): + """WSGI HTTP Handler""" + + def __init__(self, **kw): + pass + + def __call__(self, environ, start_response): + request = Request(environ) + response = Response(content_type='text/plain', + body="htest") + return response(environ, start_response) + +def main(args=sys.argv[1:]): + """CLI""" + + # parse command line + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('-p', '--port', dest='port', + type=int, default=8080, + help="port to serve on") + options = parser.parse_arguments(args) + + # instantiate WSGI handler + app = Handler() + + # serve it (Warning! Single threaded!) + server = simple_server.make_server(host='0.0.0.0', + port=options.port + app=app) + try: + server.serve_forever() + except KeyboardInterrupt: + pass + +if __name__ == '__main__': + main() +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.py Fri Sep 09 16:15:58 2016 -0700 @@ -0,0 +1,43 @@ +""" +setup packaging script for htest +""" + +import os + +version = "0.0" +dependencies = ['requests'] + +# allow use of setuptools/distribute or distutils +kw = {} +try: + from setuptools import setup + kw['entry_points'] = """ + [console_scripts] + httptest = htest.httptest +""" + kw['install_requires'] = dependencies +except ImportError: + from distutils.core import setup + kw['requires'] = dependencies + +try: + here = os.path.dirname(os.path.abspath(__file__)) + description = file(os.path.join(here, 'README.txt')).read() +except IOError: + description = '' + + +setup(name='htest', + version=version, + description="HTTP testing", + long_description=description, + classifiers=[], # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers + author='Jeff Hammel', + author_email='k0scist@gmail.com', + url='', + license='', + packages=['htest'], + include_package_data=True, + zip_safe=False, + **kw + )
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_htest.py Fri Sep 09 16:15:58 2016 -0700 @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +unit tests for htest +""" + +import os +import sys +import tempfile +import unittest + +# globals +here = os.path.dirname(os.path.abspath(__file__)) + +class htestUnitTest(unittest.TestCase): + + def test_htest(self): + tf = tempfile.mktemp() + try: + # pass + pass + finally: + if os.path.exists(tf): + os.remove(tf) + +if __name__ == '__main__': + unittest.main() +