comparison tests/testserver.py @ 46:5a89eb717987

add a test server for easy_install testing
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 01 Mar 2012 13:36:38 -0800
parents
children
comparison
equal deleted inserted replaced
45:909f577d2abb 46:5a89eb717987
1 import threading
2 from wsgiref.simple_server import make_server
3
4 class TestWSGIServer(object):
5 """threaded WSGI server for in process testing"""
6
7 def __init__(self, app, host, port):
8 self.app = app
9 self.host = host
10 self.port = int(port)
11 self.httpd = None
12
13 def start(self):
14 self.httpd = make_server(self.host, self.port, self.app)
15 self.server = threading.Thread(target=self.httpd.serve_forever)
16 self.server.setDaemon(True) # don't hang on exit
17 self.server.start()
18
19 def stop(self):
20 if self.httpd:
21 ### FIXME: There is no shutdown() method in Python 2.4...
22 try:
23 self.httpd.shutdown()
24 except AttributeError:
25 pass
26 self.httpd = None
27
28 __del__ = stop
29
30 if __name__ == '__main__':
31 def app(environ, start_response):
32 content = 'Hello world!'
33 status = '200 OK'
34 headers = [('Content-Type', 'text/plain'),
35 ('Content-Length', str(len(content)))]
36 start_response(status, headers)
37 return [content]
38 port = 8080
39 server = TestWSGIServer(app, 'localhost', port)
40 server.start()
41 f = urllib2.urlopen('http://localhost:%d/' % port)
42 response = f.read()
43 assert response == 'Hello world!'
44 server.stop()