Beispiel #1
0
    def test_multiple_matches(self):
        with patch("sys.stdout", new=StringIO()) as output:
            data = {
                "id": "test",
                "request": {
                    "url": "",
                    "method": "get",
                },
                "response": {
                    "text": "Hello World",
                },
            }

            mapping = MappingItem(data, "dummy.yml", os.getcwd())

            Log.multiple_matches([
                mapping,
                mapping,
            ])

            self.assertIn(
                "- dummy.yml\nID: test\nScenario: default\nURL: \nMethod: GET\nQuery String: \n- dummy.yml\nID: "
                "test\nScenario: default\nURL: \nMethod: GET\nQuery String:",
                output.getvalue().strip(),
            )
Beispiel #2
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 #3
0
    def test_return_correct_body_for_multiple_response(self):
        item1 = Mock(file_name="file1")
        item1.request = "request1"

        item2 = Mock(file_name="file2")
        item2.request = "request2"

        # Config = Mock(verbose=True)

        mapper = CherryPyMapper()
        mapper.cherrypy = Mock()
        mapper.cherrypy.url = Mock(return_value="some url")

        body = Log.multiple_matches([item1, item2])

        self.assertEqual(
            body,
            "- file1\nrequest1\n- file2\nrequest2\n",
        )