annotate tests/testserver.py @ 77:4415ba355f25

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