def handle_request(self): request = self.cherrypy.to_mapper_request() if Config.verbose: Log.info("Request data:") Log.normal(request) else: Log.request_url(self.cherrypy.url()) items = self.mapping_handler.mapping_item_for_mapping_request(request) if len(items) == 0: self.cherrypy.response.status = 500 Log.failed("No response found for request: {0}".format( self.cherrypy.url())) return "No response found for request" if len(items) > 1: Log.warn("Matched {0:d} items, choosing the first one".format( len(items))) if Config.verbose: Log.multiple_matches(items) matched_item = items[0] response = matched_item.response if Config.verbose: Log.log_request(matched_item.request, self.cherrypy.url()) delay = Config.delay if delay > 0: delay = delay / 1000 if Config.verbose: Log.info("Delay: {0:.3f}ms".format(delay)) sleep(delay) if response.body.body_type == BodyResponse.PYTHON: response.process_python_data({"request": request}) self.cherrypy.response.status = response.status self.fill_headers(response.headers) if Config.verbose: Log.log_response(response) return response.body_response()
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 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 test_normal(self): with patch("sys.stdout", new=StringIO()) as output: Log.normal("normal message") self.assertIn("normal message", output.getvalue().strip())
def __eq__(self, other): matches_method = self.method_matches(other.method) matches_url = self.url_matches(other.url) matches_body = self.body_matches(other.body) matches_headers = HeaderMatcher(self.headers).matches(other.headers) matches_form_fields = self.form_fields_matches(other.form_fields) matches_query_string = self.query_string_matches(other.query_string) if Config.verbose: Log.info("Mock tested:") Log.normal("Mock ID: {0}".format(self.mock_id)) Log.normal("Mock Scenario: {0}".format(self.mock_scenario)) Log.normal("Request URL: {0}".format(other.url)) Log.normal("Mock URL: {0}".format(self.url)) Log.normal("Matches Method: {0}".format( ("Yes" if matches_method else "No"))) Log.normal("Matches URL: {0}".format( ("Yes" if matches_url else "No"))) Log.normal("Matches Body: {0}".format( ("Yes" if matches_body else "No"))) Log.normal("Matches Headers: {0}".format( ("Yes" if matches_headers else "No"))) Log.normal("Matches Form Fields: {0}".format( ("Yes" if matches_form_fields else "No"))) Log.normal("Matches Query String: {0}".format( ("Yes" if matches_query_string else "No"))) return (matches_method and matches_url and matches_body and matches_headers and matches_form_fields and matches_query_string)