def setup(self): is_image = self.body.body_type == BodyResponse.IMAGE is_file = self.body.body_type == BodyResponse.FILE is_json = self.body.body_type == BodyResponse.JSON if is_image or is_file: full_path = self.body_file_path() if os.path.isfile(full_path): self.headers["Accept-Ranges"] = "bytes" self.headers["Pragma"] = "public" self.headers["Content-Length"] = os.path.getsize(full_path) mimetype = mimetypes.guess_type(full_path) if mimetype: self.headers["Content-Type"] = mimetype[0] if is_file: self.headers[ "Content-Disposition"] = 'attachment; filename="{0}"'.format( os.path.basename(full_path)) else: Log.error("File not found: {0}".format(full_path), False) self.clear() self.status = 404 elif is_json: self.headers["Content-Type"] = "application/json"
def test_error_with_fatal(self): with patch("sys.stdout", new=StringIO()) as output: exited = False try: Log.error("error message", True) except SystemExit as e: if e.code == 10: exited = True self.assertIn("error message", output.getvalue().strip()) self.assertEqual(exited, True)
def process_python_data(self, process_data): full_path = File.real_path(self.base_path, self.body.file_name) # execute module "run" function try: if os.path.isfile(full_path): # return a dict from python file module_name = File.get_filename_without_extension(full_path) spec = importlib.util.spec_from_file_location( module_name, full_path, ) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) returned_data = module.run(process_data) # fill class with dict data self.status = (returned_data["status"] if "status" in returned_data else 500) self.headers = (returned_data["headers"] if "headers" in returned_data else {}) self.body.value = (returned_data["body"] if "body" in returned_data else "") else: Log.error("File not found: {0}".format(full_path), False) self.status = 404 except Exception as e: Log.error( "Error when execute file: {0}".format( os.path.basename(full_path)), False, ) Log.normal("Path: {0}".format(full_path)) Log.normal("Error: {0}".format(repr(e))) self.status = 500
def run(args): if args.update_scenario: scenario = args.update_scenario if not scenario: scenario = Constants.DEFAULT_SCENARIO Log.info("Changing to scenario {0}...".format(scenario)) try: r = requests.get( "{0}/pymocky/update-scenario?scenario={1}".format( args.server_host, scenario, )) if r.status_code == 200: Log.ok("Scenario updated") else: Log.failed("Scenario not updated: {0:d}".format( r.status_code)) except requests.exceptions.RequestException as e: Log.error("Scenario update error: {0}".format(e)) elif args.reload: Log.info("Reloading...") try: r = requests.get("{0}/pymocky/reload".format(args.server_host)) if r.status_code == 200: Log.ok("Reloaded") else: Log.failed("Reload failed: {0:d}".format(r.status_code)) except requests.exceptions.RequestException as e: Log.error("Reload error: {0}".format(e)) elif args.version: Log.normal("Version: {0}".format(__version__)) else: if not args.path: Log.error("Path argument is required (--path or -p)") Config.sys_path_list = sys.path.copy() CherryPyServer.start()
def test_error(self): with patch("sys.stdout", new=StringIO()) as output: Log.error("error message", False) self.assertIn("error message", output.getvalue().strip())