def run_server_in_temp_folder(files=[], port=8999): """ Set up a server with some known files to run captures against. Example: with run_server_in_temp_folder(['test/assets/test.html','test/assets/test.pdf']): assert(requests.get("http://localhost/test.html") == contents_of_file("test.html")) """ # Run in temp dir. # We have to (implicitly) cwd to this so SimpleHTTPRequestHandler serves the files for us. old_cwd = os.getcwd() with tempdir.in_tempdir(): # Copy over files to current temp dir, stripping paths. for file in files: shutil.copyfile(os.path.join(old_cwd, file), os.path.basename(file)) # start server httpd = HTTPServer(('', port), SimpleHTTPRequestHandler) httpd._BaseServer__is_shut_down = multiprocessing.Event() server_thread = Process(target=httpd.serve_forever) server_thread.start() try: # run body of `with` block yield finally: # shut down server server_thread.terminate()
def run_server_in_temp_folder(files=[], port=8999): """ Set up a server with some known files to run captures against. Example: with run_server_in_temp_folder(['test/assets/test.html','test/assets/test.pdf']): assert(requests.get("http://localhost/test.html") == contents_of_file("test.html")) """ # Run in temp dir. # We have to (implicitly) cwd to this so SimpleHTTPRequestHandler serves the files for us. old_cwd = os.getcwd() with tempdir.in_tempdir(): # Copy over files to current temp dir, stripping paths. for file in files: shutil.copyfile(os.path.join(old_cwd, file), os.path.basename(file)) # start server httpd = HTTPServer(('', port), SimpleHTTPRequestHandler) httpd._BaseServer__is_shut_down = multiprocessing.Event() server_thread = Process(target=httpd.serve_forever) server_thread.start() try: # run body of `with` block yield finally: # shut down server server_thread.terminate()