Mercurial > hg > simpypi
view tests/testserver.py @ 62:7c154953acc4
test reading the pages
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 01 Mar 2012 16:20:44 -0800 |
parents | 5a89eb717987 |
children |
line wrap: on
line source
import threading from wsgiref.simple_server import make_server class TestWSGIServer(object): """threaded WSGI server for in process testing""" def __init__(self, app, host, port): self.app = app self.host = host self.port = int(port) self.httpd = None def start(self): self.httpd = make_server(self.host, self.port, self.app) self.server = threading.Thread(target=self.httpd.serve_forever) self.server.setDaemon(True) # don't hang on exit self.server.start() def stop(self): if self.httpd: ### FIXME: There is no shutdown() method in Python 2.4... try: self.httpd.shutdown() except AttributeError: pass self.httpd = None __del__ = stop if __name__ == '__main__': def app(environ, start_response): content = 'Hello world!' status = '200 OK' headers = [('Content-Type', 'text/plain'), ('Content-Length', str(len(content)))] start_response(status, headers) return [content] port = 8080 server = TestWSGIServer(app, 'localhost', port) server.start() f = urllib2.urlopen('http://localhost:%d/' % port) response = f.read() assert response == 'Hello world!' server.stop()