Exemple #1
0
    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()
Exemple #2
0
    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()
Exemple #3
0
    def form_fields_matches(self, form_fields):
        has_form_fields = self.form_fields != {}

        if has_form_fields:
            for key in self.form_fields.keys():
                if isinstance(form_fields, dict):
                    if key not in form_fields:
                        return False

                    try:
                        if not re.match(self.form_fields[key],
                                        form_fields[key]):
                            return False
                    except TypeError:
                        Log.failed("Invalid regex: {0}".format(
                            self.form_fields[key]))
                        return False
                else:
                    return False

        return True
Exemple #4
0
 def test_failed(self):
     with patch("sys.stdout", new=StringIO()) as output:
         Log.failed("failed message")
         self.assertIn("failed message", output.getvalue().strip())