comparison lemuriformes/test_server.py @ 15:0d1b8bb1d97b

SQL + data related functionality
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 17:16:52 -0800
parents
children
comparison
equal deleted inserted replaced
14:756dbd3e391e 15:0d1b8bb1d97b
1 """
2 mock server for testing purposes
3 """
4
5 # see https://realpython.com/blog/python/testing-third-party-apis-with-mock-servers/
6
7 # TODO:
8 # - python3 compatability
9 # - use multiprocessing as threading does not allow hangin thread deletion
10 from BaseHTTPServer import HTTPServer
11 from threading import Thread
12 from .port import get_free_port
13
14
15 class MockServer(HTTPServer):
16
17 hostname = 'localhost'
18
19 def __init__(self, handler):
20 HTTPServer.__init__(self,
21 (self.hostname, get_free_port()),
22 handler)
23 self.thread = None
24
25 def base_url(self):
26 return 'http://{0}:{1}/'.format(*self.server_address)
27
28 def start_thread(self):
29 self.thread = Thread(target=self.serve_forever)
30 self.thread.setDaemon(True)
31 self.thread.start()
32
33 def stop_thread(self):
34 raise NotImplementedError('TODO')
35
36 def __enter__(self):
37 self.start_thread()
38 return self # or perhaps `self.thread`?
39
40 def __exit__(self, *args):
41 pass # nothing to do? really?