Beispiel #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()
Beispiel #2
0
    def test_response(self):
        with patch("sys.stdout", new=StringIO()) as output:
            data = {
                "url": "http://localhost/pymocky",
                "method": "post",
                "body": "Hello World",
            }

            response = MappingResponse("test_id", "test_scenario", data,
                                       os.getcwd())

            Log.log_response(response)

            self.assertIn("'mock_id': 'test_id'", output.getvalue().strip())
            self.assertIn("'mock_scenario': 'test_scenario'",
                          output.getvalue().strip())
            self.assertIn("'method': 'post'", output.getvalue().strip())
            self.assertIn("'body': 'Hello World'", output.getvalue().strip())
            self.assertIn("'status': 200", output.getvalue().strip())
            self.assertIn("'headers': {}", output.getvalue().strip())
            self.assertIn("'headers': {}", output.getvalue().strip())