예제 #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()
예제 #2
0
파일: test_log.py 프로젝트: pymocky/pymocky
    def test_request(self):
        with patch("sys.stdout", new=StringIO()) as output:
            data = {
                "url": "http://localhost/pymocky",
                "method": "post",
                "body": "Hello World",
            }

            request = MappingRequest("test_id", "test_scenario", data)

            Log.log_request(request, "http://localhost/pymocky")

            self.assertIn("'mock_id': 'test_id'", output.getvalue().strip())
            self.assertIn("'mock_scenario': 'test_scenario'",
                          output.getvalue().strip())
            self.assertIn("'url': 'http://localhost/pymocky'",
                          output.getvalue().strip())
            self.assertIn("'method': 'post'", output.getvalue().strip())
            self.assertIn("'headers': {}", output.getvalue().strip())
            self.assertIn("'body': 'Hello World'", output.getvalue().strip())
            self.assertIn("'form_fields': {}", output.getvalue().strip())
            self.assertIn("'query_string': ''", output.getvalue().strip())