Commit 3d89ab37 authored by Jérome Perrin's avatar Jérome Perrin

testing/utils: support choosing working directory for ManagedHTTPServer

This allows easily serving a directory content with a server like this:

    from six.moves import SimpleHTTPServer

    class FileHTTPServer(ManagedHTTPServer):
      working_directory = profile_dir
      RequestHandler = SimpleHTTPServer.SimpleHTTPRequestHandler
parent 2f9a27f8
...@@ -119,8 +119,10 @@ class ManagedHTTPServer(ManagedResource): ...@@ -119,8 +119,10 @@ class ManagedHTTPServer(ManagedResource):
proto = 'http' proto = 'http'
# hostname to listen to, default to ipv4 address of the current test # hostname to listen to, default to ipv4 address of the current test
hostname = None # type: str hostname = None # type: str
# port to listen to, default # port to listen to, default to a free port selected with `findFreeTCPPort`
port = None # type: int port = None # type: int
# current working directory of the server process
working_directory = None # type: str
@property @property
def url(self): def url(self):
...@@ -166,10 +168,19 @@ class ManagedHTTPServer(ManagedResource): ...@@ -166,10 +168,19 @@ class ManagedHTTPServer(ManagedResource):
if not self.port: if not self.port:
self.port = findFreeTCPPort(self.hostname) self.port = findFreeTCPPort(self.hostname)
def serve_forever(server, cwd):
if cwd:
os.chdir(cwd)
server.serve_forever()
server = self._makeServer() server = self._makeServer()
self._process = multiprocessing.Process( self._process = multiprocessing.Process(
target=server.serve_forever, target=serve_forever,
name=self._name, name=self._name,
kwargs={
'server': server,
'cwd': self.working_directory,
}
) )
self._process.start() self._process.start()
# from now on, socket is used by server subprocess, we can close it # from now on, socket is used by server subprocess, we can close it
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment